Build a support agent with knowledge
This is the first application of the course, and it is the one most teams build first: a support agent. It needs three things you have already learned, working together.
- It answers from your help articles, not from what the model guesses. That is knowledge.
- It remembers the customer from one conversation to the next, so nobody has to repeat where they live. That is memory.
- It does not make a call the estimate says would go past what you allow for one customer, across every conversation. That is a cap on a durable ledger. Conversations in one process that share the ledger hold each call's estimate until its cost is recorded, so two of them cannot both take the last room. Conversations served by separate processes do not see each other's holds, so the cap is not enforced across them: serve each customer's cap from one process.
course_app_support builds it in one function.
The agent
#![allow(unused)]
fn main() {
async fn support(desk: Desk<'_>) -> Result<BuiltAgent, KitError> {
Agent::new("support")
.model(MODEL)
.instructions(
"You are the support agent of a small online shop. Search the help \
articles with search_knowledge before you answer, and answer only from \
them. Save lasting facts about the customer, such as their country, \
with remember_fact. If the articles do not cover a question, say so and \
offer to pass it to a person. Answer in at most four sentences.",
)
.knowledge(
Knowledge::dir(desk.docs)
.store(desk.db)
.embedder(Arc::new(HashingEmbedder::new(256))),
)
.memory(AgentMemory::new(desk.user, desk.session))
.max_turns(6)
.max_tokens(600)
.limits(Limits {
run: Some(20_000),
user: Some(PeriodLimit {
key: format!("user:{}", desk.user),
max_usd_micros: USER_CAP,
}),
..Limits::default()
})
.ledger(desk.ledger)
.build()
.await
}
}
Read the instructions first, because they carry the product decisions. Answer only from the articles. Save the customer's country. Admit a gap and offer a person. Each of those sentences is a behaviour you would otherwise have to hope for.
Then the pieces. .knowledge(..) indexes the help folder into desk.db and gives the agent search_knowledge. .memory(..) has no .sqlite(..) of its own here: when an agent has knowledge, memory lives in the same store file, so one file holds the articles and what the agent knows about each customer. .limits(..) caps one run at two cents and the customer at five cents in total, with the key built from the customer id your code passes in. .ledger(..) keeps those totals in a separate SQLite file that outlives every conversation.
Desk is a small struct the example uses to pass the five inputs together. Nothing in troupekit needs it.
Two conversations, a day apart
#![allow(unused)]
fn main() {
let conversations = [
(
"monday",
"Hi, I live in France. Can I still return a jacket I got three weeks ago?",
),
(
"tuesday",
"How much is shipping to me, and how long does it take?",
),
];
for (session, question) in conversations {
let desk = Desk {
docs: &docs,
db: &db,
ledger: ledger.clone(),
user: "ada",
session,
};
let agent = support(desk).await?;
let outcome = agent.run_with(question).await?;
println!("[{session}] > {question}\n{}\n", outcome.text());
}
}
Each conversation builds a fresh agent, the way a server would after a restart, with a new session id and the same customer. On Monday ada mentions France and asks about a return. On Tuesday she asks about shipping "to me", and never says where she is.
Try it
cargo run -p troupekit --features knowledge --example course_app_support
Monday's answer should use the 30-day return window from the articles. Tuesday's answer is the one to read closely: to say anything useful about shipping to "me", the agent needs the fact it saved on Monday and the international shipping article. The last line shows what ada has used of her cap, read from the ledger file.
Taking it to production
The parts you would change are the ones the example keeps simple on purpose.
- Swap
HashingEmbedderfor an embedding model from the catalog, or an on-device one, and keep the rest. - Use your real customer id as
user, and a conversation id assession. Never let either come from the model or the message text. - Keep the store file and the ledger file somewhere that survives deploys.
- Turn on tracing for the first weeks, with
trace_contentoff, so you can explain any answer without writing customer messages to disk.