AS AgentScopeAPI 手册 / API Reference

ChatModelBase

public abstract class ChatModelBase implements Model
源码Source io/agentscope/core/model/ChatModelBase.java:29–96

ChatModelBase 定义 Java 模型的统一契约:stream 返回 Flux,子类实现 doStream。它同时暴露上下文窗口、原生结构化输出等能力位。位于 io.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,子类实现 doStream。它同时暴露上下文窗口、原生结构化输出等能力位。

ChatModelBase defines the Java model contract: stream returns Flux and subclasses implement doStream. It also exposes capability flags such as context window and native structured output.

  • 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说明Description
public int getContextWindowSize()
返回上下文窗口大小。
Returns the context window size.
protected void setContextWindowSize(int contextWindowSize)
设置 `setContextWindowSize`。
Sets the context window size.
public boolean supportsNativeStructuredOutput()
是否支持供应商原生的结构化输出。
Returns the supports native structured output.
protected void setNativeStructuredOutput(boolean nativeStructuredOutput)
设置原生结构化输出。
Sets the native structured output.
public boolean supportsNativeStructuredOutputWithTools()
是否支持在工具调用同时使用原生结构化输出。
Returns the supports native structured output with tools.
protected void setNativeStructuredOutputWithTools(boolean nativeStructuredOutputWithTools)
设置原生结构化输出使用工具。
Sets the native structured output with tools.
public final Flux<ChatResponse> stream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
final
执行流式模型调用,返回 ChatResponse 流。
Stream chat completion responses.
protected abstract Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
abstract
供应商子类实现的流式调用入口。
Internal implementation for streaming chat completion responses.

方法详情Method Detail

getContextWindowSize

public int getContextWindowSize()

获取 getContextWindowSize。

Returns the context window size.

返回Returns int
返回数值结果。Returns a numeric result.
源码Source io/agentscope/core/model/ChatModelBase.java:37–40

setContextWindowSize

protected void setContextWindowSize(int contextWindowSize)

设置 setContextWindowSize。

Sets the context window size.

参数Parameters
名称Name类型Type说明Description
contextWindowSizeint上下文 Window 大小(类型 `int`)。The context window size.
源码Source io/agentscope/core/model/ChatModelBase.java:42–44

supportsNativeStructuredOutput

public boolean supportsNativeStructuredOutput()

返回支持原生结构化输出。

Returns the supports native structured output.

返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/core/model/ChatModelBase.java:46–49

setNativeStructuredOutput

protected void setNativeStructuredOutput(boolean nativeStructuredOutput)

设置原生结构化输出。

Sets the native structured output.

参数Parameters
名称Name类型Type说明Description
nativeStructuredOutputboolean原生结构化输出(类型 `boolean`)。The native structured output.
源码Source io/agentscope/core/model/ChatModelBase.java:51–53

supportsNativeStructuredOutputWithTools

public boolean supportsNativeStructuredOutputWithTools()

返回支持原生结构化输出使用工具。

Returns the supports native structured output with tools.

返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/core/model/ChatModelBase.java:55–60

setNativeStructuredOutputWithTools

protected void setNativeStructuredOutputWithTools(boolean nativeStructuredOutputWithTools)

设置原生结构化输出使用工具。

Sets the native structured output with tools.

参数Parameters
名称Name类型Type说明Description
nativeStructuredOutputWithToolsboolean原生结构化输出使用工具(类型 `boolean`)。The native structured output with tools.
源码Source io/agentscope/core/model/ChatModelBase.java:62–64

stream

public final Flux<ChatResponse> stream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
final

执行流相关操作。

Stream chat completion responses.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息列表(类型 `List`)。AgentScope messages to send to the model
toolsList<ToolSchema>工具列表(类型 `List`)。Optional list of tool schemas (null or empty if no tools)
optionsGenerateOptions选项(类型 `GenerateOptions`)。Optional generation options (null to use defaults)
返回Returns Flux<ChatResponse>
返回Flux。Flux stream of chat responses
源码Source io/agentscope/core/model/ChatModelBase.java:77–83

doStream

protected abstract Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
abstract

以流式方式执行并返回结果。

Internal implementation for streaming chat completion responses.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息列表(类型 `List`)。AgentScope messages to send to the model
toolsList<ToolSchema>工具列表(类型 `List`)。Optional list of tool schemas (null or empty if no tools)
optionsGenerateOptions选项(类型 `GenerateOptions`)。Optional generation options (null to use defaults)
返回Returns Flux<ChatResponse>
返回Flux。Flux stream of chat responses
源码Source io/agentscope/core/model/ChatModelBase.java:94–95

继承成员Inherited Members

继承自Methods inherited from Model
基于官方固定版本源码静态生成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.