1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
extern crate proc_macro;
use TokenStream;
use ;
/// Derives `rig_core::embeddings::Embed` using fields marked with `#[embed]`.
///
/// ```
/// use rig_derive::Embed;
///
/// #[derive(Embed)]
/// struct Document {
/// #[embed]
/// description: String,
/// }
/// ```
/// Derives `rig_core::tool::ContextValue` with the type name as its default key.
/// `#[context(key = "...")]` overrides the key. The type must support serde
/// serialization and deserialization.
///
/// ```
/// use rig_derive::ContextValue;
///
/// #[derive(serde::Serialize, serde::Deserialize, ContextValue)]
/// #[context(key = "session.id")]
/// struct SessionId(String);
/// ```
/// Generates a tool type, parameter struct, and uppercase static from a function
/// returning `Result<T, E>`, implementing `rig_core::tool::Tool`. The function
/// may take the call's `&mut ToolContext`. Imported context names need
/// `#[rig(context)]`; qualified Rig paths are recognized directly. The context
/// is excluded from model arguments.
///
/// Accepts `name`, `description`, `params(name = "description")`, and
/// `required(name, ...)`. Explicit names must start with an ASCII letter or `_`,
/// contain only ASCII letters, digits, `_`, or `-`, and be at most 64 bytes.
/// Description attributes override doc comments. Unknown or duplicate options
/// and parameter names are rejected.
///
/// Non-`Option` arguments are required by default. An explicit required list
/// makes omitted arguments default through serde, requiring `Default`.
/// Listing an `Option` argument as required is rejected.
///
/// A generated tool runs on JSON arguments in one call through
/// `rig_core::serve::adapters::ToolAdapter::run` (or `run_with` to pass a
/// context and read what the tool published).
///
/// ```
/// use rig_core::serve::adapters::ToolAdapter;
/// use rig_derive::rig_tool;
///
/// #[rig_tool(description = "Add integers", params(a = "First operand"), required(a))]
/// fn add(a: i32, b: i32) -> Result<i32, rig_core::tool::ToolExecutionError> {
/// Ok(a + b)
/// }
///
/// let runtime = tokio::runtime::Builder::new_current_thread().build()?;
/// let output = runtime.block_on(ToolAdapter::new(Add).run(serde_json::json!({ "a": 2, "b": 3 })))?;
/// assert_eq!(output.as_json(), Some(&serde_json::json!(5)));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```