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

[Example] Use serde and schemars to write structure output #302

Closed
cptrodgers opened this issue Dec 20, 2024 · 3 comments
Closed

[Example] Use serde and schemars to write structure output #302

cptrodgers opened this issue Dec 20, 2024 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@cptrodgers
Copy link
Contributor

cptrodgers commented Dec 20, 2024

Hi @64bit , your lib is awesome. I'm using it in my side project and it works perfectly.

I found that the structure output can be improved by use serde and schemars to make code easy to maintain like my PR #301 . I hope the PR works for you and community.

Btw, I think we can write the utility function like client.complete_structure_output<T>(...) and then user can just insert their rust struct and no need to worry about manual writing schema.
Assumtption code:

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Step {
    pub output: String,
    pub explanation: String,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct MathReasoningResponse {
    pub final_answer: String,
    pub steps: Vec<Step>,
}

let request = CreateChatCompletionRequestArgs::default()
    .max_tokens(512u32)
    .model("gpt-4o-mini")
    .messages(messages)
    .complete_structure_output<MathReasoningResponse>()
    .build()?;

let client = Client::new();
let response = client.chat().create(request).await?;

Thanks

@cptrodgers
Copy link
Contributor Author

I would love to make another to add a PR to addcomplete_structure_output fn in the library if you think it's worth.

@64bit
Copy link
Owner

64bit commented Jan 11, 2025

Thanks for a great suggestion. I'd avoid that for now to reduce maintenance burden.

I'd happily accept the example that showcases this idea without changes to library as you did in #301 , however please create it as a new example instead of updating existing one.

@64bit 64bit added the documentation Improvements or additions to documentation label Jan 11, 2025
@cptrodgers
Copy link
Contributor Author

Got the idea, just updated #301 to another directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants