dongri / openai-api-rs

OpenAI API client library for Rust (unofficial)
https://docs.rs/openai-api-rs
MIT License
290 stars 47 forks source link

JSON Mode (Run Response Format) #100

Closed lobirus closed 1 month ago

lobirus commented 1 month ago

Describe the feature or improvement you're requesting

https://platform.openai.com/docs/api-reference/runs/createRun#runs-createrun-response_format

Runs support response format, mostly to always response with a valid JSON.

Additional context

I'm not sure about the proper solution. It might be another HashMap property of CreateRunRequest but maybe you can figura a better solution. E.g. creating a specific struct, or type constants (or just a json_mode bool). They kept it as a simple object with a string value in the Node.js and Python implementations too. https://platform.openai.com/docs/guides/json-mode

Went through the code, a few branches, examples, haven't noticed its existence, sorry if it already exists.

dongri commented 1 month ago

@lobirus https://github.com/dongri/openai-api-rs/pull/101/files Added response_format parameter

let mut run_req = CreateRunRequest::new(result.id);
run_req = run_req.clone().response_format(json!({"type": "json_objects"}));

Please check if it works correctly.

lobirus commented 1 month ago

Hi @dongri, Good job, thanks. It works well for me. FYI The 5.0.4 crate is not available via cargo yet, using with GitHub repo reference for now.

If someone finds this one, the type is json_object. Singular, correct in the source code, typo above.

dongri commented 1 month ago

@lobirus sorry... I forgot to publish cargo. I published it now. https://crates.io/crates/openai-api-rs

lobirus commented 1 month ago

Thanks. Tested the 5.0.4 package too, works well.