> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Project 2 Image Captioning

> This tutorial guides you in building a Python script to generate image captions using GPT-4's vision capabilities.

In this tutorial, you’ll build a Python script that takes an image URL and generates a descriptive caption using GPT-4’s vision capabilities. Instead of DALL·E, we’ll use the GPT-4 chat completion endpoint, which can process image URLs directly and describe what it “sees.”

## Table of Contents

1. [Prerequisites](#prerequisites)
2. [Installation](#installation)
3. [Initialize the OpenAI Client](#initialize-the-openai-client)
4. [Define the Image URL](#define-the-image-url)
5. [Generate Captions Function](#generate-captions-function)
6. [Run the Script](#run-the-script)
7. [Sample Output](#sample-output)
8. [References](#references)

***

## Prerequisites

* Python 3.7+
* `pip` package manager
* An OpenAI API key with GPT-4 access
* Internet connectivity to fetch the image

<Callout icon="triangle-alert" color="#FF6B6B">
  Never hard-code your API key in a public repository. Use environment variables or a secure vault.
</Callout>

***

## Installation

Install the official OpenAI Python client:

```bash theme={null}
pip install openai
```

***

## Initialize the OpenAI Client

Import and initialize the client with your API key:

```python theme={null}
from openai import OpenAI

# Initialize the client with your API key
client = OpenAI(api_key="sk-your-api-key-here")
```

<Callout icon="lightbulb" color="#1CB2FE">
  You can find the latest OpenAI Python SDK and examples in the [openai-python GitHub repo](https://github.com/openai/openai-python).
</Callout>

***

## Define the Image URL

Specify the publicly accessible image URL you want to caption:

```python theme={null}
# Image URL to caption
image_url = "https://assets-prd.ignimgs.com/2022/06/10/netflix-one-piece-1654901410673.jpg"
```

***

## Generate Captions Function

Create a helper function that sends a chat completion request to GPT-4, including both a text prompt and the image URL. We’ll cap the response at 125 tokens to keep captions concise.

```python theme={null}
from typing import Dict

def generate_captions(image_url: str) -> str:
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": "What is this image?"},
                    {"type": "image_url", "image_url": {"url": image_url}}
                ]
            }
        ],
        max_tokens=125
    )
    # Extract the generated caption
    return response.choices[0].message.content
```

***

## Run the Script

Use the function and print the returned caption:

```python theme={null}
if __name__ == "__main__":
    caption = generate_captions(image_url)
    print(caption)
```

***

## Sample Output

```plaintext theme={null}
This image features characters from the anime and manga "One Piece." In the center is Monkey D. Luffy wearing his trademark straw hat. To his left stands Sanji with blond hair, and to his right is Nami, recognizable by her orange hair. They are members of the Straw Hat Pirates.
```

***

## References

* [OpenAI Python Client](https://github.com/openai/openai-python)
* [GPT-4 Vision](https://openai.com/product/gpt-4-vision)
* [OpenAI API Reference: Chat Completions](https://platform.openai.com/docs/api-reference/chat)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/introduction-to-openai/module/d76ba88f-ebc6-4d12-8aa5-9359bc23be72/lesson/c5fed0f6-e493-4bd2-a2b8-2bbc201e53fc" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/introduction-to-openai/module/d76ba88f-ebc6-4d12-8aa5-9359bc23be72/lesson/c7d10d6d-5077-45cd-acc1-a865d54a9f63" />
</CardGroup>
