This commit is contained in:
2025-08-31 12:50:37 +08:00
parent 692ddab386
commit ef2904aca2
3 changed files with 6 additions and 50 deletions

View File

@@ -4,13 +4,12 @@ import tempfile
import uuid import uuid
from datetime import datetime from datetime import datetime
from fastapi import APIRouter, Request, File, UploadFile, WebSocket, WebSocketDisconnect from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from fastapi.responses import JSONResponse
from Util.XueBanUtil import get_xueban_response_async
from Util.ASRClient import ASRClient from Util.ASRClient import ASRClient
from Util.ObsUtil import ObsUploader from Util.ObsUtil import ObsUploader
from Util.TTS_Pipeline import stream_and_split_text, StreamingVolcanoTTS from Util.TTS_Pipeline import stream_and_split_text, StreamingVolcanoTTS
from Util.XueBanUtil import get_xueban_response_async
# 创建路由路由器 # 创建路由路由器
router = APIRouter(prefix="/api", tags=["学伴"]) router = APIRouter(prefix="/api", tags=["学伴"])
@@ -219,48 +218,3 @@ def upload_file_to_obs(file_path: str) -> str:
except Exception as e: except Exception as e:
logger.error(f"上传文件到OBS失败: {str(e)}") logger.error(f"上传文件到OBS失败: {str(e)}")
raise raise
@router.post("/xueban/chat")
async def chat_with_xueban(request: Request):
"""
与学伴大模型聊天的接口
- 参数: request body 中的 query_text (用户查询文本)
- 返回: JSON包含聊天响应
"""
try:
# 获取请求体数据
data = await request.json()
query_text = data.get("query_text", "")
if not query_text.strip():
return JSONResponse(content={
"success": False,
"message": "查询文本不能为空"
}, status_code=400)
# 记录日志
logger.info(f"接收到学伴聊天请求: {query_text}")
# 调用异步接口获取学伴响应
response_content = []
async for chunk in get_xueban_response_async(query_text, stream=True):
response_content.append(chunk)
full_response = "".join(response_content)
# 返回响应
return JSONResponse(content={
"success": True,
"message": "聊天成功",
"data": {
"response": full_response
}
})
except Exception as e:
logger.error(f"学伴聊天失败: {str(e)}")
return JSONResponse(content={
"success": False,
"message": f"聊天处理失败: {str(e)}"
}, status_code=500)

File diff suppressed because one or more lines are too long