FastAPI Serving
Author: Donghak Lee
Peer Review:
Proofread : frimer
This is a part of LangChain Open Tutorial
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
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
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.
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
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
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.
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.
Define Request Model
It can be defined using the
Pydanticmodel.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.
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
FastAPI Serving of LangChain
Try serving a langchain with the fastAPI.
Use what you have learnt above.
Implement stream output in the fastAPI.
Last updated