Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

90 Zeilen
3.2KB

  1. import requests
  2. from config import conf
  3. from common.log import logger
  4. import os
  5. class LinkSummary:
  6. def __init__(self):
  7. pass
  8. def summary_file(self, file_path: str):
  9. file_body = {
  10. "file": open(file_path, "rb"),
  11. "name": file_path.split("/")[-1],
  12. }
  13. res = requests.post(url=self.base_url() + "/v1/summary/file", headers=self.headers(), files=file_body, timeout=(5, 180))
  14. return self._parse_summary_res(res)
  15. def summary_url(self, url: str):
  16. body = {
  17. "url": url
  18. }
  19. res = requests.post(url=self.base_url() + "/v1/summary/url", headers=self.headers(), json=body, timeout=(5, 180))
  20. return self._parse_summary_res(res)
  21. def summary_chat(self, summary_id: str):
  22. body = {
  23. "summary_id": summary_id
  24. }
  25. res = requests.post(url=self.base_url() + "/v1/summary/chat", headers=self.headers(), json=body, timeout=(5, 180))
  26. if res.status_code == 200:
  27. res = res.json()
  28. logger.debug(f"[LinkSum] chat open, res={res}")
  29. if res.get("code") == 200:
  30. data = res.get("data")
  31. return {
  32. "questions": data.get("questions"),
  33. "file_id": data.get("file_id")
  34. }
  35. else:
  36. res_json = res.json()
  37. logger.error(f"[LinkSum] summary error, status_code={res.status_code}, msg={res_json.get('message')}")
  38. return None
  39. def _parse_summary_res(self, res):
  40. if res.status_code == 200:
  41. res = res.json()
  42. logger.debug(f"[LinkSum] url summary, res={res}")
  43. if res.get("code") == 200:
  44. data = res.get("data")
  45. return {
  46. "summary": data.get("summary"),
  47. "summary_id": data.get("summary_id")
  48. }
  49. else:
  50. res_json = res.json()
  51. logger.error(f"[LinkSum] summary error, status_code={res.status_code}, msg={res_json.get('message')}")
  52. return None
  53. def base_url(self):
  54. return conf().get("linkai_api_base", "https://api.link-ai.chat")
  55. def headers(self):
  56. return {"Authorization": "Bearer " + conf().get("linkai_api_key")}
  57. def check_file(self, file_path: str, sum_config: dict) -> bool:
  58. file_size = os.path.getsize(file_path) // 1000
  59. if (sum_config.get("max_file_size") and file_size > sum_config.get("max_file_size")) or file_size > 15000:
  60. logger.warn(f"[LinkSum] file size exceeds limit, No processing, file_size={file_size}KB")
  61. return True
  62. suffix = file_path.split(".")[-1]
  63. support_list = ["txt", "csv", "docx", "pdf", "md"]
  64. if suffix not in support_list:
  65. logger.warn(f"[LinkSum] unsupported file, suffix={suffix}, support_list={support_list}")
  66. return False
  67. return True
  68. def check_url(self, url: str):
  69. if not url:
  70. return False
  71. support_list = ["http://mp.weixin.qq.com", "https://mp.weixin.qq.com"]
  72. for support_url in support_list:
  73. if url.strip().startswith(support_url):
  74. return True
  75. logger.debug("[LinkSum] unsupported url, no need to process")
  76. return False