YoutubeChannelSearchTool

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

描述

此工具旨在在特定 YouTube 频道的内容中执行语义搜索。利用 RAG(检索增强生成)方法,它提供相关的搜索结果,对于提取信息或查找特定内容而无需手动筛选视频来说,具有不可估量的价值。它简化了 YouTube 频道内的搜索过程,满足了研究人员、内容创作者以及寻求特定信息或主题的观众的需求。

安装

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

pip install 'crewai[tools]'

示例

以下示例演示了如何将 YoutubeChannelSearchTool 工具与 CrewAI 智能体一起使用:

代码
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 频道句柄初始化此工具:

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

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

智能体集成示例

以下是关于如何将 YoutubeChannelSearchTool 工具与 CrewAI 智能体集成的更详细示例:

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

代码
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 频道进行研究、内容分析和知识提取特别有用。