Class: Langchain::Tool::FileSystem

Inherits:
Object
  • Object
show all
Extended by:
Langchain::ToolDefinition
Defined in:
lib/langchain/tool/file_system.rb

Overview

A tool that wraps the Ruby file system classes.

Usage:

file_system = Langchain::Tool::FileSystem.new

Instance Method Summary collapse

Methods included from Langchain::ToolDefinition

define_function, extended, function_schemas, tool_name

Instance Method Details

#list_directory(directory_path:) ⇒ Object

[View source]

26
27
28
29
30
# File 'lib/langchain/tool/file_system.rb', line 26

def list_directory(directory_path:)
  tool_response(content: Dir.entries(directory_path))
rescue Errno::ENOENT
  tool_response(content: "No such directory: #{directory_path}")
end

#read_file(file_path:) ⇒ Object

[View source]

32
33
34
35
36
# File 'lib/langchain/tool/file_system.rb', line 32

def read_file(file_path:)
  tool_response(content: File.read(file_path))
rescue Errno::ENOENT
  tool_response(content: "No such file: #{file_path}")
end

#write_to_file(file_path:, content:) ⇒ Object

[View source]

38
39
40
41
42
43
# File 'lib/langchain/tool/file_system.rb', line 38

def write_to_file(file_path:, content:)
  File.write(file_path, content)
  tool_response(content: "File written successfully")
rescue Errno::EACCES
  tool_response(content: "Permission denied: #{file_path}")
end