Skip to content

Commit 5663132

Browse files
committed
impls and forms for each type mods
1 parent 33835f7 commit 5663132

File tree

21 files changed

+515
-479
lines changed

21 files changed

+515
-479
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::types::assistants::CreateMessageRequestContent;
2+
3+
impl From<String> for CreateMessageRequestContent {
4+
fn from(value: String) -> Self {
5+
Self::Content(value)
6+
}
7+
}
8+
9+
impl From<&str> for CreateMessageRequestContent {
10+
fn from(value: &str) -> Self {
11+
Self::Content(value.to_string())
12+
}
13+
}
14+
15+
impl Default for CreateMessageRequestContent {
16+
fn default() -> Self {
17+
Self::Content("".into())
18+
}
19+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod api;
22
mod assistant;
33
mod assistant_impls;
44
mod assistant_stream;
5+
mod impls;
56
mod message;
67
mod run;
78
mod step;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use crate::{
2+
error::OpenAIError,
3+
traits::AsyncTryFrom,
4+
types::audio::{
5+
CreateTranscriptionRequest, CreateTranslationRequest, TranscriptionChunkingStrategy,
6+
},
7+
util::create_file_part,
8+
};
9+
10+
impl AsyncTryFrom<CreateTranscriptionRequest> for reqwest::multipart::Form {
11+
type Error = OpenAIError;
12+
13+
async fn try_from(request: CreateTranscriptionRequest) -> Result<Self, Self::Error> {
14+
let audio_part = create_file_part(request.file.source).await?;
15+
16+
let mut form = reqwest::multipart::Form::new()
17+
.part("file", audio_part)
18+
.text("model", request.model);
19+
20+
if let Some(language) = request.language {
21+
form = form.text("language", language);
22+
}
23+
24+
if let Some(prompt) = request.prompt {
25+
form = form.text("prompt", prompt);
26+
}
27+
28+
if let Some(response_format) = request.response_format {
29+
form = form.text("response_format", response_format.to_string())
30+
}
31+
32+
if let Some(temperature) = request.temperature {
33+
form = form.text("temperature", temperature.to_string())
34+
}
35+
36+
if let Some(include) = request.include {
37+
for inc in include {
38+
form = form.text("include[]", inc.to_string());
39+
}
40+
}
41+
42+
if let Some(timestamp_granularities) = request.timestamp_granularities {
43+
for tg in timestamp_granularities {
44+
form = form.text("timestamp_granularities[]", tg.to_string());
45+
}
46+
}
47+
48+
if let Some(stream) = request.stream {
49+
form = form.text("stream", stream.to_string());
50+
}
51+
52+
if let Some(chunking_strategy) = request.chunking_strategy {
53+
match chunking_strategy {
54+
TranscriptionChunkingStrategy::Auto => {
55+
form = form.text("chunking_strategy", "auto");
56+
}
57+
TranscriptionChunkingStrategy::ServerVad(vad_config) => {
58+
form = form.text(
59+
"chunking_strategy",
60+
serde_json::to_string(&vad_config).unwrap().to_string(),
61+
);
62+
}
63+
}
64+
}
65+
66+
if let Some(known_speaker_names) = request.known_speaker_names {
67+
for kn in known_speaker_names {
68+
form = form.text("known_speaker_names[]", kn.to_string());
69+
}
70+
}
71+
72+
if let Some(known_speaker_references) = request.known_speaker_references {
73+
for kn in known_speaker_references {
74+
form = form.text("known_speaker_references[]", kn.to_string());
75+
}
76+
}
77+
78+
Ok(form)
79+
}
80+
}
81+
82+
impl AsyncTryFrom<CreateTranslationRequest> for reqwest::multipart::Form {
83+
type Error = OpenAIError;
84+
85+
async fn try_from(request: CreateTranslationRequest) -> Result<Self, Self::Error> {
86+
let audio_part = create_file_part(request.file.source).await?;
87+
88+
let mut form = reqwest::multipart::Form::new()
89+
.part("file", audio_part)
90+
.text("model", request.model);
91+
92+
if let Some(prompt) = request.prompt {
93+
form = form.text("prompt", prompt);
94+
}
95+
96+
if let Some(response_format) = request.response_format {
97+
form = form.text("response_format", response_format.to_string())
98+
}
99+
100+
if let Some(temperature) = request.temperature {
101+
form = form.text("temperature", temperature.to_string())
102+
}
103+
Ok(form)
104+
}
105+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::fmt::Display;
2+
3+
use crate::types::audio::{
4+
AudioResponseFormat, TimestampGranularity, TranscriptionInclude, TranslationResponseFormat,
5+
};
6+
7+
impl Display for AudioResponseFormat {
8+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9+
write!(
10+
f,
11+
"{}",
12+
match self {
13+
AudioResponseFormat::Json => "json",
14+
AudioResponseFormat::Srt => "srt",
15+
AudioResponseFormat::Text => "text",
16+
AudioResponseFormat::VerboseJson => "verbose_json",
17+
AudioResponseFormat::Vtt => "vtt",
18+
AudioResponseFormat::DiarizedJson => "diarized_json",
19+
}
20+
)
21+
}
22+
}
23+
24+
impl Display for TranslationResponseFormat {
25+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26+
write!(
27+
f,
28+
"{}",
29+
match self {
30+
TranslationResponseFormat::Json => "json",
31+
TranslationResponseFormat::Srt => "srt",
32+
TranslationResponseFormat::Text => "text",
33+
TranslationResponseFormat::VerboseJson => "verbose_json",
34+
TranslationResponseFormat::Vtt => "vtt",
35+
}
36+
)
37+
}
38+
}
39+
40+
impl Display for TimestampGranularity {
41+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42+
write!(
43+
f,
44+
"{}",
45+
match self {
46+
TimestampGranularity::Word => "word",
47+
TimestampGranularity::Segment => "segment",
48+
}
49+
)
50+
}
51+
}
52+
53+
impl Display for TranscriptionInclude {
54+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55+
write!(
56+
f,
57+
"{}",
58+
match self {
59+
TranscriptionInclude::Logprobs => "logprobs",
60+
}
61+
)
62+
}
63+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
mod audio_types;
2+
mod form;
3+
mod impls;
24
mod stream;
35

