KnowledgeBase searches a folder of documents. The agent calls it as a tool and
answers from the passages it gets back. It works the same on LiveKit and Pipecat.
On this page:
- What it gives you - what goes wrong without it
- Quickstart - a base, a tool and
start() - How it works - bake, start, look up
- What a lookup returns - three shapes
- Every argument KnowledgeBase takes - modes and limits
- Use it in an agent - the tool description and both frameworks
- Advanced - keyword mode and your own code
- Troubleshooting - the errors you may see
[knowledge] extra. See Install for the
install line and the one setting Linux needs.
What it gives you
Quickstart
agent.py
llama-index-embeddings-openai, is yours to install.
Keyword mode needs none.
How it works
- The folder holds
.txt,.mdand.pdffiles. bake()reads them, splits them into passages, and writes the index toindex_dir. Run it when you build the image.start()loads the index when the process starts. It never runs during a call.await look_up(query)searches the index and returns at mosttop_kpassages.- The content is fixed at start. A lookup never reads a file and never builds the index again.
start() reads and embeds the documents in memory. That
works. But every worker process pays the full cost, and the log warns about it.
So call bake() at build time:
What a lookup returns
look_up never raises. It returns one of three shapes, so the model can tell
them apart.
sourceis the file name, never the path. The agent may say it aloud.scoreis a similarity from the meaning search. Keyword hits have no score.- The query and the passages never go to the log.
Every argument KnowledgeBase takes
KnowledgeBase
str
required
The base name. It appears in log lines and errors.
Path
required
The folder that holds the documents.
Path | None
default:"None"
Where
bake() writes the index and start() looks for it. None never bakes.meaning | keyword | hybrid
default:"hybrid"
meaning uses embeddings. keyword uses BM25. hybrid runs both and interleaves the hits.int
default:"90"
Passage size in tokens. 90 tokens is about 65 words. From 1 to 2048.
int
default:"20"
Tokens that neighbouring passages share. From 0 to
chunk_size.int
default:"3"
How many passages a lookup returns. From 1 to 20.
float
default:"0.0"
Drop meaning hits below this score. From 0 to 1. Keyword hits are always kept.
Callable[[], BaseEmbedding] | None
default:"None"
A function that builds the llama-index embedder. Needed for
meaning and hybrid.str | None
default:"None"
The environment variable the embedder needs.
embedis a function, not an embedder. It runs atstart()orbake().- A
keywordbase takes noembedand needsmin_scoreat 0. A base that embeds needsembed. A wrong mix, a badmode,chunk_size,top_kormin_score, or achunk_overlapabovechunk_sizeraises a PydanticValidationErrorwhen you build it. It is aValueError. It names the field and the allowed values. credential_envis checked atstart()andbake(). A missing key stops the process at once and names the variable.- A baked index remembers the settings it was built with.
start()refuses an index built with other settings. Bake it again. start()andbake()raiseKnowledgeErrorfor a missing folder, a folder with no text, or a PDF with no text layer.
KnowledgeError. An agent that knows nothing should
not take calls.
Use it in an agent
KnowledgeBase.tool(name, description) builds the tool for you. The model reads its description, so say when to use it. The library adds a note on weak results, so you need not write one.
descriptionis up to you. It says when to look up.- The tool ends the description with: “The results may not answer the question. They are ordered best first, and each carries a relevance score where a higher number means a closer match. If nothing returned actually answers what was asked, say you do not have that information rather than offering the closest result.”
- The tool has one parameter,
query. The model reads it as “What to look up. Use the caller’s own words.” namedefaults tolook_up. With nodescription, the description is the note alone.- The tool is a
ToolSpec. Pass it toToolRegistry.
tools. See
Tasks and Tools.
Advanced
Keyword mode
Keyword mode needs no embedder and no API key.Use a lookup in your own code
You can callawait REFUNDS.look_up(question) in your own code. Put the
passages in a prompt you write yourself.
Troubleshooting
PermissionError: Security Violation [pathsec.open] on the first lookup
llama-index reads NLTK data files. NLTK refuses a file with more than one
hardlink, and uv makes hardlinks on Linux by default.
Fix: install with copies. Install has the
details, also for a Dockerfile.
The log warns that every worker embeds the documents
There is no baked index, so each process embeds the folder at start. Fix: runbake() when you build the image and set index_dir.
start() refuses the index
The index was built with other settings.
Fix: bake it again.
The model gets {"error": "lookup unavailable"}
look_up ran before start(), or the search failed.
Fix: call start() once at process start, before the first call.
Where to go next
Tools
The tool that wraps a lookup, and its announce line.
Tasks
Offer a lookup to one step of a task.
Install it
The
[knowledge] extra and UV_LINK_MODE.Unmute knowledge
How
agent.yaml declares a knowledge base.