OpenAIChatModel
public class OpenAIChatModel extends ChatModelBase
io/agentscope/extensions/model/openai/OpenAIChatModel.java:69–504OpenAIChatModel 位于官方 openai 扩展中,通过构建器配置 API Key、模型名、端点、格式化器与上下文窗口,实现 ChatModelBase 的流式协议。位于 io.agentscope.extensions.model.openai 包,继承自 ChatModelBase。核心成员包括 doStream、doStream0、getModelName、builder。
OpenAI Chat Model using native HTTP API.
This implementation uses direct HTTP calls to OpenAI-compatible APIs.
Features:
-
Streaming and non-streaming modes
-
Tool calling support
-
Automatic message format conversion
-
Timeout and retry configuration
-
Multi-provider support via different Formatters
Provider-specific behavior is handled by the Formatter. Use the appropriate formatter for your provider:
-
OpenAIChatFormatter- Standard OpenAI GPT models -
io.agentscope.extensions.model.openai.compat.deepseek.DeepSeekFormatter- DeepSeek Chat models -
io.agentscope.extensions.model.openai.compat.minimax.MiniMaxFormatter- MiniMax models -
io.agentscope.extensions.model.openai.compat.kimi.KimiFormatter- Kimi (Moonshot AI) models -
io.agentscope.extensions.model.openai.compat.glm.GLMFormatter- Zhipu GLM models
核心精讲Deep dive
OpenAIChatModel:OpenAI 协议实现OpenAIChatModel: the OpenAI protocol implementation
OpenAIChatModel 位于官方 openai 扩展中,通过 Builder 配置 API Key、模型名、端点、formatter 与上下文窗口,实现 ChatModelBase 的流式协议。
OpenAIChatModel lives in the official openai extension. Its builder configures API key, model name, endpoint, formatter and context window while implementing the ChatModelBase streaming protocol.
- apiKey 与 modelName 是必填项,baseUrl 支持兼容端点。apiKey and modelName are required; baseUrl supports compatible endpoints.
- formatter 可替换为 DeepSeek、GLM 等专用实现。The formatter can be replaced with specialized implementations such as DeepSeek or GLM.
- contextWindowSize 与 nativeStructuredOutput 控制预算与输出策略。contextWindowSize and nativeStructuredOutput control budget and output strategy.
构建 OpenAI 模型Build an OpenAI model
import io.agentscope.extensions.model.openai.OpenAIChatModel;
public class OpenAIModelDemo {
public static void main(String[] args) {
OpenAIChatModel model = OpenAIChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4.1-mini")
.baseUrl("https://api.openai.com/v1")
.stream(true)
.contextWindowSize(128_000)
.build();
System.out.println(model.getModelName());
}
}嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | OpenAIChatModel 构建器 Builder for OpenAIChatModel. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
protected Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) | 以流式方式执行并返回结果。 Performs the do stream operation. |
protected Flux<ChatResponse> doStream0(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) | 以流式方式执行并返回结果。 Performs the do stream 0 operation. |
public String getModelName() | 返回模型名称。 Gets the model name for logging and identification. |
public static Builder builder()static | 创建 OpenAI 模型构建器。 Creates a new builder for OpenAIChatModel. |
方法详情Method Detail
doStream
protected Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
以流式方式执行并返回结果。
Performs the do stream operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列表(类型 `List`)。The messages. |
tools | List<ToolSchema> | 工具列表(类型 `List`)。The tools. |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。The options. |
Flux<ChatResponse>io/agentscope/extensions/model/openai/OpenAIChatModel.java:93–102doStream0
protected Flux<ChatResponse> doStream0(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
以流式方式执行并返回结果。
Performs the do stream 0 operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列表(类型 `List`)。The messages. |
tools | List<ToolSchema> | 工具列表(类型 `List`)。The tools. |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。The options. |
Flux<ChatResponse>io/agentscope/extensions/model/openai/OpenAIChatModel.java:104–192getModelName
public String getModelName()
获取模型名称。
Gets the model name for logging and identification.
Stringio/agentscope/extensions/model/openai/OpenAIChatModel.java:199–202builder
public static Builder builder()
创建新的构建器用于 OpenAIChatModel。
Creates a new builder for OpenAIChatModel.
Builderio/agentscope/extensions/model/openai/OpenAIChatModel.java:209–211嵌套类详情Nested Class Detail
Builder
public static class Builder配置 API Key、模型名、端点、格式化器、流式开关与上下文窗口。位于 io.agentscope.extensions.model.openai 包。核心成员包括 apiKey、modelName、stream、generateOptions、baseUrl、endpointPath。
Builder for OpenAIChatModel.
The built model internally wraps the configuration so that calls without explicit options use the builder-provided values.