GLMFormatter
public class GLMFormatter extends OpenAIChatFormatter
io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:72–247GLM 格式化器,与模型服务通信并返回生成或向量化结果。位于 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
nameparameter in messages -
tool_choiceonly supports"auto"; forced choices are degraded, whilenoneremoves tools to preserve the no-tool-call contract -
Does NOT support the
strictparameter in tool definitions -
Does NOT support
frequency_penalty/presence_penalty/thinking_budget; they are stripped from the request -
Only supports
max_tokens;max_completion_tokensis mapped tomax_tokenswhen the latter is not set -
temperaturerange is [0.0, 1.0] (OpenAI allows up to 2.0) andtop_prange 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
构造器详情Constructor Detail
GLMFormatter
public GLMFormatter()
初始化 GLMFormatter 实例。
Creates a new instance.
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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msgs | List<Msg> | 消息列表(类型 `List`)。The msgs. |
List<OpenAIMessage>io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:80–84convertMessage
protected OpenAIMessage convertMessage(Msg msg, boolean hasMedia)
转换消息。
Converts the message.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息(类型 `Msg`)。The msg. |
hasMedia | boolean | 有 Media(类型 `boolean`)。The has media. |
OpenAIMessageio/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:86–93supportsStrict
protected boolean supportsStrict()
返回 supportsStrict 结果。
Returns the supports strict.
booleanio/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:95–98applyOptions
public void applyOptions(OpenAIRequest request, GenerateOptions options, GenerateOptions defaultOptions)
应用选项。
Performs the apply options operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。The request. |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。The options. |
defaultOptions | GenerateOptions | 默认选项(类型 `GenerateOptions`)。The default options. |
io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:100–105applyToolChoice
public void applyToolChoice(OpenAIRequest request, ToolChoice toolChoice)
应用工具选项。
Performs the apply tool choice operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。The request. |
toolChoice | ToolChoice | 工具选项(类型 `ToolChoice`)。The tool choice. |
io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:107–110ensureUserMessage
protected static List<OpenAIMessage> ensureUserMessage(List<OpenAIMessage> messages)
设置确保用户消息。
Ensure at least one user message exists in the conversation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<OpenAIMessage> | 消息到检查。the messages to check |
List<OpenAIMessage>io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:123–135applyGLMToolChoice
protected static void applyGLMToolChoice(OpenAIRequest request, ToolChoice toolChoice)
应用GLM 工具选项。
Apply GLM-specific tool choice handling.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。the request to apply tool choice to |
toolChoice | ToolChoice | 工具选项(类型 `ToolChoice`)。the requested tool choice |
io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:149–168stripUnsupportedSamplingParams
protected static void stripUnsupportedSamplingParams(OpenAIRequest request)
设置该参数。
Sanitize the request according to the GLM Chat Completions API reference.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OpenAIRequest | 请求(类型 `OpenAIRequest`)。the request to sanitize |
io/agentscope/extensions/model/openai/compat/glm/GLMFormatter.java:186–216