From c795d4b39ce86ad9b502044d96df9392c0c7dca7 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期一, 25 十一月 2024 17:39:56 +0800 Subject: [PATCH] ragflow和bisheng的会话时间统一 --- app/service/basic.py | 33 ++++++++++++++++++++------------- 1 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/service/basic.py b/app/service/basic.py index 33e5f86..26c15c1 100644 --- a/app/service/basic.py +++ b/app/service/basic.py @@ -1,4 +1,7 @@ +import json + import httpx +import requests from Log import logger @@ -42,12 +45,9 @@ url = f"{self.base_url}/exceltalk/download/excel" return await self.download_from_url(url, params={'excel_name': file_id}) - async def excel_talk_upload(self, chat_id: str, filename: str, file_content: bytes): + async def excel_talk_upload(self, chat_id: str, files): url = f"{self.base_url}/exceltalk/upload/files" params = {'chat_id': chat_id, 'is_col': '0'} - - # 鍒涘缓 FormData 瀵硅薄 - files = [('files', (filename, file_content, 'application/octet-stream'))] async with httpx.AsyncClient() as client: response = await client.post( @@ -62,20 +62,27 @@ params = {'chat_id': chat_id} data = {"query": question} headers = {'Content-Type': 'application/json'} - 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: + with requests.post(url, headers=headers, json=data, params=params, timeout=60, + stream=True) as response: + for line in response.iter_lines(): + if line: + decoded_line = line.decode("utf-8") try: - async for answer in response.aiter_text(): - print(f"response of excel_talk chat: {answer}") - yield answer + if decoded_line.startswith("data:"): + decoded_line = decoded_line[5:] + answer = json.loads(decoded_line) + yield answer except GeneratorExit as e: print(e) - return + yield {"message": "鍐呴儴閿欒", "type": "close"} + finally: + # 鍦ㄦ墍鏈夋暟鎹帴鏀跺畬姣曞悗杩斿洖close + 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 +98,4 @@ 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={'word_name': file_id}) \ No newline at end of file -- Gitblit v1.8.0