赞
踩
langchain-ChatGLM中,要使用Bing搜索问答,还得去微软开放平台获取key,很麻烦,于是觉得自己写个。
打开langchain-ChatGLM/agent/bing_search.py文件,修改为:
- #coding=utf8
-
- import os
- import requests
- from bs4 import BeautifulSoup
- from urllib.parse import urlencode
-
- def bing_search(text, result_len=3):
- keywords = text
- query = urlencode({"q":keywords})
- url = 'https://cn.bing.com/search?'+query+'&form=ANNNB1&refig=ce14eca2b3514d39a87ccd154e7b8462&sp=1&lq=0&qs=HS&sk=PRES1&sc=7-0&cvid=ce14eca2b3514d39a87ccd154e7b8462'
- headers={
- "User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
- }
- html = requests.get(url, headers=headers)
- html.encoding='utf-8'
- html_doc = html.text
- soup = BeautifulSoup(html_doc, 'html.parser')
- results = soup.find("ol", id="b_results").find_all("li")
-
- search_data = []
- for i in range(len(results)):
- row = results[i]
- if len(row.find_all("h2"))==0:
- continue
-
- h2 = row.find("h2")
- title = h2.text
- title = title.strip()
- link = h2.find("a").get("href")
-
- content = row.find("p", class_="b_algoSlug")
- content_format = content.text
-
- row_data = {
- "snippet":content_format,
- "title":title,
- "link":link
- }
- search_data.append(row_data)
- return search_data
-
-
- if __name__ == "__main__":
- r = bing_search('python')
- print(r)

修改完成之后,重启应用就可以使用了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。