From 95bfccca0260a6ff3e994ebbbbfafb61b7dab442 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期五, 22 十一月 2024 10:11:51 +0800
Subject: [PATCH] merge

---
 app/service/basic.py |   47 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/app/service/basic.py b/app/service/basic.py
index 33e5f86..9c22206 100644
--- a/app/service/basic.py
+++ b/app/service/basic.py
@@ -1,3 +1,5 @@
+import json
+
 import httpx
 
 from Log import logger
@@ -62,20 +64,30 @@
         params = {'chat_id': chat_id}
         data = {"query": question}
         headers = {'Content-Type': 'application/json'}
+        buffer = bytearray()
         async with httpx.AsyncClient(timeout=300.0) as client:
             async with client.stream("POST", url, params=params, json=data, headers=headers) as response:
                 if response.status_code == 200:
                     try:
-                        async for answer in response.aiter_text():
-                            print(f"response of excel_talk chat: {answer}")
-                            yield answer
+                        async for chunk in response.aiter_bytes():
+                            json_data = process_buffer(chunk, buffer)
+                            if json_data:
+                                yield json_data
+                                buffer.clear()
                     except GeneratorExit as e:
                         print(e)
-                        return
+                        yield {"message": "鍐呴儴閿欒", "type": "close"}
+                    finally:
+                        # 鍦ㄦ墍鏈夋暟鎹帴鏀跺畬姣曞悗璁板綍鏃ュ織
+                        logger.info("All messages received and processed - over")
+                        yield {"message": "", "type": "close"}
+
                 else:
                     yield f"Error: {response.status_code}"
 
-    async def questions_talk(self,question, chat_id: str):
+
+
+    async def questions_talk(self, question, chat_id: str):
         logger.error("---------------questions_talk--------------------------")
         url = f"{self.base_url}/questions/talk"
         params = {'chat_id': chat_id}
@@ -91,4 +103,27 @@
 
     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
+        return await self.download_from_url(url, params={'excel_name': file_id})
+
+
+def process_buffer(data, buffer):
+    def try_parse_json(data1):
+        try:
+            return True, json.loads(data1)
+        except json.JSONDecodeError:
+            return False, None
+
+    if data.startswith(b'data:'):
+        # 鍒犻櫎 'data:' 澶�
+        data = data[5:].strip()
+    else:
+        pass
+
+    # 鐩存帴鎷兼帴鍒扮紦鍐插尯灏濊瘯瑙f瀽JSON
+    buffer.extend(data.strip())
+    success, parsed_data = try_parse_json(buffer)
+    if success:
+        return parsed_data
+    else:
+        # 瑙f瀽澶辫触锛岀户缁嫾鎺�
+        return None

--
Gitblit v1.8.0