Deploy LangChain integrations to production environments.
Use when deploying to cloud platforms, configuring containers,
or setting up production infrastructure for LangChain apps.
Trigger with phrases like "deploy langchain", "langchain production deploy",
"langchain cloud run", "langchain docker", "langchain kubernetes".
Deploy LangChain integrations to production environments.
Use when deploying to cloud platforms, configuring containers,
or setting up production infrastructure for LangChain apps.
Trigger with phrases like "deploy langchain", "langchain production deploy",
"langchain cloud run", "langchain docker", "langchain kubernetes".
allowed-tools
Read, Write, Edit, Bash(docker:*), Bash(gcloud:*)
version
1.0.0
license
MIT
author
Jeremy Longshore <jeremy@intentsolutions.io>
LangChain Deploy Integration
Overview
Deploy LangChain applications to production using containers and cloud platforms with best practices for scaling and reliability.
Prerequisites
LangChain application ready for production
Docker installed
Cloud provider account (GCP, AWS, or Azure)
API keys stored in secrets manager
Instructions
Step 1: Create Dockerfile
# Dockerfile
FROM python:3.11-slim as builder
WORKDIR /app
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt
# Production stage
FROM python:3.11-slim
WORKDIR /app
# Copy installed packages from builder
COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH
# Copy application code
COPY src/ ./src/
COPY main.py .
# Create non-root user
RUN useradd --create-home appuser
USER appuser
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import requests; requests.get('http://localhost:8080/health')"
EXPOSE 8080
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
Step 2: Create FastAPI Application
# main.pyfrom fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from contextlib import asynccontextmanager
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# Initialize LLM on startup
llm = None
chain = None@asynccontextmanagerasyncdeflifespan(app: FastAPI):
global llm, chain
# Startup
llm = ChatOpenAI(
model=os.environ.get("MODEL_NAME", "gpt-4o-mini"),
max_retries=3
)
prompt = ChatPromptTemplate.from_template("{input}")
chain = prompt | llm | StrOutputParser()
yield# Shutdownpass
app = FastAPI(lifespan=lifespan)
classChatRequest(BaseModel):
input: str
max_tokens: int = 1000classChatResponse(BaseModel):
response: str@app.get("/health")asyncdefhealth():
return {"status": "healthy", "model": os.environ.get("MODEL_NAME")}
@app.post("/chat", response_model=ChatResponse)asyncdefchat(request: ChatRequest):
try:
response = await chain.ainvoke({"input": request.input})
return ChatResponse(response=response)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))