Skip to content
CrewAI 文档版本 跟随上游 源快照 2a624756 更新于 2026-10-18

YouTube 视频 RAG 搜索

YoutubeVideoSearchTool 旨在对 YouTube 视频的内容执行 RAG(检索增强生成)搜索。

YoutubeVideoSearchTool

NOTE

我们仍在改进这些工具,因此未来可能会出现意外行为或变更。

描述

该工具是 crewai_tools 包的一部分,旨在利用检索增强生成(RAG)技术在 YouTube 视频内容中执行语义搜索。 它是该包中若干针对不同来源利用 RAG 的“搜索”工具之一。 YoutubeVideoSearchTool 允许灵活搜索;用户可以在不指定视频 URL 的情况下搜索任意 YouTube 视频内容, 也可以通过提供 URL 将搜索定位到特定的 YouTube 视频。

安装

要使用 YoutubeVideoSearchTool,你必须先安装 crewai_tools 包。 该包包含 YoutubeVideoSearchTool 以及其他旨在增强数据分析与处理任务的实用工具。 在终端中执行以下命令来安装该包:

shell
pip install 'crewai[tools]'

示例

以下示例演示如何将 YoutubeVideoSearchTool 与 CrewAI 智能体一起使用:

python
from crewai import Agent, Task, Crew
from crewai_tools import YoutubeVideoSearchTool

# Initialize the tool for general YouTube video searches
youtube_search_tool = YoutubeVideoSearchTool()

# Define an agent that uses the tool
video_researcher = Agent(
    role="Video Researcher",
    goal="Extract relevant information from YouTube videos",
    backstory="An expert researcher who specializes in analyzing video content.",
    tools=[youtube_search_tool],
    verbose=True,
)

# Example task to search for information in a specific video
research_task = Task(
    description="Search for information about machine learning frameworks in the YouTube video at {youtube_video_url}",
    expected_output="A summary of the key machine learning frameworks mentioned in the video.",
    agent=video_researcher,
)

# Create and run the crew
crew = Crew(agents=[video_researcher], tasks=[research_task])
result = crew.kickoff(inputs={"youtube_video_url": "https://youtube.com/watch?v=example"})

你也可以使用特定的 YouTube 视频 URL 初始化该工具:

python
# Initialize the tool with a specific YouTube video URL
youtube_search_tool = YoutubeVideoSearchTool(
    youtube_video_url='https://youtube.com/watch?v=example'
)

# Define an agent that uses the tool
video_researcher = Agent(
    role="Video Researcher",
    goal="Extract relevant information from a specific YouTube video",
    backstory="An expert researcher who specializes in analyzing video content.",
    tools=[youtube_search_tool],
    verbose=True,
)

参数

YoutubeVideoSearchTool 接受以下参数:

  • youtube_video_url:可选。要搜索的 YouTube 视频 URL。如果在初始化时提供,智能体在使用该工具时无需再指定它。
  • config:可选。底层 RAG 系统的配置,包括 LLM 和嵌入器设置。
  • summarize:可选。是否对检索到的内容进行摘要。默认值为 False。

在智能体中使用该工具时,智能体需要提供:

  • search_query:必填。用于在视频内容中查找相关信息的搜索查询。
  • youtube_video_url:仅在初始化时未提供时需要。要搜索的 YouTube 视频 URL。

自定义模型和嵌入

默认情况下,该工具使用 OpenAI 进行嵌入和摘要。要自定义模型,你可以使用如下配置字典:

python
youtube_search_tool = YoutubeVideoSearchTool(
    config=dict(
        llm=dict(
            provider="ollama", # or google, openai, anthropic, llama2, ...
            config=dict(
                model="llama2",
                # temperature=0.5,
                # top_p=1,
                # stream=true,
            ),
        ),
        embedder=dict(
            provider="google-generativeai", # or openai, ollama, ...
            config=dict(
                model_name="gemini-embedding-001",
                task_type="RETRIEVAL_DOCUMENT",
                # title="Embeddings",
            ),
        ),
    )
)

智能体集成示例

以下是一个更详细的示例,演示如何将 YoutubeVideoSearchTool 与 CrewAI 智能体集成:

python
from crewai import Agent, Task, Crew
from crewai_tools import YoutubeVideoSearchTool

# Initialize the tool
youtube_search_tool = YoutubeVideoSearchTool()

# Define an agent that uses the tool
video_researcher = Agent(
    role="Video Researcher",
    goal="Extract and analyze information from YouTube videos",
    backstory="""You are an expert video researcher who specializes in extracting 
    and analyzing information from YouTube videos. You have a keen eye for detail 
    and can quickly identify key points and insights from video content.""",
    tools=[youtube_search_tool],
    verbose=True,
)

# Create a task for the agent
research_task = Task(
    description="""
    Search for information about recent advancements in artificial intelligence 
    in the YouTube video at {youtube_video_url}. 
    
    Focus on:
    1. Key AI technologies mentioned
    2. Real-world applications discussed
    3. Future predictions made by the speaker
    
    Provide a comprehensive summary of these points.
    """,
    expected_output="A detailed summary of AI advancements, applications, and future predictions from the video.",
    agent=video_researcher,
)

# Run the task
crew = Crew(agents=[video_researcher], tasks=[research_task])
result = crew.kickoff(inputs={"youtube_video_url": "https://youtube.com/watch?v=example"})

实现细节

YoutubeVideoSearchTool 实现为 RagTool 的子类,后者提供检索增强生成的基础功能:

python
class YoutubeVideoSearchTool(RagTool):
    name: str = "Search a Youtube Video content"
    description: str = "A tool that can be used to semantic search a query from a Youtube Video content."
    args_schema: Type[BaseModel] = YoutubeVideoSearchToolSchema

    def __init__(self, youtube_video_url: Optional[str] = None, **kwargs):
        super().__init__(**kwargs)
        if youtube_video_url is not None:
            kwargs["data_type"] = DataType.YOUTUBE_VIDEO
            self.add(youtube_video_url)
            self.description = f"A tool that can be used to semantic search a query the {youtube_video_url} Youtube Video content."
            self.args_schema = FixedYoutubeVideoSearchToolSchema
            self._generate_description()

结论

YoutubeVideoSearchTool 提供了一种使用 RAG 技术从 YouTube 视频内容中搜索和提取信息的强大方式。通过让智能体能够在视频内容中搜索,它促进了原本难以执行的信息提取和分析任务。该工具特别适用于研究、内容分析以及从视频来源中提取知识。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。