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:
Note: If you have been using
autogen
orpyautogen
, all you need to do is upgrade it using:or
as
pyautogen
,autogen
, andag2
are 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:
Agent Configuration#
config_list = autogen.config_list_from_json(
env_or_file="OAI_CONFIG_LIST",
filter_dict={
"model": ["gpt-4o-mini"],
},
)
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.