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 Imagegen GLM4 Render

From Leeroopedia
Knowledge Sources
Domains Image Generation, LLM Inference
Last Updated 2025-02-15 00:00 GMT

Overview

Renders Ollama API messages into the GLM4-MoE-Lite chat format with thinking mode support and structured tool calling.

Description

The render.go file implements the Renderer for GLM4-MoE-Lite's chat template format. It converts Ollama API messages into the GLM4 format with [gMASK]<sop> prefix, role tags (<|user|>, <|assistant|>, <|system|>, <|observation|>), thinking blocks (<think>...</think>), and tool call XML (<tool_call>func_name<arg_key>key</arg_key><arg_value>val</arg_value></tool_call>). The renderer supports three thinking modes: interleaved (between tool calls), preserved (retaining reasoning from previous turns), and turn-level (controllable via thinkValue). Tool definitions are formatted in <tools> XML blocks. The file also handles JSON formatting with spaces after : and , for GLM4 compatibility.

Usage

Used by the GLM4-MoE-Lite model during text generation to format conversation history into the model's expected input format.

Code Reference

Source Location

  • Repository: Ollama
  • File: x/imagegen/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/imagegen/models/glm4_moe_lite"

I/O Contract

Inputs

Name Type Required Description
messages []api.Message Yes Conversation messages to render
tools []api.Tool No Available tools for tool call formatting
thinkValue *api.ThinkValue No Controls thinking mode (nil = enabled)

Outputs

Name Type Description
string string Rendered prompt in GLM4 chat format
error error Rendering error

Usage Examples

renderer := &glm4_moe_lite.Renderer{}
prompt, err := renderer.Render(messages, tools, nil)
// Output: "[gMASK]<sop><|user|>Hello<|assistant|><think>"

Related Pages

Page Connections

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