From 165d75b97916335ec7b5046f7c476e494a12df51 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 22 十一月 2024 11:54:05 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/rag-gateway
---
app/service/basic.py | 102 ++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 81 insertions(+), 21 deletions(-)
diff --git a/app/service/basic.py b/app/service/basic.py
index 30ac727..259b905 100644
--- a/app/service/basic.py
+++ b/app/service/basic.py
@@ -1,4 +1,8 @@
+import json
+
import httpx
+
+from Log import logger
class BasicService:
@@ -10,21 +14,27 @@
if response.status_code not in [200, 201]:
raise Exception(f"Failed to fetch data from API: {response.text}")
response_data = response.json()
- status_code = response_data.get("status_code", 0)
- if status_code != 200:
- raise Exception(f"Failed to fetch data from API: {response.text}")
- return response_data.get("data", {})
+ return response_data
- async def download_from_url(self, url: str, params: dict):
+ async def download_from_url(self, url, params=None):
async with httpx.AsyncClient() as client:
- response = await client.get(url, params=params, stream=True)
- if response.status_code == 200:
- content_disposition = response.headers.get('Content-Disposition')
- filename = content_disposition.split('filename=')[-1].strip(
- '"') if content_disposition else 'unknown_filename'
- return response.content, filename, response.headers.get('Content-Type')
- else:
- return None, None, None
+ async with client.stream('GET', url, params=params) as response:
+ if response.status_code == 200:
+ # 鑾峰彇鏂囦欢鍚�
+ content_disposition = response.headers.get('Content-Disposition')
+ if content_disposition:
+ filename = content_disposition.split('filename=')[1].strip('"')
+ else:
+ filename = 'unknown_filename'
+
+ # 鑾峰彇鍐呭绫诲瀷
+ content_type = response.headers.get('Content-Type')
+
+ # 璇诲彇鏂囦欢鍐呭
+ content = await response.aread()
+ return content, filename, content_type
+ else:
+ raise Exception(f"Failed to download: {response.status_code}")
async def excel_talk_image_download(self, file_id: str):
url = f"{self.base_url}/exceltalk/download/image"
@@ -34,12 +44,11 @@
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(
@@ -47,22 +56,73 @@
files=files,
params=params
)
- return await self._check_response(response)
+ return self._check_response(response)
async def excel_talk(self, question: str, chat_id: str):
url = f"{self.base_url}/exceltalk/talk"
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 ragflow 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):
+ logger.error("---------------questions_talk--------------------------")
+ url = f"{self.base_url}/questions/talk"
+ params = {'chat_id': chat_id}
+ headers = {'Content-Type': 'text/plain'}
+ async with httpx.AsyncClient(timeout=1800) as client:
+ response = await client.post(
+ url,
+ data=question,
+ 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={'word_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