初步实现流式聊天
This commit is contained in:
parent
1aa5a547f2
commit
9dcfa8d047
@ -15,7 +15,8 @@ from utils.log_util import logger
|
||||
# from utils.page_util import PageResponseModel
|
||||
from utils.response_util import ResponseUtil
|
||||
from module_admin.entity.vo.ragflow_vo import RagflowListQueryModel, ListDocumentsQueryModel, UpdateFileModel, DeleteFileModel, CreateDatasetModel, DocumentIdsModel, UpdateChatAssistantModel,\
|
||||
CreateSessionWithChatModel
|
||||
CreateSessionWithChatModel, ConverseWithChatAssistantModel
|
||||
import asyncio
|
||||
# from config.env import RAGFlowConfig
|
||||
|
||||
|
||||
@ -193,6 +194,23 @@ async def create_session_with_chat(
|
||||
result = await RAGFlowService.create_session_with_chat_services(create_params)
|
||||
return parse_result(result)
|
||||
|
||||
# 与聊天助手进行对话
|
||||
@ragflowController.post('/converse_with_chat_assistant')
|
||||
async def converse_with_chat_assistant(
|
||||
converse_params: ConverseWithChatAssistantModel,
|
||||
):
|
||||
"""
|
||||
与聊天助手进行对话
|
||||
"""
|
||||
|
||||
result = await RAGFlowService.converse_with_chat_assistant_services(converse_params)
|
||||
|
||||
async for t in result:
|
||||
print(t)
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
# return parse_result(result)
|
||||
|
||||
def parse_result(result):
|
||||
code = result.get('code', 0)
|
||||
if code != 0:
|
||||
|
||||
@ -131,6 +131,18 @@ class CreateSessionWithChatModel(BaseModel):
|
||||
name: str = Field(default = None, description='会话名称')
|
||||
user_id: Optional[str] = Field(default = None, description='用户ID')
|
||||
|
||||
class ConverseWithChatAssistantModel(BaseModel):
|
||||
"""
|
||||
会话聊天模型
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
|
||||
|
||||
chat_id: str = Field(default = None, description='会话ID')
|
||||
question: str = Field(default = None, description='问题')
|
||||
stream: Optional[bool] = Field(default = True, description='是否流式返回')
|
||||
session_id: Optional[str] = Field(default = None, description='会话ID')
|
||||
user_id: Optional[str] = Field(default = None, description='用户ID')
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from utils.ragflow_asy_util import AsyncRAGFlowClient
|
||||
from module_admin.entity.vo.ragflow_vo import RagflowListQueryModel, ListDocumentsQueryModel, UpdateFileModel, DeleteFileModel, CreateDatasetModel, DocumentIdsModel, UpdateChatAssistantModel \
|
||||
,CreateSessionWithChatModel
|
||||
,CreateSessionWithChatModel, ConverseWithChatAssistantModel
|
||||
from config.env import RAGFlowConfig
|
||||
from typing import List
|
||||
import asyncio
|
||||
|
||||
class RAGFlowService:
|
||||
"""
|
||||
@ -170,4 +171,13 @@ class RAGFlowService:
|
||||
async def create_session_with_chat_services(cls, create_params: CreateSessionWithChatModel):
|
||||
async with AsyncRAGFlowClient(RAGFlowConfig.RAGFLOW_BASE_URL, RAGFlowConfig.RAGFLOW_API_KEY) as client:
|
||||
result = await client.create_session_with_chat(**(create_params.model_dump()))
|
||||
return result
|
||||
|
||||
|
||||
# 与助手聊天
|
||||
@classmethod
|
||||
async def converse_with_chat_assistant_services(cls, converse_params: ConverseWithChatAssistantModel):
|
||||
async with AsyncRAGFlowClient(RAGFlowConfig.RAGFLOW_BASE_URL, RAGFlowConfig.RAGFLOW_API_KEY) as client:
|
||||
result = await client.converse_with_chat_assistant(**(converse_params.model_dump()))
|
||||
|
||||
return result
|
||||
@ -669,6 +669,8 @@ class AsyncRAGFlowClient:
|
||||
|
||||
if session_id:
|
||||
data["session_id"] = session_id
|
||||
|
||||
print(f"开始对话: {question} {session_id}")
|
||||
if user_id:
|
||||
data["user_id"] = user_id
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user