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
# File 'lib/omakase/strategies/predict.rb', line 11

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

  request.schema.cast(chat.ask(task, with: request.attachments).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

.instruct(chat, stable, context) ⇒ Object

The stable text first and marked for the cache; the context, which changes from call to call, after it.



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

def instruct(chat, stable, context)
  chat = chat.with_instructions(stable, cache_until_here: true)
  context.empty? ? chat : chat.with_instructions(context, append: true)
end

.instructions(request) ⇒ Object

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



29
30
31
# File 'lib/omakase/strategies/predict.rb', line 29

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