PitchHut logo
Seamlessly serialize functions for LLM integration.
Pitch

Tools-rs is a dynamic framework designed for constructing and executing tools with automatic JSON schema generation, tailor-made for Large Language Model integration. Featuring compile-time function registration, type safety, async support, and rich error handling, it enhances the functionality of your applications while simplifying tool management.

Description

Tools-rs is a robust framework designed for building, registering, and executing various tools with enhanced integration capabilities for Large Language Models (LLMs). This framework simplifies the entire process of tool management, featuring automatic JSON schema generation and comprehensive type safety.

Key Features

  • Automatic Registration: Functions can be annotated with #[tool] for seamless compile-time discovery and registration.
  • JSON Schema Generation: Automatically generates schemas for LLMs, incorporating full type information to facilitate smooth integration.
  • Type Safety: Ensures high-level type safety through JSON serialization, with validations checked at compile time.
  • Async Support: Built on tokio, this framework leverages async/await for efficient asynchronous execution.
  • Error Handling: Robust error types provide contextual information for better debugging and error tracking.
  • LLM Integration: Easily export function declarations formatted for LLM function calling APIs, such as OpenAI and Anthropic.
  • Manual Registration: Supports programmatic tool registration for more dynamic scenarios when needed.
  • Inventory System: Uses the inventory crate for link-time tool collection, ensuring zero-cost runtime discovery.

Quick Start Example

Here’s a concise example of how to use Tools-rs:

use serde_json::json;
use tools_rs::{collect_tools, FunctionCall, tool};

#[tool]
/// Adds two numbers.
async fn add(pair: (i32, i32)) -> i32 {
    pair.0 + pair.1
}

#[tool]
/// Greets a person.
async fn greet(name: String) -> String {
    format!("Hello, {name}!")
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let tools = collect_tools();

    let sum = tools
        .call(FunctionCall::new(
            "add".into(),
            json!({ "pair": [3, 4] }),
        ))
        .await?.result;
    println!("add → {sum}");  // Outputs: "add → 7"

    let hi = tools
        .call(FunctionCall::new(
            "greet".into(),
            json!({ "name": "Alice" }),
        ))
        .await?.result;
    println!("greet → {hi}");  // Outputs: "greet → \"Hello, Alice!\""

    // Export function declarations for LLM APIs
    let declarations = tools.json()?;
    println!("Function declarations: {}", serde_json::to_string_pretty(&declarations)?);

    Ok(())
}

Crate Structure

Tools-rs is organized into a Rust workspace consisting of three primary crates:

  • tools-rs: The main entry point.
  • tools_core: Contains runtime implementations for tool execution, JSON schema generation, and error handling.
  • tools_macros: Provides procedural macros for automatic tool registration and schema generation.

Resources such as comprehensive examples and usage scenarios are readily available in the examples directory.

Performance and Compatibility

Tools-rs is tailored for performance with minimal overhead during tool registration and execution. It requires Rust version 1.85 or later, providing support for full type checking and graceful error handling.

Utilizing efficient data structures ensures that tool discovery remains low-cost, promoting high performance across various operations.

Consider Tools-rs when you need a powerful framework for your tools that handles LLM integrations effortlessly, providing type safety and automatic schema generation.

0 comments

No comments yet.

Sign in to be the first to comment.