Class: Ollama::PromptAdapters::Gemma4

Inherits:
Base
  • Object
show all
Defined in:
lib/ollama/prompt_adapters/gemma4.rb

Overview

Gemma 4 prompt adapter.

Gemma 4 activates thinking by prepending <|think|> to the system prompt content rather than via a think: true API flag. This adapter injects that tag automatically when think: true is requested.

Multimodal ordering: image/audio parts are expected before text, which is handled by MultimodalInput before messages are built.

Constant Summary collapse

THINK_TAG =
"<|think|>"
TOOL_INSTRUCTION =
"\n\nYou have access to tools. If a tool can help answer the user question, " \
"call it using the provided function schema."

Instance Attribute Summary

Attributes inherited from Base

#profile

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Ollama::PromptAdapters::Base

Instance Method Details

#adapt_messages(messages, think: false, tools: nil) ⇒ Object

Inject <|think|> into the system message when think is enabled. Also inject tool usage instructions if tools are present.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ollama/prompt_adapters/gemma4.rb', line 20

def adapt_messages(messages, think: false, tools: nil)
  system_idx = messages.index { |m| role_of(m) == "system" }

  if system_idx
    messages = messages.dup
    msg = messages[system_idx].dup
    content = content_of(msg)

    # Add thinking tag if requested
    content = "#{THINK_TAG}#{content}" if think && !content.start_with?(THINK_TAG)

    # Add tool instruction if tools present
    content = "#{content}#{TOOL_INSTRUCTION}" if tools&.any? && !content.include?(TOOL_INSTRUCTION)

    set_content(msg, content)
    messages[system_idx] = msg
  elsif think || tools&.any?
    # Create a system message if none exists but we need to inject tags/instructions
    content = +""
    content << THINK_TAG if think
    content << TOOL_INSTRUCTION if tools&.any?
    messages = [{ role: "system", content: content }] + messages
  end

  messages
end

#inject_think_flag?Boolean

Gemma 4 uses the system-prompt tag — do NOT send think: true to API.

Returns:

  • (Boolean)


48
49
50
# File 'lib/ollama/prompt_adapters/gemma4.rb', line 48

def inject_think_flag?
  false
end