YoutubeVideoSearchTool

我们仍在努力改进工具,因此未来可能会出现意外的行为或变化。

描述

此工具是 crewai_tools 包的一部分,旨在利用检索增强生成 (RAG) 技术在 YouTube 视频内容中执行语义搜索。它是包中利用 RAG 搜索不同来源的几种“搜索”工具之一。YoutubeVideoSearchTool 允许灵活搜索;用户无需指定视频 URL 即可在任何 YouTube 视频内容中搜索,或者通过提供视频 URL 来将搜索目标锁定到特定的 YouTube 视频。

安装

要使用 YoutubeVideoSearchTool,您必须首先安装 crewai_tools 包。此包包含 YoutubeVideoSearchTool 以及旨在增强您的数据分析和处理任务的其他实用程序。通过在您的终端中执行以下命令来安装此包

pip install 'crewai[tools]'

示例

以下示例演示了如何将 YoutubeVideoSearchTool 与 CrewAI 代理一起使用

代码
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 初始化此工具

代码
# 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 进行嵌入和总结。要自定义模型,您可以使用如下所示的 config 字典

代码
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", # or openai, ollama, ...
            config=dict(
                model="models/embedding-001",
                task_type="retrieval_document",
                # title="Embeddings",
            ),
        ),
    )
)

代理集成示例

以下是关于如何将 YoutubeVideoSearchTool 与 CrewAI 代理集成的更详细示例

代码
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 的子类实现,RagTool 提供了检索增强生成的基础功能。

代码
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 视频内容中搜索和提取信息的强大方法。通过使代理能够在视频内容中搜索,它促进了原本难以执行的信息提取和分析任务。此工具对于研究、内容分析以及从视频源提取知识特别有用。