Skip to content

Commit b5c83c0

Browse files
authored
Refactor types module (#170)
* Document `impl_from!` macro * Fix up `impl_from!` docs * Documents `impl_default!` macro * Document `impl_input!` macro * Factor out types from `assistants` module in `types` * Factor out `model` * Factor out `audio` * Factor out `image` * Factor out `file` * Factor out `fine_tune` * Factor out `moderation` * Factor out `edit` * Factor out `fine_tuning` * Factor out missed `DeleteModelResponse` into `model` * Factor out `embedding` * Factor out `chat` * Factor out `completion` and eliminate `types` * Satisfy clippy --------- Co-authored-by: Sharif Haason <[email protected]>
1 parent 2ba35ff commit b5c83c0

24 files changed

+1864
-1771
lines changed

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

Lines changed: 0 additions & 16 deletions
This file was deleted.

async-openai/src/types/audio.rs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
use bytes::Bytes;
2+
use derive_builder::Builder;
3+
use serde::{Deserialize, Serialize};
4+
5+
use super::InputSource;
6+
use crate::error::OpenAIError;
7+
8+
#[derive(Debug, Default, Clone, PartialEq)]
9+
pub struct AudioInput {
10+
pub source: InputSource,
11+
}
12+
13+
#[derive(Debug, Serialize, Default, Clone, Copy, PartialEq)]
14+
#[serde(rename_all = "snake_case")]
15+
pub enum AudioResponseFormat {
16+
#[default]
17+
Json,
18+
Text,
19+
Srt,
20+
VerboseJson,
21+
Vtt,
22+
}
23+
24+
#[derive(Debug, Serialize, Default, Clone, Copy, PartialEq)]
25+
#[serde(rename_all = "snake_case")]
26+
pub enum SpeechResponseFormat {
27+
#[default]
28+
Mp3,
29+
Opus,
30+
Aac,
31+
Flac,
32+
}
33+
34+
#[derive(Debug, Default, Serialize, Clone, PartialEq)]
35+
#[serde(rename_all = "lowercase")]
36+
#[non_exhaustive]
37+
pub enum Voice {
38+
#[default]
39+
Alloy,
40+
Echo,
41+
Fable,
42+
Onyx,
43+
Nova,
44+
Shimmer,
45+
#[serde(untagged)]
46+
Other(String),
47+
}
48+
49+
#[derive(Debug, Default, Serialize, Clone, PartialEq)]
50+
pub enum SpeechModel {
51+
#[default]
52+
#[serde(rename = "tts-1")]
53+
Tts1,
54+
#[serde(rename = "tts-1-hd")]
55+
Tts1Hd,
56+
#[serde(untagged)]
57+
Other(String),
58+
}
59+
60+
#[derive(Clone, Default, Debug, Builder, PartialEq)]
61+
#[builder(name = "CreateTranscriptionRequestArgs")]
62+
#[builder(pattern = "mutable")]
63+
#[builder(setter(into, strip_option), default)]
64+
#[builder(derive(Debug))]
65+
#[builder(build_fn(error = "OpenAIError"))]
66+
pub struct CreateTranscriptionRequest {
67+
/// The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
68+
pub file: AudioInput,
69+
70+
/// ID of the model to use. Only `whisper-1` is currently available.
71+
pub model: String,
72+
73+
/// An optional text to guide the model's style or continue a previous audio segment. The [prompt](https://platform.openai.com/docs/guides/speech-to-text/prompting) should match the audio language.
74+
pub prompt: Option<String>,
75+
76+
/// The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
77+
pub response_format: Option<AudioResponseFormat>,
78+
79+
/// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use [log probability](https://en.wikipedia.org/wiki/Log_probability) to automatically increase the temperature until certain thresholds are hit.
80+
pub temperature: Option<f32>, // default: 0
81+
82+
/// The language of the input audio. Supplying the input language in [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) format will improve accuracy and latency.
83+
pub language: Option<String>,
84+
}
85+
86+
#[derive(Debug, Deserialize, Clone, Serialize)]
87+
pub struct CreateTranscriptionResponse {
88+
pub text: String,
89+
}
90+
91+
#[derive(Clone, Default, Debug, Builder, PartialEq, Serialize)]
92+
#[builder(name = "CreateSpeechRequestArgs")]
93+
#[builder(pattern = "mutable")]
94+
#[builder(setter(into, strip_option), default)]
95+
#[builder(derive(Debug))]
96+
#[builder(build_fn(error = "OpenAIError"))]
97+
pub struct CreateSpeechRequest {
98+
/// The text to generate audio for. The maximum length is 4096 characters.
99+
pub input: String,
100+
101+
/// One of the available [TTS models](https://platform.openai.com/docs/models/tts): `tts-1` or `tts-1-hd`
102+
pub model: SpeechModel,
103+
104+
/// The voice to use when generating the audio. Supported voices are `alloy`, `echo`, `fable`, `onyx`, `nova`, and `shimmer`.
105+
pub voice: Voice,
106+
107+
/// The format to audio in. Supported formats are mp3, opus, aac, and flac.
108+
#[serde(skip_serializing_if = "Option::is_none")]
109+
pub response_format: Option<SpeechResponseFormat>,
110+
111+
/// The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
112+
#[serde(skip_serializing_if = "Option::is_none")]
113+
pub speed: Option<f32>, // default: 1.0
114+
}
115+
116+
#[derive(Clone, Default, Debug, Builder, PartialEq)]
117+
#[builder(name = "CreateTranslationRequestArgs")]
118+
#[builder(pattern = "mutable")]
119+
#[builder(setter(into, strip_option), default)]
120+
#[builder(derive(Debug))]
121+
#[builder(build_fn(error = "OpenAIError"))]
122+
pub struct CreateTranslationRequest {
123+
/// The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
124+
pub file: AudioInput,
125+
126+
/// ID of the model to use. Only `whisper-1` is currently available.
127+
pub model: String,
128+
129+
/// An optional text to guide the model's style or continue a previous audio segment. The [prompt](https://platform.openai.com/docs/guides/speech-to-text/prompting) should be in English.
130+
pub prompt: Option<String>,
131+
132+
/// The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
133+
pub response_format: Option<AudioResponseFormat>,
134+
135+
/// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use [log probability](https://en.wikipedia.org/wiki/Log_probability) to automatically increase the temperature until certain thresholds are hit.
136+
pub temperature: Option<f32>, // default: 0
137+
}
138+
139+
#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
140+
pub struct CreateTranslationResponse {
141+
pub text: String,
142+
}
143+
144+
#[derive(Debug, Clone)]
145+
pub struct CreateSpeechResponse {
146+
pub bytes: Bytes,
147+
}

0 commit comments

Comments
 (0)