AI Hints
Agentic AI · Lesson 28 of 100

Searching by Meaning

Memory the model can search.

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

Priya asks whether her wheel is covered, after a year. Your policy file says, two year warranty against manufacturing faults. The two sentences don't share a single important word. Keyword search would fail. So agents search by meaning instead.

Searching by Meaning
From the video: Searching by Meaning
  • Is my wheel covered?
  • Policies use different words
  • Search by meaning

Meaning as a location

An embedding model turns any text into a list of numbers, which act as a location in a space of meaning. Similar ideas land close together, whatever words they use. So to search, you just turn the question into a point, and you find the nearest neighbours. Our natural language course goes deep on this. Here, we'll use it.

Here are six short policies. We embed all of them once. To search, we embed the question, and compare it against every policy, using a dot product. Then we keep the two best. Run it. Look at the first question. It found the wheelset warranty, even though it says wheel, and faulty, in different ways. The second question came up with the refund line as a close second, so real search isn't perfect. That's why we fetch more than one result, and let the model pick.

search.pypython
import numpy as np
from sentence_transformers import SentenceTransformer
from kb import POLICIES

model = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
vectors = model.encode(POLICIES, normalize_embeddings=True)

def search(question, k=2):
    q = model.encode([question], normalize_embeddings=True)[0]
    scores = vectors @ q
    top = np.argsort(-scores)[:k]
    return [(round(float(scores[i]), 2), POLICIES[i]) for i in top]

for question in ["My wheel is faulty after a year", "How long until I get my money?"]:
    print(question)
    for score, text in search(question):
        print(" ", score, text[:64])
Output
My wheel is faulty after a year
  0.63 Wheelsets have a two-year warranty against manufacturing faults.
  0.33 Chains are consumable parts and are not covered by warranty.
How long until I get my money?
  0.47 Returns are free within thirty days if the item is unused.
  0.45 Refunds reach the customer's card within five working days.
Also uses kb.py
POLICIES = [
    "Returns are free within thirty days if the item is unused.",
    "Wheelsets have a two-year warranty against manufacturing faults.",
    "Helmets involved in any crash must be replaced and cannot be returned.",
    "Orders over eighty dollars ship free to Europe.",
    "Chains are consumable parts and are not covered by warranty.",
    "Refunds reach the customer's card within five working days.",
]

From demo to production

For a real system, you'd use a vector database, such as FAISS, Chroma, or pgvector. Split long documents into small chunks, so that a search returns just the useful part. And combine meaning search with keyword search, because product codes like A seventeen need exact matches.

Vector databaseFAISS, Chroma, pgvector
Chunkingsmall pieces
Hybrid searchmeaning plus keywords
Filtersby product or date

Summary

Embeddings let an agent search by meaning. Now we'll turn search into a tool, and let the agent decide when to use it.

Key takeaways
  • Embeddings capture meaning
  • Nearest neighbours are search
  • Combine with keywords
Next up: Agentic RAG — search as a tool