Skip to main content

Crate aither_derive

Crate aither_derive 

Source
Expand description

§aither-derive

Procedural macros for converting Rust functions into AI tools that can be called by language models.

This crate provides the #[tool] attribute macro that automatically generates the necessary boilerplate code to make your async functions callable by AI models through the aither framework.

§Quick Start

Transform any async function into an AI tool by adding the #[tool] attribute. Tool description comes from rustdoc on the Args struct:

use aither::Result;
use aither_derive::tool;
use schemars::JsonSchema;
use serde::Deserialize;

/// Get the current UTC time.
#[derive(JsonSchema, Deserialize)]
pub struct GetTimeArgs;

#[tool]
pub async fn get_time(_args: GetTimeArgs) -> Result<&'static str> {
    Ok("2023-10-01T12:00:00Z")
}

§Function Patterns

§Simple Parameters

use serde::Serialize;
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Debug, Serialize)]
pub struct SearchResult {
    title: String,
    url: String,
}

/// Search the web for content.
#[derive(JsonSchema, Deserialize)]
pub struct SearchArgs {
    pub keywords: Vec<String>,
    pub limit: u32,
}

#[tool]
pub async fn search(args: SearchArgs) -> Result<Vec<SearchResult>> {
    Ok(vec![])
}

§Complex Parameters with Documentation

use schemars::JsonSchema;
use serde::Deserialize;

/// Generate an image from a text prompt.
#[derive(Debug, JsonSchema, Deserialize)]
pub struct ImageArgs {
    /// The text prompt for image generation
    pub prompt: String,
    /// Image width in pixels
    #[serde(default = "default_width")]
    pub width: u32,
    /// Image height in pixels
    #[serde(default = "default_height")]
    pub height: u32,
}

fn default_width() -> u32 { 512 }
fn default_height() -> u32 { 512 }

#[tool]
pub async fn generate_image(args: ImageArgs) -> Result<String> {
    Ok(format!("Generated image: {}", args.prompt))
}

§Requirements

  • Functions must be async
  • Return type must be Result<T> where T: serde::Serialize
  • Parameters must implement serde::Deserialize and schemars::JsonSchema
  • No self parameters (static functions only)
  • No lifetime or generic parameters

Attribute Macros§

tool
Converts an async function into an AI tool that can be called by language models.