From 1963c42487b3980cb8513a2cc7669da0876c3037 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 12 十月 2024 19:50:14 +0800
Subject: [PATCH] websocket对话接口兼容ragflow流式对话

---
 app/config/config.py   |    3 
 app/service/ragflow.py |   28 +++++++++
 app/api/chat.py        |  130 +++++++++++++++++++++++++++----------------
 app/config/config.yaml |    4 +
 app/api/auth.py        |    8 --
 app/service/auth.py    |    2 
 6 files changed, 116 insertions(+), 59 deletions(-)

diff --git a/app/api/auth.py b/app/api/auth.py
index feef6d9..588d7c8 100644
--- a/app/api/auth.py
+++ b/app/api/auth.py
@@ -1,9 +1,7 @@
-from fastapi import APIRouter, Depends, Request
-from fastapi.security import OAuth2PasswordBearer
-from passlib.context import CryptContext
+from fastapi import APIRouter, Depends
 from sqlalchemy.orm import Session
 
-from app.api import Response, pwd_context, oauth2_scheme, get_current_user
+from app.api import Response, pwd_context
 from app.config.config import settings
 from app.models.base_model import get_db
 from app.models.token_model import upsert_token
@@ -14,8 +12,6 @@
 from app.service.ragflow import RagflowService
 
 router = APIRouter()
-
-
 
 
 @router.post("/register", response_model=Response)
diff --git a/app/api/chat.py b/app/api/chat.py
index c7aa2da..1fe7ad6 100644
--- a/app/api/chat.py
+++ b/app/api/chat.py
@@ -1,7 +1,7 @@
 import json
 import uuid
 
-from fastapi import WebSocket, WebSocketDisconnect, APIRouter, Request, Depends
+from fastapi import WebSocket, WebSocketDisconnect, APIRouter, Depends
 import asyncio
 import websockets
 from sqlalchemy.orm import Session
@@ -9,7 +9,8 @@
 from app.config.config import settings
 from app.models.base_model import get_db
 from app.models.user_model import UserModel
-from app.service.token import get_bisheng_token
+from app.service.ragflow import RagflowService
+from app.service.token import get_bisheng_token, get_ragflow_token
 
 router = APIRouter()
 
@@ -27,66 +28,97 @@
     await websocket.accept()
     print(f"Client {agent_id} connected")
 
-    token = get_bisheng_token(db, current_user.id)
-
     if agent_id == "0":
         agent_id = settings.bisheng_agent_id
+    elif agent_id == "1":
+        agent_id = settings.ragflow_agent_id
+        chat_id = settings.ragflow_chat_id
+
     if chat_id == "0":
         chat_id = uuid.uuid4().hex
 
-    # 杩炴帴鍒版湇鍔$
-    service_uri = f"{settings.bisheng_websocket_url}/api/v1/assistant/chat/{agent_id}?t=&chat_id={chat_id}"
-    headers = {
-        'cookie':  f"access_token_cookie={token};"
-    }
-
-    async with websockets.connect(service_uri, extra_headers=headers) as service_websocket:
-        client_websockets[chat_id] = websocket
-
+    client_websockets[chat_id] = websocket
+    if agent_id == settings.ragflow_agent_id:
+        ragflow_service = RagflowService(settings.ragflow_base_url)
+        token = get_ragflow_token(db, current_user.id)
         try:
-            # 澶勭悊瀹㈡埛绔彂鏉ョ殑娑堟伅
-            async def forward_to_service():
+            async def forward_to_ragflow():
                 while True:
                     message = await websocket.receive_json()
                     print(f"Received from client {chat_id}: {message}")
-                    # 娣诲姞 'agent_id' 鍜� 'chat_id' 瀛楁
-                    message['flow_id'] = agent_id
-                    message['chat_id'] = chat_id
-                    msg = message["message"]
-                    del message["message"]
-                    message['inputs'] = {
-                        "data": {"chatId": chat_id, "id": agent_id, "type": "assistant"},
-                        "input": msg
-                    }
-                    await service_websocket.send(json.dumps(message))
-                    print(f"Forwarded to bisheng: {message}")
+                    async for rag_response in ragflow_service.chat(token, chat_id, message["chatHistory"]):
+                        print(f"Received from ragflow: {rag_response}")
+                        json_str = rag_response[5:].strip()
+                        json_data = json.loads(json_str)
+                        if json_data.get("data") is not True:
+                            answer = json_data.get("data", {}).get("answer", "")
+                            result = {"message": answer, "type": "stream"}
+                        else:
+                            result = {"message": "", "type": "close"}
+                        await websocket.send_json(result)
+                        print(f"Forwarded to client {chat_id}: {result}")
 
-
-            # 鐩戝惉姣曟槆鍙戞潵鐨勬秷鎭苟杞彂缁欏鎴风
-            async def forward_to_client():
-                while True:
-                    message = await service_websocket.recv()
-                    print(f"Received from service S: {message}")
-                    await websocket.send_text(message)
-                    print(f"Forwarded to client {chat_id}: {message}")
-
-            # 鍚姩涓や釜浠诲姟锛屽垎鍒鐞嗗鎴风鍜屾湇鍔$鐨勬秷鎭�
+            # 鍚姩浠诲姟澶勭悊瀹㈡埛绔秷鎭�
             tasks = [
-                asyncio.create_task(forward_to_service()),
-                asyncio.create_task(forward_to_client())
+                asyncio.create_task(forward_to_ragflow())
             ]
