AS AgentScopeAPI 手册 / API Reference

GLMFormatter

public class GLMFormatter extends OpenAIChatFormatter
源码Source io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:72–247

GLM 格式化器,与模型服务通信并返回生成或向量化结果。位于 io.agentscope.extensions.model.openai.compat.glm 包,继承自 OpenAIChatFormatter。核心成员包括 doFormat、convertMessage、supportsStrict、applyOptions、applyToolChoice、ensureUserMessage。

Formatter for Zhipu AI (Z.ai) GLM models (glm-5.2, glm-5.1, glm-5, glm-4.7, glm-4.6, etc.).

Adapted to the latest Zhipu open platform Chat Completions API (https://open.bigmodel.cn/api/paas/v4/chat/completions):

  • At least one user message is required (error 1214 otherwise)

  • Does NOT support the name parameter in messages

  • tool_choice only supports "auto"; forced choices are degraded, while none removes tools to preserve the no-tool-call contract

  • Does NOT support the strict parameter in tool definitions

  • Does NOT support frequency_penalty / presence_penalty / thinking_budget; they are stripped from the request

  • Only supports max_tokens; max_completion_tokens is mapped to max_tokens when the latter is not set

  • temperature range is [0.0, 1.0] (OpenAI allows up to 2.0) and top_p range is [0.01, 1.0]; out-of-range values are clamped

Thinking mode: GLM-4.5 and later models accept a thinking object (e.g. {"type": "enabled"}). GLM-4.7 and GLM-5 series enable thinking by default. It can be controlled through GenerateOptions.additionalBodyParam("thinking", Map.of("type", "disabled")), which is passed through to the request body as-is. GLM-5.2 additionally supports the standard reasoning_effort option (GenerateOptions.reasoningEffort, default max) and streaming tool-call arguments via GenerateOptions.additionalBodyParam("tool_stream", true).

Usage:

`OpenAIChatModel.builder()
.formatter(new GLMFormatter())
.modelName("glm-5.2")
.baseUrl("https://open.bigmodel.cn/api/paas/v4")
.apiKey(apiKey)
.build();
`

构造器摘要Constructor Summary

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

方法摘要Method Summary

方法Method说明Description
protected List<OpenAIMessage> doFormat(List<Msg> msgs)
执行格式操作。
Performs the do format operation.
protected OpenAIMessage convertMessage(Msg msg, boolean hasMedia)
转换消息。
Converts the message.
protected boolean supportsStrict()
返回 `supportsStrict` 结果。
Returns the supports strict.
public void applyOptions(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)
应用选项。
Performs the apply options operation.
public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice)
应用工具选项。
Performs the apply tool choice operation.
protected static List<OpenAIMessage> ensureUserMessage(List<OpenAIMessage> messages)
static
设置确保用户消息。
Ensure at least one user message exists in the conversation.
protected static void applyGLMToolChoice(OpenAIRequest request, ToolChoice toolChoice)
static
应用GLM 工具选项。
Apply GLM-specific tool choice handling.
protected static void stripUnsupportedSamplingParams(OpenAIRequest request)
static
设置该参数。
Sanitize the request according to the GLM Chat Completions API reference.

构造器详情Constructor Detail

GLMFormatter

public GLMFormatter()

初始化 GLMFormatter 实例。

Creates a new instance.

源码Source io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:76–78

方法详情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/compat/glm/GLMFormatter.java:80–84

convertMessage

protected OpenAIMessage convertMessage(Msg msg, boolean hasMedia)

转换消息。

Converts the message.

参数Parameters
名称Name类型Type说明Description
msgMsg消息(类型 `Msg`)。The msg.
hasMediaboolean有 Media(类型 `boolean`)。The has media.
返回Returns OpenAIMessage
返回OpenAIMessage。Returns OpenAI message.
源码Source io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:86–93

supportsStrict

protected boolean supportsStrict()

返回 supportsStrict 结果。

Returns the supports strict.

返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:95–98

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/compat/glm/GLMFormatter.java:100–105

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/compat/glm/GLMFormatter.java:107–110

ensureUserMessage

protected static List<OpenAIMessage> ensureUserMessage(List<OpenAIMessage> messages)
static

设置确保用户消息。

Ensure at least one user message exists in the conversation.

参数Parameters
名称Name类型Type说明Description
messagesList<OpenAIMessage>消息到检查。the messages to check
返回Returns List<OpenAIMessage>
返回列出。messages with a user message ensured
源码Source io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:123–135

applyGLMToolChoice

protected static void applyGLMToolChoice(OpenAIRequest request, ToolChoice toolChoice)
static

应用GLM 工具选项。

Apply GLM-specific tool choice handling.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。the request to apply tool choice to
toolChoiceToolChoice工具选项(类型 `ToolChoice`)。the requested tool choice
源码Source io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:149–168

stripUnsupportedSamplingParams

protected static void stripUnsupportedSamplingParams(OpenAIRequest request)
static

设置该参数。

Sanitize the request according to the GLM Chat Completions API reference.

参数Parameters
名称Name类型Type说明Description
requestOpenAIRequest请求(类型 `OpenAIRequest`)。the request to sanitize
源码Source io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:186–216

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