Class: Ollama::ToolDSL

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

Overview

Optional class-based DSL for tools.

Subclasses produce a tool hash compatible with tools:.

Example:

class WeatherTool < Ollama::ToolDSL description "Get current weather for a city" tool_name "get_weather" input do string :city, description: "The name of the city" string :unit, optional: true, default: "celsius" end call { constructor } end

client.chat(messages: [...], tools: [WeatherTool.to_tool_hash])

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.input_schemaObject (readonly)

Returns the value of attribute input_schema.



23
24
25
# File 'lib/ollama/tool_dsl.rb', line 23

def input_schema
  @input_schema
end

.output_schemaObject (readonly)

Returns the value of attribute output_schema.



23
24
25
# File 'lib/ollama/tool_dsl.rb', line 23

def output_schema
  @output_schema
end

.tool_descriptionObject (readonly)

Returns the value of attribute tool_description.



23
24
25
# File 'lib/ollama/tool_dsl.rb', line 23

def tool_description
  @tool_description
end

.user_blockObject (readonly)

Returns the value of attribute user_block.



23
24
25
# File 'lib/ollama/tool_dsl.rb', line 23

def user_block
  @user_block
end

Class Method Details

.call(&block) ⇒ Object



42
43
44
45
# File 'lib/ollama/tool_dsl.rb', line 42

def call(&block)
  @user_block = block
  def_constructor
end

.description(text) ⇒ Object



25
26
27
# File 'lib/ollama/tool_dsl.rb', line 25

def description(text)
  @tool_description = text
end

.input(&block) ⇒ Object



34
35
36
# File 'lib/ollama/tool_dsl.rb', line 34

def input(&block)
  @input_schema = SchemaDSL.define(&block).to_h
end

.output(&block) ⇒ Object



38
39
40
# File 'lib/ollama/tool_dsl.rb', line 38

def output(&block)
  @output_schema = SchemaDSL.define(&block).to_h
end

.to_tool_hashObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ollama/tool_dsl.rb', line 47

def to_tool_hash
  function = {
    "name" => tool_name || tool_method_name,
    "description" => tool_description || "",
    "strict" => true
  }
  function["parameters"] = input_schema if input_schema

  {
    "type" => "function",
    "function" => function
  }
end

.tool_name(text = nil) ⇒ Object



29
30
31
32
# File 'lib/ollama/tool_dsl.rb', line 29

def tool_name(text = nil)
  @tool_name = text unless text.nil?
  @tool_name
end