Skip to main content
This document covers detailed usage examples and provider-specific references for each model class in AgentScope.

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.
For core concepts and design principles, see Model. For details on Msg and content blocks, see Msg.

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:
Typical workflow when calling a model directly: In AgentScope, agents communicate by passing Msg objects. When calling a model directly (outside an agent), the typical flow is:
  1. Build Msg objects with name, role, and content (text or content blocks)
  2. Use a Formatter to convert [Msg] into the provider-specific message format
  3. Call the ChatModel with the formatted messages to get a ChatResponse
When using an agent (e.g., ReActAgent), steps 2-3 are handled automatically — the agent internally manages the Msg → Formatter → Model → ChatResponse pipeline. Example workflow:
The key point: ChatModel accepts formatted messages (the output of a formatter), not raw 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, set stream=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.
Example output (each line shows accumulative text):

Reasoning

AgentScope supports reasoning models (chain-of-thought) via ThinkingBlock. When enable_thinking=True, the model’s response includes both thinking process and final answer.
The thinking content is streamed alongside text content in accumulative mode.

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 the tools parameter.
The 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
Use the Toolkit class to auto-generate JSON schemas from Python functions with docstrings. See Tool for details.

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 under agentscope.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.
The formatter module integrates token counters to support automatic prompt truncation. When a token budget is configured, the formatter uses the corresponding counter to trim messages before they are sent to the model.
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 is synthesize(), which accepts a Msg object and returns a TTSResponse containing audio data.
Basic usage:
Streaming output (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:
Key concepts:
  • 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
Usage example:
Integration with Agent: AgentScope agents can automatically synthesize speech when provided with a TTS model. The agent handles the streaming text → TTS pipeline internally.
When the agent generates streaming text responses, the TTS model automatically converts them to speech in real-time.

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)
The key difference from traditional chat models is that realtime models handle the entire voice interaction pipeline (ASR + LLM + TTS) in a single, optimized connection, minimizing latency.

Usage with RealtimeAgent

AgentScope provides RealtimeAgent to work with realtime models. The agent handles the WebSocket connection, audio streaming, and message exchange automatically.
The RealtimeAgent manages:
  • WebSocket connection lifecycle
  • Audio input/output streaming
  • Turn-taking and interruption handling
  • Tool execution (if supported by the model)
Supported providers:

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 or TextBlock 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 authentication
  • model_name: The embedding model identifier
  • dimensions: Embedding vector dimension (provider-dependent)
  • embedding_cache: Optional cache instance to avoid repeated API calls
Usage tips:
  • Use text embedding models for semantic search, clustering, and classification tasks.
  • Use multimodal embedding models for cross-modal retrieval (e.g., search images by text).
  • Enable caching for frequently embedded content to reduce API costs.
  • Batch multiple inputs in a single call for better efficiency.