pub struct FunctionTool { /* private fields */ }Expand description
A tool that wraps a callback and shapes its return value into a result (FunctionTool).
The callback receives the raw input and the ToolContext and returns a JSON value, which
becomes the result content following these rules:
- a one-key object that reads as tool result content (
{"text": ..},{"json": ..},{"image": ..},{"video": ..},{"document": ..}) is used as is, and so is an array of such objects; - a string, number or boolean becomes text;
nullbecomes the textnull;- any other array becomes JSON content
{"$value": [..]}, and any other object JSON content.
An Err becomes an error result with the text Error: <message>, except an interrupt, which
ends the tool as ToolOutcome::Interrupt.
use serde_json::json;
use strands::tools::FunctionTool;
let greet = FunctionTool::builder("greeter", "Greets a person by name")
.input_schema(json!({
"type": "object",
"properties": {"name": {"type": "string"}},
"required": ["name"]
}))
.callback(|input, _context| async move { Ok(format!("Hello, {}!", input["name"].as_str().unwrap_or("you")).into()) });Implementations§
Source§impl FunctionTool
impl FunctionTool
Trait Implementations§
Source§impl Clone for FunctionTool
impl Clone for FunctionTool
Source§impl Debug for FunctionTool
impl Debug for FunctionTool
Source§impl Tool for FunctionTool
impl Tool for FunctionTool
Source§fn description(&self) -> &str
fn description(&self) -> &str
What the tool does; it must match the spec’s description.
Source§fn tool_spec(&self) -> ToolSpec
fn tool_spec(&self) -> ToolSpec
The spec sent to the model: name, description and input schema.
Source§fn stream(&self, context: ToolContext) -> ToolStream
fn stream(&self, context: ToolContext) -> ToolStream
Runs the tool. The stream is
'static, so the agent can run it on its own task.Auto Trait Implementations§
impl !RefUnwindSafe for FunctionTool
impl !UnwindSafe for FunctionTool
impl Freeze for FunctionTool
impl Send for FunctionTool
impl Sync for FunctionTool
impl Unpin for FunctionTool
impl UnsafeUnpin for FunctionTool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreCreates a shared type from an unshared type.