Module: L::Schema

Included in:
L
Defined in:
lib/lammy/schema.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.formatted?(content) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/lammy/schema.rb', line 5

def self.formatted?(content)
  content.is_a?(Hash) && content.key?(:role) && content.key?(:content)
end

Instance Method Details

#assistant(content) ⇒ Object



21
22
23
24
25
# File 'lib/lammy/schema.rb', line 21

def assistant(content)
  return content if L::Schema.formatted?(content)

  { role: :assistant, content: content }
end

#system(content) ⇒ Object



9
10
11
12
13
# File 'lib/lammy/schema.rb', line 9

def system(content)
  return content if L::Schema.formatted?(content)

  { role: :system, content: content }
end

#to_a(object) ⇒ Object

Structured Outputs is a feature that ensures the model will always generate responses that adhere to your supplied JSON Schema, so you don’t need to worry about the model omitting a required key, or hallucinating an invalid enum value. This is a set of helper methods to help you define your JSON Schema easily.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lammy/schema.rb', line 31

def to_a(object)
  {
    'type' => 'object',
    'properties' => {
      'items' => {
        'type' => 'array', 'items' => to_h(object)
      }
    },
    'required' => ['items'],
    'additionalProperties' => false
  }
end

#to_h(object) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/lammy/schema.rb', line 44

def to_h(object)
  {
    'type' => 'object',
    "properties": object.inject({}) { |h, (k, v)| h.merge(k.to_s => { 'type' => v.to_s }) },
    "required": object.keys,
    "additionalProperties": false
  }
end

#user(content, image: nil) ⇒ Object



15
16
17
18
19
# File 'lib/lammy/schema.rb', line 15

def user(content, image: nil)
  return content if L::Schema.formatted?(content)

  { role: :user, content: content, _image: image }.compact
end