chef_agent
This commit is contained in:
52
api/agent_api.py
Normal file
52
api/agent_api.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
from services.agent_server.chef_service import ChefService
|
||||
|
||||
# agent相关的路由
|
||||
agent_router = APIRouter(prefix="/agent", tags=["Agent相关"])
|
||||
|
||||
|
||||
class ChatRequest(BaseModel):
|
||||
thread_id: str = Field(description="当前会话的id")
|
||||
message: str = Field(description="当前会话的问题")
|
||||
|
||||
|
||||
class ChatResponse(BaseModel):
|
||||
thread_id: str
|
||||
content: str
|
||||
|
||||
|
||||
@agent_router.post("/chat", response_model=ChatResponse)
|
||||
def chat(request: ChatRequest):
|
||||
|
||||
result = ChefService.chat(
|
||||
thread_id=request.thread_id,
|
||||
message=request.message,
|
||||
)
|
||||
|
||||
messages = result["messages"]
|
||||
|
||||
# 最后一条 AI 消息
|
||||
last_message = messages[-1]
|
||||
|
||||
return ChatResponse(
|
||||
thread_id=request.thread_id,
|
||||
content=last_message.content,
|
||||
)
|
||||
|
||||
@agent_router.post("/chat_stream")
|
||||
def chat(request: ChatRequest):
|
||||
|
||||
def generate_stream():
|
||||
result = ChefService.chat_stream(
|
||||
thread_id=request.thread_id,
|
||||
message=request.message,
|
||||
)
|
||||
for chunk in result:
|
||||
yield chunk
|
||||
|
||||
return StreamingResponse(
|
||||
generate_stream(),
|
||||
media_type="text/plain; charset=utf-8"
|
||||
)
|
||||
Reference in New Issue
Block a user