Implementation:Ollama Ollama XModels GLM4 Parser
| Knowledge Sources | |
|---|---|
| Domains | Model Architecture, Output Parsing |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
State-machine parser that extracts thinking content, regular content, and tool calls from GLM4-MoE-Lite model output.
Description
Implements a seven-state parser tracking the parsing context: looking for thinking open tag, collecting thinking, collecting content, and collecting tool content. When thinking is enabled, the parser starts in CollectingThinking state since GLM4's prompt ends with <think>. The eat method consumes buffer content based on current state, splitting at XML-like tags (<think>, </think>, <tool_call>, </tool_call>). Tool calls are parsed from XML format into structured api.ToolCall objects.
Usage
Used when serving GLM4-MoE-Lite models to parse the streaming text output into structured thinking, content, and tool call events.
Code Reference
Source Location
- Repository: Ollama
- File: x/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/models/glm4_moe_lite"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| s | string | Yes | New output text chunk from the model |
| done | bool | Yes | Whether this is the final chunk |
Outputs
| Name | Type | Description |
|---|---|---|
| content | string | Regular content extracted from the output |
| thinking | string | Thinking/reasoning content extracted |
| calls | []api.ToolCall | Parsed tool calls |
| err | error | Parsing error if any |
Usage Examples
parser := &Parser{}
parser.Init(tools, nil, nil)
// Process streaming output
content, thinking, calls, err := parser.Add("<think>Let me analyze...", false)
content, thinking, calls, err = parser.Add("</think>The answer is 42.", true)