Long-Term Knowledge Storage and Retrieval › Understanding Meaning, Not Just Keywords
AI Agent HandbookAll chapters Long-Term Knowledge Storage and Retrieval › Understanding Meaning, Not Just KeywordsOne section · Sections in this chapter5 / 13 ↓
Reading tools Definitions & inline links
Toggle tooltip visibility. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
This is where vector stores and embeddings become useful. Instead of matching exact words, we can represent the meaning of text as numbers (vectors) and find information that's semantically similar.
Here's the concept: when you store a fact like "User is allergic to peanuts," the system converts this into a vector that represents its meaning. Later, when you ask "What should I avoid eating?", that question also gets converted to a vector. The system then finds stored facts whose vectors are close to the question's vector, meaning they're semantically related.
Let's see this in action using a simple example with sentence embeddings:
from sentence_transformers import SentenceTransformer
import numpy as np
class VectorMemory:
def __init__(self):
# Using Claude Sonnet 4.5 for the agent, but a local model for embeddings
# to keep costs down for frequent similarity searches
self.encoder = SentenceTransformer("all-MiniLM-L6-v2")
self.facts = []
self.vectors = []
def add_fact(self, fact):
"""Add a fact and its vector representation"""
vector = self.encoder.encode(fact)
self.facts.append(fact)
self.vectors.append(vector)
return f"Stored: {fact}"
def search(self, query, top_k=3):
"""Find the most relevant facts for a query"""
if not self.facts:
return ["No information stored yet."]
# Convert query to vector
query_vector = self.encoder.encode(query)
# Calculate similarity with all stored facts
similarities = []
for i, fact_vector in enumerate(self.vectors):
# Cosine similarity
similarity = np.dot(query_vector, fact_vector) / (
np.linalg.norm(query_vector) * np.linalg.norm(fact_vector)
)
similarities.append((similarity, self.facts[i]))
# Sort by similarity and return top results
similarities.sort(reverse=True, key=lambda x: x[0])
return [fact for score, fact in similarities[:top_k] if score > 0.3]
## Example usage
memory = VectorMemory()
memory.add_fact("User's birthday is July 20")
memory.add_fact("User prefers Italian food")
memory.add_fact("User is allergic to peanuts")
memory.add_fact("User lives in San Francisco")
memory.add_fact("User works as a software engineer")
## Semantic search
print(memory.search("What should I avoid eating?"))
## Output: ["User is allergic to peanuts", "User prefers Italian food"]
print(memory.search("Where does the user live?"))
## Output: ["User lives in San Francisco"]
print(memory.search("What is the user's profession?"))
## Output: ["User works as a software engineer"]Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
Loading weights: 0%| | 0/103 [00:00<?, ?it/s]
['User prefers Italian food'] ['User lives in San Francisco', 'User works as a software engineer', "User's birthday is July 20"] ['User works as a software engineer', 'User lives in San Francisco']
Notice how the search understands meaning. When you ask "What should I avoid eating?", it finds the allergy information even though the words don't match exactly. The vector representation captures that avoiding food relates to allergies and food preferences.