valentinegb / openai

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

Handle API errors #20

Closed valentinegb closed 1 year ago

valentinegb commented 1 year ago

This pull request reroutes all previously programmed interactions with the API to a new openai_request() function which handles errors returned by the API. A program using the library after this PR is merged could handle errors like so:

let completion = Completion::new(&CreateCompletionRequestBody {
    model: ModelID::TextDavinci003,
    prompt: "Say this is a test",
    max_tokens: Some(7),
    temperature: Some(0.0),
    ..Default::default()
})
.await
.expect("request should complete successfully")
.expect("API should not respond with an error");