Skip to content
Using Vision AI To Write SEO - Free Tutorial
Browse other ways to boost conversion rate & profit

Using Vision AI To Write SEO - Free Tutorial

With OpenAI Vision being able to see and understand images, a whole new world just opened up, including helping with our store’s SEO.

In this tutorial, we're showing you how to improve your store's SEO by using OpenAI's new Vision API to automatically write SEO for your products. 

Compatible Themes: This code should work on all free Shopify themes (Dawn, Refresh, Craft, Studio, Publisher, Crave, Origin, Taste, Colorblock, Sense, Ride, Spotlight).

 

1. Set the openAI key

In your Python environment (ex: command prompt console), set your openAI API key using:

set OPENAI_API_KEY=YOUR-API-KEY

2. Execute the Vision AI Code

Adapt the Prompt and image URL for your own store. The prompt has several placeholders inside square brackets [ ] which should be changed prior to running:

import openai
import os
import re
import csv
from datetime import datetime
import pytz

# Retrieve the API key from the OPENAI_API_KEY environment variable
openai.api_key = os.getenv('OPENAI_API_KEY')

if openai.api_key is None:
    raise ValueError("No OPENAI_API_KEY environment variable found.")

client = openai.OpenAI()

response = client.chat.completions.create(
    model="gpt-4-vision-preview",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "You are an SEO expert that provides SEO-optimized information that can be used in the product listings for [YOUR STORE DESCRIPTION]. You'll be given a product image and product title as input. Your SEO optimized content will be fun and light hearted, while also providing information about the product's design, theme, vibe, how and who it can help them connect with, and how to style or wear it, [INCLUDE ANY OTHER DETAILS FOR YOUR PROMPT]. [ADDITIONAL INSTRUCTIONS HERE. FOR EXAMPLE, IF IT'S A SHIRT, YOU COULD USE: The image provided is for a product called [YOUR PRODUCT NAME]. The focus should be on the design on the shirt itself and you may ignore the surrounding elements. Only one product color (ex: shirt color) is provided, but multiple will be available, so the description and image alt text should not specifically mention the shirt color]. Can you write an SEO optimized meta description (maximum 320 characters), an SEO page title (max 70 characters), a product description (max 200 words) and an image alt text (max 125 characters) that can be used for different color variations of this product, for use in Shopify. Please format your response in 4 sections so I can programmatically separate the 3 parts by starting them with SEO Page Title, Meta Description, Product Description, and Image Alt Text, respectively."},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "YOUR-IMAGE-URL",
                        "detail": "low",
                    },
                },

            ],
        }
    ],
    max_tokens=500,
)

#print(response)

# Extracting the content from the response
response_content = response.choices[0].message.content
pattern = r'\\s*SEO Page Title:\\s*|\\s*Meta Description:\\s*|\\s*Product Description:\\s*|\\s*Image Alt Text:\\s*'
parts = re.split(pattern, response_content)

parts = parts[1:]
seo_page_title, meta_description, product_description, image_alt_text = [part.strip() for part in parts]

# Function to remove wrapping quotation marks if present
def remove_wrapping_quotes(text):
    if text.startswith('"') and text.endswith('"'):
        return text[1:-1]  # Remove the first and last character
    return text

seo_page_title = remove_wrapping_quotes(seo_page_title)
meta_description = remove_wrapping_quotes(meta_description)
product_description = remove_wrapping_quotes(product_description)
image_alt_text = remove_wrapping_quotes(image_alt_text)

# Outputting the results
print("SEO Page Title:", seo_page_title)
print("Meta Description:", meta_description)
print("Product Description:", product_description)
print("Image Alt Text:", image_alt_text)

headers = ['SEO Page Title', 'Meta Description', 'Product Description', 'Image Alt Text']
data = [seo_page_title, meta_description, product_description, image_alt_text]

eastern = pytz.timezone('US/Eastern')
current_time_in_eastern = datetime.now(eastern)
timestamp = current_time_in_eastern.strftime("%Y%m%d_%H%M%S")

csv_file = f"visionoutput_{timestamp}.csv"

# Open a new CSV file
with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
    writer = csv.writer(file)
    writer.writerow(headers)
    writer.writerow(data)

print(f"Data written to {csv_file}")

Browse other ways to boost conversion rate & profit