llm_chain/tools/
description.rs

1use serde::{ser::SerializeMap, Serialize, Serializer};
2
3/// Represents a single parameter for a tool.
4#[derive(Clone, Debug)]
5pub struct FormatPart {
6    pub key: String,
7    pub purpose: String,
8}
9
10impl FormatPart {
11    /// Creates a new `FormatPart` with the given key and purpose.
12    pub fn new(key: &str, purpose: &str) -> Self {
13        FormatPart {
14            key: key.to_string(),
15            purpose: purpose.to_string(),
16        }
17    }
18}
19
20impl<K: Into<String>, P: Into<String>> From<(K, P)> for FormatPart {
21    fn from((k, p): (K, P)) -> Self {
22        FormatPart::new(&k.into(), &p.into())
23    }
24}
25
26/// Represents the format for a tool's input or output.
27#[derive(Debug)]
28pub struct Format {
29    pub parts: Vec<FormatPart>,
30}
31
32impl Format {
33    /// Creates a new `Format` with the given parts.
34    pub fn new(parts: Vec<FormatPart>) -> Self {
35        Format { parts }
36    }
37}
38
39impl<T: AsRef<[FormatPart]>> From<T> for Format {
40    fn from(parts: T) -> Self {
41        Format::new(parts.as_ref().to_vec())
42    }
43}
44
45impl Serialize for Format {
46    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
47    where
48        S: Serializer,
49    {
50        let n = self.parts.len();
51        let mut map = serializer.serialize_map(Some(n))?;
52        for part in &self.parts {
53            map.serialize_entry(&part.key, &part.purpose)?;
54        }
55        map.end()
56    }
57}
58
59/// A trait to provide a description format for a tool.
60pub trait Describe {
61    fn describe() -> Format;
62}
63
64/// Represents the description of a tool, including its name, usage, and input/output formats.
65#[derive(Serialize, Debug)]
66pub struct ToolDescription {
67    pub name: String,
68    pub description: String,
69    pub description_context: String,
70    pub input_format: Format,
71    // #[serde(skip)]
72    // #[allow(dead_code)]
73    /// This will be used in the future.
74    pub output_format: Format,
75}
76
77impl ToolDescription {
78    /// Creates a new `ToolDescription` with the given name, description, context, and formats.
79    pub fn new(
80        name: &str,
81        description: &str,
82        description_context: &str,
83        input_format: Format,
84        output_format: Format,
85    ) -> Self {
86        ToolDescription {
87            name: name.to_string(),
88            description: description.to_string(),
89            description_context: description_context.to_string(),
90            input_format,
91            output_format,
92        }
93    }
94}