AS AgentScopeAPI 手册 / API Reference

OpenAIBaseFormatter

public abstract class OpenAIBaseFormatter extends AbstractBaseFormatter<OpenAIMessage, OpenAIResponse, OpenAIRequest>
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:48–279

用于OpenAI 聊天补全 HTTP API的基类格式化器。位于 io.agentscope.extensions.model.openai.formatter 包,继承自 AbstractBaseFormatter<OpenAIMessage, OpenAIResponse, OpenAIRequest>。核心成员包括 parseResponse、applyOptions、applyTools、applyToolChoice、buildRequest、OpenAIBaseFormatter。

Base formatter for OpenAI Chat Completion HTTP API. Provides common functionality for both single-agent and multi-agent formatters.

Subclasses must implement:

  • #doFormat(List) - Convert messages to OpenAI format

  • #applyOptions(OpenAIRequest, GenerateOptions, GenerateOptions) - Apply generation options

  • #applyTools(OpenAIRequest, List) - Apply tool schemas

  • #applyToolChoice(OpenAIRequest, ToolChoice) - Apply tool choice configuration

字段摘要Field Summary

字段Field类型Type说明Description
messageConverter
final
OpenAIMessageConverter
消息转换器字段。
The message converter field.
responseParser
final
OpenAIResponseParser
响应解析器字段。
The response parser field.

构造器摘要Constructor Summary

构造器Constructor说明Description
protected OpenAIBaseFormatter()
初始化 `OpenAIBaseFormatter` 实例。
Creates a new instance.

方法摘要Method Summary

方法Method说明Description
public ChatResponse parseResponse(OpenAIResponse response, Instant startTime)
解析响应。
Parses the response.
public abstract void applyOptions(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)
abstract
应用选项。
Apply generation options to the request.
public abstract void applyTools(OpenAIRequest request, List<ToolSchema> tools)
abstract
应用工具。
Apply tool schemas to the request.
public abstract void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice)
abstract
应用工具选项。
Apply tool choice configuration to the request.
public void applyTools(OpenAIRequest request, List<ToolSchema> tools, String baseUrl, String modelName)
应用工具。
Apply tool schemas with provider context.
public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice, String baseUrl, String modelName)
应用工具选项。
Apply tool choice with provider context.
public OpenAIRequest buildRequest(String model, List<OpenAIMessage> messages, boolean stream)
构建基础 OpenAIRequest。
Build a basic OpenAIRequest.
public OpenAIRequest buildRequest(String model, List<OpenAIMessage> messages, boolean stream, GenerateOptions options, GenerateOptions defaultOptions, List<ToolSchema> tools, ToolChoice toolChoice)
构建请求。
Build a complete OpenAIRequest with full configuration.

字段详情Field Detail

messageConverter

protected final OpenAIMessageConverter messageConverter
final

消息转换器字段。声明为 protected final OpenAIMessageConverter messageConverter。

The message converter field.

源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:56

responseParser

protected final OpenAIResponseParser responseParser
final

响应解析器字段。声明为 protected final OpenAIResponseParser responseParser。

The response parser field.

源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:57

构造器详情Constructor Detail

OpenAIBaseFormatter

protected OpenAIBaseFormatter()

初始化 OpenAIBaseFormatter 实例。

Creates a new instance.

源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:59–64

方法详情Method Detail

parseResponse

public ChatResponse parseResponse(OpenAIResponse response, Instant startTime)

解析响应。

Parses the response.

参数Parameters
名称Name类型Type说明Description
responseOpenAIResponse响应(类型 `OpenAIResponse`)。The response.
startTimeInstant启动时间(类型 `Instant`)。The start time.
返回Returns ChatResponse
返回ChatResponse。Returns chat response.
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:66–69

applyOptions

public abstract void applyOptions(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)
abstract

应用选项。

Apply generation options to the request.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。OpenAI request DTO
optionsGenerateOptions选项(类型 `GenerateOptions`)。Generation options to apply
defaultOptionsGenerateOptions默认选项(类型 `GenerateOptions`)。Default options to use if options parameter is null
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:79–81

applyTools

public abstract void applyTools(OpenAIRequest request, List<ToolSchema> tools)
abstract

应用工具。

Apply tool schemas to the request.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。OpenAI request DTO
toolsList<ToolSchema>工具列表(类型 `List`)。List of tool schemas to apply (may be null or empty)
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:90–91

applyToolChoice

public abstract void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice)
abstract

应用工具选项。

Apply tool choice configuration to the request.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。OpenAI request DTO
toolChoiceToolChoice工具选项(类型 `ToolChoice`)。Tool choice configuration (null means auto)
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:100–101

applyTools

public void applyTools(OpenAIRequest request, List<ToolSchema> tools, String baseUrl, String modelName)

应用工具。

Apply tool schemas with provider context.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。OpenAI request DTO
toolsList<ToolSchema>工具列表(类型 `List`)。Tool schemas to apply
baseUrlString基类 URL(类型 `String`)。API base URL (ignored by default)
modelNameString模型名称(类型 `String`)。Model name (ignored by default)
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:112–116

applyToolChoice

public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice, String baseUrl, String modelName)

应用工具选项。

Apply tool choice with provider context.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。OpenAI request DTO
toolChoiceToolChoice工具选项(类型 `ToolChoice`)。Tool choice configuration
baseUrlString基类 URL(类型 `String`)。API base URL (ignored by default)
modelNameString模型名称(类型 `String`)。Model name (ignored by default)
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:127–131

buildRequest

public OpenAIRequest buildRequest(String model, List<OpenAIMessage> messages, boolean stream)

构建基础 OpenAIRequest。

Build a basic OpenAIRequest.

参数Parameters
名称Name类型Type说明Description
modelString模型实例(类型 `String`)。Model name
messagesList<OpenAIMessage>消息列表(类型 `List`)。Formatted OpenAI messages
streamboolean判断是否到启用流式。Whether to enable streaming
返回Returns OpenAIRequest
返回OpenAIRequest。Basic OpenAIRequest
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:141–143

buildRequest

public OpenAIRequest buildRequest(String model, List<OpenAIMessage> messages, boolean stream, GenerateOptions options, GenerateOptions defaultOptions, List<ToolSchema> tools, ToolChoice toolChoice)

构建请求。

Build a complete OpenAIRequest with full configuration.

参数Parameters
名称Name类型Type说明Description
modelString模型实例(类型 `String`)。Model name
messagesList<OpenAIMessage>消息列表(类型 `List`)。Formatted OpenAI messages
streamboolean判断是否到启用流式。Whether to enable streaming
optionsGenerateOptions选项(类型 `GenerateOptions`)。Generation options
defaultOptionsGenerateOptions默认选项(类型 `GenerateOptions`)。Default generation options
toolsList<ToolSchema>工具列表(类型 `List`)。Tool schemas
toolChoiceToolChoice工具选项(类型 `ToolChoice`)。Tool choice configuration
返回Returns OpenAIRequest
返回OpenAIRequest。Complete OpenAIRequest ready for API call
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:159–179
基于官方固定版本源码静态生成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.