|
|
Gemini Embeddings API
Author: Venkata Sudhakar
Embeddings convert text into a vector of numbers capturing its semantic meaning. Two pieces of text about similar topics will have vectors that are close together even if they use different words. This is the foundation of semantic search: a customer searching "budget wireless earbuds for gym" finds products described as "affordable sports Bluetooth headphones" even though no words overlap - the meaning is similar so the embedding vectors are close. Gemini embeddings are state-of-the-art and available with a simple API call. The Gemini embedding model is accessed via client.models.embed_content(). Embed your product catalogue once at startup, store the vectors, and at query time embed the search query and find the most similar vectors using cosine similarity. No vector database is needed for small catalogues - numpy comparison works up to a few thousand products. For larger catalogues combine this with Vertex AI Vector Search or ChromaDB from Tutorial 303. The below example builds a semantic product search engine for a furniture store - finding relevant products even when the customer query uses completely different words than the product descriptions, something keyword search cannot do.
Semantic search - finding products by meaning,
It gives the following output showing semantic matches across different phrasings,
Query: I need furniture for a tiny flat - something that wont take much space
1. [0.9124] Compact 2-Seater Studio Sofa
2. [0.7831] Elara 3-Seater Fabric Sofa
3. [0.7204] Milano Leather L-Shape Sectional
Query: I want to improve my posture while working from home
1. [0.9287] Ergonomic Office Chair
2. [0.8912] Height-Adjustable Standing Desk
3. [0.7103] King Size Platform Bed with Storage
Query: Looking for a big cozy sofa for family movie nights
1. [0.9043] Milano Leather L-Shape Sectional
2. [0.8765] Elara 3-Seater Fabric Sofa
3. [0.7234] Compact 2-Seater Studio Sofa
Query: Need storage in my bedroom - running out of space
1. [0.9198] King Size Platform Bed with Storage
2. [0.7342] Compact 2-Seater Studio Sofa
3. [0.6891] Elara 3-Seater Fabric Sofa
# "tiny flat" correctly found "studio sofa" despite no word overlap
# "posture WFH" correctly found ergonomic chair then standing desk
# "bedroom storage" found the storage bed even though no word matches
# Keyword search would have returned zero results for most of these
Embedding practical tips: use task_type="RETRIEVAL_DOCUMENT" when embedding catalogue items and task_type="RETRIEVAL_QUERY" when embedding search queries - this improves retrieval accuracy significantly. Re-embed your catalogue whenever products are added or descriptions change. For similarity threshold: scores above 0.85 are strong matches, 0.70-0.85 are reasonable matches, below 0.70 should be filtered out. Combine semantic search with keyword search (hybrid search from Tutorial 310) for the best results - semantic finds meaning-matches, keyword finds exact model number matches. Cache embeddings in a vector database rather than recomputing every restart.
|
|