Agent-with-Toolkits-File-Management
Author: Secludor
Peer Review:
Proofread : BokyungisaGod
This is a part of LangChain Open Tutorial
Overview
When configuring an agent using LangChain, one of the biggest advantages is the integration of various features through third-party tools.
Among them, Toolkits provide a variety of integrated tools.
In this tutorial, we will learn how to manage local files using the FileManagementToolkit.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
Supplies
These are the materials needed for the practice.
GoogleNews
The GoogleNews class is the utility for fetching and parsing news from Google News RSS feeds. Here's a concise explanation of its key features:
Core Functionality
News Retrieval
Fetches news articles from Google News RSS feeds
Supports both latest news and keyword-based searches
Returns structured data in a consistent format (URL and content)
Main Methods
search_latest(): Retrieves the most recent news articlessearch_by_keyword(): Searches news based on specific keywords
Usage Example
The class handles URL encoding, RSS feed parsing, and error cases automatically, making it ideal for integration with news monitoring systems or AI agents that need access to current news content.
AgentStreamParser
The AgentStreamParser is a utility class designed to handle and process the output stream from AI agents. Here's a concise explanation of its key features:
Core Functionality
Stream Processing
Parses and processes agent actions, observations, and results in real-time
Handles three main types of events:
Tool calls (when agents use tools)
Observations (agent's findings)
Final results (agent's conclusions)
Callback System
The class uses three main callbacks:
Tool callbacks for monitoring tool usage
Observation callbacks for tracking agent findings
Result callbacks for handling final outputs
Usage Example
This parser is particularly useful for debugging agent behavior, logging agent actions, and maintaining a clear record of the agent's decision-making process.
How to Use FileManagementToolkit
FileManagementToolkit is a toolkit for local file management operations that:
Automates file management tasks
Enables AI agents to manipulate files safely
Provides comprehensive file operation tools
Security Considerations
When using FileManagementToolkit, implement these security measures:
Limit directory access using
root_dirConfigure filesystem permissions
Use
selected_toolsto restrict available operationsRun agents in sandboxed environments
Main Components
File Management Tools
CopyFileTool: Create a copy of a file in a specified location.DeleteFileTool: Delete a file.FileSearchTool: Recursively search for files in a subdirectory that match the regex pattern.MoveFileTool: Move or rename a file from one location to another.ReadFileTool: Read file from disk.WriteFileTool: Write file to disk.ListDirectoryTool: List files and directories in a specified folder.
Settings
root_dir: Set the root directory of workflows.selected_tools: Select the tools you want to use.
Dynamic Tool Creation
get_tools: create instances of the selected tools.
1. Basic Setup
The FileManagementToolkit provides essential file operation capabilities with security considerations. Let's explore how to set it up and use it safely.
Selective Tool Access
For better security, you can restrict available tools:
2. File Operations
Let's explore basic file operations using the toolkit's tools. Each operation demonstrates a core file management functionality.
3. Advanced Usage (News Articles)
Below, we combine file management with news retrieval to create and organize news articles:
Set up the agent with appropriate tools and configuration:
Example Operations
Fetch and Save News
You can check the contents of the tmp folder and see that the files have been created as shown below.

Modify Filenames
You can check the contents of the tmp folder and see that the filenames have been changed as shown below.

Organize Files
You can check the contents of the tmp folder and see that the news folder has been created and the files have been copied as shown below.

You can check the contents of the tmp folder and see that all files except for the news folder have been deleted, as shown below.

4. Error Handling Examples
To ensure robust file operations, wrap your tool calls with a safe operation function:
Last updated