From dbbc7d891c0f9837d3580842fa5326ba40a21476 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期四, 21 十一月 2024 11:46:20 +0800
Subject: [PATCH] 出题组卷

---
 app/models/agent_model.py |    2 
 app/config/config.py      |    1 
 app/api/chat.py           |   84 ++++++++++++++++++---------
 app/task/fetch_agent.py   |    3 
 app/config/config.yaml    |    3 
 app/service/basic.py      |   18 +++++
 app/api/files.py          |   15 +++++
 7 files changed, 93 insertions(+), 33 deletions(-)

diff --git a/app/api/chat.py b/app/api/chat.py
index 5e6f1da..de05de8 100644
--- a/app/api/chat.py
+++ b/app/api/chat.py
@@ -200,6 +200,7 @@
                             pass
     elif agent_type == AgentType.BASIC:
         try:
+            service = BasicService(base_url=settings.basic_base_url)
             while True:
                 # 鎺ユ敹鍓嶇娑堟伅
                 message = await websocket.receive_json()
@@ -213,36 +214,61 @@
                 if not question:
                     await websocket.send_json({"message": "Invalid request", "type": "error"})
                     continue
-
-                service = BasicService(base_url=settings.basic_base_url)
-                async for result in service.excel_talk(question, chat_id):
-                    try:
-                        if result[:5] == "data:":
-                            # 濡傛灉鏄紝鍒欐埅鍙栨帀鍓�5涓瓧绗︼紝骞跺幓闄ら灏剧┖鐧界
-                            text = result[5:].strip()
-                        else:
-                            # 鍚﹀垯锛屼繚鎸佸師鏍�
-                            text = result
+                if agent.agent_type == "questionTalk":
+                    async for result in service.questions_talk(question, chat_id):
                         try:
-                            data = json.loads(text)
-                            output = data.get("output", "")
-                            excel_name = data.get("excel_name", "")
-                            image_name = data.get("image_name", "")
-                            excel_url = None
-                            image_url = None
-                            if excel_name:
-                                excel_url = f"/api/files/download/?agent_id=basic_excel_talk&file_id={excel_name}&file_type=excel"
-                            if image_name:
-                                image_url = f"/api/files/download/?agent_id=basic_excel_talk&file_id={image_name}&file_type=image"
-                            result = {"message": output, "type": "message", "excel_url": excel_url, "image_url": image_url}
-                            await websocket.send_json(result | data)
-                        except json.JSONDecodeError as e:
-                            print(f"Error decoding JSON: {e}")
-                            print(f"Response text: {text}")
-                    except Exception as e2:
-                        result = {"message": f"鍐呴儴閿欒锛� {e2}", "type": "close"}
-                        await websocket.send_json(result)
-                        print(f"Error process message of basic agent: {e2}")
+                            if result[:5] == "data:":
+                                # 濡傛灉鏄紝鍒欐埅鍙栨帀鍓�5涓瓧绗︼紝骞跺幓闄ら灏剧┖鐧界
+                                text = result[5:].strip()
+                            else:
+                                # 鍚﹀垯锛屼繚鎸佸師鏍�
+                                text = result
+                            try:
+                                data = json.loads(text)
+                                output = data.get("output", "")
+                                file_name = data.get("filename", "")
+
+                                excel_url = None
+                                if file_name:
+                                    excel_url = f"/api/files/download/?agent_id=basic_question_talk&file_id={file_name}&file_type=word"
+                                result = {"message": output, "type": "message", "file_url": excel_url}
+                                await websocket.send_json(result | data)
+                            except json.JSONDecodeError as e:
+                                print(f"Error decoding JSON: {e}")
+                                # print(f"Response text: {text}")
+                        except Exception as e2:
+                            result = {"message": f"鍐呴儴閿欒锛� {e2}", "type": "close"}
+                            await websocket.send_json(result)
+                            print(f"Error process message of basic agent: {e2}")
+                else:
+                    async for result in service.excel_talk(question, chat_id):
+                        try:
+                            if result[:5] == "data:":
+                                # 濡傛灉鏄紝鍒欐埅鍙栨帀鍓�5涓瓧绗︼紝骞跺幓闄ら灏剧┖鐧界
+                                text = result[5:].strip()
+                            else:
+                                # 鍚﹀垯锛屼繚鎸佸師鏍�
+                                text = result
+                            try:
+                                data = json.loads(text)
+                                output = data.get("output", "")
+                                excel_name = data.get("excel_name", "")
+                                image_name = data.get("image_name", "")
+                                excel_url = None
+                                image_url = None
+                                if excel_name:
+                                    excel_url = f"/api/files/download/?agent_id=basic_excel_talk&file_id={excel_name}&file_type=excel"
+                                if image_name:
+                                    image_url = f"/api/files/download/?agent_id=basic_excel_talk&file_id={image_name}&file_type=image"
+                                result = {"message": output, "type": "message", "excel_url": excel_url, "image_url": image_url}
+                                await websocket.send_json(result | data)
+                            except json.JSONDecodeError as e:
+                                print(f"Error decoding JSON: {e}")
+                                # print(f"Response text: {text}")
+                        except Exception as e2:
+                            result = {"message": f"鍐呴儴閿欒锛� {e2}", "type": "close"}
+                            await websocket.send_json(result)
+                            print(f"Error process message of basic agent: {e2}")
         except Exception as e:
             await websocket.send_json({"message": str(e), "type": "error"})
         finally:
diff --git a/app/api/files.py b/app/api/files.py
index 968b6b3..863f174 100644
--- a/app/api/files.py
+++ b/app/api/files.py
@@ -68,6 +68,12 @@
 
             return Response(code=200, msg="", data=result)
 
+        elif agent_id == "basic_paper_agent":
+            service = BasicService(base_url=settings.basic_paper_url)
+            result = await service.paper_file_upload(chat_id, file.filename, file_content)
+
+            return Response(code=200, msg="", data=result)
+
     else:
         return Response(code=200, msg="Unsupported agent type")
 
@@ -101,6 +107,8 @@
         if agent_id == "basic_excel_talk":
             return await download_basic_file(file_id, file_type)
 
+        elif agent_id == "basic_question_talk":
+            return await download_basic_file(file_id, file_type)
     else:
         return Response(code=400, msg="Unsupported agent type")
 
@@ -137,5 +145,12 @@
             media_type=mimetype,
             headers={"Content-Disposition": f"attachment; filename={filename}"}
         )
+    elif file_type == "word":
+        content, filename, mimetype = await service.questions_talk_word_download(file_id)
+        return StreamingResponse(
+            io.BytesIO(content),
+            media_type=mimetype,
+            headers={"Content-Disposition": f"attachment; filename={filename}"}
+        )
     else:
         return Response(code=400, msg="Unsupported file type")
\ No newline at end of file
diff --git a/app/config/config.py b/app/config/config.py
index 435ee86..271c588 100644
--- a/app/config/config.py
+++ b/app/config/config.py
@@ -16,6 +16,7 @@
     PRIVATE_KEY: str
     PASSWORD_KEY: str
     basic_base_url: str = ''
+    basic_paper_url: str = ''
     def __init__(self, **kwargs):
         # Check if all required fields are provided and set them
         for field in self.__annotations__.keys():
diff --git a/app/config/config.yaml b/app/config/config.yaml
index e1f16c5..1c6842c 100644
--- a/app/config/config.yaml
+++ b/app/config/config.yaml
@@ -2,7 +2,7 @@
 sgb_base_url: http://192.168.20.119:13001
 sgb_websocket_url: ws://192.168.20.119:13001
 fwr_base_url: http://192.168.20.119:11080
-database_url: mysql+pymysql://root:infini_rag_flow@192.168.20.116:5455/rag_basic
+database_url: mysql+pymysql://root:infini_rag_flow@192.168.20.119:5455/rag_basic
 sgb_db_url: mysql+pymysql://root:1234@192.168.20.119:13306/bisheng
 fwr_db_url: mysql+pymysql://root:infini_rag_flow@192.168.20.119:15455/rag_flow
 PUBLIC_KEY: |
@@ -14,3 +14,4 @@
 fetch_fwr_agent: 鐭ヨ瘑闂瓟,鏅鸿兘闂瓟
 PASSWORD_KEY: VKinqB-8XMrwCLLrcf_PyHyo12_4PVKvWzaHjNFions=
 basic_base_url: http://192.168.20.231:8000
+basic_paper_url: http://192.168.20.231:8000
\ No newline at end of file
diff --git a/app/models/agent_model.py b/app/models/agent_model.py
index cc9120e..fbb2f45 100644
--- a/app/models/agent_model.py
+++ b/app/models/agent_model.py
@@ -128,5 +128,5 @@
 
     @staticmethod
     def is_type(record_id, t):
-        record = UnifiedAgent.get_by_id(record_id)
+        record = UnifiedAgentModel.get_by_id(record_id)
         return record and record.type == t
\ No newline at end of file
diff --git a/app/service/basic.py b/app/service/basic.py
index 733405a..f3e5db5 100644
--- a/app/service/basic.py
+++ b/app/service/basic.py
@@ -71,4 +71,20 @@
                         print(e)
                         return
                 else:
-                    yield f"Error: {response.status_code}"
\ No newline at end of file
+                    yield f"Error: {response.status_code}"
+
+    async def questions_talk(self, chat_id: str):
+        url = f"{self.base_url}/questions/talk"
+        params = {'chat_id': chat_id}
+        headers = {'Content-Type': 'text/plain'}
+        async with httpx.AsyncClient() as client:
+            response = await client.post(
+                url,
+                headers=headers,
+                params=params
+            )
+            return self._check_response(response)
+
+    async def questions_talk_word_download(self, file_id: str):
+        url = f"{self.base_url}/questions/download/word"
+        return await self.download_from_url(url, params={'excel_name': file_id})
\ No newline at end of file
diff --git a/app/task/fetch_agent.py b/app/task/fetch_agent.py
index 3ac6125..6e80963 100644
--- a/app/task/fetch_agent.py
+++ b/app/task/fetch_agent.py
@@ -115,7 +115,8 @@
             ('bfd090d589d811efb3630242ac190006', 4, '鏂囨。鏅鸿兘', 'BISHENG', 'report'),
             ('da3451da89d911efb9490242ac190006', 3, '鐭ヨ瘑闂瓟', 'RAGFLOW', 'knowledgeQA'),
             ('e96eb7a589db11ef87d20242ac190006', 5, '鏅鸿兘闂瓟', 'RAGFLOW', 'chat'),
-            ('basic_excel_talk', 6, '鏅鸿兘鏁版嵁', 'BASIC', 'excelTalk')
+            ('basic_excel_talk', 6, '鏅鸿兘鏁版嵁', 'BASIC', 'excelTalk'),
+            ('basic_question_talk', 7, '鏂囨。鍑洪', 'BASIC', 'questionTalk')
         ]
 
         for agent in initial_agents:

--
Gitblit v1.8.0