Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Model

RealtimePlaybackState

Bases:TypedDict

Source code insrc/agents/realtime/model.py
classRealtimePlaybackState(TypedDict):current_item_id:str|None"""The item ID of the current item being played."""current_item_content_index:int|None"""The index of the current item content being played."""elapsed_ms:float|None"""The number of milliseconds of audio that have been played."""

current_item_idinstance-attribute

current_item_id:str|None

The item ID of the current item being played.

current_item_content_indexinstance-attribute

current_item_content_index:int|None

The index of the current item content being played.

elapsed_msinstance-attribute

elapsed_ms:float|None

The number of milliseconds of audio that have been played.

RealtimePlaybackTracker

If you have custom playback logic or expect that audio is played with delays or at differentspeeds, create an instance of RealtimePlaybackTracker and pass it to the session. You areresponsible for tracking the audio playback progress and callingon_play_bytes oron_play_ms when the user has played some audio.

Source code insrc/agents/realtime/model.py
classRealtimePlaybackTracker:"""If you have custom playback logic or expect that audio is played with delays or at different    speeds, create an instance of RealtimePlaybackTracker and pass it to the session. You are    responsible for tracking the audio playback progress and calling `on_play_bytes` or    `on_play_ms` when the user has played some audio."""def__init__(self)->None:self._format:RealtimeAudioFormat|None=None# (item_id, item_content_index)self._current_item:tuple[str,int]|None=Noneself._elapsed_ms:float|None=Nonedefon_play_bytes(self,item_id:str,item_content_index:int,bytes:bytes)->None:"""Called by you when you have played some audio.        Args:            item_id: The item ID of the audio being played.            item_content_index: The index of the audio content in `item.content`            bytes: The audio bytes that have been fully played.        """ms=calculate_audio_length_ms(self._format,bytes)self.on_play_ms(item_id,item_content_index,ms)defon_play_ms(self,item_id:str,item_content_index:int,ms:float)->None:"""Called by you when you have played some audio.        Args:            item_id: The item ID of the audio being played.            item_content_index: The index of the audio content in `item.content`            ms: The number of milliseconds of audio that have been played.        """ifself._current_item!=(item_id,item_content_index):self._current_item=(item_id,item_content_index)self._elapsed_ms=mselse:assertself._elapsed_msisnotNoneself._elapsed_ms+=msdefon_interrupted(self)->None:"""Called by the model when the audio playback has been interrupted."""self._current_item=Noneself._elapsed_ms=Nonedefset_audio_format(self,format:RealtimeAudioFormat)->None:"""Will be called by the model to set the audio format.        Args:            format: The audio format to use.        """self._format=formatdefget_state(self)->RealtimePlaybackState:"""Will be called by the model to get the current playback state."""ifself._current_itemisNone:return{"current_item_id":None,"current_item_content_index":None,"elapsed_ms":None,}assertself._elapsed_msisnotNoneitem_id,item_content_index=self._current_itemreturn{"current_item_id":item_id,"current_item_content_index":item_content_index,"elapsed_ms":self._elapsed_ms,}

on_play_bytes

on_play_bytes(item_id:str,item_content_index:int,bytes:bytes)->None

Called by you when you have played some audio.

Parameters:

NameTypeDescriptionDefault
item_idstr

The item ID of the audio being played.

required
item_content_indexint

The index of the audio content initem.content

required
bytesbytes

The audio bytes that have been fully played.

required
Source code insrc/agents/realtime/model.py
defon_play_bytes(self,item_id:str,item_content_index:int,bytes:bytes)->None:"""Called by you when you have played some audio.    Args:        item_id: The item ID of the audio being played.        item_content_index: The index of the audio content in `item.content`        bytes: The audio bytes that have been fully played.    """ms=calculate_audio_length_ms(self._format,bytes)self.on_play_ms(item_id,item_content_index,ms)

on_play_ms

on_play_ms(item_id:str,item_content_index:int,ms:float)->None

Called by you when you have played some audio.

Parameters:

NameTypeDescriptionDefault
item_idstr

The item ID of the audio being played.

required
item_content_indexint

The index of the audio content initem.content

required
msfloat

The number of milliseconds of audio that have been played.

