Skip to content

Commit

Permalink
remove async-convert and bump rust-version (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsheldon authored Jan 12, 2025
1 parent f6792f3 commit 42dda26
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
3 changes: 1 addition & 2 deletions async-openai/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ categories = ["api-bindings", "web-programming", "asynchronous"]
keywords = ["openai", "async", "openapi", "ai"]
description = "Rust library for OpenAI"
edition = "2021"
rust-version = "1.65"
rust-version = "1.75"
license = "MIT"
readme = "README.md"
homepage = "https://github.com/64bit/async-openai"
Expand Down Expand Up @@ -43,7 +43,6 @@ tokio-stream = "0.1.17"
tokio-util = { version = "0.7.13", features = ["codec", "io-util"] }
tracing = "0.1.41"
derive_builder = "0.20.2"
async-convert = "1.0.0"
secrecy = { version = "0.10.3", features = ["serde"] }
bytes = "1.9.0"
eventsource-stream = "0.2.3"
Expand Down
10 changes: 6 additions & 4 deletions async-openai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::pin::Pin;

use bytes::Bytes;
use futures::{stream::StreamExt, Stream};
use reqwest::multipart::Form;
use reqwest_eventsource::{Event, EventSource, RequestBuilderExt};
use serde::{de::DeserializeOwned, Serialize};

Expand All @@ -11,6 +12,7 @@ use crate::{
file::Files,
image::Images,
moderation::Moderations,
util::AsyncTryFrom,
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Embeddings, FineTuning, Invites,
Models, Projects, Threads, Users, VectorStores,
};
Expand Down Expand Up @@ -266,7 +268,7 @@ impl<C: Config> Client<C> {
/// POST a form at {path} and return the response body
pub(crate) async fn post_form_raw<F>(&self, path: &str, form: F) -> Result<Bytes, OpenAIError>
where
reqwest::multipart::Form: async_convert::TryFrom<F, Error = OpenAIError>,
Form: AsyncTryFrom<F, Error = OpenAIError>,
F: Clone,
{
let request_maker = || async {
Expand All @@ -275,7 +277,7 @@ impl<C: Config> Client<C> {
.post(self.config.url(path))
.query(&self.config.query())
.headers(self.config.headers())
.multipart(async_convert::TryFrom::try_from(form.clone()).await?)
.multipart(<Form as AsyncTryFrom<F>>::try_from(form.clone()).await?)
.build()?)
};

Expand All @@ -286,7 +288,7 @@ impl<C: Config> Client<C> {
pub(crate) async fn post_form<O, F>(&self, path: &str, form: F) -> Result<O, OpenAIError>
where
O: DeserializeOwned,
reqwest::multipart::Form: async_convert::TryFrom<F, Error = OpenAIError>,
Form: AsyncTryFrom<F, Error = OpenAIError>,
F: Clone,
{
let request_maker = || async {
Expand All @@ -295,7 +297,7 @@ impl<C: Config> Client<C> {
.post(self.config.url(path))
.query(&self.config.query())
.headers(self.config.headers())
.multipart(async_convert::TryFrom::try_from(form.clone()).await?)
.multipart(<Form as AsyncTryFrom<F>>::try_from(form.clone()).await?)
.build()?)
};

Expand Down
20 changes: 7 additions & 13 deletions async-openai/src/types/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
download::{download_url, save_b64},
error::OpenAIError,
types::InputSource,
util::{create_all_dir, create_file_part},
util::{create_all_dir, create_file_part, AsyncTryFrom},
};

use bytes::Bytes;
Expand Down Expand Up @@ -821,8 +821,7 @@ impl Default for ChatCompletionRequestToolMessageContent {

// start: types to multipart from

#[async_convert::async_trait]
impl async_convert::TryFrom<CreateTranscriptionRequest> for reqwest::multipart::Form {
impl AsyncTryFrom<CreateTranscriptionRequest> for reqwest::multipart::Form {
type Error = OpenAIError;

async fn try_from(request: CreateTranscriptionRequest) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -858,8 +857,7 @@ impl async_convert::TryFrom<CreateTranscriptionRequest> for reqwest::multipart::
}
}

#[async_convert::async_trait]
impl async_convert::TryFrom<CreateTranslationRequest> for reqwest::multipart::Form {
impl AsyncTryFrom<CreateTranslationRequest> for reqwest::multipart::Form {
type Error = OpenAIError;

async fn try_from(request: CreateTranslationRequest) -> Result<Self, Self::Error> {
Expand All @@ -884,8 +882,7 @@ impl async_convert::TryFrom<CreateTranslationRequest> for reqwest::multipart::Fo
}
}

#[async_convert::async_trait]
impl async_convert::TryFrom<CreateImageEditRequest> for reqwest::multipart::Form {
impl AsyncTryFrom<CreateImageEditRequest> for reqwest::multipart::Form {
type Error = OpenAIError;

async fn try_from(request: CreateImageEditRequest) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -926,8 +923,7 @@ impl async_convert::TryFrom<CreateImageEditRequest> for reqwest::multipart::Form
}
}

#[async_convert::async_trait]
impl async_convert::TryFrom<CreateImageVariationRequest> for reqwest::multipart::Form {
impl AsyncTryFrom<CreateImageVariationRequest> for reqwest::multipart::Form {
type Error = OpenAIError;

async fn try_from(request: CreateImageVariationRequest) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -961,8 +957,7 @@ impl async_convert::TryFrom<CreateImageVariationRequest> for reqwest::multipart:
}
}

#[async_convert::async_trait]
impl async_convert::TryFrom<CreateFileRequest> for reqwest::multipart::Form {
impl AsyncTryFrom<CreateFileRequest> for reqwest::multipart::Form {
type Error = OpenAIError;

async fn try_from(request: CreateFileRequest) -> Result<Self, Self::Error> {
Expand All @@ -974,8 +969,7 @@ impl async_convert::TryFrom<CreateFileRequest> for reqwest::multipart::Form {
}
}

#[async_convert::async_trait]
impl async_convert::TryFrom<AddUploadPartRequest> for reqwest::multipart::Form {
impl AsyncTryFrom<AddUploadPartRequest> for reqwest::multipart::Form {
type Error = OpenAIError;

async fn try_from(request: AddUploadPartRequest) -> Result<Self, Self::Error> {
Expand Down
8 changes: 8 additions & 0 deletions async-openai/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ use tokio_util::codec::{BytesCodec, FramedRead};
use crate::error::OpenAIError;
use crate::types::InputSource;

pub(crate) trait AsyncTryFrom<T>: Sized {
/// The type returned in the event of a conversion error.
type Error;

/// Performs the conversion.
async fn try_from(value: T) -> Result<Self, Self::Error>;
}

pub(crate) async fn file_stream_body(source: InputSource) -> Result<Body, OpenAIError> {
let body = match source {
InputSource::Path { path } => {
Expand Down

0 comments on commit 42dda26

Please # to comment.