Class: OpenRouter::PromptTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/open_router/prompt_template.rb

Overview

Main prompt template class that handles variable interpolation, few-shot examples, and chat message formatting

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template: nil, input_variables: [], prefix: nil, suffix: nil, examples: nil, example_template: nil, partial_variables: {}) ⇒ PromptTemplate

Initialize a new PromptTemplate

Examples:

Basic template

template = PromptTemplate.new(
  template: "Translate '{text}' to {language}",
  input_variables: [:text, :language]
)

Few-shot template

template = PromptTemplate.new(
  prefix: "You are a translator. Here are some examples:",
  suffix: "Now translate: {input}",
  examples: [
    { input: "Hello", output: "Bonjour" },
    { input: "Goodbye", output: "Au revoir" }
  ],
  example_template: "Input: {input}\nOutput: {output}",
  input_variables: [:input]
)

Parameters:

  • template (String, nil) (defaults to: nil)

    Main template string with variable placeholders

  • input_variables (Array<Symbol>) (defaults to: [])

    List of required input variables

  • prefix (String, nil) (defaults to: nil)

    Optional prefix text (for few-shot templates)

  • suffix (String, nil) (defaults to: nil)

    Optional suffix text (for few-shot templates)

  • examples (Array<Hash>, nil) (defaults to: nil)

    Optional examples for few-shot learning

  • example_template (String, PromptTemplate, nil) (defaults to: nil)

    Template for formatting examples

  • partial_variables (Hash) (defaults to: {})

    Pre-filled variable values



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/open_router/prompt_template.rb', line 36

def initialize(template: nil, input_variables: [], prefix: nil, suffix: nil,
               examples: nil, example_template: nil, partial_variables: {})
  @template = template
  @input_variables = Array(input_variables).map(&:to_sym)
  @prefix = prefix
  @suffix = suffix
  @examples = examples
  @example_template = build_example_template(example_template)
  @partial_variables = partial_variables.transform_keys(&:to_sym)

  validate_configuration!
end

Instance Attribute Details

#example_templateObject (readonly)

Returns the value of attribute example_template.



7
8
9
# File 'lib/open_router/prompt_template.rb', line 7

def example_template
  @example_template
end

#examplesObject (readonly)

Returns the value of attribute examples.



7
8
9
# File 'lib/open_router/prompt_template.rb', line 7

def examples
  @examples
end

#input_variablesObject (readonly)

Returns the value of attribute input_variables.



7
8
9
# File 'lib/open_router/prompt_template.rb', line 7

def input_variables
  @input_variables
end

#prefixObject (readonly)

Returns the value of attribute prefix.



7
8
9
# File 'lib/open_router/prompt_template.rb', line 7

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



7
8
9
# File 'lib/open_router/prompt_template.rb', line 7

def suffix
  @suffix
end

#templateObject (readonly)

Returns the value of attribute template.



7
8
9
# File 'lib/open_router/prompt_template.rb', line 7

def template
  @template
end

Class Method Details

.build(&block) ⇒ Object

Class method for convenient DSL-style creation

Examples:

DSL usage

template = PromptTemplate.build do
  template "Translate '{text}' to {language}"
  variables :text, :language
end


112
113
114
115
116
# File 'lib/open_router/prompt_template.rb', line 112

def self.build(&block)
  builder = Builder.new
  builder.instance_eval(&block)
  builder.build
end

Instance Method Details

#few_shot_template?Boolean

Check if this is a few-shot template

Returns:

  • (Boolean)


101
102
103
# File 'lib/open_router/prompt_template.rb', line 101

def few_shot_template?
  !@examples.nil? && !@examples.empty?
end

#format(variables = {}) ⇒ String

Format the template with provided variables

Parameters:

  • variables (Hash) (defaults to: {})

    Variable values to interpolate

Returns:

  • (String)

    Formatted prompt text

Raises:

  • (ArgumentError)

    If required variables are missing



54
55
56
57
58
59
60
61
62
63
# File 'lib/open_router/prompt_template.rb', line 54

def format(variables = {})
  variables = @partial_variables.merge(variables.transform_keys(&:to_sym))
  validate_variables!(variables)

  if few_shot_template?
    format_few_shot(variables)
  else
    format_simple(variables)
  end
end

#partial(partial_variables = {}) ⇒ PromptTemplate

Create a partial template with some variables pre-filled

Parameters:

  • partial_variables (Hash) (defaults to: {})

    Variables to pre-fill

Returns:



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/open_router/prompt_template.rb', line 86

def partial(partial_variables = {})
  self.class.new(
    template: @template,
    input_variables: @input_variables - partial_variables.keys.map(&:to_sym),
    prefix: @prefix,
    suffix: @suffix,
    examples: @examples,
    example_template: @example_template,
    partial_variables: @partial_variables.merge(partial_variables.transform_keys(&:to_sym))
  )
end

#to_messages(variables = {}) ⇒ Array<Hash>

Format as chat messages for OpenRouter API

Parameters:

  • variables (Hash) (defaults to: {})

    Variable values to interpolate

  • role (String)

    Role for the message (user, system, assistant)

Returns:

  • (Array<Hash>)

    Messages array for OpenRouter API



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/open_router/prompt_template.rb', line 70

def to_messages(variables = {})
  formatted = format(variables)

  # Split by role markers if present (e.g., "System: ... User: ...")
  if formatted.include?("System:") || formatted.include?("Assistant:") || formatted.include?("User:")
    parse_chat_format(formatted)
  else
    # Default to single user message
    [{ role: "user", content: formatted }]
  end
end