#![allow(clippy::missing_errors_doc)]
#![allow(missing_docs)]
#![allow(clippy::unused_async)]
use aither::Result;
use aither::llm::ToolContext;
use aither::llm::tool::Progress;
use aither_derive::tool;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[tool]
pub async fn time() -> Result<&'static str> {
Ok("2023-10-01T12:00:00Z")
}
#[derive(Debug, Serialize)]
pub struct SearchResult {
title: String,
url: String,
}
#[tool]
pub async fn search(keywords: Vec<String>, max_results: u32) -> Result<Vec<SearchResult>> {
let results = keywords
.into_iter()
.take(max_results as usize)
.map(|keyword| SearchResult {
title: format!("Result for {keyword}"),
url: format!("https://example.com/search?q={keyword}"),
})
.collect();
Ok(results)
}
#[derive(Debug, JsonSchema, Deserialize)]
pub struct GenerateImageArgs {
pub prompt: String,
pub images: Vec<String>,
}
#[tool]
pub async fn generate_image(args: GenerateImageArgs) -> aither::Result<String> {
let file_name = format!("image_{}.png", args.prompt.replace(' ', "_"));
Ok(format!(
"Generated image '{file_name}' with prompt '{}'",
args.prompt
))
}
#[tool]
pub async fn crawl(urls: Vec<String>, mut cx: ToolContext) -> Result<u32> {
let total = u32::try_from(urls.len())?;
for (done, url) in (1..=total).zip(&urls) {
let progress = Progress::new(f64::from(done))
.with_total(f64::from(total))
.with_message(format!("fetched {url}"));
cx.report_progress(progress).await?;
}
Ok(total)
}
fn main() {
let mut tools = aither::llm::tool::Tools::new();
tools.register(Time).expect("time registers");
tools.register(Search).expect("search registers");
tools
.register(GenerateImage)
.expect("generate_image registers");
tools.register(Crawl).expect("crawl registers");
}