valentinegb / openai

An unofficial Rust library for the OpenAI API.
https://crates.io/crates/openai/
MIT License
61 stars 18 forks source link

Create builders for pub structs with lots of properties #16

Closed valentinegb closed 1 year ago

valentinegb commented 1 year ago

Problem

Currently, there's a little bit of boilerplate when creating, for example, a Completion. (This boilerplate being Default::default().) It doesn't look all that nice.

Solution

We can get rid of boilerplate by introducing builders. This would also allow for more flexible ways of creating completions and such, like setting echo later in code if a certain condition is met. With builders, creating a Completion would look a bit like this:

let completion = Completion::new(ModelID::TextDavinci003)
    .prompt(“Say this is a test”)
    .max_tokens(7)
    .temperature(0.0)
    .create()
    .await
    .unwrap()
    .unwrap();

Alternatives

No response

valentinegb commented 1 year ago

Postponed until #18 is done. Field requirements are already enforced by the API, so they shouldn't be enforced again in the library. Once #18 is done, builders can be more easily made with the derive_builder crate.

valentinegb commented 1 year ago

18 has been merged! Work on this is being restarted.