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.
-
#list_tools ⇒ Object
The list_tools method displays the sorted list of enabled tools.
-
#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.
54 55 56 |
# File 'lib/ollama_chat/tool_calling.rb', line 54 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.
46 47 48 |
# File 'lib/ollama_chat/tool_calling.rb', line 46 def configured_tools Array(config.tools&.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.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ollama_chat/tool_calling.rb', line 25 def default_enabled_tools result = [] config.tools.each { |n, v| if OllamaChat::Tools.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 removing the chosen tool from the list of enabled tools and sorting the list afterwards.
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ollama_chat/tool_calling.rb', line 100 def disable_tool select_tools = @enabled_tools select_tools += [ '[EXIT]' ] case chosen = OllamaChat::Utils::Chooser.choose(select_tools) when '[EXIT]', nil STDOUT.puts "Exiting chooser." return when *select_tools @enabled_tools.delete chosen puts "Disabled tool %s" % bold { chosen } 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 adding the chosen tool to the list of enabled tools and sorting the list.
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ollama_chat/tool_calling.rb', line 78 def enable_tool select_tools = configured_tools - @enabled_tools select_tools += [ '[EXIT]' ] case chosen = OllamaChat::Utils::Chooser.choose(select_tools) when '[EXIT]', nil STDOUT.puts "Exiting chooser." return when *select_tools @enabled_tools << chosen @enabled_tools.sort! puts "Enabled tool %s" % bold { chosen } end 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.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ollama_chat/tool_calling.rb', line 60 def list_tools configured_tools.each do |tool| enabled = @enabled_tools.member?(tool) ? ?✓ : ?☐ require_confirmation = config.tools[tool].require_confirmation? ? ?? : ?☐ printf( "%s %s %s\n", enabled, require_confirmation, (enabled ? bold { tool } : tool) ) end end |
#tools ⇒ Hash
The tools reader returns the registered tools for the chat session.
12 13 14 |
# File 'lib/ollama_chat/tool_calling.rb', line 12 def tools @enabled_tools.map { OllamaChat::Tools.registered[it]&.to_hash }.compact end |