Giới thiệu
Agent chạy trong notebook ≠ agent chạy trên production. Bài này cover toàn bộ pipeline từ wrap agent thành API → containerize → deploy → scale.
1. FastAPI Wrapper
from fastapi import FastAPI, WebSocket
from pydantic import BaseModel
app = FastAPI()
class AgentRequest(BaseModel):
message: str
session_id: str = None
@app.post("/chat")
async def chat(request: AgentRequest):
agent = get_or_create_agent(request.session_id)
response = await agent.run(request.message)
return {"response": response, "session_id": agent.session_id}
@app.websocket("/ws/chat")
async def websocket_chat(websocket: WebSocket):
await websocket.accept()
agent = create_agent()
while True:
data = await websocket.receive_text()
async for chunk in agent.stream(data):
await websocket.send_text(chunk)
2. Docker
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
3. Scaling Considerations
- Session affinity cho stateful agents
- Redis cho shared state
- Queue-based processing cho long-running tasks
- Cost budgets per user/session
Tóm tắt
- FastAPI + WebSocket cho real-time agent API
- Docker cho reproducible deployments
- Cloud deployment: AWS ECS, GCP Cloud Run, Railway
- Session management & scaling are key challenges
Bài tập
- Wrap SimpleAgent thành FastAPI app
- Dockerize và test locally
- Implement WebSocket streaming
- Deploy lên Railway hoặc Cloud Run