Class: OllamaChat::Tools::Weather

Inherits:
Object
  • Object
show all
Includes:
Ollama
Defined in:
lib/ollama_chat/tools/weather.rb

Overview

A module that provides tool registration and management for OllamaChat.

The Tools module serves as a registry for available tools that can be invoked during chat conversations. It maintains a collection of registered tools and provides methods for registering new tools and accessing the complete set of available tools for use in chat interactions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWeather

The initialize method sets up the weather tool with its name.



14
15
16
# File 'lib/ollama_chat/tools/weather.rb', line 14

def initialize
  @name = 'get_current_weather'
end

Instance Attribute Details

#nameString (readonly)

The name reader returns the name of the tool.

Returns:

  • (String)

    the name of the tool



21
22
23
# File 'lib/ollama_chat/tools/weather.rb', line 21

def name
  @name
end

Instance Method Details

#execute(tool_call, **opts) ⇒ String

Executes a tool call to get current weather information.

This method retrieves temperature data from a weather sensor using the DWD (German Weather Service) API and formats the result into a human-readable string including the temperature value, unit, and timestamp.

Parameters:

  • tool_call (Object)

    the tool call object containing function details

  • opts (Hash)

    additional options

Options Hash (**opts):

  • :config (ComplexConfig::Settings)

    the configuration object

Returns:

  • (String)

    a formatted weather report or error message

  • (String)

    an error message if the weather data could not be retrieved



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ollama_chat/tools/weather.rb', line 73

def execute(tool_call, **opts)
  station_id = opts[:config].tools.get_current_weather.station_id
  sensor = DWDSensor.new(
    sensor_id:   "dwd_#{station_id}",
    station_id:  ,
    logger:      Logger.new(STDOUT)
  )

  time, temp = sensor.measure

  unless time && temp
    return "Could not retrieve temperature for station #{station_id}"
  end

  unit = ?℃

  if tool_call.function.arguments.temperature_unit == 'fahrenheit'
    unit = ?℉
    temp = temp * 9.0 / 5 + 32
  end

  "The temperature was %s %s at the time of %s" % [ temp, unit, time ]
rescue StandardError => e
  "Failed to fetch weather for station #{station_id} #{e.class}: #{e.message}"
end

#to_hashHash

The to_hash method converts the tool to a hash representation.

Returns:

  • (Hash)

    a hash representation of the tool



102
103
104
# File 'lib/ollama_chat/tools/weather.rb', line 102

def to_hash
  tool.to_hash
end

#toolOllama::Tool

The tool method creates and returns a tool definition for getting current weather information.

This method constructs a tool specification that can be used to invoke a weather information service. The tool definition includes the function name, description, and parameter specifications for location and temperature unit.

Returns:

  • (Ollama::Tool)

    a tool definition for retrieving current weather information



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ollama_chat/tools/weather.rb', line 33

def tool
  Tool.new(
    type: 'function',
    function: Tool::Function.new(
      name:,
      description: 'Get the current weather for a location',
      parameters: Tool::Function::Parameters.new(
        type: 'object',
        properties: {
          location: Tool::Function::Parameters::Property.new(
            type: 'string',
            description: 'The location to get the weather for, e.g. San Francisco, CA'
          ),
          temperature_unit: Tool::Function::Parameters::Property.new(
            type: 'string',
            description: "The unit to return the temperature in, either 'celsius' or 'fahrenheit'",
            enum: %w[celsius fahrenheit]
          )
        },
        required: %w[location temperature_unit]
      )
    )
  )
end