required
Source code insrc/agents/realtime/model.py
defon_play_ms(self,item_id:str,item_content_index:int,ms:float)->None:"""Called by you when you have played some audio.    Args:        item_id: The item ID of the audio being played.        item_content_index: The index of the audio content in `item.content`        ms: The number of milliseconds of audio that have been played.    """ifself._current_item!=(item_id,item_content_index):self._current_item=(item_id,item_content_index)self._elapsed_ms=mselse:assertself._elapsed_msisnotNoneself._elapsed_ms+=ms

on_interrupted

on_interrupted()->None

Called by the model when the audio playback has been interrupted.

Source code insrc/agents/realtime/model.py
defon_interrupted(self)->None:"""Called by the model when the audio playback has been interrupted."""self._current_item=Noneself._elapsed_ms=None

set_audio_format

set_audio_format(format:RealtimeAudioFormat)->None

Will be called by the model to set the audio format.

Parameters:

NameTypeDescriptionDefault
formatRealtimeAudioFormat

The audio format to use.

required
Source code insrc/agents/realtime/model.py
defset_audio_format(self,format:RealtimeAudioFormat)->None:"""Will be called by the model to set the audio format.    Args:        format: The audio format to use.    """self._format=format

get_state

Will be called by the model to get the current playback state.

Source code insrc/agents/realtime/model.py
defget_state(self)->RealtimePlaybackState:"""Will be called by the model to get the current playback state."""ifself._current_itemisNone:return{"current_item_id":None,"current_item_content_index":None,"elapsed_ms":None,}assertself._elapsed_msisnotNoneitem_id,item_content_index=self._current_itemreturn{"current_item_id":item_id,"current_item_content_index":item_content_index,"elapsed_ms":self._elapsed_ms,}

RealtimeModelListener

Bases:ABC

A listener for realtime transport events.

Source code insrc/agents/realtime/model.py
classRealtimeModelListener(abc.ABC):"""A listener for realtime transport events."""@abc.abstractmethodasyncdefon_event(self,event:RealtimeModelEvent)->None:"""Called when an event is emitted by the realtime transport."""pass

on_eventabstractmethodasync

on_event(event:RealtimeModelEvent)->None

Called when an event is emitted by the realtime transport.

Source code insrc/agents/realtime/model.py
@abc.abstractmethodasyncdefon_event(self,event:RealtimeModelEvent)->None:"""Called when an event is emitted by the realtime transport."""pass

RealtimeModelConfig

Bases:TypedDict

Options for connecting to a realtime model.

Source code insrc/agents/realtime/model.py
classRealtimeModelConfig(TypedDict):"""Options for connecting to a realtime model."""api_key:NotRequired[str|Callable[[],MaybeAwaitable[str]]]"""The API key (or function that returns a key) to use when connecting. If unset, the model will    try to use a sane default. For example, the OpenAI Realtime model will try to use the    `OPENAI_API_KEY`  environment variable.    """url:NotRequired[str]"""The URL to use when connecting. If unset, the model will use a sane default. For example,    the OpenAI Realtime model will use the default OpenAI WebSocket URL.    """headers:NotRequired[dict[str,str]]"""The headers to use when connecting. If unset, the model will use a sane default.    Note that, when you set this, authorization header won't be set under the hood.    e.g., {"api-key": "your api key here"} for Azure OpenAI Realtime WebSocket connections.    """initial_model_settings:NotRequired[RealtimeSessionModelSettings]"""The initial model settings to use when connecting."""playback_tracker:NotRequired[RealtimePlaybackTracker]"""The playback tracker to use when tracking audio playback progress. If not set, the model will    use a default implementation that assumes audio is played immediately, at realtime speed.    A playback tracker is useful for interruptions. The model generates audio much faster than    realtime playback speed. So if there's an interruption, its useful for the model to know how    much of the audio has been played by the user. In low-latency scenarios, it's fine to assume    that audio is played back immediately at realtime speed. But in scenarios like phone calls or    other remote interactions, you can set a playback tracker that lets the model know when audio    is played to the user.    """call_id:NotRequired[str]"""Attach to an existing realtime call instead of creating a new session.    When provided, the transport connects using the `call_id` query string parameter rather than a    model name. This is used for SIP-originated calls that are accepted via the Realtime Calls API.    """

