Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions async-openai/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ repository = "https://github.com/64bit/async-openai"

[dependencies]
backoff = {version = "0.4.0", features = ["tokio"] }
base64 = "0.20.0"
futures = "0.3.25"
base64 = "0.21.0"
futures = "0.3.26"
rand = "0.8.5"
reqwest = { version = "0.11.13", features = ["json", "stream", "multipart"] }
reqwest = { version = "0.11.14", features = ["json", "stream", "multipart"] }
reqwest-eventsource = "0.4.0"
serde = { version = "1.0.152", features = ["derive", "rc"] }
serde_json = "1.0.91"
serde_json = "1.0.93"
thiserror = "1.0.38"
tokio = { version = "1.23.0", features = ["fs", "macros"] }
tokio = { version = "1.25.0", features = ["fs", "macros"] }
tokio-stream = "0.1.11"
tokio-util = { version = "0.7.4", features = ["codec", "io-util"] }
tokio-util = { version = "0.7.7", features = ["codec", "io-util"] }
tracing = "0.1.37"
derive_builder = "0.12.0"

Expand Down
4 changes: 2 additions & 2 deletions async-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
- [ ] Microsoft Azure Endpoints / AD Authentication (see [issue](https://github.com/64bit/async-openai/issues/32))
- [x] Models
- [x] Moderations
- Non-streaming requests are retried with exponential backoff when [rate limited](https://help.openai.com/en/articles/5955598-is-api-usage-subject-to-any-rate-limits) by the API server.
- Non-streaming requests are retried with exponential backoff when [rate limited](https://platform.openai.com/docs/guides/rate-limits) by the API server.
- Ergonomic Rust library with builder pattern for all request objects.

*Being a young project there could be rough edges.*

## Usage

The library reads [API key](https://beta.openai.com/account/api-keys) from the environment variable `OPENAI_API_KEY`.
The library reads [API key](https://platform.openai.com/account/api-keys) from the environment variable `OPENAI_API_KEY`.

```bash
# On macOS/Linux
Expand Down
5 changes: 4 additions & 1 deletion async-openai/src/download.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};

use base64::{engine::general_purpose, Engine as _};
use rand::{distributions::Alphanumeric, Rng};
use reqwest::Url;

Expand Down Expand Up @@ -69,7 +70,9 @@ pub(crate) async fn save_b64<P: AsRef<Path>>(b64: &str, dir: P) -> Result<PathBu

tokio::fs::write(
path.as_path(),
base64::decode(b64).map_err(|e| OpenAIError::FileSaveError(e.to_string()))?,
general_purpose::STANDARD
.decode(b64)
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?,
)
.await
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?;
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/embedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
/// Get a vector representation of a given input that can be easily
/// consumed by machine learning models and algorithms.
///
/// Related guide: [Embeddings](https://beta.openai.com/docs/guides/embeddings/what-are-embeddings)
/// Related guide: [Embeddings](https://platform.openai.com/docs/guides/embeddings/what-are-embeddings)
pub struct Embeddings<'c> {
client: &'c Client,
}
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
Client,
};

/// Files are used to upload documents that can be used with features like [Fine-tuning](https://beta.openai.com/docs/api-reference/fine-tunes).
/// Files are used to upload documents that can be used with features like [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tunes).
pub struct Files<'c> {
client: &'c Client,
}
Expand Down
6 changes: 3 additions & 3 deletions async-openai/src/fine_tune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{

/// Manage fine-tuning jobs to tailor a model to your specific training data.
///
/// Related guide: [Fine-tune models](https://beta.openai.com/docs/guides/fine-tuning)
/// Related guide: [Fine-tune models](https://platform.openai.com/docs/guides/fine-tuning)
pub struct FineTunes<'c> {
client: &'c Client,
}
Expand All @@ -23,7 +23,7 @@ impl<'c> FineTunes<'c> {
///
/// Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
///
/// [Learn more about Fine-tuning](https://beta.openai.com/docs/guides/fine-tuning)
/// [Learn more about Fine-tuning](https://platform.openai.com/docs/guides/fine-tuning)
pub async fn create(&self, request: CreateFineTuneRequest) -> Result<FineTune, OpenAIError> {
self.client.post("/fine-tunes", request).await
}
Expand All @@ -35,7 +35,7 @@ impl<'c> FineTunes<'c> {

/// Gets info about the fine-tune job.
///
/// [Learn more about Fine-tuning](https://beta.openai.com/docs/guides/fine-tuning)
/// [Learn more about Fine-tuning](https://platform.openai.com/docs/guides/fine-tuning)
pub async fn retrieve(&self, fine_tune_id: &str) -> Result<FineTune, OpenAIError> {
self.client
.get(format!("/fine-tunes/{fine_tune_id}").as_str())
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{

/// Given a prompt and/or an input image, the model will generate a new image.
///
/// Related guide: [Image generation](https://beta.openai.com/docs/guides/images/introduction)
/// Related guide: [Image generation](https://platform.openai.com/docs/guides/images/introduction)
pub struct Images<'c> {
client: &'c Client,
}
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};

/// List and describe the various models available in the API.
/// You can refer to the [Models](https://beta.openai.com/docs/models) documentation to understand what
/// You can refer to the [Models](https://platform.openai.com/docs/models) documentation to understand what
/// models are available and the differences between them.
pub struct Models<'c> {
client: &'c Client,
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

/// Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
///
/// Related guide: [Moderations](https://beta.openai.com/docs/guides/moderation/overview)
/// Related guide: [Moderations](https://platform.openai.com/docs/guides/moderation/overview)
pub struct Moderations<'c> {
client: &'c Client,
}
Expand Down
38 changes: 19 additions & 19 deletions async-openai/src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum Stop {
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateCompletionRequest {
/// ID of the model to use. You can use the [List models](https://beta.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://beta.openai.com/docs/models/overview) for descriptions of them.
/// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them.
pub model: String,

/// The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
Expand Down Expand Up @@ -103,13 +103,13 @@ pub struct CreateCompletionRequest {

/// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
///
/// [See more information about frequency and presence penalties.](https://beta.openai.com/docs/api-reference/parameter-details)
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/api-reference/parameter-details)
#[serde(skip_serializing_if = "Option::is_none")]
pub presence_penalty: Option<f32>, // min: -2.0, max: 2.0, default 0

/// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
///
/// [See more information about frequency and presence penalties.](https://beta.openai.com/docs/api-reference/parameter-details)
/// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/api-reference/parameter-details)
#[serde(skip_serializing_if = "Option::is_none")]
pub frequency_penalty: Option<f32>, // min: -2.0, max: 2.0, default: 0

Expand All @@ -129,7 +129,7 @@ pub struct CreateCompletionRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub logit_bias: Option<HashMap<String, serde_json::Value>>, // default: null

/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://beta.openai.com/docs/usage-policies/end-user-ids).
/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
}
Expand Down Expand Up @@ -178,7 +178,7 @@ pub type CompletionResponseStream =
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateEditRequest {
/// ID of the model to use. You can use the [List models](https://beta.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://beta.openai.com/docs/models/overview) for descriptions of them.
/// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them.
pub model: String,

/// The input text to use as a starting point for the edit.
Expand Down Expand Up @@ -257,7 +257,7 @@ pub struct CreateImageRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub response_format: Option<ResponseFormat>,

/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://beta.openai.com/docs/usage-policies/end-user-ids).
/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
}
Expand Down Expand Up @@ -306,7 +306,7 @@ pub struct CreateImageEditRequest {
/// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
pub response_format: Option<ResponseFormat>,

/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://beta.openai.com/docs/usage-policies/end-user-ids).
/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
pub user: Option<String>,
}

Expand All @@ -329,7 +329,7 @@ pub struct CreateImageVariationRequest {
/// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
pub response_format: Option<ResponseFormat>,

/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://beta.openai.com/docs/usage-policies/end-user-ids).
/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
pub user: Option<String>,
}

Expand Down Expand Up @@ -424,12 +424,12 @@ pub struct FileInput {
pub struct CreateFileRequest {
/// Name of the [JSON Lines](https://jsonlines.readthedocs.io/en/latest/) file to be uploaded.
///
/// If the `purpose` is set to "fine-tune", each line is a JSON record with "prompt" and "completion" fields representing your [training examples](https://beta.openai.com/docs/guides/fine-tuning/prepare-training-data).
/// If the `purpose` is set to "fine-tune", each line is a JSON record with "prompt" and "completion" fields representing your [training examples](https://platform.openai.com/docs/guides/fine-tuning/prepare-training-data).
pub file: FileInput,

/// The intended purpose of the uploaded documents.
///
/// Use "fine-tune" for [Fine-tuning](https://beta.openai.com/docs/api-reference/fine-tunes). This allows us to validate the format of the uploaded file.
/// Use "fine-tune" for [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tunes). This allows us to validate the format of the uploaded file.
pub purpose: String,
}

Expand Down Expand Up @@ -467,33 +467,33 @@ pub struct OpenAIFile {
pub struct CreateFineTuneRequest {
/// The ID of an uploaded file that contains training data.
///
/// See [upload file](https://beta.openai.com/docs/api-reference/files/upload) for how to upload a file.
/// See [upload file](https://platform.openai.com/docs/api-reference/files/upload) for how to upload a file.
///
/// Your dataset must be formatted as a JSONL file, where each training
/// example is a JSON object with the keys "prompt" and "completion".
/// Additionally, you must upload your file with the purpose `fine-tune`.
///
/// See the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning/creating-training-data) for more details.
/// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning/creating-training-data) for more details.
pub training_file: String,

/// The ID of an uploaded file that contains validation data.
///
/// If you provide this file, the data is used to generate validation
/// metrics periodically during fine-tuning. These metrics can be viewed in
/// the [fine-tuning results file](https://beta.openai.com/docs/guides/fine-tuning/analyzing-your-fine-tuned-model).
/// the [fine-tuning results file](https://platform.openai.com/docs/guides/fine-tuning/analyzing-your-fine-tuned-model).
/// Your train and validation data should be mutually exclusive.
///
/// Your dataset must be formatted as a JSONL file, where each validation
/// example is a JSON object with the keys "prompt" and "completion".
/// Additionally, you must upload your file with the purpose `fine-tune`.
///
/// See the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning/creating-training-data) for more details.
/// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning/creating-training-data) for more details.
#[serde(skip_serializing_if = "Option::is_none")]
pub validation_file: Option<String>,

/// The name of the base model to fine-tune. You can select one of "ada",
/// "babbage", "curie", "davinci", or a fine-tuned model created after 2022-04-21.
/// To learn more about these models, see the [Models](https://beta.openai.com/docs/models) documentation.
/// To learn more about these models, see the [Models](https://platform.openai.com/docs/models) documentation.
#[serde(skip_serializing_if = "Option::is_none")]
pub model: Option<String>,

Expand Down Expand Up @@ -537,7 +537,7 @@ pub struct CreateFineTuneRequest {

/// If set, we calculate classification-specific metrics such as accuracy
/// and F-1 score using the validation set at the end of every epoch.
/// These metrics can be viewed in the [results file](https://beta.openai.com/docs/guides/fine-tuning/analyzing-your-fine-tuned-model).
/// These metrics can be viewed in the [results file](https://platform.openai.com/docs/guides/fine-tuning/analyzing-your-fine-tuned-model).
///
/// In order to compute classification metrics, you must provide a
/// `validation_file`. Additionally, you must
Expand Down Expand Up @@ -643,9 +643,9 @@ pub enum EmbeddingInput {
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateEmbeddingRequest {
/// ID of the model to use. You can use the
/// [List models](https://beta.openai.com/docs/api-reference/models/list)
/// [List models](https://platform.openai.com/docs/api-reference/models/list)
/// API to see all of your available models, or see our
/// [Model overview](https://beta.openai.com/docs/models/overview)
/// [Model overview](https://platform.openai.com/docs/models/overview)
/// for descriptions of them.
pub model: String,

Expand All @@ -656,7 +656,7 @@ pub struct CreateEmbeddingRequest {
pub input: EmbeddingInput,

/// A unique identifier representing your end-user, which will help OpenAI
/// to monitor and detect abuse. [Learn more](https://beta.openai.com/docs/usage-policies/end-user-ids).
/// to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
}
Expand Down