A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python.langchain.com/docs/integrations/tools/filesystem below:

File System | 🦜️🔗 LangChain

File System

LangChain provides tools for interacting with a local file system out of the box. This notebook walks through some of them.

Note: these tools are not recommended for use outside a sandboxed environment!

%pip install -qU langchain-community

First, we'll import the tools.

from tempfile import TemporaryDirectory

from langchain_community.agent_toolkits import FileManagementToolkit


working_directory = TemporaryDirectory()

If you want to provide all the file tooling to your agent, it's easy to do so with the toolkit. We'll pass the temporary directory in as a root directory as a workspace for the LLM.

It's recommended to always pass in a root directory, since without one, it's easy for the LLM to pollute the working directory, and without one, there isn't any validation against straightforward prompt injection.

toolkit = FileManagementToolkit(
root_dir=str(working_directory.name)
)
toolkit.get_tools()
[CopyFileTool(root_dir='/tmp/tmprdvsw3tg'),
DeleteFileTool(root_dir='/tmp/tmprdvsw3tg'),
FileSearchTool(root_dir='/tmp/tmprdvsw3tg'),
MoveFileTool(root_dir='/tmp/tmprdvsw3tg'),
ReadFileTool(root_dir='/tmp/tmprdvsw3tg'),
WriteFileTool(root_dir='/tmp/tmprdvsw3tg'),
ListDirectoryTool(root_dir='/tmp/tmprdvsw3tg')]
Selecting File System Tools

If you only want to select certain tools, you can pass them in as arguments when initializing the toolkit, or you can individually initialize the desired tools.

tools = FileManagementToolkit(
root_dir=str(working_directory.name),
selected_tools=["read_file", "write_file", "list_directory"],
).get_tools()
tools
[ReadFileTool(root_dir='/tmp/tmprdvsw3tg'),
WriteFileTool(root_dir='/tmp/tmprdvsw3tg'),
ListDirectoryTool(root_dir='/tmp/tmprdvsw3tg')]
read_tool, write_tool, list_tool = tools
write_tool.invoke({"file_path": "example.txt", "text": "Hello World!"})
'File written successfully to example.txt.'

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4