How to input images in an Obscurify chat

Obscurify supports image input for models with vision capabilities, such as GPT-4 Vision. This guide will show you how to include images in your chat messages.

Prerequisites

  • An Obscurify account with funds on its balance. The API works on every plan, and new accounts may get $1 of starter credit while this month’s promo budget lasts. Your API key starts with sk-obs1_. Get it on your account page, or from your account number with POST /generate_token and {"account_number": "..."}. It’s made from your account, so you can always get the same key again.
  • Access to a model that supports image input (e.g., openai/gpt-4o)

Steps to include an image in your chat

  1. Encode your image to base64 format
  2. Prepare your chat message with the encoded image
  3. Send the request to the Obscurify API

Step 1: Encode your image to base64

First, you need to convert your image to a base64-encoded string. Here’s a Python function to do this:

import base64

def encode_image_to_base64(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

encoded_image = encode_image_to_base64("/path/to/your/image.jpg")

Step 2: Prepare your chat message

When including an image, your message should have a specific format. Here’s an example:

chat_data = {
    "messages": [
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {
                    "type": "image_url",
                    "image_url": f"data:image/jpeg;base64,{encoded_image}",
                },
            ],
        }
    ],
    "model": "openai/gpt-4o",
    "stream": False,
}

Step 3: Send the request

Now you can send this data to the Obscurify API. Here’s an example using the requests library in Python:

import requests

url = "https://obscurify.ai/chat/completions"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer YOUR_API_KEY_HERE"
}

response = requests.post(url, json=chat_data, headers=headers)
print(response.json())

Or you can use the OpenAI Python client library to send the request:

import base64
from openai import OpenAI

def encode_image_to_base64(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

encoded_image = encode_image_to_base64("/path/to/your/image.jpg")

client = OpenAI(
    base_url="https://obscurify.ai/v1",
    api_key="YOUR_API_TOKEN"
)

completion = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {
                    "type": "image_url",
                    "image_url": f"data:image/jpeg;base64,{encoded_image}",
                },
            ],
        }
    ],
)

print(completion.choices[0].message.content)