S3ReaderTool

描述

The S3ReaderTool 旨在从 Amazon S3 存储桶读取文件。该工具允许 CrewAI 智能体访问和检索存储在 S3 中的内容,非常适合需要读取存储在 AWS S3 存储中的数据、配置文件或任何其他内容的工作流程。

安装

要使用此工具,您需要安装所需的依赖项

uv add boto3

开始步骤

要有效使用 the S3ReaderTool,请按照以下步骤操作

  1. 安装依赖项:使用上面的命令安装所需的软件包。
  2. 配置 AWS 凭据:将您的 AWS 凭据设置为环境变量。
  3. 初始化工具:创建工具实例。
  4. 指定 S3 路径:提供您要读取的文件的 S3 路径。

示例

以下示例演示了如何使用 the S3ReaderTool 从 S3 存储桶读取文件

代码
from crewai import Agent, Task, Crew
from crewai_tools.aws.s3 import S3ReaderTool

# Initialize the tool
s3_reader_tool = S3ReaderTool()

# Define an agent that uses the tool
file_reader_agent = Agent(
    role="File Reader",
    goal="Read files from S3 buckets",
    backstory="An expert in retrieving and processing files from cloud storage.",
    tools=[s3_reader_tool],
    verbose=True,
)

# Example task to read a configuration file
read_task = Task(
    description="Read the configuration file from {my_bucket} and summarize its contents.",
    expected_output="A summary of the configuration file contents.",
    agent=file_reader_agent,
)

# Create and run the crew
crew = Crew(agents=[file_reader_agent], tasks=[read_task])
result = crew.kickoff(inputs={"my_bucket": "s3://my-bucket/config/app-config.json"})

参数

The S3ReaderTool 在被智能体使用时接受以下参数

  • file_path:必需。S3 文件路径格式为 s3://bucket-name/file-name

AWS 凭据

该工具需要 AWS 凭据来访问 S3 存储桶。您可以使用环境变量配置这些凭据

  • CREW_AWS_REGION:您的 S3 存储桶所在的 AWS 区域。默认为 us-east-1
  • CREW_AWS_ACCESS_KEY_ID:您的 AWS 访问密钥 ID。
  • CREW_AWS_SEC_ACCESS_KEY:您的 AWS 秘密访问密钥。

用法

当智能体使用 the S3ReaderTool 时,智能体需要提供 S3 文件路径

代码
# Example of using the tool with an agent
file_reader_agent = Agent(
    role="File Reader",
    goal="Read files from S3 buckets",
    backstory="An expert in retrieving and processing files from cloud storage.",
    tools=[s3_reader_tool],
    verbose=True,
)

# Create a task for the agent to read a specific file
read_config_task = Task(
    description="Read the application configuration file from {my_bucket} and extract the database connection settings.",
    expected_output="The database connection settings from the configuration file.",
    agent=file_reader_agent,
)

# Run the task
crew = Crew(agents=[file_reader_agent], tasks=[read_config_task])
result = crew.kickoff(inputs={"my_bucket": "s3://my-bucket/config/app-config.json"})

错误处理

The S3ReaderTool 包含了对常见 S3 问题的错误处理

  • 无效的 S3 路径格式
  • 缺失或不可访问的文件
  • 权限问题
  • AWS 凭据问题

当发生错误时,工具将返回包含错误详细信息的错误消息。

实现细节

The S3ReaderTool 使用 AWS SDK for Python (boto3) 与 S3 进行交互

代码
class S3ReaderTool(BaseTool):
    name: str = "S3 Reader Tool"
    description: str = "Reads a file from Amazon S3 given an S3 file path"
    
    def _run(self, file_path: str) -> str:
        try:
            bucket_name, object_key = self._parse_s3_path(file_path)

            s3 = boto3.client(
                's3',
                region_name=os.getenv('CREW_AWS_REGION', 'us-east-1'),
                aws_access_key_id=os.getenv('CREW_AWS_ACCESS_KEY_ID'),
                aws_secret_access_key=os.getenv('CREW_AWS_SEC_ACCESS_KEY')
            )

            # Read file content from S3
            response = s3.get_object(Bucket=bucket_name, Key=object_key)
            file_content = response['Body'].read().decode('utf-8')

            return file_content
        except ClientError as e:
            return f"Error reading file from S3: {str(e)}"

总结

The S3ReaderTool 提供了一种从 Amazon S3 存储桶读取文件的直接方法。通过使智能体能够访问存储在 S3 中的内容,它促进了需要基于云的文件访问的工作流程。此工具对于数据处理、配置管理以及任何涉及从 AWS S3 存储检索信息的任务都特别有用。