troupekit 0.4.0

Rust framework for LLM agents that run inside your own program: #[agent] functions or persona TOML files, tools, memory, document search and graph workflows that survive a restart.
Documentation
use std::error::Error;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use troupekit::agent::{Limits, PeriodLimit};
use troupekit::context::embed::HashingEmbedder;
use troupekit::core::store::UsageLedger;
use troupekit::prelude::*;
use troupekit::SqliteStore;

const MODEL: &str = "anthropic/claude-haiku-5-5";
const USER_CAP: u64 = 50_000;

/// Where one customer conversation is served from: the help articles,
/// the store file for articles and memory, the usage ledger, the customer
/// and the conversation.
struct Desk<'a> {
    docs: &'a Path,
    db: &'a Path,
    ledger: Arc<SqliteStore>,
    user: &'a str,
    session: &'a str,
}

/// Builds the support agent for one conversation: knowledge over the
/// articles, memory of the customer, and a per-customer cap counted in the
/// ledger across conversations. Input: the desk. Output: the built agent.
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
}

/// Serves two conversations with the same customer, a day apart, each on
/// a freshly built agent, then prints what the customer has spent.
/// Input: ANTHROPIC_API_KEY. Output: questions, answers and the total.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let docs = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/support/docs");
    let dir = tempfile::tempdir()?;
    let db = dir.path().join("support.db");
    let ledger = Arc::new(SqliteStore::open(dir.path().join("usage.db")).await?);
    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());
    }
    let spent = ledger.period_total("user:ada").await?;
    println!("ada has used {spent} of her {USER_CAP} millionths of a dollar");
    Ok(())
}