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

Added support for Option types #18

Merged
merged 3 commits into from
Jul 8, 2024
Merged

Added support for Option types #18

merged 3 commits into from
Jul 8, 2024

Conversation

ivanleomk
Copy link
Collaborator

We can now support option types with this new PR

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();

        assert!(result.user.is_none());

Copy link

Deploying instructor-rs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 6f48976
Status: ✅  Deploy successful!
Preview URL: https://ec6699f1.instructor-rs.pages.dev
Branch Preview URL: https://support-option.instructor-rs.pages.dev

View logs

@ivanleomk ivanleomk merged commit ddc7a63 into main Jul 8, 2024
13 checks passed
@ivanleomk ivanleomk mentioned this pull request Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant