-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a test and support for option type parsing in instructor.rs'
- Loading branch information
Showing
3 changed files
with
198 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
extern crate instruct_macros; | ||
extern crate instruct_macros_types; | ||
|
||
use instruct_macros::InstructMacro; | ||
use instruct_macros_types::{Parameter, ParameterInfo, StructInfo}; | ||
use instructor_ai::from_openai; | ||
use openai_api_rs::v1::api::Client; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::env; | ||
|
||
use openai_api_rs::v1::{ | ||
chat_completion::{self, ChatCompletionRequest}, | ||
common::GPT4_O, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_from_openai() { | ||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string()); | ||
let instructor_client = from_openai(client); | ||
|
||
#[derive(InstructMacro, Debug, Serialize, Deserialize)] | ||
struct UserInfo { | ||
name: String, | ||
age: u8, | ||
} | ||
|
||
#[derive(InstructMacro, Debug, Serialize, Deserialize)] | ||
struct MaybeUser { | ||
#[description("This is an optional user field. If the user is not present, the field will be null")] | ||
user: Option<UserInfo>, | ||
} | ||
|
||
let req = ChatCompletionRequest::new( | ||
GPT4_O.to_string(), | ||
vec![chat_completion::ChatCompletionMessage { | ||
role: chat_completion::MessageRole::user, | ||
content: chat_completion::Content::Text(String::from("It's a beautiful day out")), | ||
name: None, | ||
}], | ||
); | ||
|
||
let result = instructor_client | ||
.chat_completion::<MaybeUser>(req, 3) | ||
.unwrap(); | ||
|
||
println!("{:?}", result); | ||
// assert!(result.user.is_none()); | ||
} | ||
} |