Module: OllamaChat::ToolCalling
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/tool_calling.rb
Overview
A module that provides tool calling functionality for OllamaChat.
The ToolCalling module encapsulates methods for managing and processing tool calls within the chat application. It handles the registration and execution of tools that can be invoked during conversations, allowing the chat to interact with external systems or perform specialized tasks beyond simple text generation.
Instance Attribute Summary collapse
-
#tool_call_results ⇒ Hash
readonly
The tool_call_results reader returns the tools' results for the chat session if any.
Instance Method Summary collapse
-
#configured_tools ⇒ Array<String>
The configured_tools method returns an array of tool names configured for the chat session.
-
#default_enabled_tools ⇒ Array<String>
The default_enabled_tools method returns an array of tool names that are configured as default tools.
-
#disable_tool ⇒ Object
The disable_tool method allows the user to select and disable a tool from a list of enabled tools.
-
#enable_tool ⇒ Object
The enable_tool method allows the user to select and enable a tool from a list of available tools.
-
#enabled_tools ⇒ Array<String>
Provides access to the list of enabled tools for the chat session.
- #handle_tool_call_results?(&block) ⇒ Boolean private
-
#list_tools ⇒ Object
The list_tools method displays the sorted list of enabled tools.
-
#tool_configured?(name) ⇒ true, false
Checks whether a tool is configured in the chat configuration.
-
#tool_enabled?(name) ⇒ true, false
Checks if a tool is currently enabled for the chat session.
-
#tool_function(name) ⇒ ComplexConfig::Settings
The tool_function method retrieves a tool configuration by name from the chat's configuration.
-
#tool_paths_allowed ⇒ Object
The tool_paths_allowed method returns a hash mapping each enabled tool name to its list of allowed paths or patterns.
-
#tool_registered?(name) ⇒ true, false
Determines whether a tool has been registered in the tool registry.
-
#tools ⇒ Hash
The tools reader returns the registered tools for the chat session.
Instance Attribute Details
#tool_call_results ⇒ Hash (readonly)
The tool_call_results reader returns the tools' results for the chat session if any.
99 100 101 |
# File 'lib/ollama_chat/tool_calling.rb', line 99 def tool_call_results @tool_call_results end |
Instance Method Details
#configured_tools ⇒ Array<String>
The configured_tools method returns an array of tool names configured for the chat session.
This method retrieves the list of available tools from the configuration and returns them as a sorted array of strings. It handles cases where the tools configuration might be nil or empty by returning an empty array.
91 92 93 |
# File 'lib/ollama_chat/tool_calling.rb', line 91 def configured_tools Array(config.tools&.functions&.attribute_names&.map(&:to_s)).sort end |
#default_enabled_tools ⇒ Array<String>
The default_enabled_tools method returns an array of tool names that are configured as default tools.
This method iterates through the configured tools and collects those that are registered and have the default flag set to true. Unregistered tools are skipped with a warning message.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ollama_chat/tool_calling.rb', line 70 def default_enabled_tools result = [] config.tools.functions.each { |n, v| if tool_registered?(n) result << n.to_s if v.default else STDERR.puts "Skipping configuration for unregistered tool %s" % bold { n } end } result end |
#disable_tool ⇒ Object
The disable_tool method allows the user to select and disable a tool from a list of enabled tools.
This method presents a menu of currently enabled tools to the user, allowing them to choose which tool to disable. It uses the chooser to display the available tools and handles the user's selection by updating the session's default tools configuration in the database.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ollama_chat/tool_calling.rb', line 153 def disable_tool choose_with_state do loop do select_tools = enabled_tools select_tools = [ '[EXIT]' ] + select_tools case chosen = choose_entry(select_tools) when '[EXIT]', nil STDOUT.puts "Exiting chooser." return when *select_tools session.tools_default_enabled[chosen] = false unless session.save STDOUT.puts "Could not disable tool %s" % bold { chosen } confirm?(prompt: "\n⏎ Press any key to continue (%s). ", timeout: 3) end end end end end |
#enable_tool ⇒ Object
The enable_tool method allows the user to select and enable a tool from a list of available tools.
This method presents a menu of tools that can be enabled, excluding those that are already enabled. It uses the chooser to display the available tools and handles the user's selection by updating the session's default tools configuration in the database.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ollama_chat/tool_calling.rb', line 126 def enable_tool choose_with_state do loop do select_tools = configured_tools - enabled_tools select_tools = [ '[EXIT]' ] + select_tools case chosen = choose_entry(select_tools) when '[EXIT]', nil STDOUT.puts "Exiting chooser." return when *select_tools session.tools_default_enabled[chosen] = true unless session.save STDOUT.puts "Could not enable tool %s" % bold { chosen } confirm?(prompt: "\n⏎ Press any key to continue (%s). ", timeout: 3) end end end end end |
#enabled_tools ⇒ Array<String>
Provides access to the list of enabled tools for the chat session.
38 39 40 41 42 43 |
# File 'lib/ollama_chat/tool_calling.rb', line 38 def enabled_tools enabled = session.tools_default_enabled config.tools.functions.attribute_names.select { enabled[_1.to_s] }.map(&:to_s) end |
#handle_tool_call_results?(&block) ⇒ Boolean (private)
190 191 192 193 194 195 196 197 198 |
# File 'lib/ollama_chat/tool_calling.rb', line 190 def handle_tool_call_results?(&block) @tool_call_results.each do |tool_name, results| results.each_with_index { |content, index| block.(index, tool_name, content) } end.present? ensure @tool_call_results.clear end |
#list_tools ⇒ Object
The list_tools method displays the sorted list of enabled tools.
This method outputs to standard output the alphabetically sorted list of tool names that are currently enabled in the chat session.
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ollama_chat/tool_calling.rb', line 105 def list_tools STDOUT.puts "Registered tools:" configured_tools.each do |tool| enabled = tool_enabled?(tool) ? ?✓ : ?☐ require_confirmation = tool_function(tool).require_confirmation? ? ?? : ?☐ printf( "%s %s %s\n", enabled, require_confirmation, (enabled ? bold { tool } : tool) ) end STDOUT.puts ?┉ * Tins::Terminal.columns tools_support.show end |
#tool_configured?(name) ⇒ true, false
Checks whether a tool is configured in the chat configuration.
13 14 15 |
# File 'lib/ollama_chat/tool_calling.rb', line 13 def tool_configured?(name) config.tools.functions.attribute_set?(name) end |
#tool_enabled?(name) ⇒ true, false
Checks if a tool is currently enabled for the chat session.
49 50 51 |
# File 'lib/ollama_chat/tool_calling.rb', line 49 def tool_enabled?(name) enabled_tools.member?(name.to_s) end |
#tool_function(name) ⇒ ComplexConfig::Settings
The tool_function method retrieves a tool configuration by name from the chat's configuration.
23 24 25 |
# File 'lib/ollama_chat/tool_calling.rb', line 23 def tool_function(name) config.tools.functions[name] end |
#tool_paths_allowed ⇒ Object
The tool_paths_allowed method returns a hash mapping each enabled tool name to its list of allowed paths or patterns. @return [Hash] a hash where keys are tool names and values are the allowed path lists
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ollama_chat/tool_calling.rb', line 176 def tool_paths_allowed config.tools.functions.to_h. select { |name, value| tool_enabled?(name) && value[:allowed].present? }. sort_by(&:first). each_with_object({}) do |(name, value), hash| hash[name] = value[:allowed].filter_map do pathname = Pathname.new(_1). pathname.exist?.full? { pathname.to_path } end end end |
#tool_registered?(name) ⇒ true, false
Determines whether a tool has been registered in the tool registry.
31 32 33 |
# File 'lib/ollama_chat/tool_calling.rb', line 31 def tool_registered?(name) OllamaChat::Tools.registered?(name) end |
#tools ⇒ Hash
The tools reader returns the registered tools for the chat session.
56 57 58 59 |
# File 'lib/ollama_chat/tool_calling.rb', line 56 def tools @tools_support.off? and return [] enabled_tools.filter_map { OllamaChat::Tools.registered[it]&.to_hash } end |