Index and Search Embeddings with Chroma
OpenAI cookbook indexing and searching OpenAI embeddings in Chroma, an open-source self-hosted in-memory vector database.
Why it matters
Leverage vector databases like Chroma to securely store, index, and semantically search your own data using embeddings, enabling advanced AI applications like chatbots and topic modeling.
Outcomes
What it gets done
Set up and configure the Chroma vector database.
Load and embed your data using OpenAI embeddings.
Index embedded data into Chroma collections.
Perform semantic searches on your indexed data.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/oai-usingchromaforembeddingssearch | bash Steps
Steps in the chain
Overview
Using Chroma for Embeddings Search
An OpenAI cookbook for indexing and searching OpenAI embeddings using Chroma, an open-source self-hosted in-memory vector database. Use when moving a small-scale embeddings use case into a secure, scalable vector database for production.
What it does
Using Chroma for Embeddings Search walks through downloading data, embedding it with OpenAI, and indexing and searching it in a vector database - specifically Chroma, an easy-to-use, open-source, self-hosted, in-memory vector database designed for working with embeddings alongside LLMs. This addresses a common production need: customers who want to store and search their own embedded data in a secure environment for use cases like chatbots, topic modelling, and semantic search, rather than only at small, unscaled prototype scale.
When to use - and when NOT to
Use it when moving an embeddings-based use case (question answering, chatbot, recommendations) from a small-scale demo into a more secure, scalable setup - the notebook frames vector databases as the key component many customers are missing once performance and security requirements block them from reaching production. It is not a Chroma feature-completeness reference; it covers only the basic setup, indexing, and search flow needed to get started, after which the reader is expected to move on to more complex use cases.
Inputs and outputs
The demo flow has three stages: Setup (importing packages and setting the embedding model to use), Load data (loading a dataset that was already embedded with OpenAI embeddings in an earlier step), and the Chroma section itself - instantiating the Chroma client (ephemeral/in-memory by default, though a persistent disk-backed configuration can be set up instead), creating a separate collection for each class of embedding (titles and content, in this example), and querying each collection to confirm search works. Chroma collections support storing and filtering on arbitrary metadata, so queries can be scoped to subsets of the embedded data; for this example, only the embeddings and IDs are stored in Chroma, used to index back into the original dataframe, though Chroma can alternatively store the source text alongside vectors and return everything from one query call.
Integrations
Chroma is already integrated with OpenAI's embedding functions - the recommended way to use them is at collection-construction time, so the collection automatically knows how to embed incoming queries - though Chroma also supports bringing your own embedding function if preferred. Once a query embedding function is set, Chroma embeds and matches queries automatically without the caller re-embedding manually.
Who it's for
Developers evaluating vector database options for a production embeddings use case who want a minimal, working first pass at indexing and searching with Chroma specifically, before exploring Chroma's further capabilities - metadata where filters, updating/deleting collection data, and deployment options - documented separately in Chroma's own docs.
Source README
Using Chroma for Embeddings Search
This notebook takes you through a simple flow to download some data, embed it, and then index and search it using a selection of vector databases. This is a common requirement for customers who want to store and search our embeddings with their own data in a secure environment to support production use cases such as chatbots, topic modelling and more.
What is a Vector Database
A vector database is a database made to store, manage and search embedding vectors. The use of embeddings to encode unstructured data (text, audio, video and more) as vectors for consumption by machine-learning models has exploded in recent years, due to the increasing effectiveness of AI in solving use cases involving natural language, image recognition and other unstructured forms of data. Vector databases have emerged as an effective solution for enterprises to deliver and scale these use cases.
Why use a Vector Database
Vector databases enable enterprises to take many of the embeddings use cases we've shared in this repo (question and answering, chatbot and recommendation services, for example), and make use of them in a secure, scalable environment. Many of our customers make embeddings solve their problems at small scale but performance and security hold them back from going into production - we see vector databases as a key component in solving that, and in this guide we'll walk through the basics of embedding text data, storing it in a vector database and using it for semantic search.
Demo Flow
The demo flow is:
- Setup: Import packages and set any required variables
- Load data: Load a dataset and embed it using OpenAI embeddings
- Chroma:
- Setup: Here we'll set up the Python client for Chroma. For more details go here
- Index Data: We'll create collections with vectors for titles and content
- Search Data: We'll run a few searches to confirm it works
Once you've run through this notebook you should have a basic understanding of how to setup and use vector databases, and can move on to more complex use cases making use of our embeddings.
Setup
Import the required libraries and set the embedding model that we'd like to use.
Load data
In this section we'll load embedded data that we've prepared previous to this session.
Chroma
We'll index these embedded documents in a vector database and search them. The first option we'll look at is Chroma, an easy to use open-source self-hosted in-memory vector database, designed for working with embeddings together with LLMs.
In this section, we will:
- Instantiate the Chroma client
- Create collections for each class of embedding
- Query each collection
Instantiate the Chroma client
Create the Chroma client. By default, Chroma is ephemeral and runs in memory.
However, you can easily set up a persistent configuration which writes to disk.
Create collections
Chroma collections allow you to store and filter with arbitrary metadata, making it easy to query subsets of the embedded data.
Chroma is already integrated with OpenAI's embedding functions. The best way to use them is on construction of a collection, as follows.
Alternatively, you can 'bring your own embeddings'. More information can be found here
Populate the collections
Chroma collections allow you to populate, and filter on, whatever metadata you like. Chroma can also store the text alongside the vectors, and return everything in a single query call, when this is more convenient.
For this use-case, we'll just store the embeddings and IDs, and use these to index the original dataframe.
Search the collections
Chroma handles embedding queries for you if an embedding function is set, like in this example.
Now that you've got a basic embeddings search running, you can hop over to the Chroma docs to learn more about how to add filters to your query, update/delete data in your collections, and deploy Chroma.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.