YoutubeVideoSearchTool
我们仍在努力改进工具,因此未来可能会出现意外行为或变更。
该工具是 crewai_tools 包的一部分,旨在利用检索增强生成(RAG)技术在 Youtube 视频内容中进行语义搜索。它是该包中利用 RAG 针对不同来源的几种“搜索”工具之一。YoutubeVideoSearchTool 允许灵活搜索;用户可以在任何 Youtube 视频内容中进行搜索而无需指定视频 URL,或者通过提供其 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", # or openai, ollama, ...
config=dict(
model="models/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 视频内容中的信息。通过使代理能够在视频内容中搜索,它促进了信息提取和分析任务,而这些任务在没有此工具的情况下很难执行。该工具对于研究、内容分析以及从视频源中提取知识特别有用。