From 701af46aafc87e745d5cea4f9df8df8fc71ecf55 Mon Sep 17 00:00:00 2001 From: Feng Liang Date: Sat, 21 Oct 2023 15:39:22 +0800 Subject: [PATCH 01/10] remove Clone trait bound --- async-openai/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async-openai/src/config.rs b/async-openai/src/config.rs index e0034c95..d288e7c7 100644 --- a/async-openai/src/config.rs +++ b/async-openai/src/config.rs @@ -10,7 +10,7 @@ pub const OPENAI_ORGANIZATION_HEADER: &str = "OpenAI-Organization"; /// [crate::Client] relies on this for every API call on OpenAI /// or Azure OpenAI service -pub trait Config: Clone { +pub trait Config { fn headers(&self) -> HeaderMap; fn url(&self, path: &str) -> String; fn query(&self) -> Vec<(&str, &str)>; From 32bf00462fcb1fcec120193d80b77046a6fb50a3 Mon Sep 17 00:00:00 2001 From: Feng Liang Date: Sat, 21 Oct 2023 16:12:39 +0800 Subject: [PATCH 02/10] add 'static + Debug for Config 'static means that a type implementing `Config` cannot have references other than 'static ones --- async-openai/src/config.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/async-openai/src/config.rs b/async-openai/src/config.rs index d288e7c7..3b1efb5b 100644 --- a/async-openai/src/config.rs +++ b/async-openai/src/config.rs @@ -1,4 +1,5 @@ //! Client configurations: [OpenAIConfig] for OpenAI, [AzureConfig] for Azure OpenAI Service. +use std::fmt::Debug; use reqwest::header::{HeaderMap, AUTHORIZATION}; use secrecy::{ExposeSecret, Secret}; use serde::Deserialize; @@ -10,7 +11,7 @@ pub const OPENAI_ORGANIZATION_HEADER: &str = "OpenAI-Organization"; /// [crate::Client] relies on this for every API call on OpenAI /// or Azure OpenAI service -pub trait Config { +pub trait Config: 'static + Debug { fn headers(&self) -> HeaderMap; fn url(&self, path: &str) -> String; fn query(&self) -> Vec<(&str, &str)>; From 9496185fdc38d78d386bee4c99b44ba79b63f989 Mon Sep 17 00:00:00 2001 From: Feng Liang Date: Sat, 21 Oct 2023 16:15:48 +0800 Subject: [PATCH 03/10] remove generics on Client and use trait objects instead Use Rc not Box for clonability --- async-openai/src/audio.rs | 8 ++++---- async-openai/src/chat.rs | 8 ++++---- async-openai/src/client.rs | 37 +++++++++++++++++----------------- async-openai/src/completion.rs | 8 ++++---- async-openai/src/edit.rs | 8 ++++---- async-openai/src/embedding.rs | 8 ++++---- async-openai/src/file.rs | 8 ++++---- async-openai/src/fine_tune.rs | 8 ++++---- async-openai/src/image.rs | 8 ++++---- async-openai/src/model.rs | 8 ++++---- async-openai/src/moderation.rs | 8 ++++---- 11 files changed, 59 insertions(+), 58 deletions(-) diff --git a/async-openai/src/audio.rs b/async-openai/src/audio.rs index 137d5cf6..3d58412f 100644 --- a/async-openai/src/audio.rs +++ b/async-openai/src/audio.rs @@ -10,12 +10,12 @@ use crate::{ /// Turn audio into text /// Related guide: [Speech to text](https://platform.openai.com/docs/guides/speech-to-text) -pub struct Audio<'c, C: Config> { - client: &'c Client, +pub struct Audio<'c> { + client: &'c Client, } -impl<'c, C: Config> Audio<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Audio<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/chat.rs b/async-openai/src/chat.rs index 7efa83b3..d5711faf 100644 --- a/async-openai/src/chat.rs +++ b/async-openai/src/chat.rs @@ -8,12 +8,12 @@ use crate::{ }; /// Given a chat conversation, the model will return a chat completion response. -pub struct Chat<'c, C: Config> { - client: &'c Client, +pub struct Chat<'c> { + client: &'c Client, } -impl<'c, C: Config> Chat<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Chat<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/client.rs b/async-openai/src/client.rs index bfd51f4e..083926e0 100644 --- a/async-openai/src/client.rs +++ b/async-openai/src/client.rs @@ -1,4 +1,5 @@ use std::pin::Pin; +use std::rc::Rc; use futures::{stream::StreamExt, Stream}; use reqwest_eventsource::{Event, EventSource, RequestBuilderExt}; @@ -17,29 +18,29 @@ use crate::{ #[derive(Debug, Clone)] /// Client is a container for config, backoff and http_client /// used to make API calls. -pub struct Client { +pub struct Client { http_client: reqwest::Client, - config: C, + config: Rc, backoff: backoff::ExponentialBackoff, } -impl Client { +impl Client { /// Client with default [OpenAIConfig] pub fn new() -> Self { Self { http_client: reqwest::Client::new(), - config: OpenAIConfig::default(), + config: Rc::new(OpenAIConfig::default()), backoff: Default::default(), } } } -impl Client { +impl Client { /// Create client with [OpenAIConfig] or [crate::config::AzureConfig] - pub fn with_config(config: C) -> Self { + pub fn with_config(config: C) -> Self { Self { http_client: reqwest::Client::new(), - config, + config: Rc::new(config), backoff: Default::default(), } } @@ -61,56 +62,56 @@ impl Client { // API groups /// To call [Models] group related APIs using this client. - pub fn models(&self) -> Models { + pub fn models(&self) -> Models { Models::new(self) } /// To call [Completions] group related APIs using this client. - pub fn completions(&self) -> Completions { + pub fn completions(&self) -> Completions { Completions::new(self) } /// To call [Chat] group related APIs using this client. - pub fn chat(&self) -> Chat { + pub fn chat(&self) -> Chat { Chat::new(self) } /// To call [Edits] group related APIs using this client. - pub fn edits(&self) -> Edits { + pub fn edits(&self) -> Edits { Edits::new(self) } /// To call [Images] group related APIs using this client. - pub fn images(&self) -> Images { + pub fn images(&self) -> Images { Images::new(self) } /// To call [Moderations] group related APIs using this client. - pub fn moderations(&self) -> Moderations { + pub fn moderations(&self) -> Moderations { Moderations::new(self) } /// To call [Files] group related APIs using this client. - pub fn files(&self) -> Files { + pub fn files(&self) -> Files { Files::new(self) } /// To call [FineTunes] group related APIs using this client. - pub fn fine_tunes(&self) -> FineTunes { + pub fn fine_tunes(&self) -> FineTunes { FineTunes::new(self) } /// To call [Embeddings] group related APIs using this client. - pub fn embeddings(&self) -> Embeddings { + pub fn embeddings(&self) -> Embeddings { Embeddings::new(self) } /// To call [Audio] group related APIs using this client. - pub fn audio(&self) -> Audio { + pub fn audio(&self) -> Audio { Audio::new(self) } - pub fn config(&self) -> &C { + pub fn config(&self) -> &Rc { &self.config } diff --git a/async-openai/src/completion.rs b/async-openai/src/completion.rs index d0775292..fbe9a92b 100644 --- a/async-openai/src/completion.rs +++ b/async-openai/src/completion.rs @@ -8,12 +8,12 @@ use crate::{ /// Given a prompt, the model will return one or more predicted /// completions, and can also return the probabilities of alternative /// tokens at each position. -pub struct Completions<'c, C: Config> { - client: &'c Client, +pub struct Completions<'c> { + client: &'c Client, } -impl<'c, C: Config> Completions<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Completions<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/edit.rs b/async-openai/src/edit.rs index daca6b73..c3d8d377 100644 --- a/async-openai/src/edit.rs +++ b/async-openai/src/edit.rs @@ -7,12 +7,12 @@ use crate::{ /// Given a prompt and an instruction, the model will return /// an edited version of the prompt. -pub struct Edits<'c, C: Config> { - client: &'c Client, +pub struct Edits<'c> { + client: &'c Client, } -impl<'c, C: Config> Edits<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Edits<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/embedding.rs b/async-openai/src/embedding.rs index b6a9ab86..b42a6659 100644 --- a/async-openai/src/embedding.rs +++ b/async-openai/src/embedding.rs @@ -9,12 +9,12 @@ use crate::{ /// consumed by machine learning models and algorithms. /// /// Related guide: [Embeddings](https://platform.openai.com/docs/guides/embeddings/what-are-embeddings) -pub struct Embeddings<'c, C: Config> { - client: &'c Client, +pub struct Embeddings<'c> { + client: &'c Client, } -impl<'c, C: Config> Embeddings<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Embeddings<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/file.rs b/async-openai/src/file.rs index 57937f93..69bbee8d 100644 --- a/async-openai/src/file.rs +++ b/async-openai/src/file.rs @@ -6,12 +6,12 @@ use crate::{ }; /// 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, C: Config> { - client: &'c Client, +pub struct Files<'c> { + client: &'c Client, } -impl<'c, C: Config> Files<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Files<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/fine_tune.rs b/async-openai/src/fine_tune.rs index 04ce042b..bbc1d55a 100644 --- a/async-openai/src/fine_tune.rs +++ b/async-openai/src/fine_tune.rs @@ -11,12 +11,12 @@ use crate::{ /// Manage fine-tuning jobs to tailor a model to your specific training data. /// /// Related guide: [Fine-tune models](https://platform.openai.com/docs/guides/fine-tuning) -pub struct FineTunes<'c, C: Config> { - client: &'c Client, +pub struct FineTunes<'c> { + client: &'c Client, } -impl<'c, C: Config> FineTunes<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> FineTunes<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/image.rs b/async-openai/src/image.rs index 43d37202..dcce389d 100644 --- a/async-openai/src/image.rs +++ b/async-openai/src/image.rs @@ -10,12 +10,12 @@ use crate::{ /// Given a prompt and/or an input image, the model will generate a new image. /// /// Related guide: [Image generation](https://platform.openai.com/docs/guides/images/introduction) -pub struct Images<'c, C: Config> { - client: &'c Client, +pub struct Images<'c> { + client: &'c Client, } -impl<'c, C: Config> Images<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Images<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/model.rs b/async-openai/src/model.rs index df1a443f..683e8851 100644 --- a/async-openai/src/model.rs +++ b/async-openai/src/model.rs @@ -8,12 +8,12 @@ use crate::{ /// List and describe the various models available in the API. /// 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, C: Config> { - client: &'c Client, +pub struct Models<'c> { + client: &'c Client, } -impl<'c, C: Config> Models<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Models<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/moderation.rs b/async-openai/src/moderation.rs index b1cae0df..b0718c44 100644 --- a/async-openai/src/moderation.rs +++ b/async-openai/src/moderation.rs @@ -8,12 +8,12 @@ use crate::{ /// Given a input text, outputs if the model classifies it as violating OpenAI's content policy. /// /// Related guide: [Moderations](https://platform.openai.com/docs/guides/moderation/overview) -pub struct Moderations<'c, C: Config> { - client: &'c Client, +pub struct Moderations<'c> { + client: &'c Client, } -impl<'c, C: Config> Moderations<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Moderations<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } From 6fe3494522ffedda48dd465da9699bdd207fcf0e Mon Sep 17 00:00:00 2001 From: Feng Liang Date: Sat, 21 Oct 2023 16:17:39 +0800 Subject: [PATCH 04/10] delete unused imports --- async-openai/src/audio.rs | 1 - async-openai/src/chat.rs | 1 - async-openai/src/client.rs | 2 +- async-openai/src/completion.rs | 1 - async-openai/src/edit.rs | 1 - async-openai/src/embedding.rs | 1 - async-openai/src/file.rs | 1 - async-openai/src/fine_tune.rs | 1 - async-openai/src/image.rs | 1 - async-openai/src/model.rs | 1 - async-openai/src/moderation.rs | 1 - 11 files changed, 1 insertion(+), 11 deletions(-) diff --git a/async-openai/src/audio.rs b/async-openai/src/audio.rs index 3d58412f..fc6d45f1 100644 --- a/async-openai/src/audio.rs +++ b/async-openai/src/audio.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{ CreateTranscriptionRequest, CreateTranscriptionResponse, CreateTranslationRequest, diff --git a/async-openai/src/chat.rs b/async-openai/src/chat.rs index d5711faf..4f7b5f5e 100644 --- a/async-openai/src/chat.rs +++ b/async-openai/src/chat.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{ ChatCompletionResponseStream, CreateChatCompletionRequest, CreateChatCompletionResponse, diff --git a/async-openai/src/client.rs b/async-openai/src/client.rs index 083926e0..86393d1b 100644 --- a/async-openai/src/client.rs +++ b/async-openai/src/client.rs @@ -245,7 +245,7 @@ impl Client { .map_err(backoff::Error::Permanent)?; Ok(response) }) - .await + .await } /// Make HTTP POST request to receive SSE diff --git a/async-openai/src/completion.rs b/async-openai/src/completion.rs index fbe9a92b..05b2223a 100644 --- a/async-openai/src/completion.rs +++ b/async-openai/src/completion.rs @@ -1,6 +1,5 @@ use crate::{ client::Client, - config::Config, error::OpenAIError, types::{CompletionResponseStream, CreateCompletionRequest, CreateCompletionResponse}, }; diff --git a/async-openai/src/edit.rs b/async-openai/src/edit.rs index c3d8d377..78e2349f 100644 --- a/async-openai/src/edit.rs +++ b/async-openai/src/edit.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{CreateEditRequest, CreateEditResponse}, Client, diff --git a/async-openai/src/embedding.rs b/async-openai/src/embedding.rs index b42a6659..3ef3b805 100644 --- a/async-openai/src/embedding.rs +++ b/async-openai/src/embedding.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{CreateEmbeddingRequest, CreateEmbeddingResponse}, Client, diff --git a/async-openai/src/file.rs b/async-openai/src/file.rs index 69bbee8d..26939684 100644 --- a/async-openai/src/file.rs +++ b/async-openai/src/file.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{CreateFileRequest, DeleteFileResponse, ListFilesResponse, OpenAIFile}, Client, diff --git a/async-openai/src/fine_tune.rs b/async-openai/src/fine_tune.rs index bbc1d55a..a4a771b9 100644 --- a/async-openai/src/fine_tune.rs +++ b/async-openai/src/fine_tune.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{ CreateFineTuneRequest, FineTune, FineTuneEventsResponseStream, ListFineTuneEventsResponse, diff --git a/async-openai/src/image.rs b/async-openai/src/image.rs index dcce389d..2826e73d 100644 --- a/async-openai/src/image.rs +++ b/async-openai/src/image.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{ CreateImageEditRequest, CreateImageRequest, CreateImageVariationRequest, ImageResponse, diff --git a/async-openai/src/model.rs b/async-openai/src/model.rs index 683e8851..f040b30f 100644 --- a/async-openai/src/model.rs +++ b/async-openai/src/model.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{DeleteModelResponse, ListModelResponse, Model}, Client, diff --git a/async-openai/src/moderation.rs b/async-openai/src/moderation.rs index b0718c44..039ce882 100644 --- a/async-openai/src/moderation.rs +++ b/async-openai/src/moderation.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{CreateModerationRequest, CreateModerationResponse}, Client, From faecf42aeef192b9ff4ce651c0e04aaae59103f7 Mon Sep 17 00:00:00 2001 From: Feng Liang Date: Sat, 21 Oct 2023 16:20:24 +0800 Subject: [PATCH 05/10] delete generics in examples to make them compile --- examples/azure-openai-service/src/main.rs | 4 ++-- examples/fine-tune-cli/src/main.rs | 6 +++--- examples/function-call-stream/src/main.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/azure-openai-service/src/main.rs b/examples/azure-openai-service/src/main.rs index fc510c93..83dbbe20 100644 --- a/examples/azure-openai-service/src/main.rs +++ b/examples/azure-openai-service/src/main.rs @@ -9,7 +9,7 @@ use async_openai::{ Client, }; -async fn chat_completion_example(client: &Client) -> Result<(), Box> { +async fn chat_completion_example(client: &Client) -> Result<(), Box> { let request = CreateChatCompletionRequestArgs::default() .max_tokens(512u16) .model("gpt-3.5-turbo") @@ -60,7 +60,7 @@ async fn chat_completion_example(client: &Client) -> Result<(), Box // Ok(()) // } -async fn embedding_example(client: &Client) -> Result<(), Box> { +async fn embedding_example(client: &Client) -> Result<(), Box> { let request = CreateEmbeddingRequestArgs::default() .model("text-embedding-ada-002") .input("Why do programmers hate nature? It has too many bugs.") diff --git a/examples/fine-tune-cli/src/main.rs b/examples/fine-tune-cli/src/main.rs index cbea1194..5435f035 100644 --- a/examples/fine-tune-cli/src/main.rs +++ b/examples/fine-tune-cli/src/main.rs @@ -8,7 +8,7 @@ use async_openai::{ use clap::{arg, Command}; // TODO: Constructive error handling -async fn data(paths: Vec<&PathBuf>, client: Client) { +async fn data(paths: Vec<&PathBuf>, client: Client) { if paths.len() > 2 { println!("pls provide the trainning file path and optionally a validation file path") } else { @@ -59,7 +59,7 @@ async fn data(paths: Vec<&PathBuf>, client: Client) { } } -async fn retrieve(job_id: String, client: Client) { +async fn retrieve(job_id: String, client: Client) { let ss = client.fine_tunes().retrieve(&job_id).await.unwrap(); if let Some(ft_model) = ss.fine_tuned_model { @@ -69,7 +69,7 @@ async fn retrieve(job_id: String, client: Client) { } } -async fn completion(model: String, prompt: String, client: Client) { +async fn completion(model: String, prompt: String, client: Client) { let request = CreateCompletionRequestArgs::default() .model(model) .prompt(prompt) diff --git a/examples/function-call-stream/src/main.rs b/examples/function-call-stream/src/main.rs index d3f407c2..f6ec72e3 100644 --- a/examples/function-call-stream/src/main.rs +++ b/examples/function-call-stream/src/main.rs @@ -82,7 +82,7 @@ async fn main() -> Result<(), Box> { } async fn call_fn( - client: &Client, + client: &Client, name: &str, args: &str, ) -> Result<(), Box> { From de1297e51cf61df2e52bf3a3e92baa50556919fb Mon Sep 17 00:00:00 2001 From: Feng Liang Date: Thu, 9 Nov 2023 01:36:49 +0800 Subject: [PATCH 06/10] remove generics in newer apis --- async-openai/src/assistant_files.rs | 9 ++++----- async-openai/src/assistants.rs | 11 +++++------ async-openai/src/fine_tuning.rs | 9 ++++----- async-openai/src/image.rs | 2 +- async-openai/src/message_files.rs | 9 ++++----- async-openai/src/messages.rs | 11 +++++------ async-openai/src/runs.rs | 11 +++++------ async-openai/src/steps.rs | 9 ++++----- async-openai/src/threads.rs | 13 ++++++------- 9 files changed, 38 insertions(+), 46 deletions(-) diff --git a/async-openai/src/assistant_files.rs b/async-openai/src/assistant_files.rs index 36a2f909..fbcb35ce 100644 --- a/async-openai/src/assistant_files.rs +++ b/async-openai/src/assistant_files.rs @@ -1,7 +1,6 @@ use serde::Serialize; use crate::{ - config::Config, error::OpenAIError, types::{ AssistantFileObject, CreateAssistantFileRequest, DeleteAssistantFileResponse, @@ -11,13 +10,13 @@ use crate::{ }; /// Files attached to an assistant. -pub struct AssistantFiles<'c, C: Config> { - client: &'c Client, +pub struct AssistantFiles<'c> { + client: &'c Client, pub assistant_id: String, } -impl<'c, C: Config> AssistantFiles<'c, C> { - pub fn new(client: &'c Client, assistant_id: &str) -> Self { +impl<'c> AssistantFiles<'c> { + pub fn new(client: &'c Client, assistant_id: &str) -> Self { Self { client, assistant_id: assistant_id.into(), diff --git a/async-openai/src/assistants.rs b/async-openai/src/assistants.rs index 77882fc3..efd71713 100644 --- a/async-openai/src/assistants.rs +++ b/async-openai/src/assistants.rs @@ -1,7 +1,6 @@ use serde::Serialize; use crate::{ - config::Config, error::OpenAIError, types::{ AssistantObject, CreateAssistantRequest, DeleteAssistantResponse, ListAssistantsResponse, @@ -13,17 +12,17 @@ use crate::{ /// Build assistants that can call models and use tools to perform tasks. /// /// [Get started with the Assistants API](https://platform.openai.com/docs/assistants) -pub struct Assistants<'c, C: Config> { - client: &'c Client, +pub struct Assistants<'c> { + client: &'c Client, } -impl<'c, C: Config> Assistants<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Assistants<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } /// Assistant [AssistantFiles] API group - pub fn files(&self, assistant_id: &str) -> AssistantFiles { + pub fn files(&self, assistant_id: &str) -> AssistantFiles { AssistantFiles::new(self.client, assistant_id) } diff --git a/async-openai/src/fine_tuning.rs b/async-openai/src/fine_tuning.rs index 08fd3b74..75c44ab1 100644 --- a/async-openai/src/fine_tuning.rs +++ b/async-openai/src/fine_tuning.rs @@ -1,7 +1,6 @@ use serde::Serialize; use crate::{ - config::Config, error::OpenAIError, types::{ CreateFineTuningJobRequest, FineTuningJob, ListFineTuningJobEventsResponse, @@ -13,12 +12,12 @@ use crate::{ /// Manage fine-tuning jobs to tailor a model to your specific training data. /// /// Related guide: [Fine-tune models](https://platform.openai.com/docs/guides/fine-tuning) -pub struct FineTuning<'c, C: Config> { - client: &'c Client, +pub struct FineTuning<'c> { + client: &'c Client, } -impl<'c, C: Config> FineTuning<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> FineTuning<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } diff --git a/async-openai/src/image.rs b/async-openai/src/image.rs index a925c105..94de4936 100644 --- a/async-openai/src/image.rs +++ b/async-openai/src/image.rs @@ -19,7 +19,7 @@ impl<'c> Images<'c> { } /// Creates an image given a prompt. - pub async fn create(&self, request: CreateImageRequest) -> Result { + pub async fn create(&self, request: CreateImageRequest) -> Result { self.client.post("/images/generations", request).await } diff --git a/async-openai/src/message_files.rs b/async-openai/src/message_files.rs index c76c4b30..02658214 100644 --- a/async-openai/src/message_files.rs +++ b/async-openai/src/message_files.rs @@ -1,21 +1,20 @@ use serde::Serialize; use crate::{ - config::Config, error::OpenAIError, types::{ListMessageFilesResponse, MessageFileObject}, Client, }; /// Files attached to a message. -pub struct MessageFiles<'c, C: Config> { - client: &'c Client, +pub struct MessageFiles<'c> { + client: &'c Client, pub thread_id: String, pub message_id: String, } -impl<'c, C: Config> MessageFiles<'c, C> { - pub fn new(client: &'c Client, thread_id: &str, message_id: &str) -> Self { +impl<'c> MessageFiles<'c> { + pub fn new(client: &'c Client, thread_id: &str, message_id: &str) -> Self { Self { client, thread_id: thread_id.into(), diff --git a/async-openai/src/messages.rs b/async-openai/src/messages.rs index 1e6bdf41..7ba1876a 100644 --- a/async-openai/src/messages.rs +++ b/async-openai/src/messages.rs @@ -1,21 +1,20 @@ use serde::Serialize; use crate::{ - config::Config, error::OpenAIError, types::{CreateMessageRequest, ListMessagesResponse, MessageObject, ModifyMessageRequest}, Client, MessageFiles, }; /// Represents a message within a [thread](https://platform.openai.com/docs/api-reference/threads). -pub struct Messages<'c, C: Config> { +pub struct Messages<'c> { /// The ID of the [thread](https://platform.openai.com/docs/api-reference/threads) to create a message for. pub thread_id: String, - client: &'c Client, + client: &'c Client, } -impl<'c, C: Config> Messages<'c, C> { - pub fn new(client: &'c Client, thread_id: &str) -> Self { +impl<'c> Messages<'c> { + pub fn new(client: &'c Client, thread_id: &str) -> Self { Self { client, thread_id: thread_id.into(), @@ -23,7 +22,7 @@ impl<'c, C: Config> Messages<'c, C> { } /// Call [MessageFiles] API group - pub fn files(&self, message_id: &str) -> MessageFiles { + pub fn files(&self, message_id: &str) -> MessageFiles { MessageFiles::new(self.client, &self.thread_id, message_id) } diff --git a/async-openai/src/runs.rs b/async-openai/src/runs.rs index 6f2c15a5..b1722121 100644 --- a/async-openai/src/runs.rs +++ b/async-openai/src/runs.rs @@ -1,7 +1,6 @@ use serde::Serialize; use crate::{ - config::Config, error::OpenAIError, steps::Steps, types::{ @@ -14,13 +13,13 @@ use crate::{ /// Represents an execution run on a thread. /// /// Related guide: [Assistants](https://platform.openai.com/docs/assistants/overview) -pub struct Runs<'c, C: Config> { +pub struct Runs<'c> { pub thread_id: String, - client: &'c Client, + client: &'c Client, } -impl<'c, C: Config> Runs<'c, C> { - pub fn new(client: &'c Client, thread_id: &str) -> Self { +impl<'c> Runs<'c> { + pub fn new(client: &'c Client, thread_id: &str) -> Self { Self { client, thread_id: thread_id.into(), @@ -28,7 +27,7 @@ impl<'c, C: Config> Runs<'c, C> { } /// [Steps] API group - pub fn steps(&self, run_id: &str) -> Steps { + pub fn steps(&self, run_id: &str) -> Steps { Steps::new(self.client, &self.thread_id, run_id) } diff --git a/async-openai/src/steps.rs b/async-openai/src/steps.rs index 595288db..3f98d578 100644 --- a/async-openai/src/steps.rs +++ b/async-openai/src/steps.rs @@ -1,21 +1,20 @@ use serde::Serialize; use crate::{ - config::Config, error::OpenAIError, types::{ListRunStepsResponse, RunStepObject}, Client, }; /// Represents a step in execution of a run. -pub struct Steps<'c, C: Config> { +pub struct Steps<'c> { pub thread_id: String, pub run_id: String, - client: &'c Client, + client: &'c Client, } -impl<'c, C: Config> Steps<'c, C> { - pub fn new(client: &'c Client, thread_id: &str, run_id: &str) -> Self { +impl<'c> Steps<'c> { + pub fn new(client: &'c Client, thread_id: &str, run_id: &str) -> Self { Self { client, thread_id: thread_id.into(), diff --git a/async-openai/src/threads.rs b/async-openai/src/threads.rs index 6025a40f..e564adce 100644 --- a/async-openai/src/threads.rs +++ b/async-openai/src/threads.rs @@ -1,5 +1,4 @@ use crate::{ - config::Config, error::OpenAIError, types::{ CreateThreadAndRunRequest, CreateThreadRequest, DeleteThreadResponse, ModifyThreadRequest, @@ -11,22 +10,22 @@ use crate::{ /// Create threads that assistants can interact with. /// /// Related guide: [Assistants](https://platform.openai.com/docs/assistants/overview) -pub struct Threads<'c, C: Config> { - client: &'c Client, +pub struct Threads<'c> { + client: &'c Client, } -impl<'c, C: Config> Threads<'c, C> { - pub fn new(client: &'c Client) -> Self { +impl<'c> Threads<'c> { + pub fn new(client: &'c Client) -> Self { Self { client } } /// Call [Messages] group API to manage message in [thread_id] thread. - pub fn messages(&self, thread_id: &str) -> Messages { + pub fn messages(&self, thread_id: &str) -> Messages { Messages::new(self.client, thread_id) } /// Call [Runs] group API to manage runs in [thread_id] thread. - pub fn runs(&self, thread_id: &str) -> Runs { + pub fn runs(&self, thread_id: &str) -> Runs { Runs::new(self.client, thread_id) } From 7ee0e38366252b94f029b70ef534411239bfe571 Mon Sep 17 00:00:00 2001 From: "maple@max" Date: Thu, 18 Jan 2024 11:27:15 +0800 Subject: [PATCH 07/10] add trait bound and use Arc to fix failed test compilation due to tokio::spawn in tests that requires `Send` --- async-openai/src/client.rs | 10 +++++----- async-openai/src/config.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/async-openai/src/client.rs b/async-openai/src/client.rs index d2e76ed1..45a99fa0 100644 --- a/async-openai/src/client.rs +++ b/async-openai/src/client.rs @@ -1,5 +1,5 @@ use std::pin::Pin; -use std::rc::Rc; +use std::sync::Arc; use bytes::Bytes; use futures::{stream::StreamExt, Stream}; @@ -21,7 +21,7 @@ use crate::{ /// used to make API calls. pub struct Client { http_client: reqwest::Client, - config: Rc, + config: Arc, backoff: backoff::ExponentialBackoff, } @@ -30,7 +30,7 @@ impl Client { pub fn new() -> Self { Self { http_client: reqwest::Client::new(), - config: Rc::new(OpenAIConfig::default()), + config: Arc::new(OpenAIConfig::default()), backoff: Default::default(), } } @@ -41,7 +41,7 @@ impl Client { pub fn with_config(config: C) -> Self { Self { http_client: reqwest::Client::new(), - config: Rc::new(config), + config: Arc::new(config), backoff: Default::default(), } } @@ -129,7 +129,7 @@ impl Client { Threads::new(self) } - pub fn config(&self) -> &Rc { + pub fn config(&self) -> &Arc { &self.config } diff --git a/async-openai/src/config.rs b/async-openai/src/config.rs index 3c90084a..f2aa48f3 100644 --- a/async-openai/src/config.rs +++ b/async-openai/src/config.rs @@ -14,7 +14,7 @@ pub const OPENAI_BETA_HEADER: &str = "OpenAI-Beta"; /// [crate::Client] relies on this for every API call on OpenAI /// or Azure OpenAI service -pub trait Config: 'static + Debug { +pub trait Config: 'static + Debug + Send + Sync { fn headers(&self) -> HeaderMap; fn url(&self, path: &str) -> String; fn query(&self) -> Vec<(&str, &str)>; From 6ee3ac78079d6699cc4daf0682a44df1f56d2dfa Mon Sep 17 00:00:00 2001 From: "maple@max" Date: Thu, 18 Jan 2024 11:12:00 +0800 Subject: [PATCH 08/10] bump version --- async-openai/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async-openai/Cargo.toml b/async-openai/Cargo.toml index ef63f8a7..17c5549a 100644 --- a/async-openai/Cargo.toml +++ b/async-openai/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "async-openai" -version = "0.18.1" +version = "0.19.0" authors = [ "Himanshu Neema" ] From 639eaa4fe58f2557b2c13f6c15f45a6d042d5643 Mon Sep 17 00:00:00 2001 From: "maple@max" Date: Mon, 22 Jan 2024 12:19:34 +0800 Subject: [PATCH 09/10] add BaseConfig and new Config Enabling extra configs that can be opted in, which may be needed for LLM services other than OpenAI --- async-openai/src/config.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/async-openai/src/config.rs b/async-openai/src/config.rs index f2aa48f3..fdb5c481 100644 --- a/async-openai/src/config.rs +++ b/async-openai/src/config.rs @@ -12,9 +12,7 @@ pub const OPENAI_ORGANIZATION_HEADER: &str = "OpenAI-Organization"; /// Calls to the Assistants API require that you pass a Beta header pub const OPENAI_BETA_HEADER: &str = "OpenAI-Beta"; -/// [crate::Client] relies on this for every API call on OpenAI -/// or Azure OpenAI service -pub trait Config: 'static + Debug + Send + Sync { +pub trait BaseConfig: 'static + Debug + Send + Sync { fn headers(&self) -> HeaderMap; fn url(&self, path: &str) -> String; fn query(&self) -> Vec<(&str, &str)>; @@ -24,6 +22,18 @@ pub trait Config: 'static + Debug + Send + Sync { fn api_key(&self) -> &Secret; } +/// [crate::Client] relies on this for every API call on OpenAI +/// or Azure OpenAI service +pub trait Config: BaseConfig { + fn extra_configs(&self) -> &Extra; +} + +impl Config<()> for T { + fn extra_configs(&self) -> &() { + &() + } +} + /// Configuration for OpenAI API #[derive(Clone, Debug, Deserialize)] #[serde(default)] @@ -74,7 +84,7 @@ impl OpenAIConfig { } } -impl Config for OpenAIConfig { +impl BaseConfig for OpenAIConfig { fn headers(&self) -> HeaderMap { let mut headers = HeaderMap::new(); if !self.org_id.is_empty() { @@ -167,7 +177,7 @@ impl AzureConfig { } } -impl Config for AzureConfig { +impl BaseConfig for AzureConfig { fn headers(&self) -> HeaderMap { let mut headers = HeaderMap::new(); From 5ce558659a9bf7c488cdc7a724cce588e5824587 Mon Sep 17 00:00:00 2001 From: "maple@max" Date: Tue, 23 Jan 2024 16:43:39 +0800 Subject: [PATCH 10/10] Revert "add BaseConfig and new Config" This reverts commit 639eaa4fe58f2557b2c13f6c15f45a6d042d5643. --- async-openai/src/config.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/async-openai/src/config.rs b/async-openai/src/config.rs index fdb5c481..f2aa48f3 100644 --- a/async-openai/src/config.rs +++ b/async-openai/src/config.rs @@ -12,7 +12,9 @@ pub const OPENAI_ORGANIZATION_HEADER: &str = "OpenAI-Organization"; /// Calls to the Assistants API require that you pass a Beta header pub const OPENAI_BETA_HEADER: &str = "OpenAI-Beta"; -pub trait BaseConfig: 'static + Debug + Send + Sync { +/// [crate::Client] relies on this for every API call on OpenAI +/// or Azure OpenAI service +pub trait Config: 'static + Debug + Send + Sync { fn headers(&self) -> HeaderMap; fn url(&self, path: &str) -> String; fn query(&self) -> Vec<(&str, &str)>; @@ -22,18 +24,6 @@ pub trait BaseConfig: 'static + Debug + Send + Sync { fn api_key(&self) -> &Secret; } -/// [crate::Client] relies on this for every API call on OpenAI -/// or Azure OpenAI service -pub trait Config: BaseConfig { - fn extra_configs(&self) -> &Extra; -} - -impl Config<()> for T { - fn extra_configs(&self) -> &() { - &() - } -} - /// Configuration for OpenAI API #[derive(Clone, Debug, Deserialize)] #[serde(default)] @@ -84,7 +74,7 @@ impl OpenAIConfig { } } -impl BaseConfig for OpenAIConfig { +impl Config for OpenAIConfig { fn headers(&self) -> HeaderMap { let mut headers = HeaderMap::new(); if !self.org_id.is_empty() { @@ -177,7 +167,7 @@ impl AzureConfig { } } -impl BaseConfig for AzureConfig { +impl Config for AzureConfig { fn headers(&self) -> HeaderMap { let mut headers = HeaderMap::new();