Skip to main content

tool

Attribute Macro tool 

Source
#[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);

§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!()
    }
}