Long-Term Knowledge Storage and Retrieval › Searching Your Knowledge Base

Michael BrenndoerferJuly 3, 20252 min read
AI Agent HandbookAll chapters
Part 6 of 15Memory and Retrieval - How Agents Remember
Chapter 16Long-Term Knowledge Storage and Retrieval › Searching Your Knowledge Base
Reading section 4 of 13 · Searching Your Knowledge Base · 2 minSection 4 of 13 · 2 min

Book contents

Section 4 of 13 · 2 min read

Long-Term Knowledge Storage and Retrieval › Searching Your Knowledge BaseOne section · Sections in this chapter4 / 13 ↓
Reading tools Definitions & inline links

Toggle tooltip visibility. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

A more powerful approach is to store information in a way that allows searching by content, not just by exact keys. This is where the concept of a knowledge base comes in.

Let's expand our memory system to support searching:

In[4]:
Code
class SearchableMemory:
    def __init__(self, memory_file="assistant_knowledge.json"):
        self.memory_file = memory_file
        self.facts = self._load_facts()

    def _load_facts(self):
        """Load facts from file"""
        if os.path.exists(self.memory_file):
            with open(self.memory_file, "r") as f:
                return json.load(f)
        return []

    def _save_facts(self):
        """Save facts to file"""
        with open(self.memory_file, "w") as f:
            json.dump(self.facts, f, indent=2)

    def add_fact(self, fact, category=None):
        """Add a fact to the knowledge base"""
        entry = {
            "fact": fact,
            "category": category,
            "timestamp": str(datetime.now()),
        }
        self.facts.append(entry)
        self._save_facts()
        return f"Stored: {fact}"

    def search(self, query):
        """Search for facts containing the query text"""
        results = []
        query_lower = query.lower()

        for entry in self.facts:
            # Simple keyword search
            if query_lower in entry["fact"].lower():
                results.append(entry["fact"])
            elif entry["category"] and query_lower in entry["category"].lower():
                results.append(entry["fact"])

        return results if results else ["No matching information found."]


## Example usage
from datetime import datetime

kb = SearchableMemory()
kb.add_fact("User's birthday is July 20", category="personal")
kb.add_fact("User prefers Italian food", category="preferences")
kb.add_fact("User is allergic to peanuts", category="preferences")

## Search by keyword
print(kb.search("birthday"))
## Output: ["User's birthday is July 20"]

print(kb.search("food"))
## Output: ["User prefers Italian food"]

print(kb.search("preferences"))
## Output: ["User prefers Italian food", "User is allergic to peanuts"]
Out[4]:
Console
["User's birthday is July 20", "User's birthday is July 20", "User's birthday is July 20", "User's birthday is July 20", "User's birthday is July 20"]
['User prefers Italian food', 'User prefers Italian food', 'User prefers Italian food', 'User prefers Italian food', 'User prefers Italian food']
['User prefers Italian food', 'User is allergic to peanuts', 'User prefers Italian food', 'User is allergic to peanuts', 'User prefers Italian food', 'User is allergic to peanuts', 'User prefers Italian food', 'User is allergic to peanuts', 'User prefers Italian food', 'User is allergic to peanuts']

This is better. Now you can search for information without knowing the exact key. But there's still a problem: this only finds exact keyword matches. What if you ask, "What should I avoid eating?" The assistant needs to understand that this relates to allergies, even though the word "avoid" doesn't appear in the stored facts.