async_openai/types/audio/
stream.rs1use std::pin::Pin;
2
3use futures::Stream;
4use serde::{Deserialize, Serialize};
5
6use crate::{
7 error::OpenAIError,
8 traits::EventType,
9 types::{audio::TranscriptTextUsageTokens, LogProbProperties},
10};
11
12#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
13#[serde(tag = "type", rename_all = "snake_case")]
14pub enum CreateSpeechResponseStreamEvent {
15 #[serde(rename = "speech.audio.delta")]
17 SpeechAudioDelta(SpeechAudioDeltaEvent),
18 #[serde(rename = "speech.audio.done")]
20 SpeechAudioDone(SpeechAudioDoneEvent),
21}
22
23#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
24pub struct SpeechAudioDeltaEvent {
25 pub audio: String,
27}
28
29#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
30pub struct SpeechUsage {
31 pub input_tokens: u32,
33 pub output_tokens: u32,
35 pub total_tokens: u32,
37}
38
39#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
40pub struct SpeechAudioDoneEvent {
41 pub usage: SpeechUsage,
43}
44
45pub type SpeechResponseStream =
47 Pin<Box<dyn Stream<Item = Result<CreateSpeechResponseStreamEvent, OpenAIError>> + Send>>;
48
49#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
50pub struct TranscriptionTextSegmentEvent {
51 pub id: String,
53 pub start: f32,
55 pub end: f32,
57 pub text: String,
59 pub speaker: String,
61}
62
63#[derive(Debug, Serialize, Deserialize, Clone)]
64pub struct TranscriptionTextDeltaEvent {
65 pub delta: String,
67 pub logprobs: Option<Vec<LogProbProperties>>,
71 pub segment_id: Option<String>,
74}
75
76#[derive(Debug, Serialize, Deserialize, Clone)]
77pub struct TranscriptionTextDoneEvent {
78 pub text: String,
80 pub logprobs: Option<Vec<LogProbProperties>>,
84 pub usage: TranscriptTextUsageTokens,
86}
87
88#[derive(Debug, Serialize, Deserialize, Clone)]
89#[serde(tag = "type")]
90pub enum CreateTranscriptionResponseStreamEvent {
91 #[serde(rename = "transcript.text.segment")]
95 TranscriptTextSegment(TranscriptionTextSegmentEvent),
96 #[serde(rename = "transcript.text.delta")]
97 TranscriptTextDelta(TranscriptionTextDeltaEvent),
98 #[serde(rename = "transcript.text.done")]
102 TranscriptTextDone(TranscriptionTextDoneEvent),
103}
104
105pub type TranscriptionResponseStream =
106 Pin<Box<dyn Stream<Item = Result<CreateTranscriptionResponseStreamEvent, OpenAIError>> + Send>>;
107
108impl EventType for SpeechAudioDeltaEvent {
109 fn event_type(&self) -> &'static str {
110 "speech.audio.delta"
111 }
112}
113
114impl EventType for SpeechAudioDoneEvent {
115 fn event_type(&self) -> &'static str {
116 "speech.audio.done"
117 }
118}
119
120impl EventType for CreateSpeechResponseStreamEvent {
121 fn event_type(&self) -> &'static str {
122 match self {
123 CreateSpeechResponseStreamEvent::SpeechAudioDelta(event) => event.event_type(),
124 CreateSpeechResponseStreamEvent::SpeechAudioDone(event) => event.event_type(),
125 }
126 }
127}
128
129impl EventType for TranscriptionTextSegmentEvent {
130 fn event_type(&self) -> &'static str {
131 "transcript.text.segment"
132 }
133}
134
135impl EventType for TranscriptionTextDeltaEvent {
136 fn event_type(&self) -> &'static str {
137 "transcript.text.delta"
138 }
139}
140
141impl EventType for TranscriptionTextDoneEvent {
142 fn event_type(&self) -> &'static str {
143 "transcript.text.done"
144 }
145}
146
147impl EventType for CreateTranscriptionResponseStreamEvent {
148 fn event_type(&self) -> &'static str {
149 match self {
150 CreateTranscriptionResponseStreamEvent::TranscriptTextSegment(event) => {
151 event.event_type()
152 }
153 CreateTranscriptionResponseStreamEvent::TranscriptTextDelta(event) => {
154 event.event_type()
155 }
156 CreateTranscriptionResponseStreamEvent::TranscriptTextDone(event) => event.event_type(),
157 }
158 }
159}