Class: Ollama::Tool::Function::Parameters

Inherits:
Object
  • Object
show all
Includes:
DTO
Defined in:
lib/ollama/tool/function/parameters.rb,
lib/ollama/tool/function/parameters/property.rb

Overview

Parameters specification for a tool function

Defines the structure of parameters that a function tool accepts. This matches JSON Schema format used by Ollama's tool calling API.

Example:

parameters = Ollama::Tool::Function::Parameters.new(
type: 'object',
properties: {
  location: Ollama::Tool::Function::Parameters::Property.new(
    type: 'string',
    description: 'The location to get weather for'
  )
},
required: %w[location]
)

# Deserialize from hash
parameters = Ollama::Tool::Function::Parameters.from_hash({ type: 'object', ... })

Defined Under Namespace

Classes: Property

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DTO

#==, #as_array, #as_array_of_hashes, #as_hash, #empty?, included, #to_json

Constructor Details

#initialize(type:, properties: {}, required: []) ⇒ Parameters

Returns a new instance of Parameters.



35
36
37
38
39
# File 'lib/ollama/tool/function/parameters.rb', line 35

def initialize(type:, properties: {}, required: [])
  @type = type.to_s
  @properties = normalize_properties(properties)
  @required = Array(required).map(&:to_s)
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



33
34
35
# File 'lib/ollama/tool/function/parameters.rb', line 33

def properties
  @properties
end

#requiredObject (readonly)

Returns the value of attribute required.



33
34
35
# File 'lib/ollama/tool/function/parameters.rb', line 33

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



33
34
35
# File 'lib/ollama/tool/function/parameters.rb', line 33

def type
  @type
end

Class Method Details

.from_hash(hash) ⇒ Parameters

Create instance from hash

Parameters:

  • hash (Hash)

    Hash with type, properties, and optional required keys

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ollama/tool/function/parameters.rb', line 45

def self.from_hash(hash)
  normalized = hash.transform_keys(&:to_sym)
  props_hash = normalized[:properties] || normalized["properties"] || {}

  properties = props_hash.transform_values do |value|
    case value
    when Hash
      Property.from_hash(value)
    when Property
      value
    else
      raise Error, "Invalid property type: #{value.class}. Use Property or Hash"
    end
  end

  new(
    type: normalized[:type] || normalized["type"],
    properties: properties,
    required: normalized[:required] || normalized["required"] || []
  )
end

Instance Method Details

#as_json(*_ignored) ⇒ Object

Override as_json to use explicit attributes instead of tracked ones



75
76
77
# File 'lib/ollama/tool/function/parameters.rb', line 75

def as_json(*_ignored)
  to_h
end

#to_hObject



67
68
69
70
71
72
# File 'lib/ollama/tool/function/parameters.rb', line 67

def to_h
  hash = { "type" => @type }
  hash["properties"] = @properties.transform_values(&:to_h) unless @properties.empty?
  hash["required"] = @required unless @required.empty?
  hash
end