Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Knowledge

Memory is what an agent learns from the person in front of it. Knowledge is what you already know and want it to use: your help articles, your policies, your product docs. Without it, a model answers from whatever it picked up in training, which is not your return policy.

This lesson points an agent at a folder of thirty help articles and lets it search them.

A folder becomes a tool

#![allow(unused)]
fn main() {
async fn helpdesk(docs: &Path, db: &Path) -> Result<BuiltAgent, KitError> {
    Agent::new("helpdesk")
        .model(MODEL)
        .instructions(
            "Answer customer questions only from the shop's help articles. Search \
             them with search_knowledge before you answer. If the articles do not \
             cover the question, say so and do not guess.",
        )
        .knowledge(
            Knowledge::dir(docs)
                .store(db)
                .embedder(Arc::new(HashingEmbedder::new(256))),
        )
}

Knowledge::dir(docs) names the folder. When you call build, troupekit reads the text, Markdown and HTML files in it, cuts them into chunks, and stores each chunk with a vector in the SQLite file given to .store(..). The agent gets one new tool, search_knowledge, and calls it like any other tool.

.embedder(..) picks how text turns into vectors. HashingEmbedder is the simplest one there is: each word adds to one of 256 numbers picked by its hash, on your machine, with no network and no key. It is good enough to find an article that shares words with the question, which is all this example needs. In a real app you would use an embedding model from the catalog, or a local one, and keep everything else the same.

Two details matter in production. Search results reach the model fenced as untrusted text, because a document can contain instructions and the model should treat them as data. And files that have not changed since the last build are skipped, so a restart does not embed your whole folder again.

Questions, inside and outside the folder

#![allow(unused)]
fn main() {
for question in [
    "How long do I have to return an item?",
    "My parcel says delivered but it is not here. What do I do?",
    "Do you have a shop in Lisbon?",
] {
    println!("> {question}\n{}\n", agent.run(question).await?);
}
}

The first two are covered by articles in crates/troupekit/examples/support/docs. The third is not, and that is on purpose.

Try it

cargo run -p troupekit --features knowledge --example course_knowledge

The first answer should give the 30-day return window from returns-window.md. The second should follow the steps in shipping-missing-parcel.md. For the third, a good answer says the articles do not cover it. That behaviour comes from one sentence in the instructions, "say so and do not guess", and it is worth keeping in every knowledge agent you write. An agent that admits a gap is one a customer can trust with the rest.

If you want to see what the agent searched for and what came back, turn on tracing, which is two lessons from now.

Going further

The Knowledge concept page lists every setting: chunk size, index name, walk limits, and on-device embedders. The support agent guide builds the same help desk with more control over each step.

Next: Graphs.