bingsearch.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import requests
  2. from bs4 import BeautifulSoup as BS4
  3. import os
  4. BING_API_KEY = os.getenv("bing_api_key")
  5. def search_with_bing(query: str, search_timeout=30, top_k=6) -> list[dict]:
  6. """
  7. Search with bing and return the contexts.
  8. 参考文档: https://docs.microsoft.com/en-us/bing/search-apis/bing-web-search/overview
  9. """
  10. response = requests.get(
  11. url="https://api.bing.microsoft.com/v7.0/search",
  12. headers={"Ocp-Apim-Subscription-Key": BING_API_KEY},
  13. params={
  14. "q": query,
  15. "responseFilter": ["webpages"],
  16. "freshness": "month",
  17. "mkt": "zh-CN",
  18. },
  19. timeout=search_timeout,
  20. )
  21. try:
  22. json_content = response.json()
  23. # print(json_content)
  24. contexts = json_content["webPages"]["value"][:top_k]
  25. # logger.info("Web搜索完成")
  26. return contexts
  27. except Exception as e:
  28. # logger.error(f"搜索失败,错误原因: {e}")
  29. print(f"搜索失败,错误原因: {e}")
  30. return []
  31. def fetch_url(url):
  32. response = requests.get(url)
  33. # use beautifulsoup4 to parse html
  34. soup = BS4(response.text, "html.parser")
  35. plain_text = soup.get_text()
  36. return plain_text
  37. def bing_search_prompt(input):
  38. contents = search_with_bing(input, search_timeout=5, top_k=6)
  39. citations = "\n\n".join(
  40. [
  41. f"[[citation:{i + 1}]]\n```markdown\n{item['snippet']}\n```"
  42. for i, item in enumerate(contents)
  43. ]
  44. )
  45. prompt = f"[引用]\n{citations}\n问:{input}\n"
  46. return prompt