openfunctions-rs 0.1.0

A universal framework for creating and managing LLM tools and agents
Documentation
//! Function declaration types for LLM integration
//!
//! This module defines the structures for representing functions that can be called
//! by Large Language Models (LLMs), including their parameters and types.

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Represents a function's declaration, which can be passed to an LLM.
///
/// This structure provides the LLM with the necessary information to understand
/// what a function does and what arguments it expects.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct FunctionDeclaration {
    /// The name of the function. Must be unique within the set of available functions.
    pub name: String,

    /// A detailed description of what the function does. This is used by the LLM
    /// to decide when to call the function.
    pub description: String,

    /// A list of parameters that the function accepts.
    pub parameters: Vec<Parameter>,
}

/// Defines a single parameter for a function.
///
/// Each parameter has a name, type, description, and other constraints that help
/// the LLM provide valid arguments.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Parameter {
    /// The name of the parameter.
    pub name: String,

    /// The data type of the parameter.
    pub param_type: ParameterType,

    /// A description of the parameter's purpose.
    pub description: String,

    /// Indicates whether this parameter is required for the function call.
    pub required: bool,

    /// An optional default value for the parameter, used if no value is provided.
    /// The value is represented as a `serde_json::Value`.
    pub default: Option<serde_json::Value>,
}

/// An enumeration of supported parameter types.
///
/// These types correspond to JSON Schema types and help in validating the arguments
/// provided by the LLM.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ParameterType {
    /// A UTF-8 string.
    String,
    /// A 64-bit signed integer.
    Integer,
    /// A 64-bit floating-point number.
    Number,
    /// A boolean value (`true` or `false`).
    Boolean,
    /// An array of values. The type of items in the array is not strictly enforced at this level.
    Array,
    /// A JSON object.
    Object,
    /// A string that must be one of a predefined set of values.
    #[serde(rename = "enum")]
    Enum(Vec<String>),
}