From 8546c6883bc8d1d1dec307659d4755ce45b86f0f Mon Sep 17 00:00:00 2001 From: Robby Date: Tue, 25 Jun 2024 18:01:57 -0400 Subject: [PATCH 1/2] Add Function Calling Example --- .gitignore | 1 + examples/function-calling/Cargo.toml | 12 +++++ examples/function-calling/src/main.rs | 74 +++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 examples/function-calling/Cargo.toml create mode 100644 examples/function-calling/src/main.rs diff --git a/.gitignore b/.gitignore index 6985cf1..0f58f12 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb +.env diff --git a/examples/function-calling/Cargo.toml b/examples/function-calling/Cargo.toml new file mode 100644 index 0000000..a5ef21c --- /dev/null +++ b/examples/function-calling/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "function-calling" +version = "0.1.0" +edition = "2021" + +[dependencies] +instructor-ai = "0.1.0" +instruct-macros = "0.1.1" +openai-api-rs = "4.1.0" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +instruct-macros-types = "0.1.2" diff --git a/examples/function-calling/src/main.rs b/examples/function-calling/src/main.rs new file mode 100644 index 0000000..1d7b4ae --- /dev/null +++ b/examples/function-calling/src/main.rs @@ -0,0 +1,74 @@ +use core::fmt; +use std::env; + +use instruct_macros::InstructMacro; +use instruct_macros_types::{ParameterInfo, StructInfo}; +use instructor_ai::from_openai; +use openai_api_rs::v1::{ + api::Client, + chat_completion::{self, ChatCompletionRequest}, + common::GPT4_O, +}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +enum SearchType { + Web, + Image, + Video, +} + +impl fmt::Display for SearchType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(InstructMacro, Debug, Serialize, Deserialize)] +// This represents a single search +struct Search { + // Topic of the search + topic: String, + // Query to search for relevant content + query: String, + // Type of search + stype: SearchType, +} + +impl Search { + fn execute(&self) { + println!( + "Searching for '{}' with query '{}' using '{}'", + self.topic, self.query, self.stype + ) + } +} + +#[derive(InstructMacro, Debug, Serialize, Deserialize)] +// This represents a list of searches +struct Searches { + // List of searches + items: Vec, +} + +fn main() { + let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string()); + let instructor_client = from_openai(client); + + let req = ChatCompletionRequest::new( + GPT4_O.to_string(), + vec![chat_completion::ChatCompletionMessage { + role: chat_completion::MessageRole::user, + content: chat_completion::Content::Text(String::from( + "Search for a picture of a cat, a video of a dog, and the taxonomy of each", + )), + name: None, + }], + ); + + let searches = instructor_client + .chat_completion::(req, 3) + .unwrap(); + + searches.items.iter().for_each(|search| search.execute()); +} From 8e574ce9a72bc3a5ab4f08258625bf9d178a9d76 Mon Sep 17 00:00:00 2001 From: Robby Date: Mon, 15 Jul 2024 12:53:39 -0400 Subject: [PATCH 2/2] example updated and working --- Cargo.toml | 1 + examples/function-calling/Cargo.toml | 6 ++-- examples/function-calling/src/main.rs | 40 ++++++++++++--------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c069071..a8e852e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] members = ["instruct-macros-types", "instruct-macros", "instructor"] +exclude = ["examples"] resolver = "2" diff --git a/examples/function-calling/Cargo.toml b/examples/function-calling/Cargo.toml index a5ef21c..ecc49ab 100644 --- a/examples/function-calling/Cargo.toml +++ b/examples/function-calling/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] -instructor-ai = "0.1.0" -instruct-macros = "0.1.1" +instructor-ai = { path = "../../instructor" } +instruct-macros = { path = "../../instruct-macros" } +instruct-macros-types = { path = "../../instruct-macros-types" } openai-api-rs = "4.1.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -instruct-macros-types = "0.1.2" diff --git a/examples/function-calling/src/main.rs b/examples/function-calling/src/main.rs index 1d7b4ae..ac640bd 100644 --- a/examples/function-calling/src/main.rs +++ b/examples/function-calling/src/main.rs @@ -2,7 +2,7 @@ use core::fmt; use std::env; use instruct_macros::InstructMacro; -use instruct_macros_types::{ParameterInfo, StructInfo}; +use instruct_macros_types::{Parameter, ParameterInfo, StructInfo}; use instructor_ai::from_openai; use openai_api_rs::v1::{ api::Client, @@ -11,7 +11,7 @@ use openai_api_rs::v1::{ }; use serde::{Deserialize, Serialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(InstructMacro, Debug, Serialize, Deserialize)] enum SearchType { Web, Image, @@ -25,50 +25,46 @@ impl fmt::Display for SearchType { } #[derive(InstructMacro, Debug, Serialize, Deserialize)] -// This represents a single search struct Search { - // Topic of the search + #[description("Topic of the search")] topic: String, - // Query to search for relevant content + #[description("Query to search for relevant content")] query: String, - // Type of search + #[description("Type of search")] stype: SearchType, } impl Search { fn execute(&self) { println!( - "Searching for '{}' with query '{}' using '{}'", - self.topic, self.query, self.stype + "Executing a(n) {} Search for '{}' from query: '{}'", + self.stype, self.topic, self.query, ) } } -#[derive(InstructMacro, Debug, Serialize, Deserialize)] -// This represents a list of searches -struct Searches { - // List of searches - items: Vec, -} - fn main() { let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let instructor_client = from_openai(client); + let q = "Search for a picture of a cat"; + let req = ChatCompletionRequest::new( GPT4_O.to_string(), vec![chat_completion::ChatCompletionMessage { role: chat_completion::MessageRole::user, - content: chat_completion::Content::Text(String::from( - "Search for a picture of a cat, a video of a dog, and the taxonomy of each", - )), + content: chat_completion::Content::Text(String::from(format!( + "Consider the data below and segment it into a search quer:\n{}", + q + ))), name: None, }], ); - let searches = instructor_client - .chat_completion::(req, 3) - .unwrap(); + let search = instructor_client.chat_completion::(req, 3).unwrap(); - searches.items.iter().for_each(|search| search.execute()); + search.execute() + /* + Executing a(n) Image Search for 'cat' from query: 'picture of a cat' + */ }