邓立国Agent开发入门必读书《AI Agent智能体开发实践》1~12章试读_《ai agent 智能体开发实践》在线阅读-CSDN博客
(1)本地部署使用FastAPI将智能体包装为API服务:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="LangChain问答API")
class QuestionRequest(BaseModel):
question: str
@app.post("/ask")
def ask_question(request: QuestionRequest):
response = get_answer(request.question)
return response
#启动命令
# uvicorn main:app --reload
(2)云部署则使用Docker进行部署,Dockerfile示例如下:
#使用Python基础镜像
FROM python:3.9-slim
#设置工作目录
WORKDIR /app
#安装依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
#复制应用代码
COPY . .
#暴露端口
EXPOSE 8000
#启动应用
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]