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!
Copy
Ask AI
%pip install -qU langchain-community
First, we’ll import the tools.
Copy
Ask AI
from tempfile import TemporaryDirectoryfrom langchain_community.agent_toolkits import FileManagementToolkit# We'll make a temporary directory to avoid clutterworking_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.
Copy
Ask AI
toolkit = FileManagementToolkit( root_dir=str(working_directory.name)) # If you don't provide a root_dir, operations will default to the current working directorytoolkit.get_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.