sougou.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import logging
  2. import json
  3. from typing import Optional, List
  4. from open_webui.retrieval.web.main import SearchResult, get_filtered_results
  5. from open_webui.env import SRC_LOG_LEVELS
  6. log = logging.getLogger(__name__)
  7. log.setLevel(SRC_LOG_LEVELS["RAG"])
  8. def search_sougou(
  9. sougou_api_sid: str,
  10. sougou_api_sk: str,
  11. query: str,
  12. count: int,
  13. filter_list: Optional[List[str]] = None,
  14. ) -> List[SearchResult]:
  15. from tencentcloud.common.common_client import CommonClient
  16. from tencentcloud.common import credential
  17. from tencentcloud.common.exception.tencent_cloud_sdk_exception import (
  18. TencentCloudSDKException,
  19. )
  20. from tencentcloud.common.profile.client_profile import ClientProfile
  21. from tencentcloud.common.profile.http_profile import HttpProfile
  22. try:
  23. cred = credential.Credential(sougou_api_sid, sougou_api_sk)
  24. http_profile = HttpProfile()
  25. http_profile.endpoint = "tms.tencentcloudapi.com"
  26. client_profile = ClientProfile()
  27. client_profile.http_profile = http_profile
  28. params = json.dumps({"Query": query, "Cnt": 20})
  29. common_client = CommonClient(
  30. "tms", "2020-12-29", cred, "", profile=client_profile
  31. )
  32. results = [
  33. json.loads(page)
  34. for page in common_client.call_json("SearchPro", json.loads(params))[
  35. "Response"
  36. ]["Pages"]
  37. ]
  38. sorted_results = sorted(
  39. results, key=lambda x: x.get("scour", 0.0), reverse=True
  40. )
  41. if filter_list:
  42. sorted_results = get_filtered_results(sorted_results, filter_list)
  43. return [
  44. SearchResult(
  45. link=result.get("url"),
  46. title=result.get("title"),
  47. snippet=result.get("passage"),
  48. )
  49. for result in sorted_results[:count]
  50. ]
  51. except TencentCloudSDKException as err:
  52. log.error(f"Error in Sougou search: {err}")
  53. return []