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|>"

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) ⇒ Object

Inject <|think|> into the system message when think is enabled. If there is no system message, a bare system message is prepended.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ollama/prompt_adapters/gemma4.rb', line 18

def adapt_messages(messages, think: false)
  return messages unless think

  idx = messages.index { |m| role_of(m) == "system" }
  if idx
    msg = messages[idx]
    content = content_of(msg)
    return messages if content.start_with?(THINK_TAG)

    updated = msg.dup
    set_content(updated, "#{THINK_TAG}#{content}")
    messages = messages.dup
    messages[idx] = updated
  else
    system_msg = { role: "system", content: THINK_TAG }
    messages = [system_msg] + messages
  end

  messages
end

#inject_think_flag?Boolean

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

Returns:

  • (Boolean)


40
41
42
# File 'lib/ollama/prompt_adapters/gemma4.rb', line 40

def inject_think_flag?
  false
end