Building Vector Databases With FastAPI and ChromaDB - by Om Kamath - May, 2024 - Level Up Coding
Building Vector Databases With FastAPI and ChromaDB - by Om Kamath - May, 2024 - Level Up Coding
Search Write
Om Kamath · Follow
Published in Level Up Coding · 10 min read · May 7, 2024
464 4
Returning to writing after a lengthy break, I’m finally carving out some time
to dive into it. With the amount of innovations and new AI tech popping up, I
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 1/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
decided to take a step back and explore some of the fundamentals now. After
getting to work around APIs at my work and with Python being my language
of choice, I felt that it was a good time to explore some API development
frameworks for Python.
For all those who are starting out with backend development in Python,
there are mainly 3 frameworks available:
1. Flask
2. FastAPI
3. Django
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 2/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
than endlessly deliberating over technologies and wasting time. So, without
further delay, let’s jump right into it.
What is FastAPI?
1. Fast
2. Intuitive
3. Easy to Code
5. Automatic Docs
Among the listed features, only two really grabbed my attention: its ease of
coding (perfect for my lazy tendencies) and its foundation built on top of
Pydantic.
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 3/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
user = User(user="Om",age="21")
print(user)
Output
Even though we provided the model with age in string format, Pydantic
automatically typecasts it to an integer. This is one of the advantages of using
Pydantic over the built-in Python classes.
Setting up FastAPI
Setting up FastAPI is pretty simple and requires just a pip installation using
pip install fastapi
Once you have got FastAPI installed, you can test it out using this sample
code:
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 4/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Whatchamacallit"}
app = FastAPI() : This line creates an instance of the FastAPI class and
assigns it to the variable app . This instance represents your FastAPI
application.
async def root(): : This line defines a function named root using the
async keyword, indicating that it is an asynchronous function. This
function will handle requests to the root URL ("/") of your API.
To run the server (FastAPI uses uvicorn), ensure that you save the file as
main.py , as it will be referenced by the command fastapi dev main.py . This
command should start a localhost server that you can use for testing.
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 5/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
Terminal
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 6/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
HTTP Client
To test the code, we will use an HTTP client like Postman or HTTPie. You can
choose whichever you prefer. HTTPie’s minimalist approach appeals to me
more, but Postman is packed with more API-testing features. Won’t be diving
into it in this article.
To explain in short:
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 7/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
Vector Databases
If you’re wondering about the purpose of vector databases, they’re incredibly
powerful and play a major role in many AI startups that have emerged in the
last two years. Vector databases are utilized to store embeddings, which are
vector representations of textual data that capture the meaning of the
content. This enables various operations such as fetching data without the
need for formal querying or keyword matches. Data from vector databases is
retrieved through similarity search, which mainly employs either of these
two techniques:
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 8/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
The flow
1. Chunking the PDF Document using Langchain.
To chunk the PDF document, we will load the document using PyPDF.
Langchain offers multiple document loader methods, with PyPDF being one
of them.
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 9/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
loader = PyPDFLoader("files/samples.pdf")
pages = loader.load()
After loading the document, the next step is to chunk it. The primary
purpose of chunking documents is to break them into contextually relevant
segments that can be later fed into an LLM for Retrieval Augmented
Generation (RAG). Langchain provides a variety of text splitters to choose
from for chunking documents.
Types of text-splitters:
5. Token: Splits text on tokens. There exist a few different ways to measure
tokens.
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 10/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
splitter for basic PDF or text documents. As we become familiar with the
chunking process, we can experiment with different text splitters, but that’s
a topic for a separate article. The RecursiveCharacterTextSplitter() splits
chunks based on predefined separators such as ‘\n’ or whitespace, and the
chunk size can be set as a parameter.
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 11/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2"
We will be assigning IDs to the chunks to avoid duplication while adding the
chunks to the database. persist_directory saves the vector database to the
working directory which can be later loaded up while querying.
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 12/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2"
db = Chroma(persist_directory="chroma_db",embedding_function=embedding_function)
results = db.similarity_search(query.query, k=query.neighbours)
if "chroma_db" in os.listdir():
shutil.rmtree("chroma_db")
print(f"Deleted database and its contents.")
else:
raise FileNotFoundError("Database not found.")
functions.py :
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 13/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
warnings.filterwarnings('ignore')
loader = PyPDFLoader("files/samples.pdf")
pages = loader.load()
For mapping these functions to the endpoints, we will have to import the
functions from functions.py . After importing we can edit the main.py file we
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 14/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
main.py :
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Whatchamacallit"}
#Create database
@app.get("/create/")
async def create_database():
create_db()
return {"message": "Database created."}
#Delete database
@app.delete("/delete/")
async def delete_database():
try:
delete_persisted_db()
return {"message": "Database deleted."}
except FileNotFoundError as e:
raise HTTPException(status_code=404, detail=str(e))
#Fetch Chunks
@app.post("/neighbours/")
async def fetch_item(query: Query):
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6
db = Chroma(persist_directory="chroma_db",embedding_function=embedding_funct
results = db.similarity_search(query.query, k=query.neighbours)
return {"message": "Nearest neighbours found.", "results": results}
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 15/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
Another useful feature of FastAPI is its predefined error codes, which can be
called directly from the FastAPI library and customized to suit your needs.
We will also need define the Pydantic model for the /neighbours endpoint
query body.
models.py :
class Query(BaseModel):
query: str
neighbours: int = 3
Outputs
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 16/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 17/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 18/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 19/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
Conclusion
That concludes this simple starter tutorial, which should provide both you
and me with an understanding of the fundamental steps involved in building
an app that utilizes an LLM at the backend, coupled with basic API
development. FastAPI offers many more powerful features for
authentication and even frontend development using FastUI, which I plan to
explore in my future blogs. My only gripe while working with Langchain is
the documentation, which can be quite complicated due to the abundance of
functionalities available. It’s both a blessing and a curse. If you’re just
starting with Langchain, it can feel quite intimidating, but remember to take
it one step at a time
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 20/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
Exploring FastAPI and documenting the process has been quite enjoyable. I
hope this article has been both informative and entertaining for you as a
reader, just as it has been for me. If you have any suggestions or if you notice
any mistakes, please feel free to share your feedback in the comments
section.
Tech-enthusiast | Programming | AI
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 21/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
How I Built A Beautiful Web App The resume that got a software
Purely in Python — with Zero… engineer a $300,000 job at Google.
Using FastAPI, Jinja2 and DaisyUI. 1-page. Well-formatted.
815 7 8.3K 93
Stop Using UUIDs in Your Database Chat with CSV Files Using Google’s
How UUIDs can Destroy SQL Database Gemini Flash: No Langchain!
Performance An LLM pipeline to interact with CSV files
without Langchain
1.92K 82 143 1
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 22/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
· 9 min read · May 17, 2023 11 min read · Jan 29, 2024
2.1K 13 1K 10
Lists
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 23/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
5 min read · May 14, 2024 10 min read · Apr 29, 2024
343 5 417 2
3.4K 33 718 4
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 24/25
6/24/24, 6:08 PM Building Vector Databases with FastAPI and ChromaDB | by Om Kamath | May, 2024 | Level Up Coding
https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08 25/25