-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
featadd new field for Message struct to support tool chain calls, it …
…breaks current usage of Message api as you need a new field
- Loading branch information
1 parent
3353c21
commit 412c745
Showing
14 changed files
with
165 additions
and
85 deletions.
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,95 @@ | ||
use std::str; | ||
|
||
use anyhow::Result; | ||
use rig::{ | ||
completion::{Prompt, ToolDefinition}, | ||
providers, | ||
tool::Tool, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
|
||
#[derive(Deserialize)] | ||
struct QueryArgs { | ||
query: String, | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
#[error("Search error")] | ||
struct SearchError; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct SearchResults { | ||
results: Vec<SearchResult>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct SearchResult { | ||
title: String, | ||
url: String, | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
struct SearchApiTool; | ||
impl Tool for SearchApiTool { | ||
const NAME: &'static str = "search"; | ||
|
||
type Error = SearchError; | ||
type Args = QueryArgs; | ||
type Output = SearchResults; | ||
|
||
async fn definition(&self, _prompt: String) -> ToolDefinition { | ||
ToolDefinition { | ||
name: "search".to_string(), | ||
description: "Search on the internet | ||
." | ||
.to_string(), | ||
parameters: json!({ | ||
"type": "object", | ||
"properties": { | ||
"query": { | ||
"type": "string", | ||
"description": "The search query" | ||
}, | ||
} | ||
}), | ||
} | ||
} | ||
|
||
async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> { | ||
let result = SearchResults { | ||
results: vec![SearchResult { | ||
title: format!("Example Website with terms: {}", args.query), | ||
url: "https://example.com".to_string(), | ||
}], | ||
}; | ||
Ok(result) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), anyhow::Error> { | ||
// Load environment variables | ||
dotenvy::dotenv().ok(); | ||
|
||
// Create OpenAI client | ||
let openai_client = providers::openai::Client::from_env(); | ||
|
||
// Create agent with a single context prompt and two tools | ||
let calculator_agent = openai_client | ||
.agent(providers::openai::GPT_4O) | ||
.preamble("You are an assistant helping to find information on the internet. Use the tools provided to answer the user's question.") | ||
.max_tokens(1024) | ||
.tool(SearchApiTool) | ||
.build(); | ||
|
||
// Prompt the agent and print the response | ||
println!( | ||
"Search Agent: {}", | ||
calculator_agent | ||
.prompt("Search for 'example' and tell me the title and url of each result you find") | ||
.await? | ||
); | ||
|
||
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
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
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
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.