Class: Ollama::SchemaDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/schema_dsl.rb

Overview

Tiny Ruby DSL for JSON Schema objects.

This is additive only: it produces schema hashes accepted by the existing Client.generate(schema:) and Client.chat(format:) paths.

Example:

schema = Ollama::SchemaDSL.define do
string :direction, enum: %w[buy sell wait]
number :confidence, minimum: 0, maximum: 1
end.to_h

client.generate(prompt: "...", schema: schema)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaDSL

Returns a new instance of SchemaDSL.



20
21
22
23
# File 'lib/ollama/schema_dsl.rb', line 20

def initialize
  @properties = {}
  @required = []
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



18
19
20
# File 'lib/ollama/schema_dsl.rb', line 18

def properties
  @properties
end

#requiredObject (readonly)

Returns the value of attribute required.



18
19
20
# File 'lib/ollama/schema_dsl.rb', line 18

def required
  @required
end

Class Method Details

.define(&block) ⇒ Object



25
26
27
28
29
# File 'lib/ollama/schema_dsl.rb', line 25

def self.define(&block)
  instance = new
  instance.instance_eval(&block)
  instance
end

Instance Method Details

#array(name, items: nil, optional: false, default: nil, description: nil) ⇒ Object



55
56
57
58
59
# File 'lib/ollama/schema_dsl.rb', line 55

def array(name, items: nil, optional: false, default: nil, description: nil)
  prop = { "type" => "array" }
  prop["items"] = items if items
  add_property_entry(name, prop, optional: optional, default: default, description: description)
end

#boolean(name, optional: false, default: nil, description: nil) ⇒ Object



51
52
53
# File 'lib/ollama/schema_dsl.rb', line 51

def boolean(name, optional: false, default: nil, description: nil)
  add_property(name, "boolean", optional: optional, default: default, description: description)
end

#integer(name, minimum: nil, maximum: nil, optional: false, default: nil, description: nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ollama/schema_dsl.rb', line 43

def integer(name, minimum: nil, maximum: nil, optional: false, default: nil, description: nil)
  add_property(
    name, "integer",
    minimum: minimum, maximum: maximum, optional: optional,
    default: default, description: description
  )
end

#number(name, minimum: nil, maximum: nil, optional: false, default: nil, description: nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ollama/schema_dsl.rb', line 35

def number(name, minimum: nil, maximum: nil, optional: false, default: nil, description: nil)
  add_property(
    name, "number",
    minimum: minimum, maximum: maximum, optional: optional,
    default: default, description: description
  )
end

#object(name, properties: {}, optional: false, default: nil, description: nil) ⇒ Object



61
62
63
64
# File 'lib/ollama/schema_dsl.rb', line 61

def object(name, properties: {}, optional: false, default: nil, description: nil)
  prop = { "type" => "object", "properties" => properties }
  add_property_entry(name, prop, optional: optional, default: default, description: description)
end

#string(name, enum: nil, optional: false, default: nil, description: nil) ⇒ Object



31
32
33
# File 'lib/ollama/schema_dsl.rb', line 31

def string(name, enum: nil, optional: false, default: nil, description: nil)
  add_property(name, "string", enum: enum, optional: optional, default: default, description: description)
end

#to_hObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/ollama/schema_dsl.rb', line 66

def to_h
  schema = {
    "type" => "object",
    "properties" => properties,
    "required" => required,
    "additionalProperties" => false
  }
  schema.delete("required") if required.empty?
  schema
end