Building semantic search

Let's put embeddings to work! We'll embed the entire Green Bites menu and build a semantic search function that finds items by meaning, not just keywords.

02_embeddings.ipynb
python
menu_items = [
    "Crispy Cauliflower Tacos with chipotle lime sauce",
    "Quinoa Power Bowl with roasted vegetables and tahini",
    "Mushroom Burger with caramelized onions and vegan cheese",
    "Lentil Shepherd's Pie with sweet potato mash",
    "Thai Coconut Curry with tofu and jasmine rice",
    "Mediterranean Falafel Wrap with hummus and pickled vegetables",
    "Avocado Toast with everything bagel seasoning",
    "Chocolate Lava Cake with coconut whipped cream",
]

def semantic_search(query, items, top_k=3):
    """Search items by semantic similarity to query."""
    query_resp = litellm.embedding(model=EMBEDDING_MODEL, input=[query])
    query_vec = np.array(query_resp.data[0]["embedding"])

    item_resp = litellm.embedding(model=EMBEDDING_MODEL, input=items)
    scores = []
    for i, item in enumerate(items):
        item_vec = np.array(item_resp.data[i]["embedding"])
        score = np.dot(query_vec, item_vec) / (
            np.linalg.norm(query_vec) * np.linalg.norm(item_vec)
        )
        scores.append((item, score))

    scores.sort(key=lambda x: x[1], reverse=True)
    return scores[:top_k]

results = semantic_search("high protein meal")
for item, score in results:
    print(f"{score:.4f} | {item}")

Embed menu items and search by meaning

Notice that searching for "protein" finds the Quinoa Power Bowl and Lentil Shepherd's Pie, even though neither contains the word "protein." The embedding model learned that quinoa and lentils are protein-rich foods. This is the power of semantic search over keyword search.

Great question! Our for-loop compares the query against every single item, which is O(n) linear search. At 80,000 items, this becomes painfully slow. We need a specialized data structure that can search millions of vectors in milliseconds. That is exactly what a Vector Database does, and it is what we will build next!

Timed quiz: Quick check

Loading practice…

Ordering exercise: Steps of semantic search

Loading practice…

Flashcards: Flashcards

Loading practice…