Vectordb With Chroma
Copyright 2025 Google LLC.
Gemini API: Document Q&A with ChromaDB
| ⚠️ |
This notebook requires paid tier rate limits to run properly.
|
Overview
This tutorial demonstrates how to use the Gemini API to create a vector database and retrieve answers to questions from the database. Moreover, you will use ChromaDB{:.external}, an open-source Python tool that creates embedding databases. ChromaDB allows you to:
- Store embeddings as well as their metadata
- Embed documents and queries
- Search through the database of embeddings
In this tutorial, you'll use embeddings to retrieve an answer from a database of vectors created with ChromaDB.
Setup
First, download and install ChromaDB and the Gemini API Python library.
Then import the modules you'll use in this tutorial.
Configure your API key
Before you can use the Gemini API, you must first obtain an API key. If you don't already have one, create a key with one click in Google AI Studio.
In Colab, add the key to the secrets manager under the "🔑" in the left panel. Give it the name GEMINI_API_KEY.
Once you have the API key, pass it to the SDK. You can do this in two ways:
- Put the key in the
GEMINI_API_KEYenvironment variable (the SDK will automatically pick it up from there). - Pass the key to
genai.Client(api_key=...)
Key Point: Next, you will choose a model. Any embedding model will work for this tutorial, but for real applications it's important to choose a specific model and stick with it. The outputs of different models are not compatible with each other.
Note: At this time, the Gemini API is only available in certain regions.
models/embedding-001 models/text-embedding-004 models/gemini-embedding-exp-03-07 models/gemini-embedding-exp models/gemini-embedding-001
Data
Here is a small set of documents you will use to create an embedding database:
Creating the embedding database with ChromaDB
You will create a custom function{:.external} for performing embedding using the Gemini API. By inputting a set of documents into this custom function, you will receive vectors, or embeddings of the documents.
API changes to Embeddings with model gemini-embedding-001
For the new embeddings model, embedding-001, there is a new task type parameter and the optional title (only valid with task_type=RETRIEVAL_DOCUMENT).
These new parameters apply only to the newest embeddings models.The task types are:
| Task Type | Description |
|---|---|
| RETRIEVAL_QUERY | Specifies the given text is a query in a search/retrieval setting. |
| RETRIEVAL_DOCUMENT | Specifies the given text is a document in a search/retrieval setting. |
| SEMANTIC_SIMILARITY | Specifies the given text will be used for Semantic Textual Similarity (STS). |
| CLASSIFICATION | Specifies that the embeddings will be used for classification. |
| CLUSTERING | Specifies that the embeddings will be used for clustering. |
Now you will create the vector database. In the create_chroma_db function, you will instantiate a Chroma client. From there, you will create a collection, which is where you store your embeddings, documents, and any metadata. Note that the embedding function from above is passed as an argument to the create_collection.
Next, you use the add method to add the documents to the collection.
/tmp/ipykernel_83887/781126645.py:5: DeprecationWarning: The class GeminiEmbeddingFunction does not implement __init__. This will be required in a future version. embedding_function=GeminiEmbeddingFunction()
Confirm that the data was inserted by looking at the database:
Getting the relevant document
db is a Chroma collection object. You can call query on it to perform a nearest neighbors search to find similar embeddings or documents.
Now that you have found the relevant passage in your set of documents, you can use it make a prompt to pass into the Gemini API.
Pass a query to the prompt:
Now use the generate_content method to to generate a response from the model.
Next steps
To learn more about how you can use the embeddings, check out the examples available. To learn how to use other services in the Gemini API, visit the Python quickstart.