Module: Legion::LLM::StructuredOutput

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/structured_output.rb

Constant Summary collapse

SCHEMA_CAPABLE_MODELS =
%w[gpt-4o gpt-4o-mini gpt-4-turbo claude-3-5-sonnet claude-4-sonnet claude-4-opus].freeze

Class Method Summary collapse

Class Method Details

.generate(messages:, schema:, model: nil, provider: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/legion/llm/structured_output.rb', line 12

def generate(messages:, schema:, model: nil, provider: nil, **)
  model ||= Legion::LLM.settings[:default_model]
  result = call_with_schema(messages, schema, model, provider: provider, **)

  parsed = Legion::JSON.load(result[:content])
  { data: parsed, raw: result[:content], model: result[:model], valid: true }
rescue ::JSON::ParserError => e
  handle_parse_error(e, messages, schema, model, provider, result, **)
end