LangChain OpenTutorial
  • 🦜️🔗 The LangChain Open Tutorial for Everyone
  • 01-Basic
    • Getting Started on Windows
    • 02-Getting-Started-Mac
    • OpenAI API Key Generation and Testing Guide
    • LangSmith Tracking Setup
    • Using the OpenAI API (GPT-4o Multimodal)
    • Basic Example: Prompt+Model+OutputParser
    • LCEL Interface
    • Runnable
  • 02-Prompt
    • Prompt Template
    • Few-Shot Templates
    • LangChain Hub
    • Personal Prompts for LangChain
    • Prompt Caching
  • 03-OutputParser
    • PydanticOutputParser
    • PydanticOutputParser
    • CommaSeparatedListOutputParser
    • Structured Output Parser
    • JsonOutputParser
    • PandasDataFrameOutputParser
    • DatetimeOutputParser
    • EnumOutputParser
    • Output Fixing Parser
  • 04-Model
    • Using Various LLM Models
    • Chat Models
    • Caching
    • Caching VLLM
    • Model Serialization
    • Check Token Usage
    • Google Generative AI
    • Huggingface Endpoints
    • HuggingFace Local
    • HuggingFace Pipeline
    • ChatOllama
    • GPT4ALL
    • Video Q&A LLM (Gemini)
  • 05-Memory
    • ConversationBufferMemory
    • ConversationBufferWindowMemory
    • ConversationTokenBufferMemory
    • ConversationEntityMemory
    • ConversationKGMemory
    • ConversationSummaryMemory
    • VectorStoreRetrieverMemory
    • LCEL (Remembering Conversation History): Adding Memory
    • Memory Using SQLite
    • Conversation With History
  • 06-DocumentLoader
    • Document & Document Loader
    • PDF Loader
    • WebBaseLoader
    • CSV Loader
    • Excel File Loading in LangChain
    • Microsoft Word(doc, docx) With Langchain
    • Microsoft PowerPoint
    • TXT Loader
    • JSON
    • Arxiv Loader
    • UpstageDocumentParseLoader
    • LlamaParse
    • HWP (Hangeul) Loader
  • 07-TextSplitter
    • Character Text Splitter
    • 02. RecursiveCharacterTextSplitter
    • Text Splitting Methods in NLP
    • TokenTextSplitter
    • SemanticChunker
    • Split code with Langchain
    • MarkdownHeaderTextSplitter
    • HTMLHeaderTextSplitter
    • RecursiveJsonSplitter
  • 08-Embedding
    • OpenAI Embeddings
    • CacheBackedEmbeddings
    • HuggingFace Embeddings
    • Upstage
    • Ollama Embeddings With Langchain
    • LlamaCpp Embeddings With Langchain
    • GPT4ALL
    • Multimodal Embeddings With Langchain
  • 09-VectorStore
    • Vector Stores
    • Chroma
    • Faiss
    • Pinecone
    • Qdrant
    • Elasticsearch
    • MongoDB Atlas
    • PGVector
    • Neo4j
    • Weaviate
    • Faiss
    • {VectorStore Name}
  • 10-Retriever
    • VectorStore-backed Retriever
    • Contextual Compression Retriever
    • Ensemble Retriever
    • Long Context Reorder
    • Parent Document Retriever
    • MultiQueryRetriever
    • MultiVectorRetriever
    • Self-querying
    • TimeWeightedVectorStoreRetriever
    • TimeWeightedVectorStoreRetriever
    • Kiwi BM25 Retriever
    • Ensemble Retriever with Convex Combination (CC)
  • 11-Reranker
    • Cross Encoder Reranker
    • JinaReranker
    • FlashRank Reranker
  • 12-RAG
    • Understanding the basic structure of RAG
    • RAG Basic WebBaseLoader
    • Exploring RAG in LangChain
    • RAPTOR: Recursive Abstractive Processing for Tree-Organized Retrieval
    • Conversation-With-History
    • Translation
    • Multi Modal RAG
  • 13-LangChain-Expression-Language
    • RunnablePassthrough
    • Inspect Runnables
    • RunnableLambda
    • Routing
    • Runnable Parallel
    • Configure-Runtime-Chain-Components
    • Creating Runnable objects with chain decorator
    • RunnableWithMessageHistory
    • Generator
    • Binding
    • Fallbacks
    • RunnableRetry
    • WithListeners
    • How to stream runnables
  • 14-Chains
    • Summarization
    • SQL
    • Structured Output Chain
    • StructuredDataChat
  • 15-Agent
    • Tools
    • Bind Tools
    • Tool Calling Agent
    • Tool Calling Agent with More LLM Models
    • Iteration-human-in-the-loop
    • Agentic RAG
    • CSV/Excel Analysis Agent
    • Agent-with-Toolkits-File-Management
    • Make Report Using RAG, Web searching, Image generation Agent
    • TwoAgentDebateWithTools
    • React Agent
  • 16-Evaluations
    • Generate synthetic test dataset (with RAGAS)
    • Evaluation using RAGAS
    • HF-Upload
    • LangSmith-Dataset
    • LLM-as-Judge
    • Embedding-based Evaluator(embedding_distance)
    • LangSmith Custom LLM Evaluation
    • Heuristic Evaluation
    • Compare experiment evaluations
    • Summary Evaluators
    • Groundedness Evaluation
    • Pairwise Evaluation
    • LangSmith Repeat Evaluation
    • LangSmith Online Evaluation
    • LangFuse Online Evaluation
  • 17-LangGraph
    • 01-Core-Features
      • Understanding Common Python Syntax Used in LangGraph
      • Title
      • Building a Basic Chatbot with LangGraph
      • Building an Agent with LangGraph
      • Agent with Memory
      • LangGraph Streaming Outputs
      • Human-in-the-loop
      • LangGraph Manual State Update
      • Asking Humans for Help: Customizing State in LangGraph
      • DeleteMessages
      • DeleteMessages
      • LangGraph ToolNode
      • LangGraph ToolNode
      • Branch Creation for Parallel Node Execution
      • Conversation Summaries with LangGraph
      • Conversation Summaries with LangGraph
      • LangGrpah Subgraph
      • How to transform the input and output of a subgraph
      • LangGraph Streaming Mode
      • Errors
      • A Long-Term Memory Agent
    • 02-Structures
      • LangGraph-Building-Graphs
      • Naive RAG
      • Add Groundedness Check
      • Adding a Web Search Module
      • LangGraph-Add-Query-Rewrite
      • Agentic RAG
      • Adaptive RAG
      • Multi-Agent Structures (1)
      • Multi Agent Structures (2)
    • 03-Use-Cases
      • LangGraph Agent Simulation
      • Meta Prompt Generator based on User Requirements
      • CRAG: Corrective RAG
      • Plan-and-Execute
      • Multi Agent Collaboration Network
      • Multi Agent Collaboration Network
      • Multi-Agent Supervisor
      • 08-LangGraph-Hierarchical-Multi-Agent-Teams
      • 08-LangGraph-Hierarchical-Multi-Agent-Teams
      • SQL-Agent
      • 10-LangGraph-Research-Assistant
      • LangGraph Code Assistant
      • Deploy on LangGraph Cloud
      • Tree of Thoughts (ToT)
      • Ollama Deep Researcher (Deepseek-R1)
      • Functional API
      • Reflection in LangGraph
  • 19-Cookbook
    • 01-SQL
      • TextToSQL
      • SpeechToSQL
    • 02-RecommendationSystem
      • ResumeRecommendationReview
    • 03-GraphDB
      • Movie QA System with Graph Database
      • 05-TitanicQASystem
      • Real-Time GraphRAG QA
    • 04-GraphRAG
      • Academic Search System
      • Academic QA System with GraphRAG
    • 05-AIMemoryManagementSystem
      • ConversationMemoryManagementSystem
    • 06-Multimodal
      • Multimodal RAG
      • Shopping QnA
    • 07-Agent
      • 14-MoARAG
      • CoT Based Smart Web Search
      • 16-MultiAgentShoppingMallSystem
      • Agent-Based Dynamic Slot Filling
      • Code Debugging System
      • New Employee Onboarding Chatbot
      • 20-LangGraphStudio-MultiAgent
      • Multi-Agent Scheduler System
    • 08-Serving
      • FastAPI Serving
      • Sending Requests to Remote Graph Server
      • Building a Agent API with LangServe: Integrating Currency Exchange and Trip Planning
    • 08-SyntheticDataset
      • Synthetic Dataset Generation using RAG
    • 09-Monitoring
      • Langfuse Selfhosting
