mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-28 01:28:14 +00:00
Squashed 'packages/tools/' content from commit 78317b9c
git-subtree-dir: packages/tools git-subtree-split: 78317b9c127f18bd040c1d77e3c0840cdc9a5b38
This commit is contained in:
40
crewai_tools/tools/directory_read_tool/README.md
Normal file
40
crewai_tools/tools/directory_read_tool/README.md
Normal file
@@ -0,0 +1,40 @@
|
||||
```markdown
|
||||
# DirectoryReadTool
|
||||
|
||||
## Description
|
||||
The DirectoryReadTool is a highly efficient utility designed for the comprehensive listing of directory contents. It recursively navigates through the specified directory, providing users with a detailed enumeration of all files, including those nested within subdirectories. This tool is indispensable for tasks requiring a thorough inventory of directory structures or for validating the organization of files within directories.
|
||||
|
||||
## Installation
|
||||
Install the `crewai_tools` package to use the DirectoryReadTool in your project. If you haven't added this package to your environment, you can easily install it with pip using the following command:
|
||||
|
||||
```shell
|
||||
pip install 'crewai[tools]'
|
||||
```
|
||||
|
||||
This installs the latest version of the `crewai_tools` package, allowing access to the DirectoryReadTool and other utilities.
|
||||
|
||||
## Example
|
||||
The DirectoryReadTool is simple to use. The code snippet below shows how to set up and use the tool to list the contents of a specified directory:
|
||||
|
||||
```python
|
||||
from crewai_tools import DirectoryReadTool
|
||||
|
||||
# Initialize the tool with the directory you want to explore
|
||||
tool = DirectoryReadTool(directory='/path/to/your/directory')
|
||||
|
||||
# Use the tool to list the contents of the specified directory
|
||||
directory_contents = tool.run()
|
||||
print(directory_contents)
|
||||
```
|
||||
|
||||
This example demonstrates the essential steps to utilize the DirectoryReadTool effectively, highlighting its simplicity and user-friendly design.
|
||||
|
||||
## Arguments
|
||||
The DirectoryReadTool requires minimal configuration for use. The essential argument for this tool is as follows:
|
||||
|
||||
- `directory`: A mandatory argument that specifies the path to the directory whose contents you wish to list. It accepts both absolute and relative paths, guiding the tool to the desired directory for content listing.
|
||||
|
||||
The DirectoryReadTool provides a user-friendly and efficient way to list directory contents, making it an invaluable tool for managing and inspecting directory structures.
|
||||
```
|
||||
|
||||
This revised documentation for the DirectoryReadTool maintains the structure and content requirements as outlined, with adjustments made for clarity, consistency, and adherence to the high-quality standards exemplified in the provided documentation example.
|
||||
@@ -0,0 +1,47 @@
|
||||
import os
|
||||
from typing import Any, Optional, Type
|
||||
|
||||
from crewai.tools import BaseTool
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class FixedDirectoryReadToolSchema(BaseModel):
|
||||
"""Input for DirectoryReadTool."""
|
||||
|
||||
|
||||
class DirectoryReadToolSchema(FixedDirectoryReadToolSchema):
|
||||
"""Input for DirectoryReadTool."""
|
||||
|
||||
directory: str = Field(..., description="Mandatory directory to list content")
|
||||
|
||||
|
||||
class DirectoryReadTool(BaseTool):
|
||||
name: str = "List files in directory"
|
||||
description: str = (
|
||||
"A tool that can be used to recursively list a directory's content."
|
||||
)
|
||||
args_schema: Type[BaseModel] = DirectoryReadToolSchema
|
||||
directory: Optional[str] = None
|
||||
|
||||
def __init__(self, directory: Optional[str] = None, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
if directory is not None:
|
||||
self.directory = directory
|
||||
self.description = f"A tool that can be used to list {directory}'s content."
|
||||
self.args_schema = FixedDirectoryReadToolSchema
|
||||
self._generate_description()
|
||||
|
||||
def _run(
|
||||
self,
|
||||
**kwargs: Any,
|
||||
) -> Any:
|
||||
directory = kwargs.get("directory", self.directory)
|
||||
if directory[-1] == "/":
|
||||
directory = directory[:-1]
|
||||
files_list = [
|
||||
f"{directory}/{(os.path.join(root, filename).replace(directory, '').lstrip(os.path.sep))}"
|
||||
for root, dirs, files in os.walk(directory)
|
||||
for filename in files
|
||||
]
|
||||
files = "\n- ".join(files_list)
|
||||
return f"File paths: \n-{files}"
|
||||
Reference in New Issue
Block a user