AS AgentScopeAPI 手册 / API Reference

OpenAIChatModel

public class OpenAIChatModel extends ChatModelBase
源码Source io/agentscope/extensions/model/openai/OpenAIChatModel.java:69–504

OpenAIChatModel 位于官方 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.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息列表(类型 `List`)。The messages.
toolsList<ToolSchema>工具列表(类型 `List`)。The tools.
optionsGenerateOptions选项(类型 `GenerateOptions`)。The options.
返回Returns Flux<ChatResponse>
返回Flux。Returns flux.
源码Source io/agentscope/extensions/model/openai/OpenAIChatModel.java:93–102

doStream0

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

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

Performs the do stream 0 operation.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息列表(类型 `List`)。The messages.
toolsList<ToolSchema>工具列表(类型 `List`)。The tools.
optionsGenerateOptions选项(类型 `GenerateOptions`)。The options.
返回Returns Flux<ChatResponse>
返回Flux。Returns flux.
源码Source io/agentscope/extensions/model/openai/OpenAIChatModel.java:104–192

getModelName

public String getModelName()

获取模型名称。

Gets the model name for logging and identification.

返回Returns String
返回字符串结果。the model name, or null if not configured
源码Source io/agentscope/extensions/model/openai/OpenAIChatModel.java:199–202

builder

public static Builder builder()
static

创建新的构建器用于 OpenAIChatModel。

Creates a new builder for OpenAIChatModel.

返回Returns Builder
一种新的构建器实例。a new Builder instance
源码Source io/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.

继承成员Inherited Members

基于官方固定版本源码静态生成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.