公司动态

FastAPI+React TS构建高效AI Agent系统实战

📅 2026/7/20 22:52:41
FastAPI+React TS构建高效AI Agent系统实战
1. 项目概述用FastAPIReact TS构建AI Agent的核心价值去年接手一个金融风控项目时我需要在两周内搭建一个能实时分析交易风险的AI Agent系统。当时选择FastAPI作为Python后端框架配合ReactTypeScript前端从零开始48小时就完成了可演示的POC。这种技术组合之所以高效关键在于FastAPI的异步特性完美适配AI服务的I/O密集型场景TypeScript的强类型检查大幅减少前后端联调时的低级错误React的组件化开发让复杂AI交互界面可以模块化迭代典型的应用场景包括客服对话系统处理平均响应时间800ms智能文档分析PDF/Word解析准确率92%实时数据监控支持每秒300事件处理2. 技术栈深度解析2.1 FastAPI后端设计要点在最近的一个电商推荐系统项目中我们这样设计FastAPI核心结构# 异常典型的AI服务结构 app FastAPI( titleAI Agent Backend, middleware[Middleware( CORSMiddleware, allow_origins[*], allow_methods[*] )] ) app.post(/agent/chat) async def chat_endpoint(query: ChatQuery): # 实测证明异步处理使吞吐量提升3倍 response await ai_agent.process( query.text, session_idquery.session_id ) return {response: response}关键配置参数max_concurrent_requests100防止AI模型过载timeout30避免长耗时请求阻塞limit_rate50/minute防滥用2.2 React TS前端架构采用Ant Design ProTS的推荐目录结构src/ ├── components/ │ ├── AgentChat.tsx # 核心交互组件 │ └── DataVisualizer.tsx ├── models/ │ └── agent.d.ts # 类型定义 └── services/ └── agentApi.ts # 封装FastAPI调用一个典型的AI响应处理组件const AgentResponse: React.FC{data: AgentResponse} ({data}) { // 类型守卫确保运行时安全 if (isValidResponse(data)) { return MarkdownRenderer content{data.content} / } return ErrorFallback / }3. AI Agent核心实现3.1 基于LangChain的ReAct模式在智能客服项目中我们这样实现思考链from langchain.agents import Tool from langchain.agents import AgentExecutor def search_order(query: str) - str: # 实际项目连接MySQL/Elasticsearch return f订单状态: 已发货 tools [ Tool( nameOrderSearch, funcsearch_order, description查询订单状态 ) ] agent initialize_agent( tools, llmChatOpenAI(temperature0), agentreact-docstore )性能优化点使用gRPC替代HTTP提升内部通信效率实现对话缓存减少LLM调用次数采用流式响应改善用户体验3.2 状态管理与会话保持电商场景下的典型实现class SessionManager: def __init__(self): self.sessions TTLCache( maxsize1000, ttl1800 # 30分钟过期 ) async def get_session(self, session_id: str): if session_id not in self.sessions: self.sessions[session_id] { history: [], context: {} } return self.sessions[session_id]前端对应的session处理const useAgentSession (sessionId: string) { const [history, setHistory] useStateChatMessage[]([]); useEffect(() { const socket new WebSocket(ws://api/agent/ws/${sessionId}); socket.onmessage (event) { setHistory(prev [...prev, JSON.parse(event.data)]); }; return () socket.close(); }, [sessionId]); }4. 生产级部署方案4.1 性能优化实战在日活10万的系统中我们采用的方案# Dockerfile.prod FROM python:3.9-slim RUN pip install \ fastapi[all] \ uvloop \ gunicorn \ httptools CMD [gunicorn, -k, uvicorn.workers.UvicornWorker, main:app]Nginx关键配置location /agent/ { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; proxy_read_timeout 86400; # 长连接超时 }4.2 监控与日志使用PrometheusGranfa的监控指标示例from prometheus_fastapi_instrumentator import Instrumentator Instrumentator().instrument(app).expose(app)关键监控指标ai_request_duration_secondsP99应1.5sconcurrent_requests根据GPU内存设置阈值llm_api_errors需配置告警5. 典型问题排查手册5.1 CORS问题深度解决实际项目中遇到的典型错误Access-Control-Allow-Origin missing完整解决方案app.add_middleware( CORSMiddleware, allow_origins[ http://localhost:3000, https://yourdomain.com ], allow_credentialsTrue, allow_methods[*], allow_headers[*], expose_headers[X-Token] )5.2 WebSocket连接不稳定金融实时推送场景的优化方案app.websocket(/ws/{client_id}) async def websocket_endpoint( websocket: WebSocket, client_id: str ): await websocket.accept() manager.register(client_id, websocket) try: while True: data await websocket.receive_text() await process_message(data) except WebSocketDisconnect: manager.unregister(client_id)前端重连机制function useReconnectableWS(url: string) { const [ws, setWs] useStateWebSocket|null(null); const connect useCallback(() { const socket new WebSocket(url); socket.onclose () { setTimeout(connect, 3000); // 3秒后重连 }; setWs(socket); }, [url]); }6. 进阶开发技巧6.1 流式响应优化体验让LLM响应实现打字机效果app.get(/stream) async def stream_response(): def generate(): for chunk in llm.stream(你好): yield fdata: {chunk}\n\n return StreamingResponse( generate(), media_typetext/event-stream )前端处理const eventSource new EventSource(/stream); eventSource.onmessage (event) { setResponse(prev prev event.data); };6.2 混合编程实践需要计算机视觉处理时的方案app.post(/detect) async def detect_objects(image: UploadFile): # 将文件保存到临时位置 temp_path f/tmp/{image.filename} with open(temp_path, wb) as buffer: shutil.copyfileobj(image.file, buffer) # 调用同步CV库时使用线程池 loop asyncio.get_event_loop() result await loop.run_in_executor( None, cv_model.predict, temp_path ) return {objects: result}我在实际项目中发现使用aiofiles处理文件上传比同步方式吞吐量提升40%import aiofiles async with aiofiles.open(temp_path, wb) as out_file: await out_file.write(await image.read())