AS AgentScopeAPI 手册 / API Reference

OpenAIChatFormatter

public class OpenAIChatFormatter extends OpenAIBaseFormatter
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:46–308

用于OpenAI 聊天补全 HTTP API的格式化器。位于 io.agentscope.extensions.model.openai.formatter 包,继承自 OpenAIBaseFormatter。核心成员包括 doFormat、convertMessage、applyOptions、applyMaxTokens、applyTools、supportsStrict。

Formatter for OpenAI Chat Completion HTTP API. Converts between AgentScope Msg objects and OpenAI DTO types.

This formatter is used with the HTTP-based OpenAI client for standard OpenAI GPT models. It provides full support for:

  • All sampling parameters (temperature, top_p, penalties)

  • Tool calling with strict mode support

  • Full tool_choice options (auto, none, required, specific)

构造器摘要Constructor Summary

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

方法摘要Method Summary

方法Method说明Description
protected List<OpenAIMessage> doFormat(List<Msg> msgs)
执行格式操作。
Performs the do format operation.
protected List<OpenAIMessage> doFormat(List<Msg> msgs, GenerateOptions options)
执行格式操作。
Performs the do format operation.
protected OpenAIMessage convertMessage(Msg msg, boolean hasMedia)
转换消息。
Convert one AgentScope message to OpenAI message format.
public void applyOptions(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)
应用选项。
Performs the apply options operation.
protected void applyMaxTokens(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)
应用最大令牌数。
Performs the apply max tokens operation.
public void applyTools(OpenAIRequest request, List<ToolSchema> tools)
应用工具。
Performs the apply tools operation.
protected boolean supportsStrict()
返回 `supportsStrict` 结果。
Returns whether this formatter's target API supports the strict parameter in tool definitions.
public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice)
应用工具选项。
Performs the apply tool choice operation.
protected void applyAdditionalBodyParams(OpenAIRequest request, GenerateOptions opts)
应用 `applyAdditionalBodyParams`。
Apply additional body parameters from GenerateOptions to OpenAI request.

构造器详情Constructor Detail

OpenAIChatFormatter

public OpenAIChatFormatter()

初始化 OpenAIChatFormatter 实例。

Creates a new instance.

源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:50–52

方法详情Method Detail

doFormat

protected List<OpenAIMessage> doFormat(List<Msg> msgs)

执行格式操作。

Performs the do format operation.

参数Parameters
名称Name类型Type说明Description
msgsList<Msg>消息列表(类型 `List`)。The msgs.
返回Returns List<OpenAIMessage>
返回列出。Returns list.
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:54–65

doFormat

protected List<OpenAIMessage> doFormat(List<Msg> msgs, GenerateOptions options)

执行格式操作。

Performs the do format operation.

参数Parameters
名称Name类型Type说明Description
msgsList<Msg>消息列表(类型 `List`)。The msgs.
optionsGenerateOptions选项(类型 `GenerateOptions`)。The options.
返回Returns List<OpenAIMessage>
返回列出。Returns list.
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:67–80

convertMessage

protected OpenAIMessage convertMessage(Msg msg, boolean hasMedia)

转换消息。

Convert one AgentScope message to OpenAI message format.

参数Parameters
名称Name类型Type说明Description
msgMsg消息到转换。the message to convert
hasMediaboolean有 Media(类型 `boolean`)。whether the message contains media content
返回Returns OpenAIMessage
返回OpenAIMessage。converted OpenAI message
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:92–94

applyOptions

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

应用选项。

Performs the apply options operation.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。The request.
optionsGenerateOptions选项(类型 `GenerateOptions`)。The options.
defaultOptionsGenerateOptions默认选项(类型 `GenerateOptions`)。The default options.
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:96–167

applyMaxTokens

protected void applyMaxTokens(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)

应用最大令牌数。

Performs the apply max tokens operation.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。The request.
optionsGenerateOptions选项(类型 `GenerateOptions`)。The options.
defaultOptionsGenerateOptions默认选项(类型 `GenerateOptions`)。The default options.
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:169–183

applyTools

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

应用工具。

Performs the apply tools operation.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。The request.
toolsList<ToolSchema>工具列表(类型 `List`)。The tools.
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:185–220

supportsStrict

protected boolean supportsStrict()

返回 supportsStrict 结果。

Returns whether this formatter's target API supports the strict parameter in tool definitions.

返回Returns boolean
返回布尔值,表示判断结果。true if strict parameter is supported, false otherwise
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:231–233

applyToolChoice

public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice)

应用工具选项。

Performs the apply tool choice operation.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。The request.
toolChoiceToolChoice工具选项(类型 `ToolChoice`)。The tool choice.
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:235–264

applyAdditionalBodyParams

protected void applyAdditionalBodyParams(OpenAIRequest request, GenerateOptions opts)

应用 applyAdditionalBodyParams。

Apply additional body parameters from GenerateOptions to OpenAI request.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。The request.
optsGenerateOptionsopts(类型 `GenerateOptions`)。The opts.
源码Source io/agentscope/extensions/model/openai/formatter/OpenAIChatFormatter.java:270–307

继承成员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.