Module: Omakase::Strategies::Predict

Defined in:
lib/omakase/strategies/predict.rb

Overview

One call, no code execution: the model answers straight into the schema.

Constant Summary collapse

CORRECTION =
"Answer again as JSON, and nothing else."

Class Method Summary collapse

Class Method Details

.call(request, task: request.task) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/omakase/strategies/predict.rb', line 11

def call(request, task: request.task)
  chat = request.chat
    .with_instructions(instructions(request))
    .with_schema(request.schema.definition)

  request.schema.cast(chat.ask(task).content)
rescue ContractError => e
  # One correction turn, told exactly what was wrong with the last answer.
  request.schema.cast(chat.ask("#{e.message}\n\n#{CORRECTION}").content)
end

.instructions(request) ⇒ Object

Weaker providers treat the schema as a hint, so it goes in the prompt too.



23
24
25
# File 'lib/omakase/strategies/predict.rb', line 23

def instructions(request)
  "#{request.instructions}\n\nAnswer as JSON matching this schema:\n#{JSON.generate(request.schema.json)}"
end