pub trait Tool: Send + 'static {
type Arguments: JsonSchema + DeserializeOwned;
const NAME: &str;
const DESCRIPTION: &str;
// Required method
fn call(
&mut self,
arguments: Self::Arguments,
) -> impl Future<Output = Result> + Send;
}
Expand description
Tools that can be called by language models.
§Example
use ai_types::llm::Tool;
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(JsonSchema, Deserialize)]
struct CalculatorArgs {
operation: String,
a: f64,
b: f64,
}
struct Calculator;
impl Tool for Calculator {
const NAME: &str = "calculator";
const DESCRIPTION: &str = "Performs basic mathematical operations";
type Arguments = CalculatorArgs;
async fn call(&mut self, args: Self::Arguments) -> ai_types::Result {
match args.operation.as_str() {
"add" => Ok((args.a + args.b).to_string()),
"subtract" => Ok((args.a - args.b).to_string()),
"multiply" => Ok((args.a * args.b).to_string()),
"divide" => {
if args.b != 0.0 {
Ok((args.a / args.b).to_string())
} else {
Err(anyhow::Error::msg("Division by zero"))
}
}
_ => Err(anyhow::Error::msg("Unknown operation")),
}
}
}
Required Associated Constants§
Sourceconst DESCRIPTION: &str
const DESCRIPTION: &str
Tool description for the language model.
Required Associated Types§
Sourcetype Arguments: JsonSchema + DeserializeOwned
type Arguments: JsonSchema + DeserializeOwned
Tool arguments type. Must implement schemars::JsonSchema
and serde::de::DeserializeOwned
.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.