{ "cells": [ { "cell_type": "markdown", "id": "51c7a1f9", "metadata": {}, "source": [ "# Batch processing with the Batch API\n", "\n", "The new Batch API allows to **create async batch jobs for a lower price and with higher rate limits**.\n", "\n", "Batches will be completed within 24h, but may be processed sooner depending on global usage. \n", "\n", "Ideal use cases for the Batch API include:\n", "\n", "- Tagging, captioning, or enriching content on a marketplace or blog\n", "- Categorizing and suggesting answers for support tickets\n", "- Performing sentiment analysis on large datasets of customer feedback\n", "- Generating summaries or translations for collections of documents or articles\n", "\n", "and much more!\n", "\n", "This cookbook will walk you through how to use the Batch API with a couple of practical examples.\n", "\n", "We will start with an example to categorize movies using `gpt-4o-mini`, and then cover how we can use the vision capabilities of this model to caption images.\n", "\n", "Please note that multiple models are available through the Batch API, and that you can use the same parameters in your Batch API calls as with the Chat Completions endpoint." ] }, { "cell_type": "markdown", "id": "85ae3c4e", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": null, "id": "02e22580", "metadata": {}, "outputs": [], "source": [ "# Make sure you have the latest version of the SDK available to use the Batch API\n", "%pip install openai --upgrade" ] }, { "cell_type": "code", "execution_count": 1, "id": "726bacba", "metadata": {}, "outputs": [], "source": [ "import json\n", "from openai import OpenAI\n", "import pandas as pd\n", "from IPython.display import Image, display" ] }, { "cell_type": "code", "execution_count": 2, "id": "4ac0c9a7", "metadata": {}, "outputs": [], "source": [ "# Initializing OpenAI client - see https://platform.openai.com/docs/quickstart?context=python\n", "client = OpenAI()" ] }, { "cell_type": "markdown", "id": "5fec950f", "metadata": {}, "source": [ "## First example: Categorizing movies\n", "\n", "In this example, we will use `gpt-4o-mini` to extract movie categories from a description of the movie. We will also extract a 1-sentence summary from this description. \n", "\n", "We will use [JSON mode](https://platform.openai.com/docs/guides/text-generation/json-mode) to extract categories as an array of strings and the 1-sentence summary in a structured format. \n", "\n", "For each movie, we want to get a result that looks like this:\n", "\n", "```\n", "{\n", " categories: ['category1', 'category2', 'category3'],\n", " summary: '1-sentence summary'\n", "}\n", "```" ] }, { "cell_type": "markdown", "id": "9fc6dcd6", "metadata": {}, "source": [ "### Loading data\n", "\n", "We will use the IMDB top 1000 movies dataset for this example. " ] }, { "cell_type": "code", "execution_count": 3, "id": "1b721a0d", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | Poster_Link | \n", "Series_Title | \n", "Released_Year | \n", "Certificate | \n", "Runtime | \n", "Genre | \n", "IMDB_Rating | \n", "Overview | \n", "Meta_score | \n", "Director | \n", "Star1 | \n", "Star2 | \n", "Star3 | \n", "Star4 | \n", "No_of_Votes | \n", "Gross | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "https://m.media-amazon.com/images/M/MV5BMDFkYT... | \n", "The Shawshank Redemption | \n", "1994 | \n", "A | \n", "142 min | \n", "Drama | \n", "9.3 | \n", "Two imprisoned men bond over a number of years... | \n", "80.0 | \n", "Frank Darabont | \n", "Tim Robbins | \n", "Morgan Freeman | \n", "Bob Gunton | \n", "William Sadler | \n", "2343110 | \n", "28,341,469 | \n", "
1 | \n", "https://m.media-amazon.com/images/M/MV5BM2MyNj... | \n", "The Godfather | \n", "1972 | \n", "A | \n", "175 min | \n", "Crime, Drama | \n", "9.2 | \n", "An organized crime dynasty's aging patriarch t... | \n", "100.0 | \n", "Francis Ford Coppola | \n", "Marlon Brando | \n", "Al Pacino | \n", "James Caan | \n", "Diane Keaton | \n", "1620367 | \n", "134,966,411 | \n", "
2 | \n", "https://m.media-amazon.com/images/M/MV5BMTMxNT... | \n", "The Dark Knight | \n", "2008 | \n", "UA | \n", "152 min | \n", "Action, Crime, Drama | \n", "9.0 | \n", "When the menace known as the Joker wreaks havo... | \n", "84.0 | \n", "Christopher Nolan | \n", "Christian Bale | \n", "Heath Ledger | \n", "Aaron Eckhart | \n", "Michael Caine | \n", "2303232 | \n", "534,858,444 | \n", "
3 | \n", "https://m.media-amazon.com/images/M/MV5BMWMwMG... | \n", "The Godfather: Part II | \n", "1974 | \n", "A | \n", "202 min | \n", "Crime, Drama | \n", "9.0 | \n", "The early life and career of Vito Corleone in ... | \n", "90.0 | \n", "Francis Ford Coppola | \n", "Al Pacino | \n", "Robert De Niro | \n", "Robert Duvall | \n", "Diane Keaton | \n", "1129952 | \n", "57,300,000 | \n", "
4 | \n", "https://m.media-amazon.com/images/M/MV5BMWU4N2... | \n", "12 Angry Men | \n", "1957 | \n", "U | \n", "96 min | \n", "Crime, Drama | \n", "9.0 | \n", "A jury holdout attempts to prevent a miscarria... | \n", "96.0 | \n", "Sidney Lumet | \n", "Henry Fonda | \n", "Lee J. Cobb | \n", "Martin Balsam | \n", "John Fiedler | \n", "689845 | \n", "4,360,000 | \n", "
\n", " | asin | \n", "url | \n", "title | \n", "brand | \n", "price | \n", "availability | \n", "categories | \n", "primary_image | \n", "images | \n", "upc | \n", "... | \n", "color | \n", "material | \n", "style | \n", "important_information | \n", "product_overview | \n", "about_item | \n", "description | \n", "specifications | \n", "uniq_id | \n", "scraped_at | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "B0CJHKVG6P | \n", "https://www.amazon.com/dp/B0CJHKVG6P | \n", "GOYMFK 1pc Free Standing Shoe Rack, Multi-laye... | \n", "GOYMFK | \n", "$24.99 | \n", "Only 13 left in stock - order soon. | \n", "['Home & Kitchen', 'Storage & Organization', '... | \n", "https://m.media-amazon.com/images/I/416WaLx10j... | \n", "['https://m.media-amazon.com/images/I/416WaLx1... | \n", "NaN | \n", "... | \n", "White | \n", "Metal | \n", "Modern | \n", "[] | \n", "[{'Brand': ' GOYMFK '}, {'Color': ' White '}, ... | \n", "['Multiple layers: Provides ample storage spac... | \n", "multiple shoes, coats, hats, and other items E... | \n", "['Brand: GOYMFK', 'Color: White', 'Material: M... | \n", "02593e81-5c09-5069-8516-b0b29f439ded | \n", "2024-02-02 15:15:08 | \n", "
1 | \n", "B0B66QHB23 | \n", "https://www.amazon.com/dp/B0B66QHB23 | \n", "subrtex Leather ding Room, Dining Chairs Set o... | \n", "subrtex | \n", "NaN | \n", "NaN | \n", "['Home & Kitchen', 'Furniture', 'Dining Room F... | \n", "https://m.media-amazon.com/images/I/31SejUEWY7... | \n", "['https://m.media-amazon.com/images/I/31SejUEW... | \n", "NaN | \n", "... | \n", "Black | \n", "Sponge | \n", "Black Rubber Wood | \n", "[] | \n", "NaN | \n", "['【Easy Assembly】: Set of 2 dining room chairs... | \n", "subrtex Dining chairs Set of 2 | \n", "['Brand: subrtex', 'Color: Black', 'Product Di... | \n", "5938d217-b8c5-5d3e-b1cf-e28e340f292e | \n", "2024-02-02 15:15:09 | \n", "
2 | \n", "B0BXRTWLYK | \n", "https://www.amazon.com/dp/B0BXRTWLYK | \n", "Plant Repotting Mat MUYETOL Waterproof Transpl... | \n", "MUYETOL | \n", "$5.98 | \n", "In Stock | \n", "['Patio, Lawn & Garden', 'Outdoor Décor', 'Doo... | \n", "https://m.media-amazon.com/images/I/41RgefVq70... | \n", "['https://m.media-amazon.com/images/I/41RgefVq... | \n", "NaN | \n", "... | \n", "Green | \n", "Polyethylene | \n", "Modern | \n", "[] | \n", "[{'Brand': ' MUYETOL '}, {'Size': ' 26.8*26.8 ... | \n", "['PLANT REPOTTING MAT SIZE: 26.8\" x 26.8\", squ... | \n", "NaN | \n", "['Brand: MUYETOL', 'Size: 26.8*26.8', 'Item We... | \n", "b2ede786-3f51-5a45-9a5b-bcf856958cd8 | \n", "2024-02-02 15:15:09 | \n", "
3 | \n", "B0C1MRB2M8 | \n", "https://www.amazon.com/dp/B0C1MRB2M8 | \n", "Pickleball Doormat, Welcome Doormat Absorbent ... | \n", "VEWETOL | \n", "$13.99 | \n", "Only 10 left in stock - order soon. | \n", "['Patio, Lawn & Garden', 'Outdoor Décor', 'Doo... | \n", "https://m.media-amazon.com/images/I/61vz1Igler... | \n", "['https://m.media-amazon.com/images/I/61vz1Igl... | \n", "NaN | \n", "... | \n", "A5589 | \n", "Rubber | \n", "Modern | \n", "[] | \n", "[{'Brand': ' VEWETOL '}, {'Size': ' 16*24INCH ... | \n", "['Specifications: 16x24 Inch ', \" High-Quality... | \n", "The decorative doormat features a subtle textu... | \n", "['Brand: VEWETOL', 'Size: 16*24INCH', 'Materia... | \n", "8fd9377b-cfa6-5f10-835c-6b8eca2816b5 | \n", "2024-02-02 15:15:10 | \n", "
4 | \n", "B0CG1N9QRC | \n", "https://www.amazon.com/dp/B0CG1N9QRC | \n", "JOIN IRON Foldable TV Trays for Eating Set of ... | \n", "JOIN IRON Store | \n", "$89.99 | \n", "Usually ships within 5 to 6 weeks | \n", "['Home & Kitchen', 'Furniture', 'Game & Recrea... | \n", "https://m.media-amazon.com/images/I/41p4d4VJnN... | \n", "['https://m.media-amazon.com/images/I/41p4d4VJ... | \n", "NaN | \n", "... | \n", "Grey Set of 4 | \n", "Iron | \n", "X Classic Style | \n", "[] | \n", "NaN | \n", "['Includes 4 Folding Tv Tray Tables And one Co... | \n", "Set of Four Folding Trays With Matching Storag... | \n", "['Brand: JOIN IRON', 'Shape: Rectangular', 'In... | \n", "bdc9aa30-9439-50dc-8e89-213ea211d66a | \n", "2024-02-02 15:15:11 | \n", "
5 rows × 25 columns
\n", "