跳转到主要内容
TavilyExtractorTool 允许 CrewAI 智能体使用 Tavily API 从网页中提取结构化内容。它可以处理单个 URL 或 URL 列表,并提供控制提取深度和是否包含图片的选项。

安装

要使用 TavilyExtractorTool,你需要安装 tavily-python
pip install 'crewai[tools]' tavily-python
你还需要将你的 Tavily API 密钥设置为环境变量
export TAVILY_API_KEY='your-tavily-api-key'

用法示例

以下是在 CrewAI 智能体中初始化和使用 TavilyExtractorTool 的方法
import os
from crewai import Agent, Task, Crew
from crewai_tools import TavilyExtractorTool

# Ensure TAVILY_API_KEY is set in your environment
# os.environ["TAVILY_API_KEY"] = "YOUR_API_KEY"

# Initialize the tool
tavily_tool = TavilyExtractorTool()

# Create an agent that uses the tool
extractor_agent = Agent(
    role='Web Content Extractor',
    goal='Extract key information from specified web pages',
    backstory='You are an expert at extracting relevant content from websites using the Tavily API.',
    tools=[tavily_tool],
    verbose=True
)

# Define a task for the agent
extract_task = Task(
    description='Extract the main content from the URL https://example.com using basic extraction depth.',
    expected_output='A JSON string containing the extracted content from the URL.',
    agent=extractor_agent
)

# Create and run the crew
crew = Crew(
    agents=[extractor_agent],
    tasks=[extract_task],
    verbose=2
)

result = crew.kickoff()
print(result)

配置选项

TavilyExtractorTool 接受以下参数
  • urls (Union[List[str], str]):必需。要提取数据的单个 URL 字符串或 URL 字符串列表。
  • include_images (Optional[bool]):是否在提取结果中包含图片。默认为 False
  • extract_depth (Literal[“basic”, “advanced”]):提取深度。使用 "basic" 进行更快的表层提取,或使用 "advanced" 进行更全面的提取。默认为 "basic"
  • timeout (int):等待提取请求完成的最长时间(秒)。默认为 60

高级用法

使用高级提取处理多个 URL

# Example with multiple URLs and advanced extraction
multi_extract_task = Task(
    description='Extract content from https://example.com and https://anotherexample.org using advanced extraction.',
    expected_output='A JSON string containing the extracted content from both URLs.',
    agent=extractor_agent
)

# Configure the tool with custom parameters
custom_extractor = TavilyExtractorTool(
    extract_depth='advanced',
    include_images=True,
    timeout=120
)

agent_with_custom_tool = Agent(
    role="Advanced Content Extractor",
    goal="Extract comprehensive content with images",
    tools=[custom_extractor]
)

工具参数

你可以在初始化期间通过设置参数来自定义工具的行为
# Initialize with custom configuration
extractor_tool = TavilyExtractorTool(
    extract_depth='advanced',  # More comprehensive extraction
    include_images=True,       # Include image results
    timeout=90                 # Custom timeout
)

功能

  • 单个或多个 URL:从一个 URL 提取内容或在单个请求中处理多个 URL
  • 可配置的深度:在基础(快速)和高级(全面)提取模式之间进行选择
  • 图片支持:可选择在提取结果中包含图片
  • 结构化输出:返回包含提取内容的格式良好的 JSON
  • 错误处理:对网络超时和提取错误进行稳健处理

响应格式

该工具返回一个 JSON 字符串,表示从提供的 URL 中提取的结构化数据。具体结构取决于页面的内容和使用的 extract_depth 常见的响应元素包括:
  • 标题:页面标题
  • 内容:页面的主要文本内容
  • 图片:图片 URL 和元数据(当 include_images=True 时)
  • 元数据:额外的页面信息,如作者、描述等

用例

  • 内容分析:从竞争对手网站提取和分析内容
  • 研究:从多个来源收集结构化数据进行分析
  • 内容迁移:从现有网站提取内容以进行迁移
  • 监控:定期提取内容以进行变更检测
  • 数据收集:系统地从网络来源提取信息
请参阅 Tavily API 文档,了解有关响应结构和可用选项的详细信息。