AS AgentScopeAPI 手册 / API Reference
类class agentscope.tts

TTSModelBase

class TTSModelBase(ABC)
源码Source agentscope/tts/_tts_base.py:18–212

TTS 模型,使用 AgentScope的基类。位于 agentscope.tts 包,继承自 ABC。核心成员包括 connect、close、push、list_models、synthesize。

This base class provides a unified abstraction for both non-realtime and realtime (streaming-input) TTS models, governed by the realtime flag.

For non-realtime TTS models, only :meth:synthesize needs to be implemented. For realtime TTS models, the lifecycle is managed via the async context manager (async with model: ...) or by calling :meth:connect / :meth:close manually; :meth:push appends text chunks and returns whatever audio is currently available, while :meth:synthesize blocks until the full speech has been synthesized.

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
Parameters类class
参数,处理低延迟的实时语音与多模态交互。
Base parameter schema for TTS models. Subclasses should override this with provider-specific parameters.

构造器摘要Constructor Summary

构造器Constructor说明Description
def __init__(credential: CredentialBase, model: str, parameters: BaseModel | None = None, stream: bool = True) -> None
初始化 TTS 模型基类。
Initialize the TTS model base class.

属性摘要Attribute Summary

属性Attribute类型Type说明Description值Value
credentialCredentialBase
凭证字段。
—
modelstr
模型字段。
—
parametersBaseModel
参数字段。
—
streambool
流字段。
—
realtimebool
实时字段。
False

方法摘要Method Summary

方法Method说明Description
async def __aenter__() -> 'TTSModelBase'async
进入异步上下文并返回自身。
Enter the async context manager and initialize resources if needed.
async def __aexit__(exc_type: Any, exc_value: Any, traceback: Any) -> Noneasync
退出异步上下文并清理资源。
Exit the async context manager and clean up resources if needed.
async def connect() -> Noneasync
建立连接并完成初始化。
Connect to the TTS model and initialize resources.
async def close() -> Noneasync
关闭连接并释放相关资源。
Close the connection to the TTS model and clean up resources.
async def push(text: str, **kwargs: Any) -> TTSResponseasync
执行推送相关操作。
Append text to be synthesized and return the received TTS response. This method is non-blocking and may return an empty response if no audio is available yet.
def list_models(custom_yaml_dir: str | None = None) -> list['TTSModelCard']
列出模型。
List candidate TTS models by scanning YAML model cards.
async def synthesize(text: str | None = None, **kwargs: Any) -> TTSResponse | AsyncGenerator[TTSResponse, None]asyncabstractabstractmethod
执行 `synthesize` 方法。
Synthesize speech from the appended text. Different from :meth:`push`, this method blocks until the full speech has been synthesized.

构造器详情Constructor Detail

TTSModelBase

def __init__(credential: CredentialBase, model: str, parameters: BaseModel | None = None, stream: bool = True) -> None

初始化 TTS 模型基类。

Initialize the TTS model base class.

参数Parameters
名称Name类型Type说明Description
credentialCredentialBase凭证对象(类型 `CredentialBase`)。The credential used to authenticate against the TTS provider.
modelstrTTS 模型的名称。The name of the TTS model.
parameters默认default NoneBaseModel | NoneTTS 模型参数。The TTS model parameters.
stream默认default Truebool流(类型 `bool`)。Whether to use streaming output if supported by the model.
源码Source agentscope/tts/_tts_base.py:52–74

属性详情Attribute Detail

credential

CredentialBase

凭证字段。声明为 CredentialBase。

The credential field.

源码Source agentscope/tts/_tts_base.py:37

model

str

模型字段。声明为 str。

The model field.

源码Source agentscope/tts/_tts_base.py:40

parameters

BaseModel

参数字段。声明为 BaseModel。

The parameters field.

源码Source agentscope/tts/_tts_base.py:43

stream

bool

流字段。声明为 bool。

The stream field.

源码Source agentscope/tts/_tts_base.py:46

realtime

bool = False

实时字段。声明为 bool。

The realtime field.

源码Source agentscope/tts/_tts_base.py:49

