Skip to content
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

Add Function Calling Example #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
.env
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
members = ["instruct-macros-types", "instruct-macros", "instructor"]
exclude = ["examples"]

resolver = "2"
12 changes: 12 additions & 0 deletions examples/function-calling/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "function-calling"
version = "0.1.0"
edition = "2021"

[dependencies]
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"
70 changes: 70 additions & 0 deletions examples/function-calling/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use core::fmt;
use std::env;

use instruct_macros::InstructMacro;
use instruct_macros_types::{Parameter, 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(InstructMacro, 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)]
struct Search {
#[description("Topic of the search")]
topic: String,
#[description("Query to search for relevant content")]
query: String,
#[description("Type of search")]
stype: SearchType,
}

impl Search {
fn execute(&self) {
println!(
"Executing a(n) {} Search for '{}' from query: '{}'",
self.stype, self.topic, self.query,
)
}
}

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(format!(
"Consider the data below and segment it into a search quer:\n{}",
q
))),
name: None,
}],
);

let search = instructor_client.chat_completion::<Search>(req, 3).unwrap();

search.execute()
/*
Executing a(n) Image Search for 'cat' from query: 'picture of a cat'
*/
}