Expand description
openai_client
provides configuration, models an a http client for working with
the API of OpenAi in Rust.
§Cargo
[dependencies]
openai_client = "0.1.0"
Or via git:
[dependencies.redis_ts]
git = "https://github.com/tompro/openai_client.git"
§Usage
use openai_client::*;
// Create client
let client = OpenAiClient::new(OpenAiConfig::new("<ACCESS_TOKEN>"));
// Create request
let request = EditRequestBuilder::default()
.model("text-davinci-edit-001")
.input("What day of the wek is it?")
.instruction("Fix the spelling mistakes")
.build()
.unwrap();
// Send request
let result = client.create_edit(request).await?;
§Supported operations
To create a client you can either provide your own configuration with the access token required by OpenAi or use the default which will in turn use the default configuration. The default configuration expects the Api token environment variable OPENAI_API_KEY to be populated with your credentials.
All currently supported operations have a builder for the request payload, can be configured via a config struct and return either a struct with the expected success response or an error of type OpenAiError.
§Client
// custom configuration
let client = OpenAiClient::new(OpenAiConfig::new("<ACCESS_TOKEN>"));
// default client access token from env
let client = OpenAiClient::default();
§Models
List and describe the various models available in the API.
async fn run() -> openai_client::OpenAiResult<()> {
// fetch all models provided by OpenAi
let models = client.get_models().await?;
// fetch a specific model
let model: OpenAiModel = client.get_model("text-davinci-003").await?;
§Edits
Given a prompt and an instruction, the model will return an edited version of the prompt.
async fn run() -> openai_client::OpenAiResult<()> {
let request = EditRequestBuilder::default()
.model("text-davinci-edit-001")
.input("What day of the wek is it?")
.instruction("Fix the spelling mistakes")
.build()
.unwrap();
let result: TextResult = client.create_edit(request).await?;
assert!(!result.choices.is_empty());
§Completions
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
async fn run() -> openai_client::OpenAiResult<()> {
let request = CompletionRequestBuilder::default()
.model("text-davinci-003")
.prompt("I am so tired I could")
.build()
.unwrap();
let result: TextResult = client.create_completion(request).await?;
assert!(!result.choices.is_empty());
§Generate Image
Creates an image given a prompt.
async fn run() -> openai_client::OpenAiResult<()> {
let request = CreateImageRequestBuilder::default()
.prompt("A cute baby sea otter")
.size("1024x1024")
.n(2)
.build()
.unwrap();
let result: ImageResult = client.create_image(request).await?;
assert_eq!(!result.data.len(), 2);
Structs§
- Completion
Request - Json data required for doing text completion requests.
- Completion
Request Builder - Builder for
CompletionRequest
. - Create
Image Request - Json data required for doing image generation requests.
- Create
Image Request Builder - Builder for
CreateImageRequest
. - Edit
Request - Json data required for doing text edit requests.
- Edit
Request Builder - Builder for
EditRequest
. - Image
Item - A single image item
- Image
Result - A result returned by image operations
- Open
AiClient - Open
AiConfig - Open
AiError Response - The payload of an OpenAi error response.
- Open
AiModel - Model properties response
- Open
AiModel Permission - Model permissions response.
- Open
AiModel Response - Models list response.
- Text
Choice - A choice result for text based operations
- Text
Result - Container for a text base result.
Enums§
- Open
AiError - Open
AiResponse - A wrapper around the OpenAi response payload.