2 unstable releases
new 0.2.0 | Jan 19, 2025 |
---|---|
0.1.0 | Jan 19, 2025 |
#671 in Procedural macros
17KB
147 lines
rig-tool-macro
Rather than satisfy the rig Tool
trait explicitly, you can annotate the tools
with the #[tool]
attribute. This will automatically generate the Tool
implementation for you.
#[tool]
fn how_many_rs(s: String) -> anyhow::Result<usize> {
Ok(s.chars()
.filter(|c| *c == 'r' || *c == 'R')
.collect::<Vec<_>>()
.len())
}
and then call it:
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let agent = providers::openai::Client::from_env()
.agent(providers::openai::GPT_4O)
.tool(HowManyRs) // <- generated by the macro
.max_tokens(1024)
let res = agent.prompt("how many Rs are in the word strawberry?").await?;
println!("{}", res);
Ok(())
}
The current implementation supports standard types and non-nested inputs
Structs and nested stuff might come at some point, for now tools have to take top level inputs comprised of standard types
Adding the macros to impl
methods is also not yet supported, those have to be
top-level functions due to the global generation of the Tool
trait impl
Dependencies
~10–22MB
~318K SLoC