Powered by GitBook
On this page
  • Overview
  • Table of Contents
  • References
  • Environment Setup
  • What is FastAPI
  • FastAPI Features
  • How to run a server
  • FastAPI Fast Tutorial
  • How to run code
  • Define Path Parameters
  • Define Query Parameters
  • Define Request Model
  • Define Response Model
  • FastAPI Serving of LangChain
  1. 19-Cookbook
  2. 08-Serving

FastAPI Serving

Previous08-ServingNextSending Requests to Remote Graph Server

Last updated 7 days ago

  • Author:

  • Peer Review:

  • Proofread :

  • This is a part of

Overview

This tutorial is about FastAPI Serving. FastAPI is one of the python web frameworks that supports asynchronous programming and is very fast.

In this tutorial, we will implement the following FastAPI examples.

  • Implement different types of parameters

  • Declare an input/output data model

  • Serve a langchain with FastAPI

Table of Contents

References


Environment Setup

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

%%capture --no-stderr
%pip install langchain-opentutorial
# Install required packages
from langchain_opentutorial import package

package.install(
    [
        "uvicorn",
        "fastapi",
        "pydantic",
        "typing",
        "pydantic",
        "langchain_openai",
        "langchain_core",
        "langchain_community",
        "langchain_chroma",
    ],
    verbose=False,
    upgrade=False,
)
    [notice] A new release of pip is available: 24.3.1 -> 25.0.1
    [notice] To update, run: pip install --upgrade pip
