Class: OllamaChat::Tools::Grep

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeGrep

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

#nameOllamaChat::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.

Returns:

  • (OllamaChat::Tool)

    The tool definition



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.

Examples:

result = grep_tool.execute(tool_call, config: config)

Parameters:

  • tool_call (OllamaChat::Tool::Call)

    The tool call with arguments

  • opts (Hash)

    Additional options

Options Hash (**opts):

  • :config (Hash)

    Configuration options including tool settings

Returns:

  • (Hash)

    The execution result with command and output

Raises:

  • (StandardError)

    If the command execution fails



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 || '.').expand_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.message }.to_json
end

#to_hashHash

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.

Examples:

tool_hash = grep_tool.to_hash

Returns:

  • (Hash)

    The tool definition as a hash



92
93
94
# File 'lib/ollama_chat/tools/grep.rb', line 92

def to_hash
  tool.to_hash
end

#toolOllamaChat::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.

Returns:

  • (OllamaChat::Tool)

    The tool definition



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