Skip to content

Commit a8f7b20

Browse files
committed
openai-dialog: Implement protocol specific ways to specify the voice
1 parent cf2ff7e commit a8f7b20

4 files changed

Lines changed: 49 additions & 21 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ openai-api-rs = { workspace = true }
8181
serde_json = { workspace = true }
8282
chrono-tz = { version = "0.10.3" }
8383
url = { workspace = true }
84+
strum = { version = "0.28" }
8485

8586

8687
# For recognizing audio files in azure-transcribe.

examples/openai-dialog.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010

1111
use anyhow::{Context, Result, bail};
1212
use chrono::Utc;
13+
use clap::builder::{PossibleValuesParser, TypedValueParser};
1314
use clap::{Parser, ValueEnum};
1415
use context_switch::{InputModality, OutputModality};
1516
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
@@ -34,6 +35,8 @@ struct Cli {
3435
protocol: Option<CliProtocol>,
3536
#[arg(long)]
3637
endpoint: Option<String>,
38+
#[arg(long, value_parser = realtime_voice_value_parser())]
39+
voice: Option<types::RealtimeVoice>,
3740
}
3841

3942
#[derive(Debug, Clone, Copy, ValueEnum)]
@@ -43,6 +46,19 @@ enum CliProtocol {
4346
Azure,
4447
}
4548

49+
fn realtime_voice_value_parser() -> impl TypedValueParser<Value = types::RealtimeVoice> {
50+
PossibleValuesParser::new(<types::RealtimeVoice as strum::VariantNames>::VARIANTS).try_map(
51+
|value| {
52+
parse_realtime_voice_value(&value)
53+
.map_err(|e| format!("Invalid voice value `{value}`: {e}"))
54+
},
55+
)
56+
}
57+
58+
fn parse_realtime_voice_value(value: &str) -> Result<types::RealtimeVoice, strum::ParseError> {
59+
types::RealtimeVoice::from_str(value)
60+
}
61+
4662
impl From<CliProtocol> for Protocol {
4763
fn from(value: CliProtocol) -> Self {
4864
match value {
@@ -107,6 +123,7 @@ async fn main() -> Result<()> {
107123
.or_else(|| env::var("OPENAI_REALTIME_ENDPOINT").ok())
108124
.filter(|endpoint| !endpoint.trim().is_empty());
109125
params.protocol = cli.protocol.map(Into::into);
126+
params.voice = cli.voice;
110127
params.tools.push(get_time_function_definition());
111128

112129
let (output_sender, output_receiver) = unbounded_channel();

external/openai-api-rs

services/openai-dialog/src/lib.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,21 @@ impl Client {
352352
};
353353

354354
if let Some(voice) = params.voice {
355-
let mut audio = session.audio.unwrap_or(types::AudioConfig {
356-
input: None,
357-
output: None,
358-
});
359-
let mut output = audio.output.unwrap_or(types::AudioOutput {
360-
format: None,
361-
speed: 1.0,
362-
voice: None,
363-
});
364-
output.voice = Some(voice);
365-
audio.output = Some(output);
366-
session.audio = Some(audio);
355+
match self.protocol {
356+
RealtimeProtocol::OpenAI => {
357+
session.voice = Some(voice);
358+
}
359+
RealtimeProtocol::Azure => {
360+
session.audio = Some(types::AudioConfig {
361+
input: None,
362+
output: Some(types::AudioOutput {
363+
format: None,
364+
speed: 1.0,
365+
voice: Some(voice),
366+
}),
367+
});
368+
}
369+
}
367370
send_update = true;
368371
}
369372

@@ -554,17 +557,24 @@ impl Client {
554557
tools,
555558
tool_choice,
556559
} => {
557-
let audio = voice.map(|voice| types::AudioConfig {
558-
input: None,
559-
output: Some(types::AudioOutput {
560-
format: None,
561-
speed: 1.0,
562-
voice: Some(voice),
563-
}),
564-
});
560+
let (voice, audio) = match self.protocol {
561+
RealtimeProtocol::OpenAI => (voice, None),
562+
RealtimeProtocol::Azure => {
563+
let audio = voice.map(|voice| types::AudioConfig {
564+
input: None,
565+
output: Some(types::AudioOutput {
566+
format: None,
567+
speed: 1.0,
568+
voice: Some(voice),
569+
}),
570+
});
571+
(None, audio)
572+
}
573+
};
565574

566575
let session = types::RealtimeSession {
567576
instructions,
577+
voice,
568578
audio,
569579
tools,
570580
tool_choice,

0 commit comments

Comments
 (0)