openfunctions_rs/core/function.rs
1//! Function declaration types for LLM integration
2//!
3//! This module defines the structures for representing functions that can be called
4//! by Large Language Models (LLMs), including their parameters and types.
5
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9/// Represents a function's declaration, which can be passed to an LLM.
10///
11/// This structure provides the LLM with the necessary information to understand
12/// what a function does and what arguments it expects.
13#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
14pub struct FunctionDeclaration {
15 /// The name of the function. Must be unique within the set of available functions.
16 pub name: String,
17
18 /// A detailed description of what the function does. This is used by the LLM
19 /// to decide when to call the function.
20 pub description: String,
21
22 /// A list of parameters that the function accepts.
23 pub parameters: Vec<Parameter>,
24}
25
26/// Defines a single parameter for a function.
27///
28/// Each parameter has a name, type, description, and other constraints that help
29/// the LLM provide valid arguments.
30#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
31pub struct Parameter {
32 /// The name of the parameter.
33 pub name: String,
34
35 /// The data type of the parameter.
36 pub param_type: ParameterType,
37
38 /// A description of the parameter's purpose.
39 pub description: String,
40
41 /// Indicates whether this parameter is required for the function call.
42 pub required: bool,
43
44 /// An optional default value for the parameter, used if no value is provided.
45 /// The value is represented as a `serde_json::Value`.
46 pub default: Option<serde_json::Value>,
47}
48
49/// An enumeration of supported parameter types.
50///
51/// These types correspond to JSON Schema types and help in validating the arguments
52/// provided by the LLM.
53#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
54#[serde(rename_all = "lowercase")]
55pub enum ParameterType {
56 /// A UTF-8 string.
57 String,
58 /// A 64-bit signed integer.
59 Integer,
60 /// A 64-bit floating-point number.
61 Number,
62 /// A boolean value (`true` or `false`).
63 Boolean,
64 /// An array of values. The type of items in the array is not strictly enforced at this level.
65 Array,
66 /// A JSON object.
67 Object,
68 /// A string that must be one of a predefined set of values.
69 #[serde(rename = "enum")]
70 Enum(Vec<String>),
71}