Class: Ollama::Tool

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

Overview

Tool, Function, and Parameters classes are defined in previous files This file adds Property class to Parameters

Defined Under Namespace

Classes: Function

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:, function:) ⇒ Tool

Returns a new instance of Tool.



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

def initialize(type:, function:)
  @type = type.to_s
  @function = function
end

Instance Attribute Details

#functionObject (readonly)

Returns the value of attribute function.



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

def function
  @function
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.from_hash(hash) ⇒ Tool

Create instance from hash

Parameters:

  • hash (Hash)

    Hash with type and function keys

Returns:

  • (Tool)

    New Tool instance



39
40
41
42
43
44
45
# File 'lib/ollama/tool.rb', line 39

def self.from_hash(hash)
  normalized = hash.transform_keys(&:to_sym)
  function_hash = normalized[:function] || normalized["function"]
  function = function_hash.is_a?(Hash) ? Function.from_hash(function_hash) : function_hash

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

Instance Method Details

#as_json(*_ignored) ⇒ Object

Override as_json to use explicit attributes instead of tracked ones (since we're using to_h for our specific structure)



56
57
58
# File 'lib/ollama/tool.rb', line 56

def as_json(*_ignored)
  to_h
end

#to_hObject



47
48
49
50
51
52
# File 'lib/ollama/tool.rb', line 47

def to_h
  {
    type: @type,
    function: @function.to_h
  }
end