跳至内容

Model

TTS模型设置 dataclass

TTS模型的设置。

Source code in src/agents/voice/model.py
@dataclass
class TTSModelSettings:
    """Settings for a TTS model."""

    voice: (
        Literal["alloy", "ash", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer"] | None
    ) = None
    """
    The voice to use for the TTS model. If not provided, the default voice for the respective model
    will be used.
    """

    buffer_size: int = 120
    """The minimal size of the chunks of audio data that are being streamed out."""

    dtype: npt.DTypeLike = np.int16
    """The data type for the audio data to be returned in."""

    transform_data: (
        Callable[[npt.NDArray[np.int16 | np.float32]], npt.NDArray[np.int16 | np.float32]] | None
    ) = None
    """
    A function to transform the data from the TTS model. This is useful if you want the resulting
    audio stream to have the data in a specific shape already.
    """

    instructions: str = (
        "You will receive partial sentences. Do not complete the sentence just read out the text."
    )
    """
    The instructions to use for the TTS model. This is useful if you want to control the tone of the
    audio output.
    """

    text_splitter: Callable[[str], tuple[str, str]] = get_sentence_based_splitter()
    """
    A function to split the text into chunks. This is useful if you want to split the text into
    chunks before sending it to the TTS model rather than waiting for the whole text to be
    processed.
    """

    speed: float | None = None
    """The speed with which the TTS model will read the text. Between 0.25 and 4.0."""

语音 class-attribute instance-attribute

voice: (
    Literal[
        "alloy",
        "ash",
        "coral",
        "echo",
        "fable",
        "onyx",
        "nova",
        "sage",
        "shimmer",
    ]
    | None
) = None

TTS模型使用的声音。如果未提供,则将使用相应模型的默认声音。

buffer_size class-attribute instance-attribute

buffer_size: int = 120

正在流式输出的音频数据块的最小大小。

数据类型 class-attribute instance-attribute

dtype: DTypeLike = int16

返回音频数据的数据类型。

transform_data class-attribute instance-attribute

transform_data: (
    Callable[
        [NDArray[int16 | float32]], NDArray[int16 | float32]
    ]
    | None
) = None

一个用于转换TTS模型数据的函数。如果您希望生成的音频流数据已经具有特定形状,这个函数会很有用。

使用说明 class-attribute instance-attribute

instructions: str = "You will receive partial sentences. Do not complete the sentence just read out the text."

用于TTS模型的指令。如果您想控制音频输出的音调,这将非常有用。

文本分割器 class-attribute instance-attribute

text_splitter: Callable[[str], tuple[str, str]] = (
    get_sentence_based_splitter()
)

一个将文本分割成块的函数。如果您想在将文本发送到TTS模型之前将其分割成块,而不是等待整个文本被处理,这将非常有用。

速度 class-attribute instance-attribute

speed: float | None = None

TTS模型朗读文本的速度。介于0.25到4.0之间。

TTSModel

基类: ABC

一个可以将文本转换为音频输出的文本转语音模型。

Source code in src/agents/voice/model.py
class TTSModel(abc.ABC):
    """A text-to-speech model that can convert text into audio output."""

    @property
    @abc.abstractmethod
    def model_name(self) -> str:
        """The name of the TTS model."""
        pass

    @abc.abstractmethod
    def run(self, text: str, settings: TTSModelSettings) -> AsyncIterator[bytes]:
        """Given a text string, produces a stream of audio bytes, in PCM format.

        Args:
            text: The text to convert to audio.

        Returns:
            An async iterator of audio bytes, in PCM format.
        """
        pass

模型名称 abstractmethod property

model_name: str

TTS模型的名称。

运行 abstractmethod

run(
    text: str, settings: TTSModelSettings
) -> AsyncIterator[bytes]

给定一个文本字符串,生成PCM格式的音频字节流。

参数:

名称 类型 描述 默认值
text str

要转换为音频的文本。

required

返回值:

类型 描述
AsyncIterator[bytes]

一个异步迭代器,用于生成PCM格式的音频字节。

Source code in src/agents/voice/model.py
@abc.abstractmethod
def run(self, text: str, settings: TTSModelSettings) -> AsyncIterator[bytes]:
    """Given a text string, produces a stream of audio bytes, in PCM format.

    Args:
        text: The text to convert to audio.

    Returns:
        An async iterator of audio bytes, in PCM format.
    """
    pass

StreamedTranscriptionSession

基类: ABC

音频输入的流式转录。

Source code in src/agents/voice/model.py
class StreamedTranscriptionSession(abc.ABC):
    """A streamed transcription of audio input."""

    @abc.abstractmethod
    def transcribe_turns(self) -> AsyncIterator[str]:
        """Yields a stream of text transcriptions. Each transcription is a turn in the conversation.

        This method is expected to return only after `close()` is called.
        """
        pass

    @abc.abstractmethod
    async def close(self) -> None:
        """Closes the session."""
        pass

transcribe_turns abstractmethod

transcribe_turns() -> AsyncIterator[str]

生成文本转录的流式输出。每次转录代表对话中的一个回合。

该方法预期仅在调用close()后返回。

Source code in src/agents/voice/model.py
@abc.abstractmethod
def transcribe_turns(self) -> AsyncIterator[str]:
    """Yields a stream of text transcriptions. Each transcription is a turn in the conversation.

    This method is expected to return only after `close()` is called.
    """
    pass

关闭 abstractmethod async

close() -> None

关闭会话。

Source code in src/agents/voice/model.py
@abc.abstractmethod
async def close(self) -> None:
    """Closes the session."""
    pass

STTModelSettings dataclass

语音转文字模型的设置。

Source code in src/agents/voice/model.py
@dataclass
class STTModelSettings:
    """Settings for a speech-to-text model."""

    prompt: str | None = None
    """Instructions for the model to follow."""

    language: str | None = None
    """The language of the audio input."""

    temperature: float | None = None
    """The temperature of the model."""

    turn_detection: dict[str, Any] | None = None
    """The turn detection settings for the model when using streamed audio input."""

提示词 class-attribute instance-attribute

prompt: str | None = None

模型需要遵循的指令。

语言 class-attribute instance-attribute

language: str | None = None

音频输入的语言。

temperature class-attribute instance-attribute

temperature: float | None = None

模型的温度参数。

转向检测 class-attribute instance-attribute

turn_detection: dict[str, Any] | None = None

使用流式音频输入时模型的语音停顿检测设置。

STTModel

基类: ABC

一个能将音频输入转换为文本的语音转文字模型。

Source code in src/agents/voice/model.py
class STTModel(abc.ABC):
    """A speech-to-text model that can convert audio input into text."""

    @property
    @abc.abstractmethod
    def model_name(self) -> str:
        """The name of the STT model."""
        pass

    @abc.abstractmethod
    async def transcribe(
        self,
        input: AudioInput,
        settings: STTModelSettings,
        trace_include_sensitive_data: bool,
        trace_include_sensitive_audio_data: bool,
    ) -> str:
        """Given an audio input, produces a text transcription.

        Args:
            input: The audio input to transcribe.
            settings: The settings to use for the transcription.
            trace_include_sensitive_data: Whether to include sensitive data in traces.
            trace_include_sensitive_audio_data: Whether to include sensitive audio data in traces.

        Returns:
            The text transcription of the audio input.
        """
        pass

    @abc.abstractmethod
    async def create_session(
        self,
        input: StreamedAudioInput,
        settings: STTModelSettings,
        trace_include_sensitive_data: bool,
        trace_include_sensitive_audio_data: bool,
    ) -> StreamedTranscriptionSession:
        """Creates a new transcription session, which you can push audio to, and receive a stream
        of text transcriptions.

        Args:
            input: The audio input to transcribe.
            settings: The settings to use for the transcription.
            trace_include_sensitive_data: Whether to include sensitive data in traces.
            trace_include_sensitive_audio_data: Whether to include sensitive audio data in traces.

        Returns:
            A new transcription session.
        """
        pass

模型名称 abstractmethod property

model_name: str

STT模型的名称。

转录 abstractmethod async

transcribe(
    input: AudioInput,
    settings: STTModelSettings,
    trace_include_sensitive_data: bool,
    trace_include_sensitive_audio_data: bool,
) -> str

给定音频输入,生成文本转录。

参数:

名称 类型 描述 默认值
input AudioInput

要转录的音频输入。

required
settings STTModelSettings

用于转录的设置。

required
trace_include_sensitive_data bool

是否在跟踪记录中包含敏感数据。

required
trace_include_sensitive_audio_data bool

是否在跟踪记录中包含敏感音频数据。

required

返回值:

类型 描述
str

音频输入的文本转录。

Source code in src/agents/voice/model.py
@abc.abstractmethod
async def transcribe(
    self,
    input: AudioInput,
    settings: STTModelSettings,
    trace_include_sensitive_data: bool,
    trace_include_sensitive_audio_data: bool,
) -> str:
    """Given an audio input, produces a text transcription.

    Args:
        input: The audio input to transcribe.
        settings: The settings to use for the transcription.
        trace_include_sensitive_data: Whether to include sensitive data in traces.
        trace_include_sensitive_audio_data: Whether to include sensitive audio data in traces.

    Returns:
        The text transcription of the audio input.
    """
    pass

create_session abstractmethod async

create_session(
    input: StreamedAudioInput,
    settings: STTModelSettings,
    trace_include_sensitive_data: bool,
    trace_include_sensitive_audio_data: bool,
) -> StreamedTranscriptionSession

创建一个新的转录会话,您可以向其推送音频,并接收文本转录的实时流。

参数:

名称 类型 描述 默认值
input StreamedAudioInput

要转录的音频输入。

required
settings STTModelSettings

用于转录的设置。

required
trace_include_sensitive_data bool

是否在跟踪记录中包含敏感数据。

required
trace_include_sensitive_audio_data bool

是否在跟踪记录中包含敏感音频数据。

required

返回值:

类型 描述
StreamedTranscriptionSession

新的转录会话。

Source code in src/agents/voice/model.py
@abc.abstractmethod
async def create_session(
    self,
    input: StreamedAudioInput,
    settings: STTModelSettings,
    trace_include_sensitive_data: bool,
    trace_include_sensitive_audio_data: bool,
) -> StreamedTranscriptionSession:
    """Creates a new transcription session, which you can push audio to, and receive a stream
    of text transcriptions.

    Args:
        input: The audio input to transcribe.
        settings: The settings to use for the transcription.
        trace_include_sensitive_data: Whether to include sensitive data in traces.
        trace_include_sensitive_audio_data: Whether to include sensitive audio data in traces.

    Returns:
        A new transcription session.
    """
    pass

语音模型提供者

基类: ABC

语音模型提供者的基础接口。

模型提供者负责根据给定的名称创建语音转文本和文本转语音模型。

Source code in src/agents/voice/model.py
class VoiceModelProvider(abc.ABC):
    """The base interface for a voice model provider.

    A model provider is responsible for creating speech-to-text and text-to-speech models, given a
    name.
    """

    @abc.abstractmethod
    def get_stt_model(self, model_name: str | None) -> STTModel:
        """Get a speech-to-text model by name.

        Args:
            model_name: The name of the model to get.

        Returns:
            The speech-to-text model.
        """
        pass

    @abc.abstractmethod
    def get_tts_model(self, model_name: str | None) -> TTSModel:
        """Get a text-to-speech model by name."""

get_stt_model abstractmethod

get_stt_model(model_name: str | None) -> STTModel

通过名称获取语音转文字模型。

参数:

名称 类型 描述 默认值
model_name str | None

要获取的模型名称。

required

返回值:

类型 描述
STTModel

语音转文本模型。

Source code in src/agents/voice/model.py
@abc.abstractmethod
def get_stt_model(self, model_name: str | None) -> STTModel:
    """Get a speech-to-text model by name.

    Args:
        model_name: The name of the model to get.

    Returns:
        The speech-to-text model.
    """
    pass

get_tts_model abstractmethod

get_tts_model(model_name: str | None) -> TTSModel

通过名称获取文本转语音模型。

Source code in src/agents/voice/model.py
@abc.abstractmethod
def get_tts_model(self, model_name: str | None) -> TTSModel:
    """Get a text-to-speech model by name."""