llm_chain/tools/
description.rsuse serde::{ser::SerializeMap, Serialize, Serializer};
#[derive(Clone, Debug)]
pub struct FormatPart {
pub key: String,
pub purpose: String,
}
impl FormatPart {
pub fn new(key: &str, purpose: &str) -> Self {
FormatPart {
key: key.to_string(),
purpose: purpose.to_string(),
}
}
}
impl<K: Into<String>, P: Into<String>> From<(K, P)> for FormatPart {
fn from((k, p): (K, P)) -> Self {
FormatPart::new(&k.into(), &p.into())
}
}
#[derive(Debug)]
pub struct Format {
pub parts: Vec<FormatPart>,
}
impl Format {
pub fn new(parts: Vec<FormatPart>) -> Self {
Format { parts }
}
}
impl<T: AsRef<[FormatPart]>> From<T> for Format {
fn from(parts: T) -> Self {
Format::new(parts.as_ref().to_vec())
}
}
impl Serialize for Format {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let n = self.parts.len();
let mut map = serializer.serialize_map(Some(n))?;
for part in &self.parts {
map.serialize_entry(&part.key, &part.purpose)?;
}
map.end()
}
}
pub trait Describe {
fn describe() -> Format;
}
#[derive(Serialize, Debug)]
pub struct ToolDescription {
pub name: String,
pub description: String,
pub description_context: String,
pub input_format: Format,
pub output_format: Format,
}
impl ToolDescription {
pub fn new(
name: &str,
description: &str,
description_context: &str,
input_format: Format,
output_format: Format,
) -> Self {
ToolDescription {
name: name.to_string(),
description: description.to_string(),
description_context: description_context.to_string(),
input_format,
output_format,
}
}
}