mirror of https://github.com/0xplaygrounds/rig
Remove excessive comments and fix broken test
This commit is contained in:
parent
d0cde02f34
commit
b514a7d401
|
@ -1,48 +0,0 @@
|
||||||
# Rig Tool Macro Examples
|
|
||||||
|
|
||||||
This directory contains examples demonstrating different ways to use the `rig_tool` macro with a rig Agent.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
### 1. Simple Example (`simple.rs`)
|
|
||||||
Demonstrates the most basic usage of the macro without any attributes. Shows how to create a simple tool that adds two numbers and use it with a rig Agent.
|
|
||||||
|
|
||||||
### 2. With Description (`with_description.rs`)
|
|
||||||
Shows how to add a description to your tool using the `description` attribute. Implements a calculator that can perform basic arithmetic operations and uses it with a rig Agent.
|
|
||||||
|
|
||||||
### 3. Full Attributes (`full.rs`)
|
|
||||||
Demonstrates using all available attributes including parameter descriptions. Implements a string processor that can perform various string operations and uses it with a rig Agent.
|
|
||||||
|
|
||||||
### 4. Async Tool (`async_tool.rs`)
|
|
||||||
Demonstrates how to create and use async tools with a rig Agent, including:
|
|
||||||
- Basic async operation
|
|
||||||
- Error handling in async context
|
|
||||||
|
|
||||||
## Running the Examples
|
|
||||||
|
|
||||||
To run any example, use:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cargo run --example <example_name>
|
|
||||||
```
|
|
||||||
|
|
||||||
For example:
|
|
||||||
```bash
|
|
||||||
cargo run --example simple
|
|
||||||
cargo run --example with_description
|
|
||||||
cargo run --example full
|
|
||||||
cargo run --example async_tool
|
|
||||||
```
|
|
||||||
|
|
||||||
## Features Demonstrated
|
|
||||||
|
|
||||||
- Basic tool creation
|
|
||||||
- Optional attributes
|
|
||||||
- Parameter descriptions
|
|
||||||
- Error handling
|
|
||||||
- Async support
|
|
||||||
- Static tool instances
|
|
||||||
- Parameter validation
|
|
||||||
- Integration with rig Agent
|
|
||||||
- Natural language interaction with tools
|
|
||||||
- Tool definitions and schemas
|
|
|
@ -15,10 +15,8 @@ use tracing_subscriber;
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
async fn async_operation(input: String, delay_ms: u64) -> Result<String, rig::tool::ToolError> {
|
async fn async_operation(input: String, delay_ms: u64) -> Result<String, rig::tool::ToolError> {
|
||||||
// Simulate some async work
|
|
||||||
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
|
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
|
||||||
|
|
||||||
// Process the input
|
|
||||||
Ok(format!(
|
Ok(format!(
|
||||||
"Processed after {}ms: {}",
|
"Processed after {}ms: {}",
|
||||||
delay_ms,
|
delay_ms,
|
||||||
|
@ -28,10 +26,8 @@ async fn async_operation(input: String, delay_ms: u64) -> Result<String, rig::to
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Initialize tracing
|
|
||||||
tracing_subscriber::fmt().pretty().init();
|
tracing_subscriber::fmt().pretty().init();
|
||||||
|
|
||||||
// Create an agent with the ASYNCOPERATION tool
|
|
||||||
let async_agent = providers::openai::Client::from_env()
|
let async_agent = providers::openai::Client::from_env()
|
||||||
.agent(providers::openai::GPT_4O)
|
.agent(providers::openai::GPT_4O)
|
||||||
.preamble("You are an agent with tools access, always use the tools")
|
.preamble("You are an agent with tools access, always use the tools")
|
||||||
|
@ -39,14 +35,12 @@ async fn main() {
|
||||||
.tool(AsyncOperation)
|
.tool(AsyncOperation)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Print out the tool definition to verify
|
|
||||||
println!("Tool definition:");
|
println!("Tool definition:");
|
||||||
println!(
|
println!(
|
||||||
"ASYNCOPERATION: {}",
|
"ASYNCOPERATION: {}",
|
||||||
serde_json::to_string_pretty(&AsyncOperation.definition(String::default()).await).unwrap()
|
serde_json::to_string_pretty(&AsyncOperation.definition(String::default()).await).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test prompts
|
|
||||||
for prompt in [
|
for prompt in [
|
||||||
"What tools do you have?",
|
"What tools do you have?",
|
||||||
"Process the text 'hello world' with a delay of 1000ms",
|
"Process the text 'hello world' with a delay of 1000ms",
|
||||||
|
|
|
@ -30,10 +30,8 @@ fn string_processor(text: String, operation: String) -> Result<String, rig::tool
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Initialize tracing
|
|
||||||
tracing_subscriber::fmt().pretty().init();
|
tracing_subscriber::fmt().pretty().init();
|
||||||
|
|
||||||
// Create an agent with the STRINGPROCESSOR tool
|
|
||||||
let string_agent = providers::openai::Client::from_env()
|
let string_agent = providers::openai::Client::from_env()
|
||||||
.agent(providers::openai::GPT_4O)
|
.agent(providers::openai::GPT_4O)
|
||||||
.preamble("You are an agent with tools access, always use the tools")
|
.preamble("You are an agent with tools access, always use the tools")
|
||||||
|
@ -41,14 +39,12 @@ async fn main() {
|
||||||
.tool(StringProcessor)
|
.tool(StringProcessor)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Print out the tool definition to verify
|
|
||||||
println!("Tool definition:");
|
println!("Tool definition:");
|
||||||
println!(
|
println!(
|
||||||
"STRINGPROCESSOR: {}",
|
"STRINGPROCESSOR: {}",
|
||||||
serde_json::to_string_pretty(&StringProcessor.definition(String::default()).await).unwrap()
|
serde_json::to_string_pretty(&StringProcessor.definition(String::default()).await).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test prompts
|
|
||||||
for prompt in [
|
for prompt in [
|
||||||
"What tools do you have?",
|
"What tools do you have?",
|
||||||
"Convert 'hello world' to uppercase",
|
"Convert 'hello world' to uppercase",
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use rig::completion::Prompt;
|
use rig::completion::Prompt;
|
||||||
use rig::providers;
|
use rig::providers;
|
||||||
use rig::serde_json;
|
|
||||||
use rig_macros::rig_tool;
|
use rig_macros::rig_tool;
|
||||||
use tracing_subscriber;
|
use tracing_subscriber;
|
||||||
|
|
||||||
|
@ -51,10 +50,8 @@ fn sum_numbers(numbers: Vec<i64>) -> Result<i64, rig::tool::ToolError> {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Initialize tracing
|
|
||||||
tracing_subscriber::fmt().pretty().init();
|
tracing_subscriber::fmt().pretty().init();
|
||||||
|
|
||||||
// Create an agent with the ADD tool
|
|
||||||
let calculator_agent = providers::openai::Client::from_env()
|
let calculator_agent = providers::openai::Client::from_env()
|
||||||
.agent(providers::openai::GPT_4O)
|
.agent(providers::openai::GPT_4O)
|
||||||
.preamble("You are an agent with tools access, always use the tools")
|
.preamble("You are an agent with tools access, always use the tools")
|
||||||
|
@ -62,7 +59,6 @@ async fn main() {
|
||||||
.tool(Add)
|
.tool(Add)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Test prompts
|
|
||||||
for prompt in [
|
for prompt in [
|
||||||
"What tools do you have?",
|
"What tools do you have?",
|
||||||
"Calculate 5 + 3",
|
"Calculate 5 + 3",
|
||||||
|
|
|
@ -29,10 +29,8 @@ fn calculator(x: i32, y: i32, operation: String) -> Result<i32, rig::tool::ToolE
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Initialize tracing
|
|
||||||
tracing_subscriber::fmt().pretty().init();
|
tracing_subscriber::fmt().pretty().init();
|
||||||
|
|
||||||
// Create an agent with the CALCULATOR tool
|
|
||||||
let calculator_agent = providers::openai::Client::from_env()
|
let calculator_agent = providers::openai::Client::from_env()
|
||||||
.agent(providers::openai::GPT_4O)
|
.agent(providers::openai::GPT_4O)
|
||||||
.preamble("You are an agent with tools access, always use the tools")
|
.preamble("You are an agent with tools access, always use the tools")
|
||||||
|
@ -40,14 +38,12 @@ async fn main() {
|
||||||
.tool(Calculator)
|
.tool(Calculator)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Print out the tool definition to verify
|
|
||||||
println!("Tool definition:");
|
println!("Tool definition:");
|
||||||
println!(
|
println!(
|
||||||
"CALCULATOR: {}",
|
"CALCULATOR: {}",
|
||||||
serde_json::to_string_pretty(&CALCULATOR.definition(String::default()).await).unwrap()
|
serde_json::to_string_pretty(&CALCULATOR.definition(String::default()).await).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test prompts
|
|
||||||
for prompt in [
|
for prompt in [
|
||||||
"What tools do you have?",
|
"What tools do you have?",
|
||||||
"Calculate 5 + 3",
|
"Calculate 5 + 3",
|
||||||
|
|
|
@ -197,7 +197,6 @@ pub fn rig_tool(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the implementation
|
|
||||||
let params_struct_name = format_ident!("{}Parameters", struct_name);
|
let params_struct_name = format_ident!("{}Parameters", struct_name);
|
||||||
let static_name = format_ident!("{}", fn_name_str.to_uppercase());
|
let static_name = format_ident!("{}", fn_name_str.to_uppercase());
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,7 @@ async fn test_calculator_tool() {
|
||||||
// Test valid operations
|
// Test valid operations
|
||||||
let test_cases = vec![
|
let test_cases = vec![
|
||||||
(
|
(
|
||||||
serde_json::json!({
|
rig::serde_json::json!({
|
||||||
"x": 5,
|
"x": 5,
|
||||||
"y": 3,
|
"y": 3,
|
||||||
"operation": "add"
|
"operation": "add"
|
||||||
|
@ -82,7 +82,7 @@ async fn test_calculator_tool() {
|
||||||
8,
|
8,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
serde_json::json!({
|
rig::serde_json::json!({
|
||||||
"x": 5,
|
"x": 5,
|
||||||
"y": 3,
|
"y": 3,
|
||||||
"operation": "subtract"
|
"operation": "subtract"
|
||||||
|
@ -90,7 +90,7 @@ async fn test_calculator_tool() {
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
serde_json::json!({
|
rig::serde_json::json!({
|
||||||
"x": 5,
|
"x": 5,
|
||||||
"y": 3,
|
"y": 3,
|
||||||
"operation": "multiply"
|
"operation": "multiply"
|
||||||
|
@ -98,7 +98,7 @@ async fn test_calculator_tool() {
|
||||||
15,
|
15,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
serde_json::json!({
|
rig::serde_json::json!({
|
||||||
"x": 6,
|
"x": 6,
|
||||||
"y": 2,
|
"y": 2,
|
||||||
"operation": "divide"
|
"operation": "divide"
|
||||||
|
@ -109,11 +109,11 @@ async fn test_calculator_tool() {
|
||||||
|
|
||||||
for (input, expected) in test_cases {
|
for (input, expected) in test_cases {
|
||||||
let result = calculator.call(input).await.unwrap();
|
let result = calculator.call(input).await.unwrap();
|
||||||
assert_eq!(result, serde_json::json!(expected));
|
assert_eq!(result, rig::serde_json::json!(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test division by zero
|
// Test division by zero
|
||||||
let div_zero = serde_json::json!({
|
let div_zero = rig::serde_json::json!({
|
||||||
"x": 5,
|
"x": 5,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
"operation": "divide"
|
"operation": "divide"
|
||||||
|
@ -122,7 +122,7 @@ async fn test_calculator_tool() {
|
||||||
assert!(matches!(err, rig::tool::ToolError::ToolCallError(_)));
|
assert!(matches!(err, rig::tool::ToolError::ToolCallError(_)));
|
||||||
|
|
||||||
// Test invalid operation
|
// Test invalid operation
|
||||||
let invalid_op = serde_json::json!({
|
let invalid_op = rig::serde_json::json!({
|
||||||
"x": 5,
|
"x": 5,
|
||||||
"y": 3,
|
"y": 3,
|
||||||
"operation": "power"
|
"operation": "power"
|
||||||
|
@ -133,7 +133,7 @@ async fn test_calculator_tool() {
|
||||||
// Test sync calculator
|
// Test sync calculator
|
||||||
let sync_calculator = SyncCalculator::default();
|
let sync_calculator = SyncCalculator::default();
|
||||||
let result = sync_calculator
|
let result = sync_calculator
|
||||||
.call(serde_json::json!({
|
.call(rig::serde_json::json!({
|
||||||
"x": 5,
|
"x": 5,
|
||||||
"y": 3,
|
"y": 3,
|
||||||
"operation": "add"
|
"operation": "add"
|
||||||
|
@ -141,5 +141,5 @@ async fn test_calculator_tool() {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(result, serde_json::json!(8));
|
assert_eq!(result, rig::serde_json::json!(8));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue