langchain_rust/schemas/
tools_openai_like.rs

1use std::ops::Deref;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::tools::Tool;
7
8#[derive(Clone, Debug)]
9pub enum FunctionCallBehavior {
10    None,
11    Auto,
12    Named(String),
13}
14
15#[derive(Clone, Debug)]
16pub struct FunctionDefinition {
17    pub name: String,
18    pub description: String,
19    pub parameters: Value,
20}
21
22impl FunctionDefinition {
23    pub fn new(name: &str, description: &str, parameters: Value) -> Self {
24        FunctionDefinition {
25            name: name.trim().replace(" ", "_"),
26            description: description.to_string(),
27            parameters,
28        }
29    }
30
31    /// Generic function that can be used with both Arc<Tool>, Box<Tool>, and direct references
32    pub fn from_langchain_tool<T>(tool: &T) -> FunctionDefinition
33    where
34        T: Deref<Target = dyn Tool> + ?Sized,
35    {
36        FunctionDefinition {
37            name: tool.name().trim().replace(" ", "_"),
38            description: tool.description(),
39            parameters: tool.parameters(),
40        }
41    }
42}
43
44#[derive(Serialize, Deserialize, Debug)]
45pub struct FunctionCallResponse {
46    pub id: String,
47    #[serde(rename = "type")]
48    pub type_field: String,
49    pub function: FunctionDetail,
50}
51
52#[derive(Serialize, Deserialize, Debug)]
53pub struct FunctionDetail {
54    pub name: String,
55    ///this should be an string, and this should be passed to the tool, to
56    ///then be deserilised inside the tool, becuase just the tools knows the names of the arguments.
57    pub arguments: String,
58}
59
60impl FunctionCallResponse {
61    pub fn from_str(s: &str) -> Result<Self, serde_json::Error> {
62        serde_json::from_str(s)
63    }
64}