api_keyinstance-attribute

api_key:NotRequired[str|Callable[[],MaybeAwaitable[str]]]

The API key (or function that returns a key) to use when connecting. If unset, the model willtry to use a sane default. For example, the OpenAI Realtime model will try to use theOPENAI_API_KEY environment variable.

urlinstance-attribute

url:NotRequired[str]

The URL to use when connecting. If unset, the model will use a sane default. For example,the OpenAI Realtime model will use the default OpenAI WebSocket URL.

headersinstance-attribute

headers:NotRequired[dict[str,str]]

The headers to use when connecting. If unset, the model will use a sane default.Note that, when you set this, authorization header won't be set under the hood.e.g., {"api-key": "your api key here"} for Azure OpenAI Realtime WebSocket connections.

initial_model_settingsinstance-attribute

initial_model_settings:NotRequired[RealtimeSessionModelSettings]

The initial model settings to use when connecting.

playback_trackerinstance-attribute

playback_tracker:NotRequired[RealtimePlaybackTracker]

The playback tracker to use when tracking audio playback progress. If not set, the model willuse a default implementation that assumes audio is played immediately, at realtime speed.

A playback tracker is useful for interruptions. The model generates audio much faster thanrealtime playback speed. So if there's an interruption, its useful for the model to know howmuch of the audio has been played by the user. In low-latency scenarios, it's fine to assumethat audio is played back immediately at realtime speed. But in scenarios like phone calls orother remote interactions, you can set a playback tracker that lets the model know when audiois played to the user.

call_idinstance-attribute

call_id:NotRequired[str]

Attach to an existing realtime call instead of creating a new session.

When provided, the transport connects using thecall_id query string parameter rather than amodel name. This is used for SIP-originated calls that are accepted via the Realtime Calls API.

RealtimeModel

Bases:ABC

Interface for connecting to a realtime model and sending/receiving events.

Source code insrc/agents/realtime/model.py
classRealtimeModel(abc.ABC):"""Interface for connecting to a realtime model and sending/receiving events."""@abc.abstractmethodasyncdefconnect(self,options:RealtimeModelConfig)->None:"""Establish a connection to the model and keep it alive."""pass@abc.abstractmethoddefadd_listener(self,listener:RealtimeModelListener)->None:"""Add a listener to the model."""pass@abc.abstractmethoddefremove_listener(self,listener:RealtimeModelListener)->None:"""Remove a listener from the model."""pass@abc.abstractmethodasyncdefsend_event(self,event:RealtimeModelSendEvent)->None:"""Send an event to the model."""pass@abc.abstractmethodasyncdefclose(self)->None:"""Close the session."""pass

connectabstractmethodasync

connect(options:RealtimeModelConfig)->None

Establish a connection to the model and keep it alive.

Source code insrc/agents/realtime/model.py
@abc.abstractmethodasyncdefconnect(self,options:RealtimeModelConfig)->None:"""Establish a connection to the model and keep it alive."""pass

add_listenerabstractmethod

add_listener(listener:RealtimeModelListener)->None

Add a listener to the model.

Source code insrc/agents/realtime/model.py
@abc.abstractmethoddefadd_listener(self,listener:RealtimeModelListener)->None:"""Add a listener to the model."""pass

remove_listenerabstractmethod

remove_listener(listener:RealtimeModelListener)->None

Remove a listener from the model.

Source code insrc/agents/realtime/model.py
@abc.abstractmethoddefremove_listener(self,listener:RealtimeModelListener)->None:"""Remove a listener from the model."""pass

send_eventabstractmethodasync

send_event(event:RealtimeModelSendEvent)->None

Send an event to the model.

Source code insrc/agents/realtime/model.py
@abc.abstractmethodasyncdefsend_event(self,event:RealtimeModelSendEvent)->None:"""Send an event to the model."""pass

closeabstractmethodasync

close()->None

Close the session.

Source code insrc/agents/realtime/model.py
@abc.abstractmethodasyncdefclose(self)->None:"""Close the session."""pass

[8]ページ先頭

©2009-2025 Movatter.jp