Embedding Workflows: Searching with Math

Embedding Workflows: Searching with Math

Implement a semantic search engine. Learn to generate embeddings for a list of documents and calculate cosine similarity to find the best match.

Embedding Workflows

LLMs generate text. Embeddings generate search indexes.

Workflow

  1. Embed Documents:

    docs = ["Apple pie recipe", "Moon landing history", "Python code tutorial"]
    doc_vectors = [genai.embed_content(model="models/text-embedding-004", content=d, task_type="retrieval_document")['embedding'] for d in docs]
    
  2. Embed Query:

    query = "How to bake?"
    query_vec = genai.embed_content(model="models/text-embedding-004", content=query, task_type="retrieval_query")['embedding']
    
  3. Calculate Similarity (Dot Product):

    import numpy as np
    
    # Simple dot product for normalized vectors = Cosine Similarity
    scores = np.dot(doc_vectors, query_vec)
    
    best_doc_index = np.argmax(scores)
    print(docs[best_doc_index]) 
    # Output: "Apple pie recipe"
    

Summary

This is the foundation of RAG. You find the document with the highest dot product score, then feed that text to the LLM to answer the question.

In the next lesson, we discuss Multi-Model Pipelines.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn