实现流式聊天输出

This commit is contained in:
haotian 2025-09-08 11:22:17 +08:00
parent 9dcfa8d047
commit d7cabf856d
3 changed files with 29 additions and 7 deletions

View File

@ -24,9 +24,9 @@ JWT_SECRET_KEY = 'b01c66dc2c58dc6a0aabfe2144256be36226de378bf87f72c0c795dda67f4d
# Jwt算法
JWT_ALGORITHM = 'HS256'
# 令牌过期时间
JWT_EXPIRE_MINUTES = 14400
JWT_EXPIRE_MINUTES = 43200
# redis中令牌过期时间
JWT_REDIS_EXPIRE_MINUTES = 1440
JWT_REDIS_EXPIRE_MINUTES = 43100
# -------- 数据库配置 --------

View File

@ -203,11 +203,9 @@ async def converse_with_chat_assistant(
与聊天助手进行对话
"""
result = await RAGFlowService.converse_with_chat_assistant_services(converse_params)
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)

View File

@ -5,6 +5,8 @@ from module_admin.entity.vo.ragflow_vo import RagflowListQueryModel, ListDocumen
from config.env import RAGFlowConfig
from typing import List
import asyncio
import string
import re
class RAGFlowService:
"""
@ -180,4 +182,26 @@ class RAGFlowService:
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
i = 0
async for t in result:
try:
answer = t["data"].get("answer", "")
answer = cls.clean_text(answer)
print(repr(answer[i:]))
i = len(answer)
except Exception as e:
print(e)
print(t)
await asyncio.sleep(0.2)
@classmethod
def clean_text(cls,text):
# 只保留 数字、中文、英文、常见标点
translator = str.maketrans('', '', string.whitespace)
clean = text.translate(translator) # 删除所有空白字符(包括 \n, space 等)
return clean