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/difyService.py | 46 ++++++++++++++++++++++------------------------
1 files changed, 22 insertions(+), 24 deletions(-)
diff --git a/app/service/difyService.py b/app/service/difyService.py
index 6b6a59a..43dff22 100644
--- a/app/service/difyService.py
+++ b/app/service/difyService.py
@@ -5,9 +5,8 @@
from typing import Union, Dict, List
from fastapi import HTTPException
from starlette import status
-from watchdog.observers.fsevents2 import message
-# from Log import logger
+from Log import logger
from app.config.config import settings
from app.utils.rsa_crypto import RagflowCrypto
@@ -18,6 +17,8 @@
def _handle_response(self, response: httpx.Response) -> Union[Dict, List]:
if response.status_code != 200:
+ if response.status_code == 201:
+ return response.json()
return {}
data = response.json()
@@ -36,7 +37,7 @@
elif isinstance(data.get("data"), list):
return data.get("data", [])
else:
- return {}
+ return data
async def register(self, username: str, password: str):
password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
@@ -65,26 +66,25 @@
raise Exception("Authorization header not found in response")
return authorization
- async def chat(self, token: str, chat_id: str, message: str, upload_file_id: str, conversation_id: str):
+ async def chat(self, token: str, user_id: int, message: str, upload_file_id: str, conversation_id: str):
target_url = f"{self.base_url}/v1/chat-messages"
- files = [
+ files = []
+ if upload_file_id:
+ files = [
{
"type": "image",
- "transfer_method": "remote_url",
- "url": "https://cloud.dify.ai/logo/logo-site.png",
- "upload_file_id":""
+ "transfer_method": "local_file",
+ "url": "",
+ "upload_file_id": upload_file_id
}
]
- if upload_file_id:
- files[0]["transfer_method"] = "local_file"
- files[0]["upload_file_id"] = upload_file_id
data = {
"inputs": {},
"query": message,
"response_mode": "streaming",
- "conversation_id": conversation_id,
- "user": chat_id,
+ "conversation_id": "",
+ "user": str(user_id),
"files": files
}
@@ -97,7 +97,7 @@
if response.status_code == 200:
try:
async for answer in response.aiter_text():
- print(f"response of ragflow chat: {answer}")
+ # print(f"response of ragflow chat: {answer}")
yield answer
except GeneratorExit as e:
print(e)
@@ -118,24 +118,22 @@
return data
return data.get("message", [])
- async def upload(self, token: str, filename: str, file: bytes) -> dict:
- url = f"{self.base_url}/console/api/files/upload"
+ async def upload(self, token: str, filename: str, file: bytes, user_id) -> dict:
+ url = f"{self.base_url}/v1/files/upload"
headers = {
- 'Content-Type': 'application/json',
+ # 'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}
-
+ data = {
+ 'user': str(user_id)
+ }
# 鍒涘缓琛ㄥ崟鏁版嵁锛屽寘鍚枃浠�
files = {"file": (filename, file)}
async with httpx.AsyncClient() as client:
- response = await client.post(url, headers=headers, files=files)
+ response = await client.post(url, headers=headers, files=files, data=data)
data = self._handle_response(response)
- # file_path = data.get("file_path", "")
- result = {
- "file_path": data
- }
- return result
+ return data
--
Gitblit v1.8.0