Explorar el Código

feat: knowledge base send file

zhayujie hace 2 años
padre
commit
d67d6b7948
Se han modificado 1 ficheros con 22 adiciones y 0 borrados
  1. 22 0
      bot/linkai/link_ai_bot.py

+ 22 - 0
bot/linkai/link_ai_bot.py

@@ -15,6 +15,7 @@ from config import conf, pconf
 import threading
 from common import memory, utils
 import base64
+import os
 
 class LinkAIBot(Bot):
     # authentication failed
@@ -389,6 +390,11 @@ class LinkAIBot(Bot):
             for url in image_urls:
                 if url.endswith(".mp4"):
                     reply_type = ReplyType.VIDEO_URL
+                elif url.endswith(".pdf") or url.endswith(".doc") or url.endswith(".docx"):
+                    reply_type = ReplyType.FILE
+                    url = _download_file(url)
+                    if not url:
+                        continue
                 else:
                     reply_type = ReplyType.IMAGE_URL
                 reply = Reply(reply_type, url)
@@ -396,6 +402,22 @@ class LinkAIBot(Bot):
         except Exception as e:
             logger.error(e)
 
+
+def _download_file(url: str):
+    try:
+        file_path = "tmp"
+        if not os.path.exists(file_path):
+            os.makedirs(file_path)
+        file_name = url.split("/")[-1]  # 获取文件名
+        file_path = os.path.join(file_path, file_name)
+        response = requests.get(url)
+        with open(file_path, "wb") as f:
+            f.write(response.content)
+        return file_path
+    except Exception as e:
+        logger.warn(e)
+
+
 class LinkAISessionManager(SessionManager):
     def session_msg_query(self, query, session_id):
         session = self.build_session(session_id)