chapter five

5 Building knowledge bases with RAG

 

This chapter covers

  • Why agents need external knowledge bases
  • Different search methods: keyword, vector, graph, and structure-based search
  • Implementing vector search from scratch with embeddings, chunking, and similarity calculation
  • Building structure-based search tools for file system exploration
  • Extending agent capabilities through the Callback pattern

In Chapters 2 through 4, we built the foundation of an AI agent: connecting to LLMs, implementing tool use, and creating the agent loop. Now we enter a new phase. As figure 5.1 shows, we’ll now enhance our basic agent with context engineering strategies. This chapter tackles the first of these enhancements: building knowledge bases with RAG (Retrieval-Augmented Generation).

Figure 5.1 Book structure overview - Chapter 5 in focus.

We’ll begin with the basic components of vector search: how embeddings capture text meaning, why chunking is necessary, and how vector databases operate. We’ll implement a mini vector search system to experience the full flow. Next, we extend to structure-based search using GAIA benchmark zip file problems, where the agent navigates folder structures and reads files like a human developer. Finally, we introduce the Callback pattern for extending agent behavior, implementing Human in the Loop approval and automatic search result compression.

5.1 The problem of using internal data

5.1.1 The simple case: Single file

5.1.2 What if there are multiple files?

5.1.3 What if the data is large or extensive?

5.2 Types of search methods

5.2.1 Keyword search

5.2.2 Vector search

5.2.3 Graph search

5.2.4 Structure-based search

5.3 Practicing vector search

5.3.1 Embedding: Converting text to vectors

5.3.2 Chunking: Dividing long text into search units

5.3.3 Implementing vector search

5.3.4 Exercise: Finding relevant information from web search results

5.3.5 Structure-based search

5.3.6 Preparing the GAIA dataset

5.3.7 Implementing file system tools

5.3.8 Connecting tools to the agent

5.3.9 Solving GAIA zip file problems

5.4 Extending agents with callbacks

5.4.1 The need for agent extension

5.4.2 Implementing tool callbacks

5.4.3 Human in the loop: Tool execution approval

5.4.4 Compressing search results

5.5 Summary