agentscope.ttsTTSModelBase
class TTSModelBase(ABC)
agentscope/tts/_tts_base.py:18–212TTS 模型,使用 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 |
|---|---|---|---|
credential | CredentialBase | 凭证字段。 | — |
model | str | 模型字段。 | — |
parameters | BaseModel | 参数字段。 | — |
stream | bool | 流字段。 | — |
realtime | bool | 实时字段。 | 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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
credential | CredentialBase | 凭证对象(类型 `CredentialBase`)。The credential used to authenticate against the TTS provider. |
model | str | TTS 模型的名称。The name of the TTS model. |
parameters默认default None | BaseModel | None | TTS 模型参数。The TTS model parameters. |
stream默认default True | bool | 流(类型 `bool`)。Whether to use streaming output if supported by the model. |
agentscope/tts/_tts_base.py:52–74属性详情Attribute Detail
credential
CredentialBase凭证字段。声明为 CredentialBase。
The credential field.
agentscope/tts/_tts_base.py:37model
str模型字段。声明为 str。
The model field.
agentscope/tts/_tts_base.py:40parameters
BaseModel参数字段。声明为 BaseModel。
The parameters field.
agentscope/tts/_tts_base.py:43stream
bool流字段。声明为 bool。
The stream field.
agentscope/tts/_tts_base.py:46realtime
bool = False实时字段。声明为 bool。
The realtime field.
agentscope/tts/_tts_base.py:49方法详情Method Detail
__aenter__
async def __aenter__() -> 'TTSModelBase'
进入异步上下文并返回自身。
Enter the async context manager and initialize resources if needed.
agentscope/tts/_tts_base.py:76–81__aexit__
async def __aexit__(exc_type: Any, exc_value: Any, traceback: Any) -> None
退出异步上下文并清理资源。
Exit the async context manager and clean up resources if needed.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
exc_type | Any | exc 类型(类型 `Any`)。The exc type. |
exc_value | Any | exc 值(类型 `Any`)。The exc value. |
traceback | Any | traceback(类型 `Any`)。The traceback. |
agentscope/tts/_tts_base.py:83–91connect
async def connect() -> None
建立连接并完成初始化。
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.
agentscope/tts/_tts_base.py:93–101close
async def close() -> None
关闭连接并释放相关资源。
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.
agentscope/tts/_tts_base.py:103–110push
async def push(text: str, **kwargs: Any) -> TTSResponse
执行推送相关操作。
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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
text | str | 文本(类型 `str`)。The text chunk to be synthesized. |
kwargs | Any | 关键字参数(类型 `Any`)。The kwargs. |
agentscope/tts/_tts_base.py:112–138list_models
def list_models(custom_yaml_dir: str | None = None) -> list['TTSModelCard']
列出模型。
List candidate TTS models by scanning YAML model cards.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
custom_yaml_dir默认default None | str | None | 自定义 yaml 目录(类型 `str | None`)。The custom YAML directory. If `None`, uses the `_models` directory next to the concrete subclass's source file. |
agentscope/tts/_tts_base.py:141–184synthesize
async def synthesize(text: str | None = None, **kwargs: Any) -> TTSResponse | AsyncGenerator[TTSResponse, None]
执行 synthesize 方法。
Synthesize speech from the appended text.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
text默认default None | str | 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. |
kwargs | Any | 关键字参数(类型 `Any`)。The kwargs. |
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.