#[tool]Expand description
Transforms a function or method into a tool with generated args struct and ToolArgs implementation.
§Usage
§Transform a Function
ⓘ
use tiny_loop::{Agent, tool::tool, llm::OpenAIProvider};
#[tool]
async fn get_weather(
/// City name
city: String,
) -> String {
format!("Weather in {}: Sunny", city)
}
let agent = Agent::new(OpenAIProvider::new())
.tool(get_weather);§Transform Methods
For methods, use .bind():
ⓘ
#[derive(Clone)]
struct Database;
#[tool]
impl Database {
/// Query the database
async fn query(
self,
/// SQL query
sql: String,
) -> String {
format!("Results for: {}", sql)
}
}
let db = Database;
let agent = Agent::new(OpenAIProvider::new())
.bind(db, Database::query);§Custom Tool Name
ⓘ
#[tool(name = "weather_api")]
async fn get_weather(
/// City name
city: String,
) -> String {
todo!()
}
#[tool]
impl Database {
#[name = "db_query"]
async fn query(self, sql: String) -> String {
todo!()
}
}§Serde Attributes
Serde attributes like #[serde(rename = "...")] can be applied to parameters:
ⓘ
#[tool]
async fn fetch(
#[serde(rename = "URL")]
url: String,
) -> String {
todo!()
}§Macro Expansion
§Transform a Function
Input function:
ⓘ
/// Fetch a URL.
#[tool]
pub async fn fetch(
/// URL to fetch
url: String,
) -> String {
todo!()
}Expands to:
ⓘ
/// Arguments for the `fetch` tool.
#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct FetchArgs {
/// URL to fetch
pub url: String,
}
impl tiny_loop::tool::ToolArgs for FetchArgs {
const TOOL_NAME: &'static str = "fetch";
const TOOL_DESCRIPTION: &'static str = "Fetch a URL.";
}
/// Fetch a URL.
pub async fn fetch(args: FetchArgs) -> String {
let FetchArgs { url } = args;
todo!()
}§Transform Methods
Input method:
ⓘ
impl Database {
/// Query database
#[tool]
pub async fn query(
self,
/// SQL query
sql: String,
) -> String {
todo!()
}
}Expands to:
ⓘ
/// Arguments for the `query` tool.
#[derive(serde::Deserialize, schemars::JsonSchema)]
pub struct QueryArgs {
/// SQL query
pub sql: String,
}
impl tiny_loop::tool::ToolArgs for QueryArgs {
const TOOL_NAME: &'static str = "query";
const TOOL_DESCRIPTION: &'static str = "Query database";
}
impl Database {
/// Query database
pub async fn query(self, args: QueryArgs) -> String {
let QueryArgs { sql } = args;
todo!()
}
}