Adding YouTube Search Capability to AG2 - AG2
Adding YouTube Search Capability to AG2
The YouTube Search integration in AG2 allows users to search for YouTube videos and retrieve video details directly within the AG2 framework. This is useful for extracting valuable information from video content, staying updated with the latest tutorials, reviews, and educational materials.
Installation
To get started with the YouTube Search integration in AG2, follow these steps:
Install AG2 with the google-search extra, which includes the necessary dependencies for YouTube search. Since our examples also use openai, install it as well:
pip install -U ag2[openai,google-search]
Note: If you have been using
autogenorag2, all you need to do is upgrade it using:pip install -U autogen[openai,google-search]or
pip install -U ag2[openai,google-search]as
autogen, andag2are aliases for the same PyPI package.
Imports
import os
import autogen
from autogen import AssistantAgent
from autogen.tools.experimental import YoutubeSearchTool
Setup YouTube Data API
Before using the YouTube Search tool, you need to set up a YouTube Data API key:
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Navigate to APIs & Services > Library
- Search for YouTube Data API v3 and enable it
- Go to APIs & Services > Credentials
- Click on Create Credentials > API key and copy your API key
- Set the API key as an environment variable:
export YOUTUBE_API_KEY="your_api_key"
Agent Configuration
config_list = autogen.config_list_from_json(
env_or_file="OAI_CONFIG_LIST",
filter_dict={
"model": ["gpt-5-nano"],
},
)
assistant = AssistantAgent(
name="assistant",
llm_config={"config_list": config_list},
)
YoutubeSearchTool Initialization
Create a YoutubeSearchTool with your YouTube API key.
youtube_api_key = os.getenv("YOUTUBE_API_KEY")
assert youtube_api_key is not None, "Please set YOUTUBE_API_KEY environment variable"
# Create the YouTube search tool with your API key
youtube_tool = YoutubeSearchTool(
youtube_api_key=youtube_api_key,
)
# Register the tool with the assistant
youtube_tool.register_for_llm(assistant)
YouTube Search
Let’s start with a basic search to find YouTube videos on a particular topic.
run_response = assistant.run(
message="Find the latest YouTube videos on large language models. List the titles and provide brief summaries.",
tools=assistant.tools,
max_turns=2,
user_input=False,
)
run_response.process()