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 Render

From Leeroopedia
Knowledge Sources
Domains Model Architecture, Chat Template
Last Updated 2025-02-15 00:00 GMT

Overview

Renders conversation messages into the GLM4-MoE-Lite chat format, handling system prompts, thinking blocks, tool definitions, and tool call formatting.

Description

The Renderer struct implements message rendering for GLM4 models. Produces the GLM4 chat format with [gMASK]<sop> prefix, role tags (<|user|>, <|assistant|>, <|system|>, <|observation|>), thinking blocks (<think>...</think>), and XML-style tool calls (<tool_call>...</tool_call>). Supports three thinking modes: interleaved thinking between tool calls, preserved thinking across turns, and per-turn thinking control via thinkValue. Tool arguments are rendered in GLM4's custom XML format with <arg_key>/<arg_value> tags.

Usage

Called by the server to render messages into the model-specific prompt format before tokenization and inference.

Code Reference

Source Location

  • Repository: Ollama
  • File: x/models/glm4_moe_lite/render.go
  • Lines: 1-175

Signature

type Renderer struct{}

func (r *Renderer) Render(messages []api.Message, tools []api.Tool, thinkValue *api.ThinkValue) (string, error)
func renderToolArguments(args api.ToolCallFunctionArguments) string
func formatToolJSON(raw []byte) string

Import

import "github.com/ollama/ollama/x/models/glm4_moe_lite"

I/O Contract

Inputs

Name Type Required Description
messages []api.Message Yes Conversation history
tools []api.Tool No Available tools for the model
thinkValue *api.ThinkValue No Controls thinking mode (nil=enabled, false=disabled)

Outputs

Name Type Description
prompt string Rendered prompt string in GLM4 chat format
error error Rendering error (currently always nil)

Usage Examples

renderer := &glm4_moe_lite.Renderer{}
prompt, err := renderer.Render(
    []api.Message{
        {Role: "user", Content: "What is 2+2?"},
    },
    nil,  // no tools
    nil,  // thinking enabled by default
)
// Result: "[gMASK]<sop><|user|>What is 2+2?<|assistant|><think>"

Related Pages

Page Connections

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