# Set environment variables
from langchain_opentutorial import set_env

set_env(
    {
        "OPENAI_API_KEY": "",
        "LANGCHAIN_API_KEY": "",
        "LANGCHAIN_TRACING_V2": "true",
        "LANGCHAIN_ENDPOINT": "https://api.smith.langchain.com",
        "LANGCHAIN_PROJECT": "FastAPI-Serving",
    }
)
Environment variables have been set successfully.

You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.

[Note] This is not necessary if you've already set the required API keys in previous steps.

# Load API keys from .env file
from dotenv import load_dotenv

load_dotenv(override=True)
False

What is FastAPI

FastAPI is a modern, high-performance web framework for building APIs with Python, based on standard Python type hints.

Key features include:

  • Speed: Built on Starlette and Pydantic, it is fully compatible with these tools and delivers extremely high performance—on par with NodeJS and Go—making it one of the fastest Python frameworks available.

  • Fast coding: Increases feature development speed by approximately 200% to 300%.

  • Fewer bugs: Reduces human (developer) errors by around 40%.

  • Intuitive: Offers excellent editor support with autocomplete everywhere, reducing debugging time.

  • Easy: Designed to be simple to use and learn, cutting down on the time needed to read documentation.

  • Robust: Provides production-ready code along with automatically generated interactive documentation.

  • Standards-based: Built on open, fully compatible standards for APIs, such as OpenAPI (formerly known as Swagger) and JSON Schema.

FastAPI Features

Key Features:

  • Supports asynchronous programming.

  • Provides automatically updating interactive API documentation (Swagger UI), allowing you to interact with your API directly.

  • Boosts coding speed with excellent editor support through autocomplete and type checking.

  • Seamlessly integrates security and authentication, enabling use without compromising your database or data models while incorporating numerous security features—including those from Starlette.

  • Automatically handles dependency injection, making it easy to use.

  • Built on Starlette and Pydantic, ensuring full compatibility.

How to run a server

You can find the API documentation in the /docs path and interact with it directly via the Try it out button.

To spin up a live server, you can copy the code to a .py file and run it by typing uvicorn [file name]:[FastAPI instance] --reload in a shell.

For this tutorial, we'll temporarily run the server from the .ipynb file with the following code

import uvicorn
import nest_asynci

nest_asyncio.apply()
uvicorn.run(app)

FastAPI Fast Tutorial

Quickly learn how to communicate with the API via FastAPI.

  • Create an instance of FastAPI with FastAPI().

  • Define a path operation decorator to communicate with the path by setting the HTTP Method on the path.

How to run code

When you run the code block, it's loading infinitely, which means the server is running.

We recommend testing the API at http://127.0.0.1:8000/docs

import uvicorn
import nest_asyncio
from fastapi import FastAPI

app = FastAPI()  ## create FastAPI instance


# FastAPI decorators are used to set routing paths
@app.get("/")
def read_root():
    return "hello"


nest_asyncio.apply()
uvicorn.run(app)
INFO:     Started server process [26086]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:54044 - "GET / HTTP/1.1" 200 OK


INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [26086]

Define Path Parameters

  • You can set parameters on a path and use them as variables inside a function by setting the arguments of the function.

  • You can declare the type of the path parameters in your function using Python's standard type annotations.

  • FastAPI will automatically ‘parse’ the request to validate the type of the data.

app = FastAPI()  # create FastAPI instance


# Declare route parameters by adding parameters to the route.
@app.get("/chat/{chat_id}")
def read_chat(chat_id: int):  # Pass the path parameter as a parameter of the function.
    return {"chat_id": chat_id}


nest_asyncio.apply()
uvicorn.run(app)
INFO:     Started server process [26086]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:54093 - "GET / HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:54094 - "GET /chat/123 HTTP/1.1" 200 OK
INFO:     127.0.0.1:54094 - "GET /chat/hello HTTP/1.1" 422 Unprocessable Entity


INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [26086]

Define Query Parameters

  • If you declare a function parameter other than as part of a path parameter, FastAPI automatically interprets it as a query parameter.

  • Query parameters can be declared as optional parameters by setting their default value to None.

app = FastAPI()


# Declare the path parameter and the query parameter.
@app.get("/chat/{chat_id}")
def read_item(chat_id: int, item_id: int, q: str | None = None):
    # item_id, q is the query parameter, and q is an optional parameter.
    return {"chat_id": chat_id, "item_id": item_id, "q": q}


nest_asyncio.apply()
uvicorn.run(app)
INFO:     Started server process [26086]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
    INFO:     Shutting down
    INFO:     Waiting for application shutdown.
    INFO:     Application shutdown complete.
    INFO:     Finished server process [26086]

Define Request Model

  • It can be defined using the Pydantic model.

  • Request is the data sent from the client to the API. Response is the data that the API sends back to the client.

  • You can declare the request body, path, and query parameters together.

Note: It is not recommended to include a body in a GET request.

from pydantic import BaseModel

app = FastAPI()


# Define an Item class that is the Request Model.
class Item(BaseModel):
    name: str
    description: str | None = None  # Optionally set it by declaring a default value.
    price: float
    tax: float | None = None


@app.post("/items/{item_id}")
async def create_item(item_id: int, item: Item, q: str | None = None):
    result = {"item_id": item_id, **item.model_dump()}
    # if q exists, add q to result
    if q:
        result.update({"q": q})
    # add price_with_tax if tax exists
    if item.tax is not None:
        price_with_tax = item.price + item.tax
        result.update({"price_with_tax": price_with_tax})
    return result


nest_asyncio.apply()
uvicorn.run(app)
INFO:     Started server process [26086]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
    INFO:     Shutting down
    INFO:     Waiting for application shutdown.
    INFO:     Application shutdown complete.
    INFO:     Finished server process [26086]

Define Response Model

You can define the return type by adding the response_model parameter to the path operation decorator.

This allows you to exclude sensitive data received from the input model from the output.

FastAPI provides the following features when setting the output model

  • Converting output data to type declarations

  • Data validation

  • Add JSON schema to the response in the Swagger UI

from typing import Any

app = FastAPI()


class PostIn(BaseModel):
    postId: str
    password: str
    description: str | None = None  # Optionally set it by declaring a default value.
    content: str


class PostOut(BaseModel):
    postId: str
    description: str | None = None  # Optionally set it by declaring a default value.
    content: str


