|
|
|
@ -22,6 +22,9 @@ from app.api.chat.ai.chat_service import ( |
|
|
|
|
get_scenic_detail_data, |
|
|
|
|
get_scenic_parking_data |
|
|
|
|
) |
|
|
|
|
# 导入用于异步执行同步函数的模块 |
|
|
|
|
from concurrent.futures import ThreadPoolExecutor |
|
|
|
|
import asyncio |
|
|
|
|
|
|
|
|
|
router = APIRouter() |
|
|
|
|
|
|
|
|
@ -48,6 +51,14 @@ async def h5_chat_stream(request: Request, inp: ChatIn): |
|
|
|
|
if not user_id: |
|
|
|
|
raise HTTPException(status_code=400, detail="缺少用户 ID") |
|
|
|
|
|
|
|
|
|
knowledge = None |
|
|
|
|
spot = await extract_spot(inp.message) |
|
|
|
|
if spot: |
|
|
|
|
# 使用线程池异步执行同步函数 |
|
|
|
|
loop = asyncio.get_event_loop() |
|
|
|
|
with ThreadPoolExecutor() as executor: |
|
|
|
|
knowledge_task = loop.run_in_executor(executor, fetch_and_parse_markdown, user_id, spot) |
|
|
|
|
|
|
|
|
|
# 从 Redis 中获取用户的对话历史 |
|
|
|
|
redis_client = request.app.state.redis_client |
|
|
|
|
conversation_history_key = f"conversation:{user_id}" |
|
|
|
@ -67,6 +78,7 @@ async def h5_chat_stream(request: Request, inp: ChatIn): |
|
|
|
|
cat = await classify(inp.message) |
|
|
|
|
|
|
|
|
|
async def content_stream() -> AsyncGenerator[str, None]: |
|
|
|
|
global knowledge |
|
|
|
|
nonlocal conversation_history |
|
|
|
|
try: |
|
|
|
|
if is_quick_question: |
|
|
|
@ -78,20 +90,21 @@ async def h5_chat_stream(request: Request, inp: ChatIn): |
|
|
|
|
else: |
|
|
|
|
# 原来的逻辑 |
|
|
|
|
if cat == "游玩判断": |
|
|
|
|
spot = await extract_spot(inp.message) |
|
|
|
|
data = await query_flow(request, spot) |
|
|
|
|
knowledge = fetch_and_parse_markdown(user_id,spot) |
|
|
|
|
# 等待知识库查询结果 |
|
|
|
|
if spot: |
|
|
|
|
knowledge = await knowledge_task |
|
|
|
|
#如果知识库返回的内容不包含"知识库内未找到相应资源"则拼接字符串 |
|
|
|
|
if "知识库内未找到相应资源" not in knowledge: |
|
|
|
|
if knowledge and "知识库内未找到相应资源" not in knowledge: |
|
|
|
|
data += "知识库查询到的景区内容:"+ knowledge |
|
|
|
|
async for chunk in gen_markdown_stream(inp.message, data, inp.language, conversation_history): |
|
|
|
|
yield chunk |
|
|
|
|
else: |
|
|
|
|
spot = await extract_spot(inp.message) |
|
|
|
|
# 等待知识库查询结果 |
|
|
|
|
if spot: |
|
|
|
|
knowledge = fetch_and_parse_markdown(user_id, spot) |
|
|
|
|
if "知识库内未找到相应资源" not in knowledge: |
|
|
|
|
inp.message += ";知识库查询到的景区内容:"+ knowledge |
|
|
|
|
knowledge = await knowledge_task |
|
|
|
|
if knowledge and "知识库内未找到相应资源" not in knowledge: |
|
|
|
|
inp.message += ";知识库查询到的景区内容:"+ knowledge |
|
|
|
|
async for chunk in ai_chat_stream(inp, conversation_history): |
|
|
|
|
yield chunk |
|
|
|
|
|
|
|
|
|