ChatModelBase
public abstract class ChatModelBase implements Model
io/agentscope/core/model/ChatModelBase.java:29–96ChatModelBase 定义 Java 模型的统一契约:stream 返回 Fluxio.agentscope.core.model 包,实现 Model。核心成员包括 getContextWindowSize、setContextWindowSize、supportsNativeStructuredOutput、setNativeStructuredOutput、supportsNativeStructuredOutputWithTools、setNativeStructuredOutputWithTools。
Abstract base class for all models in the AgentScope framework.
This class provides common functionality for model including basic model invocation and tracing.
核心精讲Deep dive
ChatModelBase:响应式模型抽象ChatModelBase: the reactive model abstraction
ChatModelBase 定义 Java 模型的统一契约:stream 返回 Flux
ChatModelBase defines the Java model contract: stream returns Flux
- stream 是唯一执行入口,返回的 Flux 可被 Reactor 组合与背压控制。stream is the single execution entry point, returning a Flux that composes with Reactor and backpressure.
- doStream 由具体供应商实现,负责协议调用与分块解析。doStream is implemented by providers and handles protocol calls and chunk parsing.
- supportsNativeStructuredOutput 等方法让上层选择原生或工具式结构化输出。Capability methods such as supportsNativeStructuredOutput let callers choose native or tool-based structured output.
直接消费模型流Consume the raw model stream
import io.agentscope.core.message.Msg;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.model.ChatModelBase;
import io.agentscope.core.model.ChatResponse;
import io.agentscope.core.model.GenerateOptions;
import io.agentscope.extensions.model.openai.OpenAIChatModel;
import java.util.List;
import reactor.core.publisher.Flux;
public class ModelStreamDemo {
public static void main(String[] args) {
ChatModelBase model = OpenAIChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4.1-mini")
.build();
Flux<ChatResponse> chunks = model.stream(
List.of(Msg.builderForRole(MsgRole.USER)
.name("user")
.textContent("Hello")
.build()),
List.of(),
GenerateOptions.builder().build());
chunks.doOnNext(chunk -> System.out.println(chunk.getContent()))
.blockLast();
}
}方法摘要Method Summary
方法详情Method Detail
getContextWindowSize
public int getContextWindowSize()
获取 getContextWindowSize。
Returns the context window size.
intio/agentscope/core/model/ChatModelBase.java:37–40setContextWindowSize
protected void setContextWindowSize(int contextWindowSize)
设置 setContextWindowSize。
Sets the context window size.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
contextWindowSize | int | 上下文 Window 大小(类型 `int`)。The context window size. |
io/agentscope/core/model/ChatModelBase.java:42–44supportsNativeStructuredOutput
public boolean supportsNativeStructuredOutput()
返回支持原生结构化输出。
Returns the supports native structured output.
booleanio/agentscope/core/model/ChatModelBase.java:46–49setNativeStructuredOutput
protected void setNativeStructuredOutput(boolean nativeStructuredOutput)
设置原生结构化输出。
Sets the native structured output.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
nativeStructuredOutput | boolean | 原生结构化输出(类型 `boolean`)。The native structured output. |
io/agentscope/core/model/ChatModelBase.java:51–53supportsNativeStructuredOutputWithTools
public boolean supportsNativeStructuredOutputWithTools()
返回支持原生结构化输出使用工具。
Returns the supports native structured output with tools.
booleanio/agentscope/core/model/ChatModelBase.java:55–60setNativeStructuredOutputWithTools
protected void setNativeStructuredOutputWithTools(boolean nativeStructuredOutputWithTools)
设置原生结构化输出使用工具。
Sets the native structured output with tools.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
nativeStructuredOutputWithTools | boolean | 原生结构化输出使用工具(类型 `boolean`)。The native structured output with tools. |
io/agentscope/core/model/ChatModelBase.java:62–64stream
public final Flux<ChatResponse> stream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
执行流相关操作。
Stream chat completion responses.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列表(类型 `List`)。AgentScope messages to send to the model |
tools | List<ToolSchema> | 工具列表(类型 `List`)。Optional list of tool schemas (null or empty if no tools) |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。Optional generation options (null to use defaults) |
Flux<ChatResponse>io/agentscope/core/model/ChatModelBase.java:77–83doStream
protected abstract Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
以流式方式执行并返回结果。
Internal implementation for streaming chat completion responses.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列表(类型 `List`)。AgentScope messages to send to the model |
tools | List<ToolSchema> | 工具列表(类型 `List`)。Optional list of tool schemas (null or empty if no tools) |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。Optional generation options (null to use defaults) |
Flux<ChatResponse>io/agentscope/core/model/ChatModelBase.java:94–95