Tool

Trait Tool 

Source
pub trait Tool:
    Sized
    + Send
    + Sync {
    type Error: Error + Send + Sync + 'static;
    type Args: for<'a> Deserialize<'a> + Send + Sync;
    type Output: Serialize;

    const NAME: &'static str;

    // Required methods
    fn definition(
        &self,
        _prompt: String,
    ) -> impl Future<Output = ToolDefinition> + Send + Sync;
    fn call(
        &self,
        args: Self::Args,
    ) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send;

    // Provided method
    fn name(&self) -> String { ... }
}
Expand description

Trait that represents a simple LLM tool

§Example

use rig::{
    completion::ToolDefinition,
    tool::{ToolSet, Tool},
};

#[derive(serde::Deserialize)]
struct AddArgs {
    x: i32,
    y: i32,
}

#[derive(Debug, thiserror::Error)]
#[error("Math error")]
struct MathError;

#[derive(serde::Deserialize, serde::Serialize)]
struct Adder;

impl Tool for Adder {
    const NAME: &'static str = "add";

    type Error = MathError;
    type Args = AddArgs;
    type Output = i32;

    async fn definition(&self, _prompt: String) -> ToolDefinition {
        ToolDefinition {
            name: "add".to_string(),
            description: "Add x and y together".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "x": {
                        "type": "number",
                        "description": "The first number to add"
                    },
                    "y": {
                        "type": "number",
                        "description": "The second number to add"
                    }
                }
            })
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        let result = args.x + args.y;
        Ok(result)
    }
}

Required Associated Constants§

Source

const NAME: &'static str

The name of the tool. This name should be unique.

Required Associated Types§

Source

type Error: Error + Send + Sync + 'static

The error type of the tool.

Source

type Args: for<'a> Deserialize<'a> + Send + Sync

The arguments type of the tool.

Source

type Output: Serialize

The output type of the tool.

Required Methods§

Source

fn definition( &self, _prompt: String, ) -> impl Future<Output = ToolDefinition> + Send + Sync

A method returning the tool definition. The user prompt can be used to tailor the definition to the specific use case.

Source

fn call( &self, args: Self::Args, ) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send

The tool execution method. Both the arguments and return value are a String since these values are meant to be the output and input of LLM models (respectively)

Provided Methods§

Source

fn name(&self) -> String

A method returning the name of the tool.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<M: CompletionModel> Tool for Agent<M>

Source§

const NAME: &'static str = "agent_tool"

Source§

type Error = PromptError

Source§

type Args = AgentToolArgs

Source§

type Output = String