Skip to content

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

Closed
@cptrodgers

Description

@cptrodgers

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions