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}
76
77#[derive(Debug, Serialize, Deserialize, Clone)]
78#[serde(tag = "type")]
79pub enum CreateTranscriptionResponseStreamEvent {
80 #[serde(rename = "transcript.text.segment")]
84 TranscriptTextSegment(TranscriptionTextSegmentEvent),
85 #[serde(rename = "transcript.text.delta")]
86 TranscriptTextDelta(TranscriptionTextDeltaEvent),
87 #[serde(rename = "transcript.text.done")]
91 TranscriptTextDone(TranscriptionTextDoneEvent),
92}
93
94#[cfg(feature = "_api")]
95pub type TranscriptionResponseStream =
96 crate::types::stream::StreamResponse<CreateTranscriptionResponseStreamEvent>;
97
98#[cfg(feature = "_api")]
100pub type SpeechResponseStream =
101 crate::types::stream::StreamResponse<CreateSpeechResponseStreamEvent>;
102
103#[cfg(feature = "_api")]
104macro_rules! impl_event_type {
105 ($($ty:ty => $event_type:expr),* $(,)?) => {
106 $(
107 impl crate::traits::EventType for $ty {
108 fn event_type(&self) -> &'static str {
109 $event_type
110 }
111 }
112 )*
113 };
114}
115
116#[cfg(feature = "_api")]
117impl_event_type! {
118 SpeechAudioDeltaEvent => "speech.audio.delta",
119 SpeechAudioDoneEvent => "speech.audio.done",
120 TranscriptionTextSegmentEvent => "transcript.text.segment",
121 TranscriptionTextDeltaEvent => "transcript.text.delta",
122 TranscriptionTextDoneEvent => "transcript.text.done",
123}
124
125#[cfg(feature = "_api")]
126impl crate::traits::EventType for CreateSpeechResponseStreamEvent {
127 fn event_type(&self) -> &'static str {
128 match self {
129 CreateSpeechResponseStreamEvent::SpeechAudioDelta(event) => event.event_type(),
130 CreateSpeechResponseStreamEvent::SpeechAudioDone(event) => event.event_type(),
131 }
132 }
133}
134
135#[cfg(feature = "_api")]
136impl crate::traits::EventType for CreateTranscriptionResponseStreamEvent {
137 fn event_type(&self) -> &'static str {
138 match self {
139 CreateTranscriptionResponseStreamEvent::TranscriptTextSegment(event) => {
140 event.event_type()
141 }
142 CreateTranscriptionResponseStreamEvent::TranscriptTextDelta(event) => {
143 event.event_type()
144 }
145 CreateTranscriptionResponseStreamEvent::TranscriptTextDone(event) => event.event_type(),
146 }
147 }
148}