发布于 2026-01-06 8 阅读
0

从提示到生产:使用 FastAPI 将 LangChain Agent Docker 化

从提示到生产:使用 FastAPI 将 LangChain Agent Docker 化

借助 Docker、FastAPI 和 LangChain,在一个无缝的容器化管道中,大幅提升您的 AI 应用部署速度。

🧠 概述

随着人工智能应用变得越来越复杂,管理依赖项、提供端点服务以及确保顺利部署成为重中之重。本文将介绍如何将封装了 FastAPI 的 LangChain 代理容器化,从而为您的智能应用创建一个可直接部署、适用于生产环境的容器。

到最后,你将:

  • 创建 LangChain 代理
  • 使用 FastAPI 进行封装,以获得简洁的 REST 接口。
  • 将整个设置容器化
  • 只需一条命令即可在任何地方运行它

📦 先决条件

开始之前,请确保您已准备好:

  • Docker 已安装
  • Python 3.10+(用于本地测试)
  • OpenAI API 密钥或 LangChain 支持的任何 LLM 密钥

📁 项目结构

langchain-agent-api/
├── agent_app/
│ ├── main.py
│ └── agent.py
├── requirements.txt
├── Dockerfile
└── .env

PostgreSQL

✨ 第一步:创建 LangChain 代理

agent_app/agent.py

from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain.utilities import SerpAPIWrapper
import os

def create_agent():
    llm = OpenAI(temperature=0, openai_api_key=os.getenv("OPENAI_API_KEY"))
    search = SerpAPIWrapper()
    tools = [Tool(name="Search", func=search.run, description="Useful for answering general questions.")]
    agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
    return agent
Enter fullscreen mode Exit fullscreen mode

🚀 第二步:使用 FastAPI
agent_app/main.py进行封装

from fastapi import FastAPI
from pydantic import BaseModel
from agent import create_agent

app = FastAPI()
agent = create_agent()

class Query(BaseModel):
    question: str

@app.post("/ask")
async def ask_question(query: Query):
    response = agent.run(query.question)
    return {"response": response}
Enter fullscreen mode Exit fullscreen mode

📄 第三步:定义需求
requirements.txt

fastapi
uvicorn
langchain
openai
python-dotenv
Enter fullscreen mode Exit fullscreen mode

💡 根据需要添加 serpapi 或其他工具。

🛠️ 第 4 步:Dockerfile
Dockerfile

FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY agent_app ./agent_app
COPY .env .

CMD ["uvicorn", "agent_app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Enter fullscreen mode Exit fullscreen mode

🔑 第 5 步:添加环境变量
.env

OPENAI_API_KEY=您的 OpenAI 密钥
SERPAPI_API_KEY=您的 SerPAPI 密钥

⚠️ 切勿将 .env 文件提交到公共仓库。生产环境中请使用 Docker 密钥或 CI/CD 环境变量。

🧪 第 6 步:构建并运行
🧱 构建 Docker 镜像

docker build -t langchain-agent-api .
Enter fullscreen mode Exit fullscreen mode

🚀 运行容器

docker run --env-file .env -p 8000:8000 langchain-agent-api
Enter fullscreen mode Exit fullscreen mode

📬 试用
运行后,使用以下命令测试您的代理:

curl -X POST http://localhost:8000/ask \
-H "Content-Type: application/json" \
-d '{"question": "Who is the CEO of OpenAI?"}'
Enter fullscreen mode Exit fullscreen mode

您的容器化 LangChain 代理应该会在几秒钟内响应!🤖

📦 额外提示:添加 Docker Compose(可选)
docker-compose.yml


version: "3.8"
services:
  langchain:
    build: .
    ports:
      - "8000:8000"
    env_file:
      - .env
Enter fullscreen mode Exit fullscreen mode

然后运行:


docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

🏁 总结:
您现在拥有一个可用于生产环境的容器化 LangChain 代理,并通过 FastAPI 提供服务。无论您是构建内部 AI 工具还是部署到云端,此设置都能为您提供可重复性、可移植性和强大的功能。

文章来源:https://dev.to/moni121189/from-prompt-to-production-dockerizing-a-langchain-agent-with-fastapi-1pe3