跳转到主要内容

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 进行嵌入和摘要。要自定义模型,您可以使用如下所示的配置字典:
代码
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代理集成
代码
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的子类实现,它提供了检索增强生成的基本功能。
代码
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视频内容中搜索和提取信息。通过使代理能够在视频内容中搜索,它促进了信息提取和分析任务,这些任务在没有此工具的情况下将难以执行。此工具对于研究、内容分析和从视频源提取知识特别有用。