Class GLMFormatter
- All Implemented Interfaces:
io.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse, OpenAIRequest>
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();
-
Field Summary
Fields inherited from class io.agentscope.extensions.model.openai.formatter.OpenAIBaseFormatter
messageConverter, responseParser -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static voidapplyGLMToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice) Apply GLM-specific tool choice handling.voidapplyOptions(OpenAIRequest request, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions) Apply generation options to the request.voidapplyToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice) Apply tool choice configuration to the request.protected OpenAIMessageconvertMessage(io.agentscope.core.message.Msg msg, boolean hasMedia) Convert one AgentScope message to OpenAI message format.protected List<OpenAIMessage>protected static List<OpenAIMessage>ensureUserMessage(List<OpenAIMessage> messages) Ensure at least one user message exists in the conversation.protected static voidSanitize the request according to the GLM Chat Completions API reference.protected booleanReturns whether this formatter's target API supports the strict parameter in tool definitions.Methods inherited from class io.agentscope.extensions.model.openai.formatter.OpenAIChatFormatter
applyAdditionalBodyParams, applyMaxTokens, applyTools, doFormatMethods inherited from class io.agentscope.extensions.model.openai.formatter.OpenAIBaseFormatter
applyToolChoice, applyTools, buildRequest, buildRequest, parseResponseMethods inherited from class io.agentscope.core.formatter.AbstractBaseFormatter
convertToolResultToString, extractTextContent, extractTextContent, format, format, formatRoleLabel, getOptionOrDefault, hasMediaContent, saveBase64DataToTempFile, shouldBypassHistory
-
Constructor Details
-
GLMFormatter
public GLMFormatter()
-
-
Method Details
-
doFormat
- Overrides:
doFormatin classOpenAIChatFormatter
-
convertMessage
Description copied from class:OpenAIChatFormatterConvert one AgentScope message to OpenAI message format.Provider-specific subclasses may override this when the target API supports a slightly different message shape.
- Overrides:
convertMessagein classOpenAIChatFormatter- Parameters:
msg- the message to converthasMedia- whether the message contains media content- Returns:
- converted OpenAI message
-
supportsStrict
protected boolean supportsStrict()Description copied from class:OpenAIChatFormatterReturns whether this formatter's target API supports the strict parameter in tool definitions.Subclasses can override this method to disable strict parameter for providers that don't support it (e.g., DeepSeek).
- Overrides:
supportsStrictin classOpenAIChatFormatter- Returns:
- true if strict parameter is supported, false otherwise
-
applyOptions
public void applyOptions(OpenAIRequest request, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions) Description copied from class:OpenAIBaseFormatterApply generation options to the request. Subclasses implement provider-specific option handling.- Specified by:
applyOptionsin interfaceio.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse, OpenAIRequest> - Overrides:
applyOptionsin classOpenAIChatFormatter- Parameters:
request- OpenAI request DTOoptions- Generation options to applydefaultOptions- Default options to use if options parameter is null
-
applyToolChoice
Description copied from class:OpenAIBaseFormatterApply tool choice configuration to the request. Subclasses implement provider-specific tool choice handling.- Specified by:
applyToolChoicein interfaceio.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse, OpenAIRequest> - Overrides:
applyToolChoicein classOpenAIChatFormatter- Parameters:
request- OpenAI request DTOtoolChoice- Tool choice configuration (null means auto)
-
ensureUserMessage
Ensure at least one user message exists in the conversation.GLM API requires at least one user message. If no user message exists, a placeholder is added at the end (error 1214 otherwise).
This method is static to allow sharing with
GLMMultiAgentFormatter.- Parameters:
messages- the messages to check- Returns:
- messages with a user message ensured
-
applyGLMToolChoice
protected static void applyGLMToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice) Apply GLM-specific tool choice handling.Per the latest Zhipu API reference,
tool_choicedefaults to and only supports"auto". Unsupported forced choices are degraded to"auto". ForToolChoice.None, tools are removed instead.This method is static to allow sharing with
GLMMultiAgentFormatter.- Parameters:
request- the request to apply tool choice totoolChoice- the requested tool choice
-
stripUnsupportedSamplingParams
Sanitize the request according to the GLM Chat Completions API reference.Applied adaptations:
frequency_penalty/presence_penalty/thinking_budgetare not supported and removedmax_completion_tokensis not supported; it is mapped tomax_tokenswhenmax_tokensis not settemperatureis clamped to [0.0, 1.0] andtop_pto [0.01, 1.0]
This method is static to allow sharing with
GLMMultiAgentFormatter.- Parameters:
request- the request to sanitize
-