class Riffer::Agent::StructuredOutput
Attributes
Public Class Methods
Source
# File lib/riffer/agent/structured_output.rb, line 12 def initialize(params, strategy: :native) @params = params @strategy = strategy end
Public Instance Methods
Source
# File lib/riffer/agent/structured_output.rb, line 25 def json_schema(strict: false) @params.to_json_schema(strict: strict) end
Source
# File lib/riffer/agent/structured_output.rb, line 45 def parse_and_validate(json_string) json, parsed = Riffer::Agent::StructuredOutput::Parser.extract(json_string) return Result.new(error: "JSON parse error: no JSON object found in the response") if parsed.nil? validated = @params.validate(parsed) Result.new(object: validated, json: json) rescue Riffer::ValidationError => e Result.new(error: "Validation error: #{e.message}") end
Source
# File lib/riffer/agent/structured_output.rb, line 32 def prompt_instructions schema = json_schema [ "When you give your final answer, respond with only a JSON object that conforms to " \ "the following JSON Schema, with no other text before or after it:", JSON.generate(schema), "Example of the shape (placeholder values):\n#{JSON.generate(placeholder(schema))}", "Only call tools when the request needs them. If you can answer directly, reply with the JSON object right away.", ].join("\n\n") end
Worded for the final answer only, so the model can still call tools first.
Source
# File lib/riffer/agent/structured_output.rb, line 19 def prompted? @strategy == :prompted end