openai-cookbook/examples/gpt4o/introduction_to_gpt4o.ipynb

810 lines
1.2 MiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to GPT-4o\n",
"GPT-4o (\"o\" for \"omni\") is designed to handle a combination of text, audio, and video inputs, and can generate outputs in text, audio, and image formats.\n",
"\n",
"### Background\n",
"Before GPT-4o, users could interact with ChatGPT using Voice Mode, which operated with three separate models. GPT-4o will integrate these capabilities into a single model that's trained across text, vision, and audio. This unified approach ensures that all inputs—whether text, visual, or auditory—are processed cohesively by the same neural network.\n",
"\n",
"### Current API Capabilities\n",
"Currently, the API supports `{text, image}` inputs only, with `{text}` outputs, the same modalities as `gpt-4-turbo`. Additional modalities, including audio, will be introduced soon. This guide will help you get started with using GPT-4o for text, image, and video understanding.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Getting Started"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Install OpenAI SDK for Python\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade openai --quiet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Configure the OpenAI client and submit a test request\n",
"To setup the client for our use, we need to create an API key to use with our request. Skip these steps if you already have an API key for usage. \n",
"\n",
"You can get an API key by following these steps:\n",
"1. [Create a new project](https://help.openai.com/en/articles/9186755-managing-your-work-in-the-api-platform-with-projects)\n",
"2. [Generate an API key in your project](https://platform.openai.com/api-keys)\n",
"3. (RECOMMENDED, BUT NOT REQUIRED) [Setup your API key for all projects as an env var](https://platform.openai.com/docs/quickstart/step-2-set-up-your-api-key)\n",
"\n",
"Once we have this setup, let's start with a simple {text} input to the model for our first request. We'll use both `system` and `user` messages for our first request, and we'll receive a response from the `assistant` role."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI \n",
"import os\n",
"\n",
"## Set the API key and model name\n",
"MODEL=\"gpt-4o\"\n",
"client = OpenAI(api_key=os.environ.get(\"OPENAI_API_KEY\", \"<your OpenAI API key if not set as an env var>\"))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Assistant: Of course! \n",
"\n",
"\\[ 2 + 2 = 4 \\]\n",
"\n",
"If you have any other questions, feel free to ask!\n"
]
}
],
"source": [
"completion = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant. Help me with my math homework!\"}, # <-- This is the system message that provides context to the model\n",
" {\"role\": \"user\", \"content\": \"Hello! Could you solve 2+2?\"} # <-- This is the user message for which the model will generate a response\n",
" ]\n",
")\n",
"\n",
"print(\"Assistant: \" + completion.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Image Processing\n",
"GPT-4o can directly process images and take intelligent actions based on the image. We can provide images in two formats:\n",
"1. Base64 Encoded\n",
"2. URL\n",
"\n",
"Let's first view the image we'll use, then try sending this image as both Base64 and as a URL link to the API"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnEAAAEcCAAAAACNZL39AAAACXBIWXMAAAsSAAALEgHS3X78AAAfF0lEQVR42u2d63LjuLKlv0xAAMmyXKf3mT3v/34zs0902S2JFICcH3K55Lsk60JKWBHd0W1bFJhYyAQSeRGjouKM0CqCisq4isq4iorKuIrKuIqKyriKyriKyriKisq4iiuFryI4BwTEfKLe8FTGnYVv4qw4S3iplKtW9QyE82TFMlqIlXGVEKdGF7xJInvKDwb5cevrr6r5U69pg7af2RAGaJdiLlXGVZyScE5my42ouwU6s6FZVsZVnAjNutw9Aob4BN0CaJdSKuMqTiLcu8dmnV0xA6RZuwGZDfjEDbtJKuNOBrUwiGmhXQAaejHDFZdddm64WblXxp1MsmjBJ54OCj6rZAPxSTXhwuJW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NFKR0uOv59hQb1GSauL+2r3tNS+rkOCx388Pyz4HJcDSWG5aWGOjEd58k+8SLIS2j74hPvuzR9RuIKCP1FB35iHQe+aCam/OKMcPfI/S8Nf27BXIFowzvHjMq496EGtEPevjNQc5kwvCtEIQzzB9zF9dvJGYeII9EMeXu5NevcrJ5vWH7XjnJxUWNHdoQFR7PMvDxpBZohmrw3zz7zOCddsUF9Fg0pO1Zl6/Yhsc6smphEpZNWXG5zTBwlVfvwlTGxk4Oa5lfx/a6IZt4Jp276sDZ8HkOd8NPrOGh6l3F5O2ROECngE2hBNYXh8FTL42Byt1zWvl4lyizDm/RNEXQwdamMgXAe5qcex4osCNuXrGaIJxacK47iSITVZVfg1M6qpXsk3b0o/efaITa
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import Image, display, Audio, Markdown\n",
"import base64\n",
"\n",
"IMAGE_PATH = \"data/triangle.png\"\n",
"\n",
"# Preview image for context\n",
"display(Image(IMAGE_PATH))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Base64 Image Processing"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"To find the area of the triangle, we can use Heron's formula. First, we need to find the semi-perimeter of the triangle.\n",
"\n",
"The sides of the triangle are 6, 5, and 9.\n",
"\n",
"1. Calculate the semi-perimeter \\( s \\):\n",
"\\[ s = \\frac{a + b + c}{2} = \\frac{6 + 5 + 9}{2} = 10 \\]\n",
"\n",
"2. Use Heron's formula to find the area \\( A \\):\n",
"\\[ A = \\sqrt{s(s-a)(s-b)(s-c)} \\]\n",
"\n",
"Substitute the values:\n",
"\\[ A = \\sqrt{10(10-6)(10-5)(10-9)} \\]\n",
"\\[ A = \\sqrt{10 \\cdot 4 \\cdot 5 \\cdot 1} \\]\n",
"\\[ A = \\sqrt{200} \\]\n",
"\\[ A = 10\\sqrt{2} \\]\n",
"\n",
"So, the area of the triangle is \\( 10\\sqrt{2} \\) square units.\n"
]
}
],
"source": [
"# Open the image file and encode it as a base64 string\n",
"def encode_image(image_path):\n",
" with open(image_path, \"rb\") as image_file:\n",
" return base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
"\n",
"base64_image = encode_image(IMAGE_PATH)\n",
"\n",
"response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant that responds in Markdown. Help me with my math homework!\"},\n",
" {\"role\": \"user\", \"content\": [\n",
" {\"type\": \"text\", \"text\": \"What's the area of the triangle?\"},\n",
" {\"type\": \"image_url\", \"image_url\": {\n",
" \"url\": f\"data:image/png;base64,{base64_image}\"}\n",
" }\n",
" ]}\n",
" ],\n",
" temperature=0.0,\n",
")\n",
"\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### URL Image Processing"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"To find the area of the triangle, we can use Heron's formula. Heron's formula states that the area of a triangle with sides of length \\(a\\), \\(b\\), and \\(c\\) is:\n",
"\n",
"\\[ \\text{Area} = \\sqrt{s(s-a)(s-b)(s-c)} \\]\n",
"\n",
"where \\(s\\) is the semi-perimeter of the triangle:\n",
"\n",
"\\[ s = \\frac{a + b + c}{2} \\]\n",
"\n",
"For the given triangle, the side lengths are \\(a = 5\\), \\(b = 6\\), and \\(c = 9\\).\n",
"\n",
"First, calculate the semi-perimeter \\(s\\):\n",
"\n",
"\\[ s = \\frac{5 + 6 + 9}{2} = \\frac{20}{2} = 10 \\]\n",
"\n",
"Now, apply Heron's formula:\n",
"\n",
"\\[ \\text{Area} = \\sqrt{10(10-5)(10-6)(10-9)} \\]\n",
"\\[ \\text{Area} = \\sqrt{10 \\cdot 5 \\cdot 4 \\cdot 1} \\]\n",
"\\[ \\text{Area} = \\sqrt{200} \\]\n",
"\\[ \\text{Area} = 10\\sqrt{2} \\]\n",
"\n",
"So, the area of the triangle is \\(10\\sqrt{2}\\) square units.\n"
]
}
],
"source": [
"response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant that responds in Markdown. Help me with my math homework!\"},\n",
" {\"role\": \"user\", \"content\": [\n",
" {\"type\": \"text\", \"text\": \"What's the area of the triangle?\"},\n",
" {\"type\": \"image_url\", \"image_url\": {\n",
" \"url\": \"https://upload.wikimedia.org/wikipedia/commons/e/e2/The_Algebra_of_Mohammed_Ben_Musa_-_page_82b.png\"}\n",
" }\n",
" ]}\n",
" ],\n",
" temperature=0.0,\n",
")\n",
"\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Video Processing\n",
"While it's not possible to directly send a video to the API, GPT-4o can understand videos if you sample frames and then provide them as images. It performs better at this task than GPT-4 Turbo.\n",
"\n",
"Since GPT-4o in the API does not yet support audio-in (as of May 2024), we'll use a combination of GPT-4o and Whisper to process both the audio and visual for a provided video, and showcase two usecases:\n",
"1. Summarization\n",
"2. Question and Answering\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup for Video Processing\n",
"We'll use two python packages for video processing - opencv-python and moviepy. \n",
"\n",
"These require [ffmpeg](https://ffmpeg.org/about.html), so make sure to install this beforehand. Depending on your OS, you may need to run `brew install ffmpeg` or `sudo apt install ffmpeg`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install opencv-python --quiet\n",
"%pip install moviepy --quiet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Process the video into two components: frames and audio"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"from moviepy.editor import VideoFileClip\n",
"import time\n",
"import base64\n",
"\n",
"# We'll be using the OpenAI DevDay Keynote Recap video. You can review the video here: https://www.youtube.com/watch?v=h02ti0Bl6zk\n",
"VIDEO_PATH = \"data/keynote_recap.mp4\""
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MoviePy - Writing audio in data/keynote_recap.mp3\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"MoviePy - Done.\n",
"Extracted 218 frames\n",
"Extracted audio to data/keynote_recap.mp3\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r"
]
}
],
"source": [
"def process_video(video_path, seconds_per_frame=2):\n",
" base64Frames = []\n",
" base_video_path, _ = os.path.splitext(video_path)\n",
"\n",
" video = cv2.VideoCapture(video_path)\n",
" total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))\n",
" fps = video.get(cv2.CAP_PROP_FPS)\n",
" frames_to_skip = int(fps * seconds_per_frame)\n",
" curr_frame=0\n",
"\n",
" # Loop through the video and extract frames at specified sampling rate\n",
" while curr_frame < total_frames - 1:\n",
" video.set(cv2.CAP_PROP_POS_FRAMES, curr_frame)\n",
" success, frame = video.read()\n",
" if not success:\n",
" break\n",
" _, buffer = cv2.imencode(\".jpg\", frame)\n",
" base64Frames.append(base64.b64encode(buffer).decode(\"utf-8\"))\n",
" curr_frame += frames_to_skip\n",
" video.release()\n",
"\n",
" # Extract audio from video\n",
" audio_path = f\"{base_video_path}.mp3\"\n",
" clip = VideoFileClip(video_path)\n",
" clip.audio.write_audiofile(audio_path, bitrate=\"32k\")\n",
" clip.audio.close()\n",
" clip.close()\n",
"\n",
" print(f\"Extracted {len(base64Frames)} frames\")\n",
" print(f\"Extracted audio to {audio_path}\")\n",
" return base64Frames, audio_path\n",
"\n",
"# Extract 1 frame per second. You can adjust the `seconds_per_frame` parameter to change the sampling rate\n",
"base64Frames, audio_path = process_video(VIDEO_PATH, seconds_per_frame=1)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCALQBQADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD4Dooor6g/lcKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {
"image/jpeg": {
"width": 600
}
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" <audio controls=\"controls\" >\n",
" <source src=\"data:audio/mpeg;base64,SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU4LjI5LjEwMAAAAAAAAAAAAAAA//tQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8AAB+PAAziXQADBggLDRASFRcaHB8hJCYpKy4wMzU4Oj1AQkZIS01QUlVXWlxfYWRmaWtucHN1eHp9gIKFh4qMkJKVl5qcn6GkpqmrrrCztbi6vcDCxcfKzM/R1Nba3N/h5Obp6+7w8/X4+v0AAAAATGF2YzU4LjU0AAAAAAAAAAAAAAAAJAU0AAAAAAAM4l1dmhK4AAAAAAAAAAAAAAAAAAAAAP/7EGQAD/AAAGkAAAAIAAANIAAAAQAAAaQAAAAgAAA0gAAABAUQKxYyaP/mjIKIFYqaMnPzJpUx8eTAfuTAz4CD8MHfgy0p0DOUWP4gqYR24rJ+wBBIBamBwyjXBuQSvMpccm1qsYxi//sSZCIP8AAAaQAAAAgAAA0gAAABAAABpAAAACAAADSAAAAEgGaL0VOC5ASBJQ73I/uwxtkNYvd+/RVrTSoAB0RmACncQCJRQfBmKSihL11Oos9n0a7g4zAS7oGjySnpFxsZQ7Tm0Mcx//sQZESP8AAAaQAAAAgAAA0gAAABAAABpAAAACAAADSAAAAE3TJ0QNh4UGD3mm/16dfx+v//Q4vUTMLN7jBhSjGpl6m+rf/31cf8KT8mHhogqBBzeILNZ6z1N/1AB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AbPAwkBIA4g2cAp5wQI8Rf/I9DAIReyxobS0Aqb/
" Your browser does not support the audio element.\n",
" </audio>\n",
" "
],
"text/plain": [
"<IPython.lib.display.Audio object>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## Display the frames and audio for context\n",
"display_handle = display(None, display_id=True)\n",
"for img in base64Frames:\n",
" display_handle.update(Image(data=base64.b64decode(img.encode(\"utf-8\")), width=600))\n",
" time.sleep(0.025)\n",
"\n",
"Audio(audio_path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example 1: Summarization\n",
"Now that we have both the video frames and the audio, let's run a few different tests to generate a video summary to compare the results of using the models with different modalities. We should expect to see that the summary generated with context from both visual and audio inputs will be the most accurate, as the model is able to use the entire context from the video.\n",
"\n",
"1. Visual Summary\n",
"2. Audio Summary\n",
"3. Visual + Audio Summary\n",
"\n",
"#### Visual Summary\n",
"The visual summary is generated by sending the model only the frames from the video. With just the frames, the model is likely to capture the visual aspects, but will miss any details discussed by the speaker."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"## Video Summary: OpenAI DevDay Keynote Recap\n",
"\n",
"The video appears to be a keynote recap from OpenAI's DevDay event. Here are the key points covered in the video:\n",
"\n",
"1. **Introduction and Event Overview**:\n",
" - The video starts with the title \"OpenAI DevDay\" and transitions to \"Keynote Recap.\"\n",
" - The event venue is shown, with attendees gathering and the stage set up.\n",
"\n",
"2. **Keynote Presentation**:\n",
" - A speaker, presumably from OpenAI, takes the stage to present.\n",
" - The presentation covers various topics related to OpenAI's latest developments and announcements.\n",
"\n",
"3. **Announcements**:\n",
" - **GPT-4 Turbo**: Introduction of GPT-4 Turbo, highlighting its enhanced capabilities and performance.\n",
" - **JSON Mode**: A new feature that allows for structured data output in JSON format.\n",
" - **Function Calling**: Demonstration of improved function calling capabilities, making interactions more efficient.\n",
" - **Context Length and Control**: Enhancements in context length and user control over the model's responses.\n",
" - **Better Knowledge Integration**: Improvements in the model's knowledge base and retrieval capabilities.\n",
"\n",
"4. **Product Demonstrations**:\n",
" - **DALL-E 3**: Introduction of DALL-E 3 for advanced image generation.\n",
" - **Custom Models**: Announcement of custom models, allowing users to tailor models to specific needs.\n",
" - **API Enhancements**: Updates to the API, including threading, retrieval, and code interpreter functionalities.\n",
"\n",
"5. **Pricing and Token Efficiency**:\n",
" - Discussion on GPT-4 Turbo pricing, emphasizing cost efficiency with reduced input and output tokens.\n",
"\n",
"6. **New Features and Tools**:\n",
" - Introduction of new tools and features for developers, including a variety of GPT-powered applications.\n",
" - Emphasis on building with natural language and the ease of creating custom applications.\n",
"\n",
"7. **Closing Remarks**:\n",
" - The speaker concludes the presentation, thanking the audience and highlighting the future of OpenAI's developments.\n",
"\n",
"The video ends with the OpenAI logo and the event title \"OpenAI DevDay.\"\n"
]
}
],
"source": [
"response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are generating a video summary. Please provide a summary of the video. Respond in Markdown.\"},\n",
" {\"role\": \"user\", \"content\": [\n",
" \"These are the frames from the video.\",\n",
" *map(lambda x: {\"type\": \"image_url\", \n",
" \"image_url\": {\"url\": f'data:image/jpg;base64,{x}', \"detail\": \"low\"}}, base64Frames)\n",
" ],\n",
" }\n",
" ],\n",
" temperature=0,\n",
")\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The results are as expected - the model is able to capture the high level aspects of the video visuals, but misses the details provided in the speech.\n",
"\n",
"#### Audio Summary\n",
"The audio summary is generated by sending the model the audio transcript. With just the audio, the model is likely to bias towards the audio content, and will miss the context provided by the presentations and visuals.\n",
"\n",
"`{audio}` input for GPT-4o isn't currently available but will be coming soon! For now, we use our existing `whisper-1` model to process the audio"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"### Summary\n",
"\n",
"Welcome to OpenAI's first-ever Dev Day. Key announcements include:\n",
"\n",
"- **GPT-4 Turbo**: A new model supporting up to 128,000 tokens of context, featuring JSON mode for valid JSON responses, improved instruction following, and better knowledge retrieval from external documents or databases. It is also significantly cheaper than GPT-4.\n",
"- **New Features**: \n",
" - **Dolly 3**, **GPT-4 Turbo with Vision**, and a new **Text-to-Speech model** are now available in the API.\n",
" - **Custom Models**: A program where OpenAI researchers help companies create custom models tailored to their specific use cases.\n",
" - **Increased Rate Limits**: Doubling tokens per minute for established GPT-4 customers and allowing requests for further rate limit changes.\n",
"- **GPTs**: Tailored versions of ChatGPT for specific purposes, programmable through conversation, with options for private or public sharing, and a forthcoming GPT Store.\n",
"- **Assistance API**: Includes persistent threads, built-in retrieval, a code interpreter, and improved function calling.\n",
"\n",
"OpenAI is excited about the future of AI integration and looks forward to seeing what users will create with these new tools. The event concludes with an invitation to return next year for more advancements.\n"
]
}
],
"source": [
"# Transcribe the audio\n",
"transcription = client.audio.transcriptions.create(\n",
" model=\"whisper-1\",\n",
" file=open(audio_path, \"rb\"),\n",
")\n",
"## OPTIONAL: Uncomment the line below to print the transcription\n",
"#print(\"Transcript: \", transcription.text + \"\\n\\n\")\n",
"\n",
"response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\":\"\"\"You are generating a transcript summary. Create a summary of the provided transcription. Respond in Markdown.\"\"\"},\n",
" {\"role\": \"user\", \"content\": [\n",
" {\"type\": \"text\", \"text\": f\"The audio transcription is: {transcription.text}\"}\n",
" ],\n",
" }\n",
" ],\n",
" temperature=0,\n",
")\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The audio summary is biased towards the content discussed during the speech, but comes out with much less structure than the video summary.\n",
"\n",
"#### Audio + Visual Summary\n",
"The Audio + Visual summary is generated by sending the model both the visual and the audio from the video at once. When sending both of these, the model is expected to better summarize since it can perceive the entire video at once."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"## Video Summary: OpenAI Dev Day\n",
"\n",
"### Introduction\n",
"- The video begins with the title \"OpenAI Dev Day\" and transitions to a keynote recap.\n",
"\n",
"### Event Overview\n",
"- The event is held at a venue with a sign reading \"OpenAI Dev Day.\"\n",
"- Attendees are seen entering and gathering in a large hall.\n",
"\n",
"### Keynote Presentation\n",
"- The keynote speaker introduces the event and announces the launch of GPT-4 Turbo.\n",
"- **GPT-4 Turbo**:\n",
" - Supports up to 128,000 tokens of context.\n",
" - Introduces a new feature called JSON mode for valid JSON responses.\n",
" - Improved function calling capabilities.\n",
" - Enhanced instruction-following and knowledge retrieval from external documents or databases.\n",
" - Knowledge updated up to April 2023.\n",
" - Available in the API along with DALL-E 3, GPT-4 Turbo with Vision, and a new Text-to-Speech model.\n",
"\n",
"### Custom Models\n",
"- Launch of a new program called Custom Models.\n",
" - Researchers will collaborate with companies to create custom models tailored to specific use cases.\n",
" - Higher rate limits and the ability to request changes to rate limits and quotas directly in API settings.\n",
"\n",
"### Pricing and Performance\n",
"- **GPT-4 Turbo**:\n",
" - 3x cheaper for prompt tokens and 2x cheaper for completion tokens compared to GPT-4.\n",
" - Doubling the tokens per minute for established GPT-4 customers.\n",
"\n",
"### Introduction of GPTs\n",
"- **GPTs**:\n",
" - Tailored versions of ChatGPT for specific purposes.\n",
" - Combine instructions, expanded knowledge, and actions for better performance and control.\n",
" - Can be created without coding, through conversation.\n",
" - Options to make GPTs private, share publicly, or create for company use in ChatGPT Enterprise.\n",
" - Announcement of the upcoming GPT Store.\n",
"\n",
"### Assistance API\n",
"- **Assistance API**:\n",
" - Includes persistent threads for handling long conversation history.\n",
" - Built-in retrieval and code interpreter with a working Python interpreter in a sandbox environment.\n",
" - Improved function calling.\n",
"\n",
"### Conclusion\n",
"- The speaker emphasizes the potential of integrating intelligence everywhere, providing \"superpowers on demand.\"\n",
"- Encourages attendees to return next year, hinting at even more advanced developments.\n",
"- The event concludes with thanks to the attendees.\n",
"\n",
"### Closing\n",
"- The video ends with the OpenAI logo and a final thank you message.\n"
]
}
],
"source": [
"## Generate a summary with visual and audio\n",
"response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\":\"\"\"You are generating a video summary. Create a summary of the provided video and its transcript. Respond in Markdown\"\"\"},\n",
" {\"role\": \"user\", \"content\": [\n",
" \"These are the frames from the video.\",\n",
" *map(lambda x: {\"type\": \"image_url\", \n",
" \"image_url\": {\"url\": f'data:image/jpg;base64,{x}', \"detail\": \"low\"}}, base64Frames),\n",
" {\"type\": \"text\", \"text\": f\"The audio transcription is: {transcription.text}\"}\n",
" ],\n",
" }\n",
"],\n",
" temperature=0,\n",
")\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After combining both the video and audio, we're able to get a much more detailed and comprehensive summary for the event which uses information from both the visual and audio elements from the video.\n",
"\n",
"### Example 2: Question and Answering\n",
"For the Q&A, we'll use the same concept as before to ask questions of our processed video while running the same 3 tests to demonstrate the benefit of combining input modalities:\n",
"1. Visual Q&A\n",
"2. Audio Q&A\n",
"3. Visual + Audio Q&A "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"QUESTION = \"Question: Why did Sam Altman have an example about raising windows and turning the radio on?\""
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Visual QA: \n",
"Sam Altman used the example about raising windows and turning the radio on to demonstrate the function calling capability of GPT-4 Turbo. The example illustrated how the model can interpret and execute multiple commands in a more structured and efficient manner. The \"before\" and \"after\" comparison showed how the model can now directly call functions like `raise_windows()` and `radio_on()` based on natural language instructions, showcasing improved control and functionality.\n"
]
}
],
"source": [
"qa_visual_response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"Use the video to answer the provided question. Respond in Markdown.\"},\n",
" {\"role\": \"user\", \"content\": [\n",
" \"These are the frames from the video.\",\n",
" *map(lambda x: {\"type\": \"image_url\", \"image_url\": {\"url\": f'data:image/jpg;base64,{x}', \"detail\": \"low\"}}, base64Frames),\n",
" QUESTION\n",
" ],\n",
" }\n",
" ],\n",
" temperature=0,\n",
")\n",
"print(\"Visual QA:\\n\" + qa_visual_response.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Audio QA:\n",
"The provided transcription does not include any mention of Sam Altman or an example about raising windows and turning the radio on. Therefore, I cannot provide an answer based on the given transcription.\n"
]
}
],
"source": [
"qa_audio_response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\":\"\"\"Use the transcription to answer the provided question. Respond in Markdown.\"\"\"},\n",
" {\"role\": \"user\", \"content\": f\"The audio transcription is: {transcription.text}. \\n\\n {QUESTION}\"},\n",
" ],\n",
" temperature=0,\n",
")\n",
"print(\"Audio QA:\\n\" + qa_audio_response.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Both QA:\n",
"Sam Altman used the example of raising windows and turning the radio on to demonstrate the improved function calling capabilities of GPT-4 Turbo. The example illustrated how the model can now handle multiple function calls more effectively and follow instructions better. In the \"before\" scenario, the model had to be prompted separately for each action, whereas in the \"after\" scenario, the model could handle both actions in a single prompt, showcasing its enhanced ability to manage and execute multiple tasks simultaneously.\n"
]
}
],
"source": [
"qa_both_response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\":\"\"\"Use the video and transcription to answer the provided question.\"\"\"},\n",
" {\"role\": \"user\", \"content\": [\n",
" \"These are the frames from the video.\",\n",
" *map(lambda x: {\"type\": \"image_url\", \n",
" \"image_url\": {\"url\": f'data:image/jpg;base64,{x}', \"detail\": \"low\"}}, base64Frames),\n",
" {\"type\": \"text\", \"text\": f\"The audio transcription is: {transcription.text}\"},\n",
" QUESTION\n",
" ],\n",
" }\n",
" ],\n",
" temperature=0,\n",
")\n",
"print(\"Both QA:\\n\" + qa_both_response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Comparing the three answers, the most accurate answer is generated by using both the audio and visual from the video. Sam Altman did not discuss the raising windows or radio on during the Keynote, but referenced an improved capability for the model to execute multiple functions in a single request while the examples were shown behind him.\n",
"\n",
"## Conclusion\n",
"Integrating many input modalities such as audio, visual, and textual, significantly enhances the performance of the model on a diverse range of tasks. This multimodal approach allows for more comprehensive understanding and interaction, mirroring more closely how humans perceive and process information. \n",
"\n",
"Currently, GPT-4o in the API supports text and image inputs, with audio capabilities coming soon."
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}