Skip to content

Commit 8f80c5b

Browse files
committed
create sdk.rs which add SDK like functionality on types
1 parent 5663132 commit 8f80c5b

File tree

5 files changed

+84
-78
lines changed

5 files changed

+84
-78
lines changed

async-openai/src/types/audio/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod audio_types;
22
mod form;
33
mod impls;
4+
mod sdk;
45
mod stream;
56

67
pub use audio_types::*;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::{error::OpenAIError, types::audio::CreateSpeechResponse, util::create_all_dir};
2+
use std::path::Path;
3+
4+
impl CreateSpeechResponse {
5+
pub async fn save<P: AsRef<Path>>(&self, file_path: P) -> Result<(), OpenAIError> {
6+
let dir = file_path.as_ref().parent();
7+
8+
if let Some(dir) = dir {
9+
create_all_dir(dir)?;
10+
}
11+
12+
tokio::fs::write(file_path, &self.bytes)
13+
.await
14+
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?;
15+
16+
Ok(())
17+
}
18+
}

async-openai/src/types/images/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod form;
22
mod image;
33
mod impls;
4+
mod sdk;
45
mod stream;
56

67
pub use image::*;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use crate::{
2+
download::{download_url, save_b64},
3+
error::OpenAIError,
4+
types::images::{Image, ImagesResponse},
5+
util::create_all_dir,
6+
};
7+
use std::path::{Path, PathBuf};
8+
9+
impl ImagesResponse {
10+
/// Save each image in a dedicated Tokio task and return paths to saved files.
11+
/// For [ResponseFormat::Url] each file is downloaded in dedicated Tokio task.
12+
pub async fn save<P: AsRef<Path>>(&self, dir: P) -> Result<Vec<PathBuf>, OpenAIError> {
13+
create_all_dir(dir.as_ref())?;
14+
15+
let mut handles = vec![];
16+
for id in self.data.clone() {
17+
let dir_buf = PathBuf::from(dir.as_ref());
18+
handles.push(tokio::spawn(async move { id.save(dir_buf).await }));
19+
}
20+
21+
let results = futures::future::join_all(handles).await;
22+
let mut errors = vec![];
23+
let mut paths = vec![];
24+
25+
for result in results {
26+
match result {
27+
Ok(inner) => match inner {
28+
Ok(path) => paths.push(path),
29+
Err(e) => errors.push(e),
30+
},
31+
Err(e) => errors.push(OpenAIError::FileSaveError(e.to_string())),
32+
}
33+
}
34+
35+
if errors.is_empty() {
36+
Ok(paths)
37+
} else {
38+
Err(OpenAIError::FileSaveError(
39+
errors
40+
.into_iter()
41+
.map(|e| e.to_string())
42+
.collect::<Vec<String>>()
43+
.join("; "),
44+
))
45+
}
46+
}
47+
}
48+
49+
impl Image {
50+
async fn save<P: AsRef<Path>>(&self, dir: P) -> Result<PathBuf, OpenAIError> {
51+
match self {
52+
Image::Url { url, .. } => download_url(url, dir).await,
53+
Image::B64Json { b64_json, .. } => save_b64(b64_json, dir).await,
54+
}
55+
}
56+
}

async-openai/src/types/impls.rs

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
use std::path::{Path, PathBuf};
22

3-
use crate::{
4-
download::{download_url, save_b64},
5-
error::OpenAIError,
6-
types::{
7-
audio::{AudioInput, CreateSpeechResponse},
8-
chat::{Prompt, StopConfiguration},
9-
embeddings::EmbeddingInput,
10-
files::FileInput,
11-
images::{Image, ImageInput, ImagesResponse},
12-
moderations::ModerationInput,
13-
InputSource,
14-
},
15-
util::create_all_dir,
3+
use crate::types::{
4+
audio::AudioInput,
5+
chat::{Prompt, StopConfiguration},
6+
embeddings::EmbeddingInput,
7+
files::FileInput,
8+
images::ImageInput,
9+
moderations::ModerationInput,
10+
InputSource,
1611
};
1712

1813
use bytes::Bytes;
@@ -146,71 +141,6 @@ impl_input!(AudioInput);
146141
impl_input!(FileInput);
147142
impl_input!(ImageInput);
148143

149-
impl ImagesResponse {
150-
/// Save each image in a dedicated Tokio task and return paths to saved files.
151-
/// For [ResponseFormat::Url] each file is downloaded in dedicated Tokio task.
152-
pub async fn save<P: AsRef<Path>>(&self, dir: P) -> Result<Vec<PathBuf>, OpenAIError> {
153-
create_all_dir(dir.as_ref())?;
154-
155-
let mut handles = vec![];
156-
for id in self.data.clone() {
157-
let dir_buf = PathBuf::from(dir.as_ref());
158-
handles.push(tokio::spawn(async move { id.save(dir_buf).await }));
159-
}
160-
161-
let results = futures::future::join_all(handles).await;
162-
let mut errors = vec![];
163-
let mut paths = vec![];
164-
165-
for result in results {
166-
match result {
167-
Ok(inner) => match inner {
168-
Ok(path) => paths.push(path),
169-
Err(e) => errors.push(e),
170-
},
171-
Err(e) => errors.push(OpenAIError::FileSaveError(e.to_string())),
172-
}
173-
}
174-
175-
if errors.is_empty() {
176-
Ok(paths)
177-
} else {
178-
Err(OpenAIError::FileSaveError(
179-
errors
180-
.into_iter()
181-
.map(|e| e.to_string())
182-
.collect::<Vec<String>>()
183-
.join("; "),
184-
))
185-
}
186-
}
187-
}
188-
189-
impl CreateSpeechResponse {
190-
pub async fn save<P: AsRef<Path>>(&self, file_path: P) -> Result<(), OpenAIError> {
191-
let dir = file_path.as_ref().parent();
192-
193-
if let Some(dir) = dir {
194-
create_all_dir(dir)?;
195-
}
196-
197-
tokio::fs::write(file_path, &self.bytes)
198-
.await
199-
.map_err(|e| OpenAIError::FileSaveError(e.to_string()))?;
200-
201-
Ok(())
202-
}
203-
}
204-
205-
impl Image {
206-
async fn save<P: AsRef<Path>>(&self, dir: P) -> Result<PathBuf, OpenAIError> {
207-
match self {
208-
Image::Url { url, .. } => download_url(url, dir).await,
209-
Image::B64Json { b64_json, .. } => save_b64(b64_json, dir).await,
210-
}
211-
}
212-
}
213-
214144
macro_rules! impl_from_for_integer_array {
215145
($from_typ:ty, $to_typ:ty) => {
216146
impl<const N: usize> From<[$from_typ; N]> for $to_typ {

0 commit comments

Comments
 (0)