|
@@ -0,0 +1,68 @@
|
|
|
|
|
+import httpx
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class BasicService:
|
|
|
|
|
+ def __init__(self, base_url: str):
|
|
|
|
|
+ self.base_url = base_url
|
|
|
|
|
+
|
|
|
|
|
+ def _check_response(self, response: httpx.Response):
|
|
|
|
|
+ """检查响应并处理错误"""
|
|
|
|
|
+ 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", {})
|
|
|
|
|
+
|
|
|
|
|
+ async def download_from_url(self, url: str, params: dict):
|
|
|
|
|
+ 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 def excel_talk_image_download(self, file_id: str):
|
|
|
|
|
+ url = f"{self.base_url}/exceltalk/download/image"
|
|
|
|
|
+ return await self.download_from_url(url, params={'images_name': file_id})
|
|
|
|
|
+
|
|
|
|
|
+ async def excel_talk_excel_download(self, file_id: str):
|
|
|
|
|
+ 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):
|
|
|
|
|
+ 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(
|
|
|
|
|
+ url,
|
|
|
|
|
+ files=files,
|
|
|
|
|
+ params=params
|
|
|
|
|
+ )
|
|
|
|
|
+ return await 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'}
|
|
|
|
|
+ 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
|
|
|
|
|
+ except GeneratorExit as e:
|
|
|
|
|
+ print(e)
|
|
|
|
|
+ return
|
|
|
|
|
+ else:
|
|
|
|
|
+ yield f"Error: {response.status_code}"
|