-
-            done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
-
-            # 鍙栨秷鏈畬鎴愮殑浠诲姟
-            for task in pending:
-                task.cancel()
-                try:
-                    await task
-                except asyncio.CancelledError:
-                    pass
-
+            await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
         except WebSocketDisconnect:
             print(f"Client {chat_id} disconnected")
         finally:
             del client_websockets[chat_id]
+
+    else:
+        token = get_bisheng_token(db, current_user.id)
+        service_uri = f"{settings.bisheng_websocket_url}/api/v1/assistant/chat/{agent_id}?t=&chat_id={chat_id}"
+        headers = {'cookie': f"access_token_cookie={token};"}
+
+        async with websockets.connect(service_uri, extra_headers=headers) as service_websocket:
+
+            try:
+                # 澶勭悊瀹㈡埛绔彂鏉ョ殑娑堟伅
+                async def forward_to_service():
+                    while True:
+                        message = await websocket.receive_json()
+                        print(f"Received from client {chat_id}: {message}")
+                        # 娣诲姞 'agent_id' 鍜� 'chat_id' 瀛楁
+                        message['flow_id'] = agent_id
+                        message['chat_id'] = chat_id
+                        msg = message["message"]
+                        del message["message"]
+                        message['inputs'] = {
+                            "data": {"chatId": chat_id, "id": agent_id, "type": "assistant"},
+                            "input": msg
+                        }
+                        await service_websocket.send(json.dumps(message))
+                        print(f"Forwarded to bisheng: {message}")
+
+                # 鐩戝惉姣曟槆鍙戞潵鐨勬秷鎭苟杞彂缁欏鎴风
+                async def forward_to_client():
+                    while True:
+                        message = await service_websocket.recv()
+                        print(f"Received from service S: {message}")
+                        await websocket.send_text(message)
+                        print(f"Forwarded to client {chat_id}: {message}")
+
+                # 鍚姩涓や釜浠诲姟锛屽垎鍒鐞嗗鎴风鍜屾湇鍔$鐨勬秷鎭�
+                tasks = [
+                    asyncio.create_task(forward_to_service()),
+                    asyncio.create_task(forward_to_client())
+                ]
+                done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
+
+                # 鍙栨秷鏈畬鎴愮殑浠诲姟
+                for task in pending:
+                    task.cancel()
+                    try:
+                        await task
+                    except asyncio.CancelledError:
+                        pass
+
+            except WebSocketDisconnect:
+                print(f"Client {chat_id} disconnected")
+            finally:
+                del client_websockets[chat_id]
+
+
diff --git a/app/config/config.py b/app/config/config.py
index 10c3cb8..e3d254f 100644
--- a/app/config/config.py
+++ b/app/config/config.py
@@ -11,7 +11,8 @@
     PUBLIC_KEY: str
     PRIVATE_KEY: str
     bisheng_agent_id: str
-
+    ragflow_agent_id: str
+    ragflow_chat_id: str
     def __init__(self, **kwargs):
         # Check if all required fields are provided and set them
         for field in self.__annotations__.keys():
diff --git a/app/config/config.yaml b/app/config/config.yaml
index db7638b..d3e94a4 100644
--- a/app/config/config.yaml
+++ b/app/config/config.yaml
@@ -8,4 +8,6 @@
   MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB
   -----END PUBLIC KEY-----
 PRIVATE_KEY: str
-bisheng_agent_id: 29dd57cf-1bd6-440d-af2c-2aac1c954770
\ No newline at end of file
+bisheng_agent_id: 29dd57cf-1bd6-440d-af2c-2aac1c954770
+ragflow_agent_id: 690f42554ac84ed7b8bf7605db603b2f
+ragflow_chat_id: e1d131a1b89b488e97c2194d9e2d345c
\ No newline at end of file
diff --git a/app/service/auth.py b/app/service/auth.py
index 09a4917..896b8d9 100644
--- a/app/service/auth.py
+++ b/app/service/auth.py
@@ -35,7 +35,7 @@
     if expires_delta:
         expire = datetime.utcnow() + expires_delta
     else:
-        expire = datetime.utcnow() + timedelta(minutes=15)
+        expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
     to_encode.update({"exp": expire})
     encoded_jwt = encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
     return encoded_jwt
diff --git a/app/service/ragflow.py b/app/service/ragflow.py
index 699ffbe..5fbe175 100644
--- a/app/service/ragflow.py
+++ b/app/service/ragflow.py
@@ -29,4 +29,30 @@
             )
             if response.status_code != 200:
                 raise Exception(f"Ragflow login failed: {response.text}")
-            return response.json().get('data', {}).get('access_token')
+                # 浠庡搷搴斿ご涓彁鍙� Authorization 瀛楁
+            authorization = response.headers.get('Authorization')
+            if not authorization:
+                raise Exception("Authorization header not found in response")
+            return authorization
+
+    async def chat(self, token: str, chat_id: str, chat_history: list):
+        data = {
+            "conversation_id": chat_id,
+            "messages": chat_history
+        }
+        target_url = f"{self.base_url}/v1/conversation/completion"
+        async with httpx.AsyncClient() as client:
+            headers = {
+                'Content-Type': 'application/json',
+                'Authorization': token
+            }
+            # 鍒涘缓娴佸紡璇锋眰
+            async with client.stream("POST", target_url, json=data, headers=headers) as response:
+                # 妫�鏌ュ搷搴旂姸鎬佺爜
+                if response.status_code == 200:
+                    # 娴佸紡璇诲彇鍝嶅簲
+                    async for answer in response.aiter_text():
+                        yield answer
+                else:
+                    yield f"Error: {response.status_code}"
+

--
Gitblit v1.8.0