46
pub use audio_types::*;

async-openai/src/types/chat/impls.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::Display;
2+
13
use crate::types::chat::{
24
ChatCompletionFunctionCall, ChatCompletionNamedToolChoice,
35
ChatCompletionRequestAssistantMessage, ChatCompletionRequestAssistantMessageContent,
@@ -8,7 +10,7 @@ use crate::types::chat::{
810
ChatCompletionRequestSystemMessageContent, ChatCompletionRequestToolMessage,
911
ChatCompletionRequestToolMessageContent, ChatCompletionRequestUserMessage,
1012
ChatCompletionRequestUserMessageContent, ChatCompletionRequestUserMessageContentPart,
11-
FunctionName, ImageUrl,
13+
FunctionName, ImageUrl, Role,
1214
};
1315

1416
impl From<ChatCompletionRequestUserMessage> for ChatCompletionRequestMessage {
@@ -314,3 +316,19 @@ impl From<String> for ImageUrl {
314316
}
315317
}
316318
}
319+
320+
impl Display for Role {
321+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
322+
write!(
323+
f,
324+
"{}",
325+
match self {
326+
Role::User => "user",
327+
Role::System => "system",
328+
Role::Assistant => "assistant",
329+
Role::Function => "function",
330+
Role::Tool => "tool",
331+
}
332+
)
333+
}
334+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::{
2+
error::OpenAIError, traits::AsyncTryFrom, types::containers::CreateContainerFileRequest,
3+
util::create_file_part,
4+
};
5+
6+
impl AsyncTryFrom<CreateContainerFileRequest> for reqwest::multipart::Form {
7+
type Error = OpenAIError;
8+
9+
async fn try_from(request: CreateContainerFileRequest) -> Result<Self, Self::Error> {
10+
let mut form = reqwest::multipart::Form::new();
11+
12+
// Either file or file_id should be provided
13+
if let Some(file_source) = request.file {
14+
let file_part = create_file_part(file_source).await?;
15+
form = form.part("file", file_part);
16+
} else if let Some(file_id) = request.file_id {
17+
form = form.text("file_id", file_id);
18+
}
19+
20+
Ok(form)
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod api;
22
mod container;
3+
mod form;
34

45
pub use api::*;
56
pub use container::*;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::{
2+
error::OpenAIError, traits::AsyncTryFrom, types::files::CreateFileRequest,
3+
util::create_file_part,
4+
};
5+
6+
impl AsyncTryFrom<CreateFileRequest> for reqwest::multipart::Form {
7+
type Error = OpenAIError;
8+
9+
async fn try_from(request: CreateFileRequest) -> Result<Self, Self::Error> {
10+
let file_part = create_file_part(request.file.source).await?;
11+
let mut form = reqwest::multipart::Form::new()
12+
.part("file", file_part)
13+
.text("purpose", request.purpose.to_string());
14+
15+
if let Some(expires_after) = request.expires_after {
16+
form = form
17+
.text("expires_after[anchor]", expires_after.anchor.to_string())
18+
.text("expires_after[seconds]", expires_after.seconds.to_string());
19+
}
20+
Ok(form)
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::fmt::Display;
2+
3+
use crate::types::files::{FileExpirationAfterAnchor, FilePurpose};
4+
5+
impl Display for FilePurpose {
6+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7+
write!(
8+
f,
9+
"{}",
10+
match self {
11+
Self::Assistants => "assistants",
12+
Self::Batch => "batch",
13+
Self::FineTune => "fine-tune",
14+
Self::Vision => "vision",
15+
Self::UserData => "user_data",
16+
Self::Evals => "evals",
17+
}
18+
)
19+
}
20+
}
21+
22+
impl Display for FileExpirationAfterAnchor {
23+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24+
write!(
25+
f,
26+
"{}",
27+
match self {
28+
Self::CreatedAt => "created_at",
29+
}
30+
)
31+
}
32+
}

0 commit comments

Comments
 (0)