跳转到内容

YouTube 频道 RAG 搜索

该工具旨在对特定 Youtube 频道的内容进行语义搜索。
借助 RAG(检索增强生成)方法,它会返回相关搜索结果,
让你无需手动翻阅视频即可提取信息或查找特定内容。
它简化了在 Youtube 频道内的搜索流程,适用于研究人员、内容创作者以及寻找特定信息或主题的观看者。

要使用 YoutubeChannelSearchTool,必须安装 crewai_tools 包。请在你的 shell 中执行以下命令进行安装:

Terminal window
pip install 'crewai[tools]'

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

from crewai import Agent, Task, Crew
from crewai_tools import YoutubeChannelSearchTool
# 初始化工具,用于通用 YouTube 频道搜索
youtube_channel_tool = YoutubeChannelSearchTool()
# 定义一个使用该工具的智能体
channel_researcher = Agent(
role="Channel Researcher",
goal="Extract relevant information from YouTube channels",
backstory="An expert researcher who specializes in analyzing YouTube channel content.",
tools=[youtube_channel_tool],
verbose=True,
)
# 示例任务:在特定频道中搜索信息
research_task = Task(
description="Search for information about machine learning tutorials in the YouTube channel {youtube_channel_handle}",
expected_output="A summary of the key machine learning tutorials available on the channel.",
agent=channel_researcher,
)
# 创建并运行 crew
crew = Crew(agents=[channel_researcher], tasks=[research_task])
result = crew.kickoff(inputs={"youtube_channel_handle": "@exampleChannel"})

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

# 使用特定 YouTube 频道 handle 初始化工具
youtube_channel_tool = YoutubeChannelSearchTool(
youtube_channel_handle='@exampleChannel'
)
# 定义一个使用该工具的智能体
channel_researcher = Agent(
role="Channel Researcher",
goal="Extract relevant information from a specific YouTube channel",
backstory="An expert researcher who specializes in analyzing YouTube channel content.",
tools=[youtube_channel_tool],
verbose=True,
)

YoutubeChannelSearchTool 接受以下参数:

  • youtube_channel_handle:可选。要搜索的 YouTube 频道 handle。如果在初始化时提供,智能体在使用工具时就无需再指定。如果 handle 不以 @ 开头,工具会自动补上。
  • config:可选。底层 RAG 系统的配置,包括 LLM 和 embedder 设置。
  • summarize:可选。是否对检索到的内容进行摘要。默认值为 False

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

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

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

youtube_channel_tool = YoutubeChannelSearchTool(
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",
),
),
)
)

下面是将 YoutubeChannelSearchTool 与 CrewAI 智能体集成的更详细示例:

from crewai import Agent, Task, Crew
from crewai_tools import YoutubeChannelSearchTool
# 初始化工具
youtube_channel_tool = YoutubeChannelSearchTool()
# 定义一个使用该工具的智能体
channel_researcher = Agent(
role="Channel Researcher",
goal="Extract and analyze information from YouTube channels",
backstory="""You are an expert channel researcher who specializes in extracting
and analyzing information from YouTube channels. You have a keen eye for detail
and can quickly identify key points and insights from video content across an entire channel.""",
tools=[youtube_channel_tool],
verbose=True,
)
# 为智能体创建任务
research_task = Task(
description="""
Search for information about data science projects and tutorials
in the YouTube channel {youtube_channel_handle}.
Focus on:
1. Key data science techniques covered
2. Popular tutorial series
3. Most viewed or recommended videos
Provide a comprehensive summary of these points.
""",
expected_output="A detailed summary of data science content available on the channel.",
agent=channel_researcher,
)
# 运行任务
crew = Crew(agents=[channel_researcher], tasks=[research_task])
result = crew.kickoff(inputs={"youtube_channel_handle": "@exampleDataScienceChannel"})

YoutubeChannelSearchTool 被实现为 RagTool 的子类,它提供了检索增强生成的基础功能:

class YoutubeChannelSearchTool(RagTool):
name: str = "Search a Youtube Channels content"
description: str = "A tool that can be used to semantic search a query from a Youtube Channels content."
args_schema: Type[BaseModel] = YoutubeChannelSearchToolSchema
def __init__(self, youtube_channel_handle: Optional[str] = None, **kwargs):
super().__init__(**kwargs)
if youtube_channel_handle is not None:
kwargs["data_type"] = DataType.YOUTUBE_CHANNEL
self.add(youtube_channel_handle)
self.description = f"A tool that can be used to semantic search a query the {youtube_channel_handle} Youtube Channels content."
self.args_schema = FixedYoutubeChannelSearchToolSchema
self._generate_description()
def add(
self,
youtube_channel_handle: str,
**kwargs: Any,
) -> None:
if not youtube_channel_handle.startswith("@"):
youtube_channel_handle = f"@{youtube_channel_handle}"
super().add(youtube_channel_handle, **kwargs)

YoutubeChannelSearchTool 通过 RAG 技术提供了一种强大的方式来搜索并从 YouTube 频道内容中提取信息。通过允许智能体在整个频道的视频中搜索,它能够完成原本难以执行的信息提取和分析任务。对于研究、内容分析以及从 YouTube 频道中进行知识提取来说,这个工具都非常有用。