Add an AI agent to a Rust app
Add the ed-agent crate, load a model and send a message.
cargo add ed-agent
cargo add async-trait
cargo add tokio --features macros,rt-multi-thread
Chat
use ed_agent::{Ed, GgufModelConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let ed = Ed::new();
// Downloads the weights on first use, then loads them from the local cache.
ed.load(GgufModelConfig::qwen25_1_5b(), None, None).await?;
let reply = ed.send("Summarise this thread.").await;
println!("{}", reply.reply.unwrap_or_default());
Ok(())
}
The first load downloads the model weights, so it can take a long time.
Tools and approval
Register a function as a tool and decide who approves it.
use std::sync::Arc;
use async_trait::async_trait;
use ed_agent::{
AgentConfig, AgentToolDefinition, ApprovalDecision, ApprovalHandler, ApprovalRequest, Ed,
GgufModelConfig, NoopSink, ToolExecutor,
};
/// Your app's functions. The model can only call what you register.
struct AppTools;
#[async_trait]
impl ToolExecutor for AppTools {
async fn execute(&self, tool_name: &str, _arguments: &str) -> Result<String, String> {
match tool_name {
"delete_draft" => Ok("The draft was deleted.".into()),
other => Err(format!("unknown tool: {other}")),
}
}
}
/// Ed asks this before it runs any mutating tool.
struct AskTheUser;
#[async_trait]
impl ApprovalHandler for AskTheUser {
async fn approve(&self, request: ApprovalRequest) -> ApprovalDecision {
println!("Allow {}? (this example always says no)", request.call.name);
ApprovalDecision::Deny
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let ed = Ed::with_agent(
NoopSink,
Arc::new(AppTools),
Arc::new(AskTheUser),
AgentConfig::default(),
);
ed.register_tool(AgentToolDefinition::mutating(
"delete_draft",
"Delete the current draft",
r#"{"type":"object","properties":{}}"#,
))
.await?;
ed.load(GgufModelConfig::qwen25_1_5b(), None, None).await?;
let reply = ed.run("Delete my draft.").await?;
println!("{}", reply.text);
Ok(())
}
A read-only tool runs without asking. A mutating tool goes through your ApprovalHandler.
API reference: docs.rs/ed-agent.