File

The FileTools toolkit enables Agents to read and write files on the local file system.

Example

The following agent will generate an answer and save it in a file.

1from kern.agent import Agent
2from kern.tools.file import FileTools
3
4agent = Agent(tools=[FileTools()])
5agent.print_response("What is the most advanced LLM currently? Save the answer to a file.", markdown=True)

Toolkit Params

ParameterTypeDefaultDescription
base_dirPathNoneSpecifies the base directory path for file operations
enable_save_fileboolTrueEnables functionality to save files
enable_delete_fileboolFalseEnables functionality to delete files
enable_read_fileboolTrueEnables functionality to read files
enable_read_file_chunksboolTrueEnables functionality to read files in chunks
enable_replace_file_chunkboolTrueEnables functionality to update files in chunks
enable_list_filesboolTrueEnables functionality to list files in directories
enable_search_filesboolTrueEnables functionality to search for files by name pattern
enable_search_contentboolTrueEnables functionality to search file contents (search_content)
allboolFalseEnables all functionality when set to True
expose_base_directoryboolFalseAdds 'base_directory' to the tool responses if set to True
exclude_patternsOptional[List[str]]noise dirsfnmatch-style patterns skipped by list_files, search_files, and search_content. Pass [] to disable exclusion. Does not parse .gitignore.
max_file_lengthint10000000Maximum file length to read in bytes. Reading will fail if the file is larger.
max_file_linesint100000Maximum number of lines to read from a file. Reading will fail if the file has more lines.
line_separatorstr"\n"The separator to use when interacting with chunks.

Toolkit Functions

NameDescription
save_fileSaves the contents to a file called file_name and returns the file name if successful.
read_fileReads the contents of the file file_name and returns the contents if successful.
read_file_chunksReads the contents of the file file_name in chunks and returns the contents if successful.
replace_file_chunkPartial replace of the contents of the file file_name
delete_fileDeletes the file file_name if successful.
list_filesReturns a list of files in the base directory
search_filesFinds files by name using an fnmatch-style pattern.
search_contentSearches file contents for a query within an optional directory, returning up to limit matches.

Developer Resources