Skip to content

Building AI agents, atomically

add YT knowledge extraction tutorial

KennyVaneetvelde5f59d06df84f
diff --git a/examples/notebooks/youtube_knowledge_extraction.ipynb b/examples/notebooks/youtube_knowledge_extraction.ipynb
new file mode 100644
index 0000000..4907edb
--- /dev/null
+++ b/examples/notebooks/youtube_knowledge_extraction.ipynb
@@ -0,0 +1,344 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# YouTube Knowledge Extraction Tutorial\n",
+ "\n",
+ "This tutorial demonstrates how to use the Atomic Agents library to create an agent that extracts knowledge and insights from YouTube video transcripts.\n",
+ "\n",
+ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/username/repo/blob/main/youtube_knowledge_extraction_tutorial.ipynb)\n",
+ "\n",
+ "## Prerequisites\n",
+ "\n",
+ "Before proceeding, make sure you have a good understanding of the following libraries:\n",
+ "\n",
+ "- **Pydantic**: A data validation and settings management library using Python type annotations. ([Pydantic GitHub](https://github.com/pydantic/pydantic))\n",
+ "- **Instructor**: A Python library that simplifies working with structured outputs from large language models (LLMs). ([Instructor GitHub](https://github.com/jxnl/instructor))\n",
+ "\n",
+ "You'll also need an OpenAI API key to use the GPT models. If you don't have one, you can sign up at [OpenAI's website](https://openai.com/).\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 1: Install Required Packages\n",
+ "\n",
+ "First, let's install the necessary packages for our YouTube knowledge extraction agent."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Install required packages\n",
+ "%pip install atomic-agents openai instructor python-dotenv"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Set Up OpenAI API Key\n",
+ "\n",
+ "To use the OpenAI API, you need to set up your API key. You have three options:\n",
+ "\n",
+ "1. Enter it directly in the code (not recommended for shared notebooks)\n",
+ "2. Use a .env file\n",
+ "3. Input the key manually when prompted\n",
+ "\n",
+ "Choose the method that best suits your needs and security requirements."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "from dotenv import load_dotenv\n",
+ "\n",
+ "# Option 1: Set the API key directly (replace with your actual API key)\n",
+ "# os.environ[\"OPENAI_API_KEY\"] = \"your-api-key-here\"\n",
+ "\n",
+ "# Option 2: Load from .env file\n",
+ "load_dotenv()\n",
+ "\n",
+ "# Option 3: Input the key manually\n",
+ "if \"OPENAI_API_KEY\" not in os.environ:\n",
+ " os.environ[\"OPENAI_API_KEY\"] = input(\"Enter your OpenAI API key: \")\n",
+ "\n",
+ "# Verify that the API key is set\n",
+ "if not os.getenv(\"OPENAI_API_KEY\"):\n",
+ " raise ValueError(\"OpenAI API key is not set. Please set it using one of the provided methods.\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Import Libraries\n",
+ "\n",
+ "Now, let's import the necessary libraries for creating the YouTube knowledge extraction agent."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import instructor\n",
+ "import openai\n",
+ "from pydantic import BaseModel, Field\n",
+ "from rich.console import Console\n",
+ "from rich.markdown import Markdown\n",
+ "from typing import List, Optional\n",
+ "\n",
+ "from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentConfig\n",
+ "from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfo\n",
+ "from atomic_agents.lib.tools.yt_transcript_scraper import YouTubeTranscriptTool, YouTubeTranscriptToolConfig, YouTubeTranscriptToolSchema"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: Initialize Components\n",
+ "\n",
+ "Let's initialize the necessary components including the console, client, and YouTube transcript scraper tool."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "console = Console()\n",
+ "client = instructor.from_openai(openai.OpenAI())\n",
+ "yt_scraper_tool = YouTubeTranscriptTool(config=YouTubeTranscriptToolConfig())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 5: Define System Prompt Information\n",
+ "\n",
+ "Now, we'll define the system prompt information including background, steps, output instructions, and context providers. This information guides the agent's behavior and output format."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "class YtTranscriptProvider(SystemPromptContextProviderBase):\n",
+ " def __init__(self, **kwargs):\n",
+ " super().__init__(**kwargs)\n",
+ " self.transcript = None\n",
+ " self.duration = None\n",
+ " self.metadata = None\n",
+ " \n",
+ " def get_info(self) -> str:\n",
+ " return f'VIDEO TRANSCRIPT: \"{self.transcript}\"\\n\\nDURATION: {self.duration}\\n\\nMETADATA: {self.metadata}'\n",
+ " \n",
+ "transcript_provider = YtTranscriptProvider(title='YouTube Transcript')\n",
+ "\n",
+ "system_prompt_info = SystemPromptInfo(\n",
+ " background=[\n",
+ " 'This Assistant is an expert at extracting knowledge and other insightful and interesting information from YouTube transcripts.'\n",
+ " ],\n",
+ " steps=[\n",
+ " 'Analyse the YouTube transcript thoroughly to extract the most valuable insights, facts, and recommendations.',\n",
+ " 'Adhere strictly to the provided schema when extracting information from the input content.',\n",
+ " 'Ensure that the output matches the field descriptions, types and constraints exactly.',\n",
+ " ],\n",
+ " output_instructions=[\n",
+ " 'Only output Markdown-compatible strings.',\n",
+ " 'Ensure you follow ALL these instructions when creating your output.'\n",
+ " ],\n",
+ " context_providers={'yt_transcript': transcript_provider}\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 6: Define Response Model\n",
+ "\n",
+ "Let's define the response model with detailed descriptions, constraints, and field types. This model will structure the output of our knowledge extraction agent."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "class ResponseModel(BaseModel):\n",
+ " summary: str = Field(..., description=\"A short summary of the content, including who is presenting and the content being discussed.\")\n",
+ " insights: List[str] = Field(..., min_items=5, max_items=5, description=\"exactly 5 of the best insights and ideas from the input.\")\n",
+ " quotes: List[str] = Field(None, min_items=5, max_items=5, description=\"exactly 5 of the most surprising, insightful, and/or interesting quotes from the input.\")\n",
+ " habits: Optional[List[str]] = Field(None, min_items=5, max_items=5, description=\"exactly 5 of the most practical and useful personal habits mentioned by the speakers.\")\n",
+ " facts: List[str] = Field(..., min_items=5, max_items=5, description=\"exactly 5 of the most surprising, insightful, and/or interesting valid facts about the greater world mentioned in the content.\")\n",
+ " recommendations: List[str] = Field(..., min_items=5, max_items=5, description=\"exactly 5 of the most surprising, insightful, and/or interesting recommendations from the content.\") \n",
+ " references: List[str] = Field(..., description=\"All mentions of writing, art, tools, projects, and other sources of inspiration mentioned by the speakers.\")\n",
+ " one_sentence_takeaway: str = Field(..., description=\"The most potent takeaways and recommendations condensed into a single 20-word sentence.\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 7: Create the Chat Agent\n",
+ "\n",
+ "Now, let's create a chat agent with the specified model, system prompt generator, and response model."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "agent = BaseChatAgent(\n",
+ " config=BaseChatAgentConfig(\n",
+ " client=client,\n",
+ " system_prompt_generator=SystemPromptGenerator(system_prompt_info),\n",
+ " model='gpt-3.5-turbo',\n",
+ " output_schema=ResponseModel\n",
+ " )\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 8: Get YouTube Video URL\n",
+ "\n",
+ "Let's prompt the user to enter a YouTube video URL and use the YouTube transcript scraper tool to extract the transcript, duration, and metadata."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "video_url = input('Enter the YouTube video URL: ')\n",
+ "scraped_transcript = yt_scraper_tool.run(YouTubeTranscriptToolSchema(video_url=video_url))\n",
+ "transcript_provider.transcript = scraped_transcript.transcript\n",
+ "transcript_provider.duration = scraped_transcript.duration\n",
+ "transcript_provider.metadata = scraped_transcript.metadata\n",
+ "\n",
+ "print(f\"Successfully scraped transcript for video: {scraped_transcript.metadata.get('title', 'Unknown Title')}\")\n",
+ "print(f\"Video duration: {scraped_transcript.duration} seconds\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 9: Run the Agent\n",
+ "\n",
+ "Now, let's run the agent to extract knowledge and insights from the YouTube video transcript."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print(\"Analyzing the transcript. This may take a few moments...\")\n",
+ "response = agent.run(agent.input_schema(chat_message='Perform your assignment on the YouTube video transcript present in your context. Do not reply with anything other than the output of the assignment.'))\n",
+ "print(\"Analysis complete!\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 10: Format and Print the Response\n",
+ "\n",
+ "Finally, let's convert the response to a dictionary, format it as a Markdown string, and print the response in a pretty Markdown format."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "response_dict = response.model_dump()\n",
+ "\n",
+ "def format_markdown_section(title, items):\n",
+ " if isinstance(items, list):\n",
+ " return f\"## {title}\\n\" + \"\\n\".join([f\"- {item}\" for item in items]) + \"\\n\"\n",
+ " return f\"## {title}\\n{items}\\n\"\n",
+ "\n",
+ "markdown_string = \"\"\n",
+ "for key, value in response_dict.items():\n",
+ " title = key.replace('_', ' ').title()\n",
+ " markdown_string += format_markdown_section(title, value)\n",
+ "\n",
+ "markdown_response = Markdown(markdown_string)\n",
+ "console.print(markdown_response)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Conclusion\n",
+ "\n",
+ "Congratulations! You've successfully created a YouTube knowledge extraction agent using the Atomic Agents library. Here's a summary of what we accomplished:\n",
+ "\n",
+ "1. Set up the environment and installed necessary packages.\n",
+ "2. Configured the OpenAI API key securely.\n",
+ "3. Initialized components including the OpenAI client and YouTube transcript scraper tool.\n",
+ "4. Defined a system prompt to guide the agent's behavior.\n",
+ "5. Created a structured response model to format the extracted information.\n",
+ "6. Built a chat agent using the Atomic Agents library.\n",
+ "7. Extracted and processed a YouTube video transcript.\n",
+ "8. Ran the agent to analyze the transcript and generate insights.\n",
+ "9. Formatted and displayed the results in a readable Markdown format.\n",
+ "\n",
+ "This agent can be further customized by modifying the system prompt information, response model, and other components to suit specific requirements. For example, you could adapt it to focus on different aspects of the video content or integrate it with other tools and APIs.\n",
+ "\n",
+ "Remember to handle API keys securely in your projects, especially when sharing or deploying your code."
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.12.3"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4
+ }
\ No newline at end of file