Skip to main content

RealtimeClient

Struct RealtimeClient 

Source
pub struct RealtimeClient { /* private fields */ }
Expand description

Builder for creating Realtime API connections.

§Example

use openai_tools::realtime::{RealtimeClient, Modality, Voice};
use openai_tools::common::models::RealtimeModel;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = RealtimeClient::new();
    client
        .model(RealtimeModel::Gpt4oRealtimePreview)
        .modalities(vec![Modality::Text, Modality::Audio])
        .voice(Voice::Alloy)
        .instructions("You are a helpful assistant.");

    let mut session = client.connect().await?;
    // Use session...
    session.close().await?;
    Ok(())
}

Implementations§

Source§

impl RealtimeClient

Source

pub fn new() -> Self

Create a new RealtimeClient for OpenAI API.

Loads the API key from the OPENAI_API_KEY environment variable.

Source

pub fn with_auth(auth: AuthProvider) -> Self

Create a new RealtimeClient with a custom authentication provider.

Source

pub fn azure() -> Result<Self>

Create a new RealtimeClient for Azure OpenAI API.

Source

pub fn detect_provider() -> Result<Self>

Create a new RealtimeClient by auto-detecting the provider.

Source

pub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self

Creates a new RealtimeClient with URL-based provider detection.

Source

pub fn from_url<S: Into<String>>(url: S) -> Result<Self>

Creates a new RealtimeClient from URL using environment variables.

Source

pub fn with_api_key(api_key: impl Into<String>) -> Self

👎Deprecated since 0.3.0: Use with_auth(AuthProvider::OpenAI(...)) instead

Create a new RealtimeClient with an explicit API key.

Source

pub fn auth(&self) -> &AuthProvider

Returns the authentication provider.

Source

pub fn model(&mut self, model: RealtimeModel) -> &mut Self

Set the model for the Realtime API.

§Example
use openai_tools::realtime::RealtimeClient;
use openai_tools::common::models::RealtimeModel;

let mut client = RealtimeClient::new();
client.model(RealtimeModel::Gpt4oRealtimePreview);
Source

pub fn model_id(&mut self, model_id: impl Into<String>) -> &mut Self

👎Deprecated since 0.2.0: Use model(RealtimeModel) instead for type safety

Set the model using a string ID (for backward compatibility).

Prefer using [model] with RealtimeModel enum for type safety.

Source

pub fn modalities(&mut self, modalities: Vec<Modality>) -> &mut Self

Set the supported modalities.

Source

pub fn instructions(&mut self, instructions: impl Into<String>) -> &mut Self

Set the system instructions.

Source

pub fn voice(&mut self, voice: Voice) -> &mut Self

Set the voice for audio output.

Source

pub fn input_audio_format(&mut self, format: AudioFormat) -> &mut Self

Set the input audio format.

Source

pub fn output_audio_format(&mut self, format: AudioFormat) -> &mut Self

Set the output audio format.

Source

pub fn enable_transcription(&mut self, model: TranscriptionModel) -> &mut Self

Enable input audio transcription.

Source

pub fn transcription(&mut self, config: InputAudioTranscription) -> &mut Self

Set input audio transcription configuration.

Source

pub fn server_vad(&mut self, config: ServerVadConfig) -> &mut Self

Set Server VAD turn detection.

Source

pub fn semantic_vad(&mut self, config: SemanticVadConfig) -> &mut Self

Set Semantic VAD turn detection.

Source

pub fn disable_turn_detection(&mut self) -> &mut Self

Disable turn detection (manual mode).

Source

pub fn tools(&mut self, tools: Vec<Tool>) -> &mut Self

Set available tools for function calling.

Accepts Tool from the common module and converts to RealtimeTool.

Source

pub fn realtime_tools(&mut self, tools: Vec<RealtimeTool>) -> &mut Self

Set available realtime tools directly.

Source

pub fn temperature(&mut self, temp: f32) -> &mut Self

Set the sampling temperature.

Source

pub async fn connect(&self) -> Result<RealtimeSession>

Connect to the Realtime API.

Returns a RealtimeSession for sending and receiving events.

Trait Implementations§

Source§

impl Clone for RealtimeClient

Source§

fn clone(&self) -> RealtimeClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RealtimeClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RealtimeClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more