outfox-openai 0.7.0

Openai for outfox
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use bytes::Bytes;
use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::error::OpenAIError;
use crate::spec::InputSource;
use crate::spec::audio::{LogProbProperties, TranscriptTextUsageDuration, TranscriptionUsage};

// openapi spec type: VoiceIdsShared
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Voice {
    #[default]
    Alloy,
    Ash,
    Ballad,
    Coral,
    Echo,
    Fable,
    Onyx,
    Nova,
    Sage,
    Shimmer,
    Verse,
    #[serde(untagged)]
    Other(String),
}

#[derive(Debug, Default, Clone, PartialEq)]
pub struct AudioInput {
    pub source: InputSource,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AudioResponseFormat {
    #[default]
    Json,
    Text,
    Srt,
    VerboseJson,
    Vtt,
    DiarizedJson,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum TranslationResponseFormat {
    #[default]
    Json,
    Text,
    Srt,
    VerboseJson,
    Vtt,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum SpeechResponseFormat {
    #[default]
    Mp3,
    Opus,
    Aac,
    Flac,
    Pcm,
    Wav,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum SpeechModel {
    #[default]
    #[serde(rename = "tts-1")]
    Tts1,
    #[serde(rename = "tts-1-hd")]
    Tts1Hd,
    #[serde(rename = "gpt-4o-mini-tts")]
    Gpt4oMiniTts,
    #[serde(untagged)]
    Other(String),
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TimestampGranularity {
    Word,
    #[default]
    Segment,
}

#[derive(Clone, Default, Debug, Builder, PartialEq)]
#[builder(name = "CreateTranscriptionRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateTranscriptionRequest {
    /// The audio file object (not file name) to transcribe, in one of these formats:
    /// flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
    pub file: AudioInput,

    /// ID of the model to use. The options are `gpt-4o-transcribe`, `gpt-4o-mini-transcribe`,
    /// `whisper-1` (which is powered by our open source Whisper V2 model), and
    /// `gpt-4o-transcribe-diarize`.
    pub model: String,

    /// 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) (e.g. `en`) format will improve
    /// accuracy and latency.
    pub language: Option<String>,

    /// 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. This field is not supported when using `gpt-4o-transcribe-diarize`.
    pub prompt: Option<String>,

    /// The format of the output, in one of these options: `json`, `text`, `srt`, `verbose_json`,
    /// `vtt`, or `diarized_json`. For `gpt-4o-transcribe` and `gpt-4o-mini-transcribe`, the
    /// only supported format is `json`. For `gpt-4o-transcribe-diarize`, the supported formats
    /// are `json`, `text`, and `diarized_json`, with `diarized_json` required to receive
    /// speaker annotations.
    pub response_format: Option<AudioResponseFormat>,

    /// TThe 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.
    pub temperature: Option<f32>, // default: 0

    /// Additional information to include in the transcription response.

    /// `logprobs` will return the log probabilities of the tokens in the
    /// response to understand the model's confidence in the transcription.
    /// `logprobs` only works with response_format set to `json` and only with
    /// the models `gpt-4o-transcribe` and `gpt-4o-mini-transcribe`. This field is not supported
    /// when using `gpt-4o-transcribe-diarize`.
    pub include: Option<Vec<TranscriptionInclude>>,

    /// The timestamp granularities to populate for this transcription. `response_format` must be
    /// set `verbose_json` to use timestamp granularities. Either or both of these options are
    /// supported: `word`, or `segment`. Note: There is no additional latency for segment
    /// timestamps, but generating word timestamps incurs additional latency. This option is
    /// not available for `gpt-4o-transcribe-diarize`.
    pub timestamp_granularities: Option<Vec<TimestampGranularity>>,

    /// If set to true, the model response data will be streamed to the client
    /// as it is generated using [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format).
    /// See the [Streaming section of the Speech-to-Text guide](https://platform.openai.com/docs/guides/speech-to-text?lang=curl#streaming-transcriptions)
    /// for more information.
    /// Note: Streaming is not supported for the `whisper-1` model and will be ignored.    
    pub stream: Option<bool>,

    /// Controls how the audio is cut into chunks. When set to `"auto"`, the server first
    /// normalizes loudness and then uses voice activity detection (VAD) to choose boundaries.
    /// `server_vad` object can be provided to tweak VAD detection parameters manually. If
    /// unset, the audio is transcribed as a single block. Required when using
    /// `gpt-4o-transcribe-diarize` for inputs longer than 30 seconds.
    pub chunking_strategy: Option<TranscriptionChunkingStrategy>,

    /// Optional list of speaker names that correspond to the audio samples provided in
    /// `known_speaker_references[]`. Each entry should be a short identifier (for example
    /// `customer` or `agent`). Up to 4 speakers are supported.
    pub known_speaker_names: Option<Vec<String>>,

    /// Optional list of audio samples (as [data
    /// URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs)) that contain
    /// known speaker references matching `known_speaker_names[]`. Each sample must be between 2
    /// and 10 seconds, and can use any of the same input audio formats supported by `file`.
    pub known_speaker_references: Option<Vec<String>>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "snake_case")]
pub enum TranscriptionChunkingStrategy {
    #[default]
    Auto,
    #[serde(untagged)]
    ServerVad(VadConfig),
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "snake_case")]
pub enum VadConfigType {
    #[default]
    ServerVad,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
pub struct VadConfig {
    pub kind: VadConfigType,
    /// Amount of audio to include before the VAD detected speech (in milliseconds). Default: 300.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prefix_padding_ms: Option<u32>,

    /// Duration of silence to detect speech stop (in milliseconds).
    /// With shorter values the model will respond more quickly,
    /// but may jump in on short pauses from the user. Default: 200.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub silence_duration_ms: Option<u32>,

    /// Sensitivity threshold (0.0 to 1.0) for voice activity detection. A
    /// higher threshold will require louder audio to activate the model, and
    /// thus might perform better in noisy environments. Default: 0.5.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub threshold: Option<f32>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum TranscriptionInclude {
    Logprobs,
}

/// Represents a transcription response returned by model, based on the provided
/// input.
#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct CreateTranscriptionResponseJson {
    /// The transcribed text.
    pub text: String,

    /// The log probabilities of the tokens in the transcription. Only returned with the models
    /// `gpt-4o-transcribe` and `gpt-4o-mini-transcribe` if `logprobs` is added to the `include`
    /// array.
    pub logprobs: Option<Vec<LogProbProperties>>,

    /// Token usage statistics for the request.
    pub usage: TranscriptionUsage,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "lowercase")]
pub enum CreateTranscriptionResponseDiarizedJsonTask {
    Transcribe,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CreateTranscriptionResponseDiarizedJson {
    /// The type of task that was run. Always `transcribe`.
    pub task: Option<CreateTranscriptionResponseDiarizedJsonTask>,

    /// Duration of the input audio in seconds.
    pub duration: Option<f32>,

    /// The concatenated transcript text for the entire audio input.
    pub text: String,

    /// Segments of the transcript annotated with timestamps and speaker labels.
    pub segments: Vec<TranscriptionDiarizedSegment>,

    /// Token or duration usage statistics for the request.
    pub usage: TranscriptionUsage,
}

/// Represents a verbose json transcription response returned by model, based on
/// the provided input.
#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct CreateTranscriptionResponseVerboseJson {
    /// The language of the input audio.
    pub language: String,

    /// The duration of the input audio.
    pub duration: f32,

    /// The transcribed text.
    pub text: String,

    /// Extracted words and their corresponding timestamps.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub words: Option<Vec<TranscriptionWord>>,

    /// Segments of the transcribed text and their corresponding details.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub segments: Option<Vec<TranscriptionSegment>>,

    /// Usage statistics for models billed by audio input duration.
    pub usage: TranscriptTextUsageDuration,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct TranscriptionWord {
    /// The text content of the word.
    pub word: String,

    /// Start time of the word in seconds.
    pub start: f32,

    /// End time of the word in seconds.
    pub end: f32,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum TranscriptionDiarizedSegmentType {
    #[serde(rename = "transcript.text.segment")]
    TranscriptTextSegment,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TranscriptionDiarizedSegment {
    /// The type of the segment. Always `transcript.text.segment`.
    pub kind: TranscriptionDiarizedSegmentType,

    /// Unique identifier for the segment.
    pub id: String,

    /// Start timestamp of the segment in seconds.
    pub start: f32,

    /// End timestamp of the segment in seconds.
    pub end: f32,

    /// Transcript text for this segment.
    pub text: String,

    /// Speaker label for this segment.
    /// When known speakers are provided, the label matches known_speaker_names[].
    /// Otherwise speakers are labeled sequentially using capital letters (`A`, `B`, ...).
    pub speaker: String,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct TranscriptionSegment {
    /// Unique identifier of the segment.
    pub id: u32,

    // Seek offset of the segment.
    pub seek: u32,

    /// Start time of the segment in seconds.
    pub start: f32,

    /// End time of the segment in seconds.
    pub end: f32,

    /// Text content of the segment.
    pub text: String,

    /// Array of token IDs for the text content.
    pub tokens: Vec<u32>,

    /// Temperature parameter used for generating the segment.
    pub temperature: f32,

    /// Average logprob of the segment. If the value is lower than -1, consider
    /// the logprobs failed.
    pub avg_logprob: f32,

    /// Compression ratio of the segment. If the value is greater than 2.4,
    /// consider the compression failed.
    pub compression_ratio: f32,

    /// Probability of no speech in the segment. If the value is higher than 1.0
    /// and the `avg_logprob` is below -1, consider this segment silent.
    pub no_speech_prob: f32,
}

#[derive(Clone, Default, Debug, Builder, PartialEq, Serialize, Deserialize)]
#[builder(name = "CreateSpeechRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateSpeechRequest {
    /// The text to generate audio for. The maximum length is 4096 characters.
    pub input: String,

    /// One of the available [TTS models](https://platform.openai.com/docs/models#tts): `tts-1`,
    /// `tts-1-hd` or `gpt-4o-mini-tts`.
    pub model: SpeechModel,

    /// The voice to use when generating the audio. Supported voices are `alloy`, `ash`, `coral`,
    /// `echo`, `fable`, `onyx`, `nova`, `sage`, `shimmer` and `verse`.

    /// The voice to use when generating the audio. Supported voices are `alloy`, `ash`, `ballad`,
    /// `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, `shimmer`, and `verse`. Previews of the
    /// voices are available in the [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options).
    pub voice: Voice,

    /// Control the voice of your generated audio with additional instructions.
    /// Does not work with `tts-1` or `tts-1-hd`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,

    /// The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`, `wav`, and
    /// `pcm`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<SpeechResponseFormat>,

    /// The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub speed: Option<f32>, // default: 1.0

    /// The format to stream the audio in. Supported formats are `sse` and `audio`. `sse` is not
    /// supported for `tts-1` or `tts-1-hd`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream_format: Option<StreamFormat>,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum StreamFormat {
    #[default]
    #[serde(rename = "sse")]
    SSE,
    #[serde(rename = "audio")]
    Audio,
}

#[derive(Clone, Default, Debug, Builder, PartialEq)]
#[builder(name = "CreateTranslationRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateTranslationRequest {
    /// The audio file object (not file name) translate, in one of these
    /// formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
    pub file: AudioInput,

    /// ID of the model to use. Only `whisper-1` (which is powered by our open
    /// source Whisper V2 model) is currently available.
    pub model: String,

    /// 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.
    pub prompt: Option<String>,

    /// The format of the transcript output, in one of these options: json, text, srt,
    /// verbose_json, or vtt.
    pub response_format: Option<TranslationResponseFormat>,

    /// 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.
    pub temperature: Option<f32>, // default: 0
}

#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
pub struct CreateTranslationResponseJson {
    pub text: String,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct CreateTranslationResponseVerboseJson {
    /// The language of the output translation (always `english`).
    pub language: String,
    /// The duration of the input audio.
    pub duration: String,
    /// The translated text.
    pub text: String,
    /// Segments of the translated text and their corresponding details.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub segments: Option<Vec<TranscriptionSegment>>,
}

#[derive(Debug, Clone)]
pub struct CreateSpeechResponse {
    pub bytes: Bytes,
}

/// A consent recording used to authorize creation of a custom voice.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct VoiceConsentResource {
    /// The object type, which is always `audio.voice_consent`.
    pub object: String,
    /// The consent recording identifier.
    pub id: String,
    /// The label provided when the consent recording was uploaded.
    pub name: String,
    /// The BCP 47 language tag for the consent phrase (for example, `en-US`).
    pub language: String,
    /// The Unix timestamp (in seconds) for when the consent recording was created.
    pub created_at: u64,
}

/// Request to create a voice consent recording.
#[derive(Clone, Default, Debug, Builder, PartialEq)]
#[builder(name = "CreateVoiceConsentRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateVoiceConsentRequest {
    /// The label to use for this consent recording.
    pub name: String,
    /// The consent audio recording file. Maximum size is 10 MiB.
    /// Supported MIME types: `audio/mpeg`, `audio/wav`, `audio/x-wav`, `audio/ogg`,
    /// `audio/aac`, `audio/flac`, `audio/webm`, `audio/mp4`.
    pub recording: AudioInput,
    /// The BCP 47 language tag for the consent phrase (for example, `en-US`).
    pub language: String,
}

/// Request to update a voice consent recording (metadata only).
#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
#[builder(name = "UpdateVoiceConsentRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct UpdateVoiceConsentRequest {
    /// The updated label for this consent recording.
    pub name: String,
}

/// The voice consent deletion object.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct VoiceConsentDeletedResource {
    /// The consent recording identifier.
    pub id: String,
    /// The object type, which is always `audio.voice_consent`.
    pub object: String,
    /// Whether the consent recording was deleted.
    pub deleted: bool,
}

