Class: Ollama::Tool::Function

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

Overview

Function specification for a tool, defining name, description, and parameters.

Defined Under Namespace

Classes: Parameters

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(name:, description:, parameters: nil) ⇒ Function

Returns a new instance of Function.



29
30
31
32
33
34
35
36
37
# File 'lib/ollama/tool/function.rb', line 29

def initialize(name:, description:, parameters: nil)
  @name = name.to_s
  @description = description.to_s
  @parameters = parameters || Function::Parameters.new(
    type: "object",
    properties: {},
    required: []
  )
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



27
28
29
# File 'lib/ollama/tool/function.rb', line 27

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/ollama/tool/function.rb', line 27

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



27
28
29
# File 'lib/ollama/tool/function.rb', line 27

def parameters
  @parameters
end

Class Method Details

.from_hash(hash) ⇒ Function

Create instance from hash

Parameters:

  • hash (Hash)

    Hash with name, description, and optional parameters

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ollama/tool/function.rb', line 43

def self.from_hash(hash)
  normalized = hash.transform_keys(&:to_sym)
  params_hash = normalized[:parameters] || normalized["parameters"]

  parameters = if params_hash.is_a?(Hash)
                 Function::Parameters.from_hash(params_hash)
               elsif params_hash
                 params_hash
               end

  new(
    name: normalized[:name] || normalized["name"],
    description: normalized[:description] || normalized["description"],
    parameters: parameters
  )
end

Instance Method Details

#as_json(*_ignored) ⇒ Object

Override as_json to use explicit attributes instead of tracked ones



70
71
72
# File 'lib/ollama/tool/function.rb', line 70

def as_json(*_ignored)
  to_h
end

#to_hObject



60
61
62
63
64
65
66
67
# File 'lib/ollama/tool/function.rb', line 60

def to_h
  hash = {
    name: @name,
    description: @description
  }
  hash[:parameters] = @parameters.to_h if @parameters
  hash
end