Skip to main content

litellm_rs/core/models/openai/
tools.rs

1//! Tool and function calling types for OpenAI-compatible API
2//!
3//! This module defines structures for function calling (legacy) and tool calling,
4//! including function definitions, tool choices, and tool calls.
5
6use serde::{Deserialize, Serialize};
7
8/// Function definition (legacy)
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct Function {
11    /// Function name
12    pub name: String,
13    /// Function description
14    pub description: Option<String>,
15    /// Function parameters schema
16    pub parameters: Option<serde_json::Value>,
17}
18
19/// Function call (legacy)
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct FunctionCall {
22    /// Function name
23    pub name: String,
24    /// Function arguments (JSON string)
25    pub arguments: String,
26}
27
28/// Tool definition
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct Tool {
31    /// Tool type
32    #[serde(rename = "type")]
33    pub tool_type: String,
34    /// Function definition
35    pub function: Function,
36}
37
38/// Tool choice
39#[derive(Debug, Clone, Serialize, Deserialize)]
40#[serde(untagged)]
41pub enum ToolChoice {
42    /// No tool calls allowed
43    None(String), // "none"
44    /// Automatic tool selection
45    Auto(String), // "auto"
46    /// Tool calls required
47    Required(String), // "required"
48    /// Specific tool to use
49    Specific(ToolChoiceFunction),
50}
51
52/// Specific tool choice
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct ToolChoiceFunction {
55    /// Tool type
56    #[serde(rename = "type")]
57    pub tool_type: String,
58    /// Function specification
59    pub function: ToolChoiceFunctionSpec,
60}
61
62/// Tool choice function specification
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct ToolChoiceFunctionSpec {
65    /// Function name
66    pub name: String,
67}
68
69/// Tool call
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct ToolCall {
72    /// Tool call ID
73    pub id: String,
74    /// Tool type
75    #[serde(rename = "type")]
76    pub tool_type: String,
77    /// Function call
78    pub function: FunctionCall,
79}
80
81impl From<FunctionCall> for crate::core::types::tools::FunctionCall {
82    fn from(value: FunctionCall) -> Self {
83        Self {
84            name: value.name,
85            arguments: value.arguments,
86        }
87    }
88}
89
90impl From<crate::core::types::tools::FunctionCall> for FunctionCall {
91    fn from(value: crate::core::types::tools::FunctionCall) -> Self {
92        Self {
93            name: value.name,
94            arguments: value.arguments,
95        }
96    }
97}
98
99impl From<ToolCall> for crate::core::types::tools::ToolCall {
100    fn from(value: ToolCall) -> Self {
101        Self {
102            id: value.id,
103            tool_type: value.tool_type,
104            function: value.function.into(),
105        }
106    }
107}
108
109impl From<crate::core::types::tools::ToolCall> for ToolCall {
110    fn from(value: crate::core::types::tools::ToolCall) -> Self {
111        Self {
112            id: value.id,
113            tool_type: value.tool_type,
114            function: value.function.into(),
115        }
116    }
117}
118
119/// Function call delta (legacy)
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct FunctionCallDelta {
122    /// Function name
123    pub name: Option<String>,
124    /// Function arguments delta
125    pub arguments: Option<String>,
126}
127
128/// Tool call delta
129#[derive(Debug, Clone, Serialize, Deserialize)]
130pub struct ToolCallDelta {
131    /// Tool call index
132    pub index: u32,
133    /// Tool call ID
134    pub id: Option<String>,
135    /// Tool type
136    #[serde(rename = "type")]
137    pub tool_type: Option<String>,
138    /// Function call delta
139    pub function: Option<FunctionCallDelta>,
140}