ChatModel
Text generation, streaming, reasoning, and tools API.
TTS Models
Non-realtime and realtime text-to-speech synthesis.
Realtime Models
Bidirectional WebSocket streaming for voice agents.
Embedding Models
Vector representations for retrieval and similarity search.
ChatModel
Basic Usage
All chat model classes share a unified__call__ interface. The input to __call__ is the formatted messages — the result of applying a formatter to Msg objects. This formatted input matches the exact format expected by the underlying API provider.
Method signature:
Msg objects. When calling a model directly (outside an agent), the typical flow is:
- Build
Msgobjects withname,role, andcontent(text or content blocks) - Use a Formatter to convert
[Msg]into the provider-specific message format - Call the ChatModel with the formatted messages to get a
ChatResponse
ReActAgent), steps 2-3 are handled automatically — the agent internally manages the Msg → Formatter → Model → ChatResponse pipeline.
Example workflow:
Msg objects. This design allows each model to receive input in its native API format. The model returns a ChatResponse object containing the generated content and usage information.
Streaming
To enable streaming, setstream=True in the constructor. When streaming is enabled, __call__ returns an async generator that yields ChatResponse instances.
Streaming in AgentScope is accumulative — each chunk contains all previous content plus newly generated content, not just the delta. This simplifies consumption since you always have the complete current state without tracking deltas.
Reasoning
AgentScope supports reasoning models (chain-of-thought) viaThinkingBlock. When enable_thinking=True, the model’s response includes both thinking process and final answer.
Tools API
AgentScope provides a unified tools interface across all providers. Tools are defined using a standardized JSON schema format and passed to the model via thetools parameter.
tool_choice parameter controls invocation behavior:
"auto": Model decides whether to call a tool"none": No tools will be called"required": Model must call at least one tool"<function_name>": Force a specific tool
Provider Reference
AgentScope supports multiple chat model providers. Each provider has a corresponding model class and formatter:For detailed provider-specific parameters and examples, refer to the original documentation or source code.
Token Counting
AgentScope provides a token counter module underagentscope.token to estimate the number of tokens in a set of messages before sending them to a model. This is useful for managing context window budgets and implementing prompt truncation strategies.
Supported providers:
DashScope does not provide a token-counting API. For DashScope (Qwen) models, use
HuggingFaceTokenCounter with the corresponding Qwen tokenizer instead.TTS Models
TTS (Text-to-Speech) models convert text into audio. AgentScope supports both non-realtime and realtime TTS models.Non-Realtime TTS
Non-realtime TTS models require complete text before synthesis. The core method issynthesize(), which accepts a Msg object and returns a TTSResponse containing audio data.
stream=True) returns audio chunks progressively:
Realtime TTS
Realtime TTS models accept streaming text input — text chunks can be fed incrementally as they become available (e.g., from a streaming chat model). This enables the lowest possible latency. Core methods:- Stateful processing: Only one streaming session can be active at a time, identified by
msg.id - Incremental input: Use
push()to submit text chunks as they arrive - Finalization: Use
synthesize()to complete the session and get remaining audio
Realtime Models
Realtime models provide bidirectional, persistent communication over WebSocket, designed primarily for voice agent scenarios where the user speaks and the model responds with speech in real-time.Principle
Realtime models maintain a persistent WebSocket connection that supports:- Bidirectional streaming: Audio/text input and audio/text output flow simultaneously
- Low latency: Server-side VAD (Voice Activity Detection) enables natural turn-taking
- Multimodal input: Audio, text, images (provider-dependent)
- Tool support: Some providers support function calling in realtime (e.g., OpenAI, Gemini)
Usage with RealtimeAgent
AgentScope providesRealtimeAgent to work with realtime models. The agent handles the WebSocket connection, audio streaming, and message exchange automatically.
RealtimeAgent manages:
- WebSocket connection lifecycle
- Audio input/output streaming
- Turn-taking and interruption handling
- Tool execution (if supported by the model)
Embedding Models
Embedding models generate vector representations for text, images, and other data types. These embeddings are used for retrieval, similarity search, and as input features for downstream tasks.Core Method
All embedding models share a unified__call__ interface that accepts input data and returns an EmbeddingResponse:
Text Embedding
Text embedding models accept text strings orTextBlock objects:
Multimodal Embedding
Multimodal embedding models accept text, images, and videos using content blocks:For image and video inputs, use
ImageBlock with URLSource (for publicly accessible URLs) or Base64Source (for base64-encoded data). The example above uses text for simplicity.Provider Reference
AgentScope supports multiple embedding model providers:
Common parameters:
api_key: API key for authenticationmodel_name: The embedding model identifierdimensions: Embedding vector dimension (provider-dependent)embedding_cache: Optional cache instance to avoid repeated API calls