跳转到内容

YouTube 视频 RAG 搜索

该工具是 crewai_tools 包的一部分,旨在利用检索增强生成(RAG)技术在 Youtube 视频内容中进行语义搜索。
它是包中若干“Search”工具之一,这些工具分别针对不同来源使用 RAG。
YoutubeVideoSearchTool 在搜索时提供了灵活性:用户可以在不指定视频 URL 的情况下搜索任意 Youtube 视频内容,
也可以通过提供视频 URL 将搜索目标限定到某个特定 Youtube 视频。

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

Terminal window
pip install 'crewai[tools]'

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

from crewai import Agent, Task, Crew
from crewai_tools import YoutubeVideoSearchTool
# 初始化工具,用于通用 YouTube 视频搜索
youtube_search_tool = YoutubeVideoSearchTool()
# 定义一个使用该工具的智能体
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,
)
# 示例任务:在特定视频中搜索信息
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,
)
# 创建并运行 crew
crew = Crew(agents=[video_researcher], tasks=[research_task])
result = crew.kickoff(inputs={"youtube_video_url": "https://youtube.com/watch?v=example"})

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

# 使用特定 YouTube 视频 URL 初始化工具
youtube_search_tool = YoutubeVideoSearchTool(
youtube_video_url='https://youtube.com/watch?v=example'
)
# 定义一个使用该工具的智能体
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:可选。要搜索的视频 URL。如果在初始化时提供,智能体在使用工具时就无需再指定它。
  • config:可选。底层 RAG 系统的配置,包括 LLM 和 embedder 设置。
  • summarize:可选。是否对检索到的内容进行摘要。默认值为 False

在与智能体一起使用时,智能体需要提供:

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

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

youtube_search_tool = YoutubeVideoSearchTool(
config=dict(
llm=dict(
provider="ollama", # 或 google、openai、anthropic、llama2 等
config=dict(
model="llama2",
# temperature=0.5,
# top_p=1,
# stream=true,
),
),
embedder=dict(
provider="google-generativeai", # 或 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
# 初始化工具
youtube_search_tool = YoutubeVideoSearchTool()
# 定义一个使用该工具的智能体
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,
)
# 为智能体创建任务
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,
)
# 运行任务
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 视频内容中提取信息。通过允许智能体在视频内容中进行搜索,它能够完成原本难以执行的信息提取和分析任务。对于研究、内容分析以及从视频来源中进行知识提取来说,这个工具都非常有用。