async_openai/types/audio/
stream.rs1use serde::{Deserialize, Serialize};
2
3use crate::types::audio::{LogProbProperties, TranscriptTextUsageTokens};
4
5#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
6#[serde(tag = "type", rename_all = "snake_case")]
7pub enum CreateSpeechResponseStreamEvent {
8 #[serde(rename = "speech.audio.delta")]
10 SpeechAudioDelta(SpeechAudioDeltaEvent),
11 #[serde(rename = "speech.audio.done")]
13 SpeechAudioDone(SpeechAudioDoneEvent),
14}
15
16#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
17pub struct SpeechAudioDeltaEvent {
18 pub audio: String,
20}
21
22#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
23pub struct SpeechUsage {
24 pub input_tokens: u32,
26 pub output_tokens: u32,
28 pub total_tokens: u32,
30}
31
32#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
33pub struct SpeechAudioDoneEvent {
34 pub usage: SpeechUsage,
36}
37
38#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
39pub struct TranscriptionTextSegmentEvent {
40 pub id: String,
42 pub start: f32,
44 pub end: f32,
46 pub text: String,
48 pub speaker: String,
50}
51
52#[derive(Debug, Serialize, Deserialize, Clone)]
53pub struct TranscriptionTextDeltaEvent {
54 pub delta: String,
56 pub logprobs: Option<Vec<LogProbProperties>>,
60 pub segment_id: Option<String>,
63}
64
65#[derive(Debug, Serialize, Deserialize, Clone)]
66pub struct TranscriptionTextDoneEvent {
67 pub text: String,
69 pub logprobs: Option<Vec<LogProbProperties>>,
73 pub usage: TranscriptTextUsageTokens,
75 #[serde(skip_serializing_if = "Option::is_none")]
78 pub languages: Option<Vec<crate::types::audio::TranscriptionLanguage>>,
79}
80
81#[derive(Debug, Serialize, Deserialize, Clone)]
82#[serde(tag = "type")]
83pub enum CreateTranscriptionResponseStreamEvent {
84 #[serde(rename = "transcript.text.segment")]
88 TranscriptTextSegment(TranscriptionTextSegmentEvent),
89 #[serde(rename = "transcript.text.delta")]
90 TranscriptTextDelta(TranscriptionTextDeltaEvent),
91 #[serde(rename = "transcript.text.done")]
95 TranscriptTextDone(TranscriptionTextDoneEvent),
96}
97
98#[cfg(feature = "_api")]
99pub type TranscriptionResponseStream =
100 crate::types::stream::StreamResponse<CreateTranscriptionResponseStreamEvent>;
101
102#[cfg(feature = "_api")]
104pub type SpeechResponseStream =
105 crate::types::stream::StreamResponse<CreateSpeechResponseStreamEvent>;
106
107#[cfg(feature = "_api")]
108macro_rules! impl_event_type {
109 ($($ty:ty => $event_type:expr),* $(,)?) => {
110 $(
111 impl crate::traits::EventType for $ty {
112 fn event_type(&self) -> &'static str {
113 $event_type
114 }
115 }
116 )*
117 };
118}
119
120#[cfg(feature = "_api")]
121impl_event_type! {
122 SpeechAudioDeltaEvent => "speech.audio.delta",
123 SpeechAudioDoneEvent => "speech.audio.done",
124 TranscriptionTextSegmentEvent => "transcript.text.segment",
125 TranscriptionTextDeltaEvent => "transcript.text.delta",
126 TranscriptionTextDoneEvent => "transcript.text.done",
127}
128
129#[cfg(feature = "_api")]
130impl crate::traits::EventType for CreateSpeechResponseStreamEvent {
131 fn event_type(&self) -> &'static str {
132 match self {
133 CreateSpeechResponseStreamEvent::SpeechAudioDelta(event) => event.event_type(),
134 CreateSpeechResponseStreamEvent::SpeechAudioDone(event) => event.event_type(),
135 }
136 }
137}
138
139#[cfg(feature = "_api")]
140impl crate::traits::EventType for CreateTranscriptionResponseStreamEvent {
141 fn event_type(&self) -> &'static str {
142 match self {
143 CreateTranscriptionResponseStreamEvent::TranscriptTextSegment(event) => {
144 event.event_type()
145 }
146 CreateTranscriptionResponseStreamEvent::TranscriptTextDelta(event) => {
147 event.event_type()
148 }
149 CreateTranscriptionResponseStreamEvent::TranscriptTextDone(event) => event.event_type(),
150 }
151 }
152}