How to Deploy Langchain App: Step-by-Step Guide
Langchain app, wrap your Langchain logic in a web server like FastAPI and deploy it on a cloud platform such as Heroku or Vercel. Use uvicorn to run the server locally and push your code to the cloud with a Git repository for easy deployment.Syntax
Deploying a Langchain app typically involves these parts:
- Langchain code: Your core logic using Langchain library.
- Web server: A lightweight server like FastAPI to handle requests.
- ASGI server: Uvicorn to run the FastAPI app.
- Deployment platform: Cloud services like Heroku or Vercel to host your app.
This setup lets users interact with your Langchain app over the web.
from fastapi import FastAPI from langchain.llms import OpenAI app = FastAPI() llm = OpenAI() @app.get("/generate") async def generate_text(prompt: str): response = llm(prompt) return {"result": response}
Example
This example shows a simple Langchain app using FastAPI. It takes a prompt from the URL query and returns generated text.
Run this locally with uvicorn main:app --reload and visit http://localhost:8000/generate?prompt=Hello.
from fastapi import FastAPI from langchain.llms import OpenAI app = FastAPI() llm = OpenAI() @app.get("/generate") async def generate_text(prompt: str): response = llm(prompt) return {"result": response} # To run locally: # uvicorn main:app --reload
Common Pitfalls
1. Missing environment variables: Langchain often needs API keys (like OpenAI API key). Forgetting to set these causes errors.
2. Not using an ASGI server: Running FastAPI without Uvicorn or another ASGI server will not work.
3. Ignoring deployment platform requirements: Platforms like Heroku require a Procfile and requirements.txt.
4. Not pinning dependencies: Always specify versions in requirements.txt to avoid unexpected breaks.
## Wrong: Running FastAPI app without Uvicorn # python main.py ## Right: Use Uvicorn to run # uvicorn main:app --host 0.0.0.0 --port 8000
Quick Reference
Steps to deploy your Langchain app:
- Write your Langchain logic inside a FastAPI app.
- Create
requirements.txtwith all dependencies. - Add a
Procfilefor Heroku or configure your cloud platform. - Push your code to a Git repository.
- Deploy on Heroku, Vercel, or similar platforms.
- Set environment variables like API keys on the platform dashboard.
- Run and test your app online.