0
0
LangchainHow-ToBeginner ยท 4 min read

How to Deploy Langchain App: Step-by-Step Guide

To deploy a 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.

python
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.

python
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
Output
{"result": "Hello! How can I help you today?"}
โš ๏ธ

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.

bash
## 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.txt with all dependencies.
  • Add a Procfile for 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.
โœ…

Key Takeaways

Wrap Langchain code in a FastAPI app to serve it over the web.
Use Uvicorn to run your FastAPI app locally and in production.
Set all required API keys as environment variables before deployment.
Prepare deployment files like requirements.txt and Procfile for cloud platforms.
Test your deployed app by sending requests to its public URL.