DEV Community

Josmel Noel
Josmel Noel

Posted on

Vector Search in Practice: Using Embeddings with FAISS and Chroma

Hello, fellow developers! Today, we're going to dive into the world of vector search using two powerful libraries: FAISS and Chroma. We'll see how these tools can help us build efficient and effective search solutions, especially in the context of large-scale text data.

Prerequisites

Before we begin, make sure you have Python 3.x installed on your machine. Additionally, you should install the following packages:

pip install faiss torch pandas
Enter fullscreen mode Exit fullscreen mode

For Chroma, follow the installation guide.

Understanding Embeddings

Embeddings are a way to represent words or documents as vectors in a lower-dimensional space while preserving their semantic meaning. This makes it possible to perform vector operations on these representations, such as calculating similarity between them, which can be useful for tasks like search and recommendation systems.

Using FAISS for Vector Search

FAISS (Facebook AI Similarity Search) is a library developed by Facebook AI Research for efficient similarity search and clustering of dense vectors. We'll use FAISS to perform vector search on our precomputed embeddings.

First, let's prepare some sample data:

import pandas as pd
from torch.nn import TextCNN

# Load some text data (e.g., from a CSV file)
data = pd.read_csv('data.csv')

# Define and train a TextCNN model to extract features (embeddings) for our text data
model = TextCNN(...)  # Define your own TextCNN architecture here
X = model(data['text'])  # Extract embeddings from the text data
Enter fullscreen mode Exit fullscreen mode

Now, let's index our embeddings using FAISS:

from faiss.index import IndexFlatIP

n_dim = X.shape[1]
index = IndexFlatIP(n_dim)  # Create a flat IP (Inner Product) index
index.add(X)  # Add our embeddings to the index
Enter fullscreen mode Exit fullscreen mode

Finally, we can search for similar vectors using the search() method:

query = X[0]  # Use any query vector you want
D, I = index.search(query, k=5)  # Find the top-k nearest neighbors
print("Top 5 nearest neighbors:", [(data['text'][i], D[i]) for i in I])
Enter fullscreen mode Exit fullscreen mode

Using Chroma for Embeddings and Vector Search

Chroma is a tool developed by Hugging Face that simplifies the process of working with embeddings and vector search. It provides pre-trained models for extracting text embeddings, as well as a simple API for performing search operations.

First, let's prepare some sample data:

from chromadavis.text_index.simple import SimpleTextIndex

# Load some text data (e.g., from a CSV file)
data = pd.read_csv('data.csv')

# Create a simple text index using Chroma and add our text data to it
index = SimpleTextIndex(pretrained='distilbert-base-nli-mean-tokens')
index.add_documents(data['text'])
Enter fullscreen mode Exit fullscreen mode

Now, we can perform vector search on our index:

query = data['text'][0]  # Use any query you want
results = index.query(query)
print("Top 5 nearest neighbors:", [(d, score) for d, score in results[:5]])
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

In this post, we've seen how to use FAISS and Chroma for efficient vector search on text data. By representing our data as embeddings and performing similarity searches, we can build powerful search solutions that provide relevant results quickly. Give it a try in your next project, and let us know how it goes!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!