Class: OpenRouter::PromptTemplate
- Inherits:
-
Object
- Object
- OpenRouter::PromptTemplate
- 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
-
#example_template ⇒ Object
readonly
Returns the value of attribute example_template.
-
#examples ⇒ Object
readonly
Returns the value of attribute examples.
-
#input_variables ⇒ Object
readonly
Returns the value of attribute input_variables.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
-
.build(&block) ⇒ Object
Class method for convenient DSL-style creation.
Instance Method Summary collapse
-
#few_shot_template? ⇒ Boolean
Check if this is a few-shot template.
-
#format(variables = {}) ⇒ String
Format the template with provided variables.
-
#initialize(template: nil, input_variables: [], prefix: nil, suffix: nil, examples: nil, example_template: nil, partial_variables: {}) ⇒ PromptTemplate
constructor
Initialize a new PromptTemplate.
-
#partial(partial_variables = {}) ⇒ PromptTemplate
Create a partial template with some variables pre-filled.
-
#to_messages(variables = {}) ⇒ Array<Hash>
Format as chat messages for OpenRouter API.
Constructor Details
#initialize(template: nil, input_variables: [], prefix: nil, suffix: nil, examples: nil, example_template: nil, partial_variables: {}) ⇒ PromptTemplate
Initialize a new PromptTemplate
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_template ⇒ Object (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 |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
7 8 9 |
# File 'lib/open_router/prompt_template.rb', line 7 def examples @examples end |
#input_variables ⇒ Object (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 |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/open_router/prompt_template.rb', line 7 def prefix @prefix end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
7 8 9 |
# File 'lib/open_router/prompt_template.rb', line 7 def suffix @suffix end |
#template ⇒ Object (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
Instance Method Details
#few_shot_template? ⇒ Boolean
Check if this is a few-shot template
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
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
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
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/open_router/prompt_template.rb', line 70 def (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 |