方法详情Method Detail

__aenter__

async def __aenter__() -> 'TTSModelBase'
async

进入异步上下文并返回自身。

Enter the async context manager and initialize resources if needed.

返回Returns 'TTSModelBase'
返回'TTSModelBase'。Returns TTS model base.
源码Source agentscope/tts/_tts_base.py:76–81

__aexit__

async def __aexit__(exc_type: Any, exc_value: Any, traceback: Any) -> None
async

退出异步上下文并清理资源。

Exit the async context manager and clean up resources if needed.

参数Parameters
名称Name类型Type说明Description
exc_typeAnyexc 类型(类型 `Any`)。The exc type.
exc_valueAnyexc 值(类型 `Any`)。The exc value.
tracebackAnytraceback(类型 `Any`)。The traceback.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/tts/_tts_base.py:83–91

connect

async def connect() -> None
async

建立连接并完成初始化。

Connect to the TTS model and initialize resources.

说明Notes

  • 注意:该行为受运行时配置或底层实现影响,建议结合对应源码与版本说明确认。Note: Only relevant for realtime TTS models — realtime subclasses must override this. The default is a no-op so that non-realtime models can ignore the lifecycle hooks; :meth:`__aenter__` only calls it when `realtime` is True.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/tts/_tts_base.py:93–101

close

async def close() -> None
async

关闭连接并释放相关资源。

Close the connection to the TTS model and clean up resources.

说明Notes

  • 注意:该行为受运行时配置或底层实现影响,建议结合对应源码与版本说明确认。Note: Only relevant for realtime TTS models — realtime subclasses must override this. See :meth:`connect` for the rationale behind the no-op default.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/tts/_tts_base.py:103–110

push

async def push(text: str, **kwargs: Any) -> TTSResponse
async

执行推送相关操作。

To receive all the synthesized speech, call :meth:synthesize after pushing all the text chunks.

说明Notes

  • 注意:该行为受运行时配置或底层实现影响,建议结合对应源码与版本说明确认。Note: Only relevant for realtime TTS models — realtime subclasses must override this. Non-realtime models should call :meth:`synthesize` directly and never reach this method.
参数Parameters
名称Name类型Type说明Description
textstr文本(类型 `str`)。The text chunk to be synthesized.
kwargsAny关键字参数(类型 `Any`)。The kwargs.
返回Returns TTSResponse
返回TTSResponse。`TTSResponse`:
源码Source agentscope/tts/_tts_base.py:112–138

list_models

def list_models(custom_yaml_dir: str | None = None) -> list['TTSModelCard']

列出模型。

List candidate TTS models by scanning YAML model cards.

参数Parameters
名称Name类型Type说明Description
custom_yaml_dir默认default Nonestr | None自定义 yaml 目录(类型 `str | None`)。The custom YAML directory. If `None`, uses the `_models` directory next to the concrete subclass's source file.
返回Returns list['TTSModelCard']
返回'TTSModelCard'列表。`list[TTSModelCard]`:
源码Source agentscope/tts/_tts_base.py:141–184

synthesize

async def synthesize(text: str | None = None, **kwargs: Any) -> TTSResponse | AsyncGenerator[TTSResponse, None]
asyncabstractabstractmethod

执行 synthesize 方法。

Synthesize speech from the appended text.

参数Parameters
名称Name类型Type说明Description
text默认default Nonestr | None文本(类型 `str | None`)。The text to be synthesized. If `None`, this method will wait for all previously pushed text to be synthesized and return the last synthesized TTSResponse.
kwargsAny关键字参数(类型 `Any`)。The kwargs.
返回Returns TTSResponse | AsyncGenerator[TTSResponse, None]
返回TTSResponse|AsyncGenerator[TTSResponse,None]。`TTSResponse | AsyncGenerator[TTSResponse, None]`:
源码Source agentscope/tts/_tts_base.py:187–212

嵌套类详情Nested Class Detail

Parameters

class Parameters

参数,处理低延迟的实时语音与多模态交互。位于 agentscope.tts 包。

Base parameter schema for TTS models. Declared in agentscope.tts.

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.