VectorStoreIndex: searching documents by meaning
A VectorStoreIndex embeds every document once and stores the vectors. A question is embedded and compared with all of them, the search lesson 2 did by hand.
from llama_index.core import Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
from llama_index.core import Document, VectorStoreIndex
documents = [
Document(text="You can get a full refund within 30 days of delivery.", metadata={"file_name": "refunds.md"}),
Document(text="Standard delivery takes 3 to 5 working days.", metadata={"file_name": "delivery.md"}),
Document(text="The LMP-204 desk lamp has a known cable fault.", metadata={"file_name": "lamps.md"}),
]
index = VectorStoreIndex.from_documents(documents)
print(type(index).__name__, len(index.docstore.docs))A Document is text plus metadata, a dictionary of facts about it such as the file it came from. from_documents splits each document into chunks, embeds them with Settings.embed_model and keeps them in memory.
Retrieving
retriever = index.as_retriever(similarity_top_k=2)
for question in ["How do I get my money back?", "My parcel is late"]:
results = retriever.retrieve(question)
print(question, [(r.metadata["file_name"], round(r.score, 3)) for r in results])as_retriever(similarity_top_k=2) returns the two closest chunks for a question, each with its score, the cosine similarity. Both questions that defeated keyword search in lesson 1 now find the right document first.
The docs call this top-k retrieval. It always returns k results, relevant or not: the second result for each question is simply the next closest document. Lesson 11 deals with results that are close enough to be returned and still irrelevant.
- Ask
"LMP-204"and look at the scores. - Set
similarity_top_k=3. - Add a fourth document about opening hours and ask when the shop is open.
Slow is fine. Stopping is the only problem.