-
Notifications
You must be signed in to change notification settings - Fork 308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: handle multiple tool calls #224
Open
vacekj
wants to merge
1
commit into
0xPlaygrounds:main
Choose a base branch
from
vacekj:179-handle-multiple-tool-calls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
use anyhow::Result; | ||
use rig::{ | ||
completion::{Chat, Message, Prompt, ToolDefinition}, | ||
providers, | ||
tool::Tool, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
|
||
#[derive(Deserialize)] | ||
struct OperationArgs { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
#[error("Math error")] | ||
struct MathError; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
struct Adder; | ||
impl Tool for Adder { | ||
const NAME: &'static str = "add"; | ||
|
||
type Error = MathError; | ||
type Args = OperationArgs; | ||
type Output = i32; | ||
|
||
async fn definition(&self, _prompt: String) -> ToolDefinition { | ||
ToolDefinition { | ||
name: "add".to_string(), | ||
description: "Add x and y together".to_string(), | ||
parameters: json!({ | ||
"type": "object", | ||
"properties": { | ||
"x": { | ||
"type": "number", | ||
"description": "The first number to add" | ||
}, | ||
"y": { | ||
"type": "number", | ||
"description": "The second number to add" | ||
} | ||
} | ||
}), | ||
} | ||
} | ||
|
||
async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> { | ||
tracing::info!("Adding {} and {}", args.x, args.y); | ||
let result = args.x + args.y; | ||
Ok(result) | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
struct Subtract; | ||
impl Tool for Subtract { | ||
const NAME: &'static str = "subtract"; | ||
|
||
type Error = MathError; | ||
type Args = OperationArgs; | ||
type Output = i32; | ||
|
||
async fn definition(&self, _prompt: String) -> ToolDefinition { | ||
serde_json::from_value(json!({ | ||
"name": "subtract", | ||
"description": "Subtract y from x (i.e.: x - y)", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"x": { | ||
"type": "number", | ||
"description": "The number to substract from" | ||
}, | ||
"y": { | ||
"type": "number", | ||
"description": "The number to substract" | ||
} | ||
} | ||
} | ||
})) | ||
.expect("Tool Definition") | ||
} | ||
|
||
async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> { | ||
tracing::info!("Subtracting {} from {}", args.y, args.x); | ||
let result = args.x - args.y; | ||
Ok(result) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), anyhow::Error> { | ||
// Create local client | ||
let local = providers::openai::Client::from_url("", "http://192.168.0.10:11434/v1"); | ||
|
||
let span = info_span!("calculator_agent"); | ||
|
||
// Create agent with a single context prompt and two tools | ||
let calculator_agent = local | ||
.agent("c4ai-command-r7b-12-2024-abliterated") | ||
.preamble("You are a calculator here to help the user perform arithmetic operations. Use the tools provided to answer the user's question.") | ||
.tool(Adder) | ||
.tool(Subtract) | ||
.max_tokens(1024) | ||
.build(); | ||
|
||
// Initialize chat history | ||
let mut chat_history = Vec::new(); | ||
println!("Calculator Agent: Ready to help with calculations! (Type 'quit' to exit)"); | ||
|
||
loop { | ||
print!("\nYou: "); | ||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input)?; | ||
let input = input.trim(); | ||
|
||
if input.to_lowercase() == "quit" { | ||
break; | ||
} | ||
|
||
// Add user message to history | ||
chat_history.push(Message { | ||
role: "user".into(), | ||
content: input.into(), | ||
}); | ||
|
||
// Get response from agent | ||
let response = calculator_agent | ||
.chat(input, chat_history.clone()) | ||
.instrument(span.clone()) | ||
.await?; | ||
|
||
// Add assistant's response to history | ||
chat_history.push(Message { | ||
role: "assistant".into(), | ||
content: response.clone(), | ||
}); | ||
|
||
println!("Calculator Agent: {}", response); | ||
} | ||
|
||
println!("\nGoodbye!"); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not correct approach, from the model of OpenAI you can have just one message or 1 to many tool calls:

So I propose something like this: