Class: OmniAI::Schema::Format
- Inherits:
-
Object
- Object
- OmniAI::Schema::Format
- Defined in:
- lib/omniai/schema/format.rb
Overview
Constant Summary collapse
- BLOCK_REGEX =
/```(?:json)?\s*(?<text>.+)\s*```/m
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name:, schema:) ⇒ Format
constructor
A new instance of Format.
- #parse(text) ⇒ Hash
-
#prompt ⇒ String
A helper used for LLMs that do not support passing in a schema using a dedicated argument.
- #serialize(additional_properties: false) ⇒ Hash
Constructor Details
#initialize(name:, schema:) ⇒ Format
Returns a new instance of Format.
46 47 48 49 |
# File 'lib/omniai/schema/format.rb', line 46 def initialize(name:, schema:) @name = name @schema = schema end |
Instance Attribute Details
#name ⇒ String
22 23 24 |
# File 'lib/omniai/schema/format.rb', line 22 def name @name end |
#schema ⇒ OmniAI::Schema::Object
26 27 28 |
# File 'lib/omniai/schema/format.rb', line 26 def schema @schema end |
Class Method Details
.deserialize(data) ⇒ OmniAI::Schema::Format
37 38 39 40 41 42 |
# File 'lib/omniai/schema/format.rb', line 37 def self.deserialize(data) name = data["name"] || data[:name] schema = OmniAI::Schema.deserialize(data["schema"] || data[:schema]) new(name:, schema:) end |
Instance Method Details
#parse(text) ⇒ Hash
72 73 74 75 76 77 78 79 |
# File 'lib/omniai/schema/format.rb', line 72 def parse(text) match = BLOCK_REGEX.match(text) text = match[:text] if match schema.parse(JSON.parse(text)) rescue JSON::ParserError => e raise OmniAI::ParseError, "Unable to parse JSON text=#{text.inspect} message=#{e..inspect}." end |
#prompt ⇒ String
A helper used for LLMs that do not support passing in a schema using a dedicated argument.
87 88 89 90 91 92 93 94 95 |
# File 'lib/omniai/schema/format.rb', line 87 def prompt <<~TEXT Your must respond with ONLY valid JSON matching this exact schema: #{JSON.generate(schema.serialize)} Do not include any preamble, explanation, heredocs, etc. Return only the JSON. TEXT end |
#serialize(additional_properties: false) ⇒ Hash
57 58 59 60 61 62 |
# File 'lib/omniai/schema/format.rb', line 57 def serialize(additional_properties: false) { name:, schema: schema.serialize(additional_properties:), } end |