Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama XModels GLM4 Parser

From Leeroopedia
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)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment