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.
-
#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.
33 34 35 |
# File 'lib/ollama_chat/tool_calling.rb', line 33 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.
25 26 27 |
# File 'lib/ollama_chat/tool_calling.rb', line 25 def configured_tools Array(config.tools&.attribute_names&.map(&:to_s)).sort 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.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ollama_chat/tool_calling.rb', line 72 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ollama_chat/tool_calling.rb', line 50 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.
39 40 41 |
# File 'lib/ollama_chat/tool_calling.rb', line 39 def list_tools puts @enabled_tools.sort 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 |