feat: 支持文字输入对话
This commit is contained in:
41
server.py
41
server.py
@@ -130,6 +130,47 @@ async def voice_chat(
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@app.post("/voice/text", response_model=VoiceResponse)
|
||||
async def text_chat(
|
||||
text: str = Form(..., description="文本消息"),
|
||||
conversation_id: Optional[str] = Form(None, description="对话ID")
|
||||
):
|
||||
"""
|
||||
文字聊天接口
|
||||
转发到模型服务
|
||||
"""
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
form = aiohttp.FormData()
|
||||
form.add_field('text', text)
|
||||
if conversation_id:
|
||||
form.add_field('conversation_id', conversation_id)
|
||||
|
||||
async with session.post(
|
||||
f"{MODEL_SERVICE_URL}/api/voice/text",
|
||||
data=form,
|
||||
timeout=aiohttp.ClientTimeout(total=120)
|
||||
) as resp:
|
||||
if resp.status != 200:
|
||||
error_text = await resp.text()
|
||||
logger.error(f"Model service error: {error_text}")
|
||||
raise HTTPException(status_code=resp.status, detail=error_text)
|
||||
|
||||
data = await resp.json()
|
||||
return VoiceResponse(
|
||||
reply=data["reply"],
|
||||
conversation_id=data["conversation_id"],
|
||||
timestamp=data.get("timestamp", datetime.now().isoformat())
|
||||
)
|
||||
|
||||
except aiohttp.ClientError as e:
|
||||
logger.error(f"Connection error: {e}")
|
||||
raise HTTPException(status_code=503, detail="模型服务连接失败")
|
||||
except Exception as e:
|
||||
logger.error(f"Text chat error: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@app.delete("/conversation/{conversation_id}")
|
||||
async def delete_conversation(conversation_id: str):
|
||||
"""删除对话"""
|
||||
|
||||
Reference in New Issue
Block a user