How to build an AI Product (4) - Efficiently Searching Vector Spaces with Annoy

nter Annoy (Approximate Nearest Neighbors Oh Yeah) – a powerful library designed for fast approximate nearest neighbor search.


Vector space searching is at the heart of many AI applications, be it recommending a similar article or finding a product that matches user preferences. However, as data scales up, searching these vector spaces becomes computationally expensive. Enter Annoy (Approximate Nearest Neighbors Oh Yeah) – a powerful library designed for fast approximate nearest neighbor search. Today, we'll explore how to use Annoy in tandem with text embeddings to efficiently search through a collection of text phrases.

Step 1: Setting up the Environment

Before diving in, ensure you have the necessary libraries installed:

pip install sentence-transformers annoy

Step 2: Extracting Text Embeddings

For our text embeddings, we’ll employ the Sentence Transformers library:

from sentence_transformers import SentenceTransformer

# Initialize the Sentence Transformer model
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')

# Sample collection of text phrases
documents = [
    "Artificial Intelligence in Healthcare",
    "Deep Learning for Image Recognition",
    "Natural Language Processing in Customer Support",
    "Reinforcement Learning in Gaming"

# Extract embeddings
embeddings = model.encode(documents)

Step 3: Building the Annoy Index

With our vectors ready, let's build the Annoy index:

from annoy import AnnoyIndex

# Define the vector dimensions and the metric (euclidean, angular, etc.)
t = AnnoyIndex(embeddings[0].shape[0], 'angular')

# Populate the index
for idx, vector in enumerate(embeddings):
    t.add_item(idx, vector)

# Build the index  # 10 trees, increase for more accuracy'text_embeddings_index.ann')

Step 4: Querying the Annoy Index

Now that we have our index ready, we can query it to find similar text phrases:

# Load the index
u = AnnoyIndex(embeddings[0].shape[0], 'angular')

# Querying the model
query = "How does AI help in medical field?"
query_embedding = model.encode([query])

# Find the top 2 nearest neighbors
indices = u.get_nns_by_vector(query_embedding[0], 2)

# Display the similar documents
for idx in indices:


Annoy provides a swift and space-efficient method to search large vector spaces.

Paired with powerful text embeddings from Sentence Transformers, we can build highly responsive systems for document similarity, recommendation engines, and more.

As you scale your applications, remember to fine-tune parameters, such as the number of trees in Annoy, to strike a balance between speed and accuracy.

In our next post, we'll explore further optimizations and dive into real-world applications of vector space search. Stay tuned!

🤖 Want to Build the Next Big AI Product?

Join our hands-on, real-life bootcamp and transform your ideas into groundbreaking AI solutions.

Sign Up Now