Class: OllamaChat::Tools::FileContext
- Inherits:
-
Object
- Object
- OllamaChat::Tools::FileContext
- Includes:
- Ollama
- Defined in:
- lib/ollama_chat/tools/file_context.rb
Overview
A tool for creating file context based on glob patterns.
This tool allows the chat client to generate context information about files matching a specified glob pattern. It integrates with the Ollama tool calling system to provide detailed file information to the language model for more accurate responses.
The tool searches for files using the provided glob pattern and generates structured context data that includes file contents, sizes, and metadata.
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
Returns the name of the tool.
Instance Method Summary collapse
-
#execute(tool_call, **opts) ⇒ String
Executes the file context generation operation.
-
#initialize ⇒ OllamaChat::Tools::FileContext
constructor
Initializes a new file_context tool instance.
-
#to_hash ⇒ Hash
Converts the tool to a hash representation.
-
#tool ⇒ Ollama::Tool
Creates and returns a tool definition for generating file context.
Constructor Details
#initialize ⇒ OllamaChat::Tools::FileContext
Initializes a new file_context tool instance.
16 17 18 |
# File 'lib/ollama_chat/tools/file_context.rb', line 16 def initialize @name = 'file_context' end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns the name of the tool.
23 24 25 |
# File 'lib/ollama_chat/tools/file_context.rb', line 23 def name @name end |
Instance Method Details
#execute(tool_call, **opts) ⇒ String
Executes the file context generation operation.
This method searches for files matching the provided glob pattern and generates structured context data including file contents, sizes, and metadata.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ollama_chat/tools/file_context.rb', line 67 def execute(tool_call, **opts) config = opts[:config] pattern = tool_call.function.arguments.pattern directory = Pathname.new(tool_call.function.arguments.directory || ?.) format = config.context.format search_path = directory + pattern ContextSpook::generate_context(verbose: true, format:) do |context| context do Dir.glob(search_path).each do |filename| File.file?(filename) or next file filename end end end.send("to_#{format.downcase}") end |
#to_hash ⇒ Hash
Converts the tool to a hash representation.
This method provides a standardized way to serialize the tool definition for use in tool calling systems.
90 91 92 |
# File 'lib/ollama_chat/tools/file_context.rb', line 90 def to_hash tool.to_hash end |
#tool ⇒ Ollama::Tool
Creates and returns a tool definition for generating file context.
This method constructs the function signature that describes what the tool does, its parameters, and required fields. The tool expects a pattern parameter to be provided for file searching.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ollama_chat/tools/file_context.rb', line 32 def tool Tool.new( type: 'function', function: Tool::Function.new( name:, description: 'Create a context that provides information about files '\ 'and their content in order to give more accurate answers for a query', parameters: Tool::Function::Parameters.new( type: 'object', properties: { pattern: Tool::Function::Parameters::Property.new( type: 'string', description: 'Glob pattern to search for (e.g., "**/*.rb", "lib/**/*.rb")' ), directory: Tool::Function::Parameters::Property.new( type: 'string', description: 'Directory to search in (defaults to current directory)' ), }, required: ['pattern'] ) ) ) end |