Class: GEPA::Strategies::InstructionProposalSignature

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/gepa/strategies/instruction_proposal.rb

Constant Summary collapse

PROMPT_TEMPLATE =
"I provided an assistant with the following instructions to perform a task for me:\n```\n<curr_instructions>\n```\n\nThe following are examples of different task inputs provided to the assistant along with the assistant's response for each of them, and some feedback on how the assistant's response could be better:\n```\n<inputs_outputs_feedback>\n```\n\nYour task is to write a new instruction for the assistant.\n\nRead the inputs carefully and identify the input format and infer detailed task description about the task I wish to solve with the assistant.\n\nRead all the assistant responses and the corresponding feedback. Identify all niche and domain specific factual information about the task and include it in the instruction, as a lot of it may not be available to the assistant in the future. The assistant may have utilized a generalizable strategy to solve the task, if so, include that in the instruction as well.\n\nProvide the new instructions within ``` blocks.\n"

Class Method Summary collapse

Class Method Details

.input_keysObject



31
32
33
# File 'lib/gepa/strategies/instruction_proposal.rb', line 31

def self.input_keys
  %w[current_instruction_doc dataset_with_feedback]
end

.output_extractor(output) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gepa/strategies/instruction_proposal.rb', line 48

def self.output_extractor(output)
  stripped = output.to_s.strip
  return { 'new_instruction' => stripped } if stripped.count('```') < 2

  first = stripped.index('```')
  last = stripped.rindex('```')
  if first.nil? || last.nil? || first == last
    { 'new_instruction' => stripped.delete_prefix('```').delete_suffix('```').strip }
  else
    inner = stripped[(first + 3)...last].strip
    { 'new_instruction' => inner.empty? ? stripped : inner }
  end
end

.output_keysObject



36
37
38
# File 'lib/gepa/strategies/instruction_proposal.rb', line 36

def self.output_keys
  %w[new_instruction]
end

.prompt_renderer(input) ⇒ Object



41
42
43
44
45
# File 'lib/gepa/strategies/instruction_proposal.rb', line 41

def self.prompt_renderer(input)
  prompt = PROMPT_TEMPLATE.dup
  prompt = prompt.sub('<curr_instructions>', input.fetch('current_instruction_doc', ''))
  prompt.sub('<inputs_outputs_feedback>', render_samples(input.fetch('dataset_with_feedback', [])))
end

.run(lm, input_dict) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gepa/strategies/instruction_proposal.rb', line 68

def self.run(lm, input_dict)
  prompt = prompt_renderer(input_dict)
  raw_output = if lm.respond_to?(:call)
    lm.call(prompt)
  else
    response = lm.raw_chat([{ role: 'user', content: prompt }])
    response.respond_to?(:content) ? response.content : response
  end

  output_extractor(raw_output.to_s)
end