AI Hints
Home›RAG Tutorial›Vector Search
RAG · Lesson 30 of 120

Queries and Documents

Task types.

⏱ 2 min read▶ Video at 29:56🎓 Free
Watch this lesson in the RAG courseWatch on YouTube ↗Full RAG course

A question and an answer don't look alike. Questions are short, and end in a question mark. Documents are longer statements. Some embedding models can embed each one in its own way, to match them better.

Queries and Documents
From the video: Queries and Documents
  • Questions are short
  • Documents are long
  • Embed each their way

Two ways to embed

Embed documents is for chunks, and embed query is for questions. Behind the scenes, Gemini is told the task type, retrieval document, or retrieval query. Run it. The question scores higher against the battery sentence than the store hours.

task_types.pypython
import numpy as np
from langchain_google_genai import GoogleGenerativeAIEmbeddings

embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")

docs = ["The Volt 500 battery charges fully in about 4 hours.",
        "York is open Monday to Saturday, 9am to 5:30pm."]
doc_vectors = np.array(embeddings.embed_documents(docs))
q = np.array(embeddings.embed_query("How many hours until my e-bike is full again?"))

scores = doc_vectors @ q / (np.linalg.norm(doc_vectors, axis=1) * np.linalg.norm(q))
for d, s in zip(docs, scores):
    print(round(float(s), 2), d)
Output
0.73 The Volt 500 battery charges fully in about 4 hours.
0.59 York is open Monday to Saturday, 9am to 5:30pm.

The rule

Use embed documents when building the index, and embed query for every question. With LangChain's vector stores, this happens automatically. If you switch models later, re-embed everything, because vectors from different models can't be compared.

keyvalue
Chunksembed_documents
Questionsembed_query
Vector storesdo it for you
New modelre-embed all

Summary

Embed chunks as documents, and questions as queries. Never mix vectors from different models. Next, vector stores.

Key takeaways
  • embed_documents
  • embed_query
  • One model per index
Next up: Vector Stores — save and search