Implementation:Ollama Ollama Imagegen GLM4 Parser
| Knowledge Sources | |
|---|---|
| Domains | Image Generation, LLM Inference |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Parses GLM4-MoE-Lite model output to extract thinking content, regular content, and tool calls from structured XML tags.
Description
The parser.go file implements a streaming state machine parser for GLM4 model output. It tracks states including looking for think open tags, collecting thinking content, collecting regular content, and collecting tool call XML. The parser handles GLM4's convention where the prompt ends with <think> when thinking is enabled, so output starts directly in thinking mode. It processes <think>/<think>, <tool_call>/<tool_call> boundaries, eating whitespace during transitions. The Add method processes incremental text and returns separated content, thinking, and parsed tool calls (with XML argument extraction).
Usage
Used during GLM4-MoE-Lite text generation to parse streamed output tokens into structured thinking blocks, content, and tool calls for the Ollama API.
Code Reference
Source Location
- Repository: Ollama
- File: x/imagegen/models/glm4_moe_lite/parser.go
- Lines: 1-479
Signature
type Parser struct {
state parserState
buffer strings.Builder
tools []api.Tool
}
func (p *Parser) HasToolSupport() bool
func (p *Parser) HasThinkingSupport() bool
func (p *Parser) Init(tools []api.Tool, lastMessage *api.Message, thinkValue *api.ThinkValue) []api.Tool
func (p *Parser) Add(s string, done bool) (content string, thinking string, calls []api.ToolCall, err error)
Import
import "github.com/ollama/ollama/x/imagegen/models/glm4_moe_lite"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| s | string | Yes | Incremental text from token generation |
| done | bool | Yes | Whether generation has finished |
| tools | []api.Tool | No | Available tools for tool call validation |
Outputs
| Name | Type | Description |
|---|---|---|
| content | string | Parsed regular content text |
| thinking | string | Parsed thinking/reasoning text |
| calls | []api.ToolCall | Parsed tool calls with function name and arguments |
| err | error | Parsing error if tool call XML is malformed |
Usage Examples
parser := &glm4_moe_lite.Parser{}
parser.Init(tools, nil, nil)
content, thinking, toolCalls, err := parser.Add("<think>Let me analyze...", false)
// thinking = "Let me analyze..."
content, thinking, toolCalls, err = parser.Add("</think>The answer is 42", true)
// content = "The answer is 42"