@@ -27,13 +27,6 @@ pub fn get_weather(location: String) {
2727 Ok (weather . to_string ())
2828}
2929```
30- The macro will infer the following fields from the function:
31-
32- - ** name** :
33- - ** description** : The function's doc comment. you can use rust style documentation with examples for few-shot prompting.
34- - ** input_schema** : The schema of the function's input(argument) type. remember that whatever you define as
35- arguements should implement the ` schemars::JsonSchema ` trait or be natively supported by the ` schemars ` crate.
36- - ** execute** : The function itself. The body will be executed when the AI model calls the tool.
3730
3831A tool has 4 components:
3932
@@ -63,24 +56,48 @@ You can define your own tools in aisdk by instantiating the `Tool` and related s
6356it to one of the AI model text generation builders.
6457
6558``` rust
59+ use super :: {Tool , ToolExecute };
60+ use serde_json :: Value ;
61+
62+ // define tool function body, should return Result<String, String>
63+ #[allow(unused_variables)]
6664let func = ToolExecute :: new (Box :: new (| inp : Value | {
67- Ok (format! (" hello {}" , inp . get (" recipient" ). unwrap ()))
65+ // Ai SDK will pass in a json object with the following structure
66+ // ```json
67+ // {
68+ // "location": "New York"
69+ // }
70+ // ```
71+ let location = inp . get (" location" ). unwrap ();
72+ Ok (format! (" Cloudy" ))
6873}));
6974
75+ // define tool input structure
7076#[derive(schemars:: JsonSchema , Debug )]
77+ #[allow(dead_code)]
7178struct ToolInput {
72- recipient : String ,
79+ location : String ,
7380}
7481
82+ // change tool arguments to json schema
83+ // Which will be similar to the following
84+ // ```json
85+ // "properties": {
86+ // "location": {
87+ // "type": "string"
88+ // }
89+ // }
7590let schema = schemars :: schema_for! (ToolInput );
7691
77- let my_tool = Tool :: builder ()
78- . name (" my-tool" ) // this is the name in which the Ai will use to call the tool
79- . description (" my-description" ) // describes what the tool does, helpful for the Ai
80- . input_schema (schema . clone ()) // the schema of the input. Ai will use this to generate inputs
81- . execute (func ) // the function that will be called when the Ai calls the tool
92+ // bring it all together
93+ let get_weather_tool = Tool :: builder ()
94+ . name (" get-weather" )
95+ . description (" Get the weather information given a location" )
96+ . input_schema (schema . clone ())
97+ . execute (func )
8298 . build ()
8399 . unwrap ();
100+
84101```
85102
86103## Registering the tool
@@ -89,12 +106,13 @@ To register the tool with the AI model, you need to add it to text generation bu
89106appends the tool to the list of tools that the AI model will use to generate text.
90107
91108``` rust
109+ // call the model with the tool
92110let result = LanguageModelRequest :: builder ()
93111 . model (OpenAI :: new (" gpt-4o" ))
94112 . system (" You are a helpful assistant with access to tools." )
95113 . prompt (" What is the weather in New York?" )
96- . with_tool (get_weather )
114+ . with_tool (get_weather_tool ) // you don't need to call it with.
97115 . build ()
98116 . generate_text ()
99- . await ? ;
117+ . await ;
100118```
0 commit comments