Class: OllamaChat::Tools::Grep
- Inherits:
-
Object
- Object
- OllamaChat::Tools::Grep
- Includes:
- Ollama
- Defined in:
- lib/ollama_chat/tools/grep.rb
Overview
A tool for searching files using grep command.
This tool allows the chat client to execute grep commands to search for patterns in files. It integrates with the Ollama tool calling system to provide file search capabilities within the language model’s context.
Instance Attribute Summary collapse
-
#name ⇒ OllamaChat::Tool
readonly
Returns the tool definition for use with the Ollama API.
Instance Method Summary collapse
-
#execute(tool_call, **opts) ⇒ Hash
Executes the grep command with the provided arguments.
-
#initialize ⇒ Grep
constructor
Initializes a new Grep tool instance.
-
#to_hash ⇒ Hash
Converts the tool to a hash representation.
-
#tool ⇒ OllamaChat::Tool
Returns the tool definition for use with the Ollama API.
Constructor Details
#initialize ⇒ Grep
Initializes a new Grep tool instance
10 11 12 |
# File 'lib/ollama_chat/tools/grep.rb', line 10 def initialize @name = 'execute_grep' end |
Instance Attribute Details
#name ⇒ OllamaChat::Tool (readonly)
Returns the tool definition for use with the Ollama API
This method returns a tool definition that describes the grep tool’s capabilities, parameters, and usage for the LLM.
20 21 22 |
# File 'lib/ollama_chat/tools/grep.rb', line 20 def name @name end |
Instance Method Details
#execute(tool_call, **opts) ⇒ Hash
Executes the grep command with the provided arguments
This method runs the grep command with the specified pattern, path, and maximum results limit. It uses Shellwords.escape for security and OllamaChat::Utils::Fetcher to execute the command.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ollama_chat/tools/grep.rb', line 70 def execute(tool_call, **opts) config = opts[:config] args = tool_call.function.arguments pattern = Shellwords.escape(args.pattern) path = Shellwords.escape(Pathname.new(args.path || '.').) max_results = args.max_results || 100 cmd = config.tools.execute_grep.cmd % { max_results:, path:, pattern: } result = OllamaChat::Utils::Fetcher.execute(cmd, &:read) { cmd:, result: }.to_json rescue StandardError => e { error: e, message: e. }.to_json end |
#to_hash ⇒ Hash
Converts the tool to a hash representation
This method returns the tool definition as a hash, which is useful for serialization or when working with APIs that expect hash representations.
92 93 94 |
# File 'lib/ollama_chat/tools/grep.rb', line 92 def to_hash tool.to_hash end |
#tool ⇒ OllamaChat::Tool
Returns the tool definition for use with the Ollama API
This method returns a tool definition that describes the grep tool’s capabilities, parameters, and usage for the LLM.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ollama_chat/tools/grep.rb', line 28 def tool Tool.new( type: 'function', function: Tool::Function.new( name:, description: 'Search for a pattern in files using grep', parameters: Tool::Function::Parameters.new( type: 'object', properties: { pattern: Tool::Function::Parameters::Property.new( type: 'string', description: 'The regex pattern to search for' ), path: Tool::Function::Parameters::Property.new( type: 'string', description: 'Directory or file to search (defaults to current directory)' ), max_results: Tool::Function::Parameters::Property.new( type: 'integer', description: 'Maximum number of matches to return (optional)' ) }, required: %w[pattern] ) ) ) end |