ToolTrait

Trait ToolTrait 

Source
pub trait ToolTrait {
    // Required methods
    fn name(&self) -> String;
    fn description(&self) -> String;
    fn input_schema(&self) -> Value;
    fn execute(&self, input: Value) -> Value;
}
Expand description

Trait for implementing custom tools that agents can use.

Tools extend agent capabilities by allowing them to interact with external systems, perform calculations, access databases, make API calls, and more.

§Examples

use ceylon_next::tools::ToolTrait;
use serde_json::{json, Value};

struct CalculatorTool;

impl ToolTrait for CalculatorTool {
    fn name(&self) -> String {
        "calculator".to_string()
    }

    fn description(&self) -> String {
        "Performs basic arithmetic operations (add, subtract, multiply, divide)".to_string()
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "enum": ["add", "subtract", "multiply", "divide"]
                },
                "a": {"type": "number"},
                "b": {"type": "number"}
            },
            "required": ["operation", "a", "b"]
        })
    }

    fn execute(&self, input: Value) -> Value {
        let op = input["operation"].as_str().unwrap();
        let a = input["a"].as_f64().unwrap();
        let b = input["b"].as_f64().unwrap();

        let result = match op {
            "add" => a + b,
            "subtract" => a - b,
            "multiply" => a * b,
            "divide" => a / b,
            _ => 0.0,
        };

        json!({"result": result})
    }
}

// Use the tool with an agent
use ceylon_next::agent::Agent;

let mut agent = Agent::new("Calculator Agent", "openai::gpt-4");
agent.add_tool(CalculatorTool);

Required Methods§

Source

fn name(&self) -> String

Returns the unique name of the tool.

This name is used by the LLM to identify and invoke the tool.

Source

fn description(&self) -> String

Returns a human-readable description of what the tool does.

This description helps the LLM understand when and how to use the tool.

Source

fn input_schema(&self) -> Value

Returns the JSON schema defining the tool’s input parameters.

The schema should follow the JSON Schema specification and define all required and optional parameters for the tool.

Source

fn execute(&self, input: Value) -> Value

Executes the tool with the given input and returns the result.

§Arguments
  • input - A JSON value containing the tool’s input parameters
§Returns

A JSON value containing the tool’s output

Implementors§