'commit'
This commit is contained in:
@@ -4,13 +4,12 @@ import tempfile
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, Request, File, UploadFile, WebSocket, WebSocketDisconnect
|
||||
from fastapi.responses import JSONResponse
|
||||
from Util.XueBanUtil import get_xueban_response_async
|
||||
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
|
||||
|
||||
from Util.ASRClient import ASRClient
|
||||
from Util.ObsUtil import ObsUploader
|
||||
from Util.TTS_Pipeline import stream_and_split_text, StreamingVolcanoTTS
|
||||
|
||||
from Util.XueBanUtil import get_xueban_response_async
|
||||
|
||||
# 创建路由路由器
|
||||
router = APIRouter(prefix="/api", tags=["学伴"])
|
||||
@@ -218,49 +217,4 @@ def upload_file_to_obs(file_path: str) -> str:
|
||||
raise Exception(error_msg)
|
||||
except Exception as e:
|
||||
logger.error(f"上传文件到OBS失败: {str(e)}")
|
||||
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)
|
||||
raise
|
Reference in New Issue
Block a user