Skip to main content

tool_fn

Attribute Macro tool_fn 

Source
#[tool_fn]
Expand description

Derives a [Tool] implementation from an async function.

The function’s parameters become the tool’s JSON Schema properties. Doc comments on the function become the tool description; doc comments on individual parameters become property descriptions.

§Supported types

String, i8i128, u8u128, isize, usize, f32, f64, bool, Option<T> (marks the parameter as not required).

§Attributes

  • name = "..." — override the tool name (defaults to the function name)
  • description = "..." — override the description (defaults to doc comments)
  • crate_path = "..." — override the path to the daimon crate (defaults to ::daimon)

§Example

use daimon::prelude::*;
use daimon::tool_fn;

/// Adds two numbers together.
#[tool_fn]
async fn add(
    /// The first number.
    a: f64,
    /// The second number.
    b: f64,
) -> daimon::Result<ToolOutput> {
    Ok(ToolOutput::text(format!("{}", a + b)))
}

// Use the generated struct:
let agent = Agent::builder()
    .model(model)
    .tool(Add) // PascalCase struct generated from `add`
    .build()?;