主题
YouTube 频道 RAG 搜索
YoutubeChannelSearchTool旨在对 YouTube 频道的内容执行 RAG(检索增强生成)搜索。
YoutubeChannelSearchTool
NOTE
我们仍在改进这些工具,因此未来可能会出现意外行为或变更。
描述
该工具旨在特定 YouTube 频道的内容中执行语义搜索。 利用 RAG(检索增强生成)方法,它可提供相关的搜索结果, 使其在无需手动浏览视频的情况下提取信息或查找特定内容方面极具价值。 它简化了 YouTube 频道内的搜索流程,服务于寻求特定信息或主题的研究人员、内容创作者和观众。
安装
要使用 YoutubeChannelSearchTool,必须安装 crewai_tools 包。在 shell 中执行以下命令进行安装:
shell
pip install 'crewai[tools]'示例
以下示例演示如何将 YoutubeChannelSearchTool 与 CrewAI 智能体一起使用:
python
from crewai import Agent, Task, Crew
from crewai_tools import YoutubeChannelSearchTool
# Initialize the tool for general YouTube channel searches
youtube_channel_tool = YoutubeChannelSearchTool()
# Define an agent that uses the tool
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,
)
# Example task to search for information in a specific channel
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,
)
# Create and run the crew
crew = Crew(agents=[channel_researcher], tasks=[research_task])
result = crew.kickoff(inputs={"youtube_channel_handle": "@exampleChannel"})你也可以使用特定的 YouTube 频道句柄初始化该工具:
python
# Initialize the tool with a specific YouTube channel handle
youtube_channel_tool = YoutubeChannelSearchTool(
youtube_channel_handle='@exampleChannel'
)
# Define an agent that uses the tool
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 频道句柄。如果在初始化时提供,智能体在使用该工具时无需再指定它。如果句柄不以 '@' 开头,将自动添加。
- config:可选。底层 RAG 系统的配置,包括 LLM 和嵌入器设置。
- summarize:可选。是否对检索到的内容进行摘要。默认值为
False。
在智能体中使用该工具时,智能体需要提供:
- search_query:必填。用于在频道内容中查找相关信息的搜索查询。
- youtube_channel_handle:仅在初始化时未提供时需要。要搜索的 YouTube 频道句柄。
自定义模型和嵌入
默认情况下,该工具使用 OpenAI 进行嵌入和摘要。要自定义模型,你可以使用如下配置字典:
python
youtube_channel_tool = YoutubeChannelSearchTool(
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",
),
),
)
)智能体集成示例
以下是一个更详细的示例,演示如何将 YoutubeChannelSearchTool 与 CrewAI 智能体集成:
python
from crewai import Agent, Task, Crew
from crewai_tools import YoutubeChannelSearchTool
# Initialize the tool
youtube_channel_tool = YoutubeChannelSearchTool()
# Define an agent that uses the tool
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,
)
# Create a task for the agent
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,
)
# Run the task
crew = Crew(agents=[channel_researcher], tasks=[research_task])
result = crew.kickoff(inputs={"youtube_channel_handle": "@exampleDataScienceChannel"})实现细节
YoutubeChannelSearchTool 实现为 RagTool 的子类,后者提供检索增强生成的基础功能:
python
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 频道中提取知识。