async_llm/types/
chat_tool.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::error::Error;
5
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
7#[serde(tag = "type")]
8#[serde(rename_all = "snake_case")]
9pub enum ChatTool {
10    Function { function: ChatToolFunction },
11}
12
13#[derive(Debug, Clone, Builder, Default, Serialize, Deserialize, PartialEq)]
14#[builder(setter(into, strip_option), default)]
15#[builder(derive(Debug))]
16#[builder(build_fn(error = Error))]
17pub struct ChatToolFunction {
18    /// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
19    pub name: String,
20
21    /// A description of what the function does, used by the model to choose when and how to call the function.
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub description: Option<String>,
24
25    /// The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/text-generation/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.
26    ///
27    /// Omitting `parameters` defines a function with an empty parameter list.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub parameters: Option<serde_json::Value>,
30
31    /// Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the `parameters` field. Only a subset of JSON Schema is supported when `strict` is `true`. Learn more about Structured Outputs in the [function calling guide](https://platform.openai.com/docs/guides/function-calling).
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub strict: Option<bool>,
34}
35
36impl Into<ChatTool> for ChatToolFunction {
37    fn into(self) -> ChatTool {
38        ChatTool::Function { function: self }
39    }
40}
41
42impl ChatToolFunction {
43    pub fn new(name: impl Into<String>) -> Self {
44        Self {
45            name: name.into(),
46            description: None,
47            parameters: None,
48            strict: None,
49        }
50    }
51
52    pub fn description(mut self, description: impl Into<String>) -> Self {
53        self.description = Some(description.into());
54        self
55    }
56
57    pub fn strict(mut self, value: bool) -> Self {
58        self.strict = Some(value);
59        self
60    }
61
62    pub fn parameters(mut self, parameters: serde_json::Value) -> Self {
63        self.parameters = Some(parameters);
64        self
65    }
66}