当前位置:   article > 正文

langchain-ChatGLM自己写一个微软的搜索接口_langchain bing search

langchain bing search

langchain-ChatGLM中,要使用Bing搜索问答,还得去微软开放平台获取key,很麻烦,于是觉得自己写个。

打开langchain-ChatGLM/agent/bing_search.py文件,修改为:

  1. #coding=utf8
  2. import os
  3. import requests
  4. from bs4 import BeautifulSoup
  5. from urllib.parse import urlencode
  6. def bing_search(text, result_len=3):
  7. keywords = text
  8. query = urlencode({"q":keywords})
  9. url = 'https://cn.bing.com/search?'+query+'&form=ANNNB1&refig=ce14eca2b3514d39a87ccd154e7b8462&sp=1&lq=0&qs=HS&sk=PRES1&sc=7-0&cvid=ce14eca2b3514d39a87ccd154e7b8462'
  10. headers={
  11. "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'
  12. }
  13. html = requests.get(url, headers=headers)
  14. html.encoding='utf-8'
  15. html_doc = html.text
  16. soup = BeautifulSoup(html_doc, 'html.parser')
  17. results = soup.find("ol", id="b_results").find_all("li")
  18. search_data = []
  19. for i in range(len(results)):
  20. row = results[i]
  21. if len(row.find_all("h2"))==0:
  22. continue
  23. h2 = row.find("h2")
  24. title = h2.text
  25. title = title.strip()
  26. link = h2.find("a").get("href")
  27. content = row.find("p", class_="b_algoSlug")
  28. content_format = content.text
  29. row_data = {
  30. "snippet":content_format,
  31. "title":title,
  32. "link":link
  33. }
  34. search_data.append(row_data)
  35. return search_data
  36. if __name__ == "__main__":
  37. r = bing_search('python')
  38. print(r)

 修改完成之后,重启应用就可以使用了。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/546757
推荐阅读
相关标签
  

闽ICP备14008679号