@app.post("/posts", response_model=PostOut)
async def create_Post(post: PostIn) -> Any:
    return post


nest_asyncio.apply()
uvicorn.run(app)
INFO:     Started server process [26086]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
    INFO:     Shutting down
    INFO:     Waiting for application shutdown.
    INFO:     Application shutdown complete.
    INFO:     Finished server process [26086]

FastAPI Serving of LangChain

  • Try serving a langchain with the fastAPI.

  • Use what you have learnt above.

  • Implement stream output in the fastAPI.

from typing import List
from fastapi import FastAPI
from dotenv import load_dotenv
from langchain_chroma import Chroma
from pydantic import BaseModel, Field
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from fastapi.responses import StreamingResponse
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import JsonOutputParser

DB_PATH = "../data/chroma_db"

load_dotenv()


# Define the chat output data structure.
class ChatReturnType(BaseModel):
    question: str = Field(description="question")
    answer: str = Field(description="answer")


# Defines the chat stream output data structure.
class ChatReturnStreamType(BaseModel):
    question: str = Field(description="question")
    answer: str = Field(description="answer")


# Define the Add contents input data type.
class AddContentsInType(BaseModel):
    content: List[str]
    source: List[dict]


# Define the Add contents output data type.
class AddContentsOutType(BaseModel):
    content: List[str]
    source: List[dict]
    id: List[str]


chroma = Chroma(
    collection_name="FastApiServing",
    persist_directory=DB_PATH,
    embedding_function=OpenAIEmbeddings(),
)

retriever = chroma.as_retriever(
    search_kwargs={
        "k": 4,
    }
)

parser = JsonOutputParser(pydantic_object=ChatReturnType)

prompt = ChatPromptTemplate(
    [
        ("system", "You are a friendly AI assistant. Answer questions concisely.’"),
        (
            "system",
            "Answer the question based only on the following context: {context}",
        ),
        ("user", "#Format: {format_instructions}\n\n#Question: {question}"),
    ]
)
prompt = prompt.partial(format_instructions=parser.get_format_instructions())

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | JsonOutputParser()
)

app = FastAPI()


@app.post("/invoke", response_model=ChatReturnType)
def sync_chat(message: str):
    response = chain.invoke(message)
    return response


@app.post("/ainvoke", response_model=ChatReturnType)
async def async_chat(message: str):
    response = await chain.ainvoke(message)
    return response


@app.post("/stream", response_model=ChatReturnStreamType)
def sync_stream_chat(message: str):
    def event_stream():
        try:
            for chunk in chain.stream(message):
                if len(chunk) > 0:
                    yield f"{chunk}"
        except Exception as e:
            yield f"data: {str(e)}\n\n"

    return StreamingResponse(event_stream(), media_type="text/event-stream")


@app.post("/astream", response_model=ChatReturnStreamType)
async def async_stream_chat(message: str):
    async def event_stream():
        try:
            async for chunk in chain.astream(message):
                if len(chunk) > 0:
                    yield f"{chunk}"
        except Exception as e:
            yield f"data: {str(e)}\n\n"

    return StreamingResponse(event_stream(), media_type="text/event-stream")


@app.post("/add-contents", response_model=AddContentsOutType)
async def add_content(input: AddContentsInType):
    id = chroma.add_texts(input.content, metadatas=input.source)
    output = input.model_copy(update={"id": id})
    return output


@app.post("/async-add-contents", response_model=AddContentsOutType)
async def async_add_content(input: AddContentsInType):
    id = await chroma.aadd_texts(input.content, metadatas=input.source)
    output = input.model_copy(update={"id": id})
    return output


nest_asyncio.apply()
uvicorn.run(app)
INFO:     Started server process [26086]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:56950 - "POST /add-contents HTTP/1.1" 200 OK


INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [26086]

Set up the environment. You may refer to for more details.

You can checkout the for more details.

FastAPI
langchain_reference
Environment Setup
langchain-opentutorial
Donghak Lee
frimer
LangChain Open Tutorial
Overview
Environment Setup
What is FastAPI
FastAPI Fast Tutorial
FastAPI Serving of LangChain