主题
S3 读取工具
S3ReaderTool使 CrewAI 智能体能够从 Amazon S3 存储桶读取文件。
S3ReaderTool
描述
S3ReaderTool 旨在从 Amazon S3 存储桶读取文件。该工具允许 CrewAI 智能体访问并检索存储在 S3 中的内容,非常适合需要读取数据、配置文件或存储在 AWS S3 存储中的任何其他内容的工作流。
安装
要使用此工具,你需要安装所需的依赖项:
shell
uv add boto3入门步骤
要有效使用 S3ReaderTool,请按以下步骤操作:
- 安装依赖:使用上面的命令安装所需的包。
- 配置 AWS 凭证:将 AWS 凭证设置为环境变量。
- 初始化工具:创建该工具的一个实例。
- 指定 S3 路径:提供要读取文件的 S3 路径。
示例
以下示例演示如何使用 S3ReaderTool 从 S3 存储桶读取文件:
python
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"})参数
当由智能体使用时,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 秘密访问密钥。
用量
在将 S3ReaderTool 与智能体配合使用时,智能体需要提供 S3 文件路径:
python
# 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"})错误处理
S3ReaderTool 包含针对常见 S3 问题的错误处理:
- S3 路径格式无效
- 文件缺失或无法访问
- 权限问题
- AWS 凭证问题
发生错误时,该工具将返回一条错误消息,其中包含有关该问题的详细信息。
实现细节
S3ReaderTool 使用 AWS SDK for Python(boto3)与 S3 交互:
python
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)}"结论
S3ReaderTool 提供了一种直接的方式从 Amazon S3 存储桶读取文件。通过让智能体访问存储在 S3 中的内容,它有助于实现需要基于云的文件访问的工作流。该工具对数据处理、配置管理以及任何需要从 AWS S3 存储中检索信息的任务尤其有用。