/// The voice consent list object.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct VoiceConsentListResource {
    /// The object type, which is always `list`.
    pub object: String,
    /// The list of voice consent recordings.
    pub data: Vec<VoiceConsentResource>,
    /// The ID of the first voice consent recording in the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub first_id: Option<String>,
    /// The ID of the last voice consent recording in the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_id: Option<String>,
    /// Whether there are more voice consent recordings available.
    pub has_more: bool,
}

/// A custom voice that can be used for audio output.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct VoiceResource {
    /// The object type, which is always `audio.voice`.
    pub object: String,
    /// The voice identifier, which can be referenced in API endpoints.
    pub id: String,
    /// The name of the voice.
    pub name: String,
    /// The Unix timestamp (in seconds) for when the voice was created.
    pub created_at: u64,
}

/// Request to create a custom voice.
#[derive(Clone, Default, Debug, Builder, PartialEq)]
#[builder(name = "CreateVoiceRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
pub struct CreateVoiceRequest {
    /// The name of the new voice.
    pub name: String,
    /// The sample audio recording file. Maximum size is 10 MiB.
    /// Supported MIME types: `audio/mpeg`, `audio/wav`, `audio/x-wav`, `audio/ogg`,
    /// `audio/aac`, `audio/flac`, `audio/webm`, `audio/mp4`.
    pub audio_sample: AudioInput,
    /// The consent recording ID (for example, `cons_1234`).
    pub consent: String,
}