OpenAIBaseFormatter
public abstract class OpenAIBaseFormatter extends AbstractBaseFormatter<OpenAIMessage, OpenAIResponse, OpenAIRequest>
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 |
|---|---|---|
messageConverterfinal | OpenAIMessageConverter | 消息转换器字段。 The message converter field. |
responseParserfinal | OpenAIResponseParser | 响应解析器字段。 The response parser field. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
protected OpenAIBaseFormatter() | 初始化 `OpenAIBaseFormatter` 实例。 Creates a new instance. |
方法摘要Method Summary
字段详情Field Detail
messageConverter
protected final OpenAIMessageConverter messageConverter
消息转换器字段。声明为 protected final OpenAIMessageConverter messageConverter。
The message converter field.
io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:56responseParser
protected final OpenAIResponseParser responseParser
响应解析器字段。声明为 protected final OpenAIResponseParser responseParser。
The response parser field.
io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:57构造器详情Constructor Detail
OpenAIBaseFormatter
protected OpenAIBaseFormatter()
初始化 OpenAIBaseFormatter 实例。
Creates a new instance.
io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:59–64方法详情Method Detail
parseResponse
public ChatResponse parseResponse(OpenAIResponse response, Instant startTime)
解析响应。
Parses the response.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
response | OpenAIResponse | 响应(类型 `OpenAIResponse`)。The response. |
startTime | Instant | 启动时间(类型 `Instant`)。The start time. |
ChatResponseio/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:66–69applyOptions
public abstract void applyOptions(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)
应用选项。
Apply generation options to the request.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。OpenAI request DTO |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。Generation options to apply |
defaultOptions | GenerateOptions | 默认选项(类型 `GenerateOptions`)。Default options to use if options parameter is null |
io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:79–81applyTools
public abstract void applyTools(OpenAIRequest request, List<ToolSchema> tools)
应用工具。
Apply tool schemas to the request.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。OpenAI request DTO |
tools | List<ToolSchema> | 工具列表(类型 `List`)。List of tool schemas to apply (may be null or empty) |
io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:90–91applyToolChoice
public abstract void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice)
应用工具选项。
Apply tool choice configuration to the request.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。OpenAI request DTO |
toolChoice | ToolChoice | 工具选项(类型 `ToolChoice`)。Tool choice configuration (null means auto) |
io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:100–101applyTools
public void applyTools(OpenAIRequest request, List<ToolSchema> tools, String baseUrl, String modelName)
应用工具。
Apply tool schemas with provider context.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。OpenAI request DTO |
tools | List<ToolSchema> | 工具列表(类型 `List`)。Tool schemas to apply |
baseUrl | String | 基类 URL(类型 `String`)。API base URL (ignored by default) |
modelName | String | 模型名称(类型 `String`)。Model name (ignored by default) |
io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:112–116applyToolChoice
public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice, String baseUrl, String modelName)
应用工具选项。
Apply tool choice with provider context.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。OpenAI request DTO |
toolChoice | ToolChoice | 工具选项(类型 `ToolChoice`)。Tool choice configuration |
baseUrl | String | 基类 URL(类型 `String`)。API base URL (ignored by default) |
modelName | String | 模型名称(类型 `String`)。Model name (ignored by default) |
io/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:127–131buildRequest
public OpenAIRequest buildRequest(String model, List<OpenAIMessage> messages, boolean stream)
构建基础 OpenAIRequest。
Build a basic OpenAIRequest.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
model | String | 模型实例(类型 `String`)。Model name |
messages | List<OpenAIMessage> | 消息列表(类型 `List`)。Formatted OpenAI messages |
stream | boolean | 判断是否到启用流式。Whether to enable streaming |
OpenAIRequestio/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:141–143buildRequest
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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
model | String | 模型实例(类型 `String`)。Model name |
messages | List<OpenAIMessage> | 消息列表(类型 `List`)。Formatted OpenAI messages |
stream | boolean | 判断是否到启用流式。Whether to enable streaming |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。Generation options |
defaultOptions | GenerateOptions | 默认选项(类型 `GenerateOptions`)。Default generation options |
tools | List<ToolSchema> | 工具列表(类型 `List`)。Tool schemas |
toolChoice | ToolChoice | 工具选项(类型 `ToolChoice`)。Tool choice configuration |
OpenAIRequestio/agentscope/extensions/model/openai/formatter/OpenAIBaseFormatter.java:159–179