use std::error::Error;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use troupekit::agent::EventSink;
use troupekit::core::event::Event;
use troupekit::core::ids::RunId;
use troupekit::local::llama::{LlamaModel, LlamaSettings};
use troupekit::local::{LocalChat, LocalChatSettings};
use troupekit::trace::{CallbackSink, Trace};
use troupekit::SqliteStore;
use troupekit_kit::{Agent, BuiltAgent, Storage};
const CONTEXT: usize = 1_024;
const MAX_TOKENS: u32 = 960;
const USAGE: &str = "usage: kill_model_call start <db> <gguf> | resume <db> <gguf> <run id>";
struct PrintRun(AtomicBool);
impl EventSink for PrintRun {
fn emit(&self, event: Event) {
if !self.0.swap(true, Ordering::SeqCst) {
println!("RUN {}", event.run_id);
std::io::stdout().flush().expect("stdout flushes");
}
}
}
async fn agent(db: &Path, gguf: &Path) -> Result<BuiltAgent, Box<dyn Error>> {
let model = LlamaModel::load(gguf, LlamaSettings::default().with_context(CONTEXT))?;
let settings = LocalChatSettings::default().with_ban_eos(true);
let chat = LocalChat::new(Arc::new(model), "local/test").with_settings(settings);
let store = SqliteStore::open(db).await?;
let said = AtomicBool::new(false);
let sink = CallbackSink::new(move |line| {
let local_start =
line.contains("\"t\":\"start\"") && line.contains("\"name\":\"model.local\"");
if local_start && !said.swap(true, Ordering::SeqCst) {
println!("GENERATING");
std::io::stdout().flush().expect("stdout flushes");
}
});
let built = Agent::new("storyteller")
.instructions("Write a very long story. /no_think")
.model("local/test")
.model_client(Arc::new(chat))
.max_tokens(MAX_TOKENS)
.checkpoints(Storage::custom(Arc::new(store)))
.events(Arc::new(PrintRun(AtomicBool::new(false))))
.trace([Trace::Models])
.trace_to(Arc::new(sink))
.build()
.await?;
Ok(built)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = std::env::args().skip(1).collect();
let (mode, db, gguf) = match args.as_slice() {
[mode, db, gguf, ..] => (mode.as_str(), PathBuf::from(db), PathBuf::from(gguf)),
_ => return Err(USAGE.into()),
};
let built = agent(&db, &gguf).await?;
match (mode, args.get(3)) {
("start", None) => {
let text = built.run("Tell the story.").await?;
println!("DONE {}", text.len());
}
("resume", Some(id)) => {
let run_id: RunId = serde_json::from_str(&format!("\"{id}\""))?;
let text = built.resume(run_id).await?;
println!("RESUMED {}", text.len());
}
_ => return Err(USAGE.into()),
}
Ok(())
}