Generate SEO content programmatically with the ChatGPT API and Python

SamProgrammatic SEO Leave a Comment

Despite not always being hugely useful for website users, the presence long-form content on web pages continues to play an important role in determining SEO rankings. However, producing this content at the scale required for large websites has traditionally been very expensive. Often, websites will choose to produce copy for only the pages that target the highest volume keywords, but this could potentially mean missing out on significant gains from long-tail keywords.

Nowadays ChatGPT has made it fast and incredibly cheap to produce reasonably high-quality SEO content, but the chat interface that you’re probably familiar with isn’t that useful when you need to create content for hundreds or thousands of pages. That’s where the ChatGPT API comes in.

In this article I will provide a few examples of what I mean by long-form SEO content. Then I will show you how use Python, the ChatGPT API, and Google Colab to generate content at scale and supercharge your programmatic SEO strategy.

What is long-form SEO content?

By ‘long form content’, I am referring to any copy on a page that is longer than a sentence or two, and whose primary purpose is to improve SEO. Essentially, this copy is providing an opportunity to add relevant keywords to the page, allowing Google to build a better understanding about the purpose of the page, and the user intents that it serves.

How Important is long-form content for SEO?

I have a few first-hand experiences that demonstrate the SEO impact that adding SEO content to landing pages can have.

Experience #1 : Canva

One of the projects I led during my time as international SEO lead at Canva was the launch of the ‘templates marketplace’ in a number of international locales. In the screenshot below you can see an example of the long-form SEO content on the Business Cards templates page.

When we launched this site section in new locales, we translated everything from the English version of the page into the target language using a third-party translation vendor. This was expensive and time-consuming, so the question arose as to whether we should bother doing it at all; especially for the smaller markets like Poland or The Netherlands. After running a few tests however, it became clear that the text did play a role in helping to boost rankings, particularly for the more competitive keywords.

Experience #2 : Uber Car Share

The screenshot below shows another example of some SEO content this time on the Uber Car Share website. I worked as SEO lead for the company shortly before they were acquired by Uber, and managed a large project to completely overhaul the website which resulted in a more-than-doubling of organic traffic, and top-3 rankings for all the most important keywords like ‘car hire sydney’ etc. Whilst there were many optimisations made to the site structure, internal linking and performance, I believe that the long-form content on each of the landing pages was also an important factor that led to the amazing results.

The approach I took to creating the content involved using a large spreadsheet with columns containing synonyms or alternative phrases and then using a formula to concatenate them in various ways. There were a few disadvantages to this approach:

  1. Phrases and parts of sentences were repeated frequently across all the pages, eg. it wasn’t completely unique
  2. I wasn’t able to incorporate any specific details that would help to improve the relevancy of the page in the eyes of Google, for example, mentioning certain landmarks or places of interest within each location
  3. The content was bland and uninteresting
  4. It was fairly time-consuming to create the spreadsheet and work out how to use formulas to get the desired results

Fortunately, all of these problems can be overcome by using the ChatGPT API.

Experience #3 : E-commerce

A final example is from an e-commerce dropshipping website that I use to test SEO ideas, Acoustic Foam Shop. The screenshot below shows an example of a category page where I added a paragraph of copy that was written by ChatGPT. Whilst the site doesn’t get much organic traffic overall, I did see an uptick in Search Console impressions after adding the content to the category pages.

How much does it cost to use the ChatGPT API

The cost of using ChatGPT to generate content depends upon:

  1. The amount of information you provide in the prompt. For example, if you have a lengthy prompt containing examples for the model to follow when generating a response, the cost will be higher than if you provide a short prompt like; ‘write me an seo paragraph about car hire in sydney’.
  2. The length of the response that is generated.

I’m not going to go into any more details here because quite frankly, this isn’t my area of expertise. What I can say however, is that in my experience of using the tool, it is very cheap. We’re talking a few cents for every dozen or so paragraphs that are generated.

As long as you don’t mistakenly generate thousands of pieces of content at a time (which is very possible if you don’t know what you’re doing!) then you don’t need to worry about costs.

Whether you know what you’re doing or not, it’s wise to set monthly usage limits that will prevent any unexpectedly huge bills.

Instructions

Setup the API key

You’ll need to have a ChatGPT account and have set up billing information. Then you can setup the API key here. Copy and paste the details somewhere safe for you to use later.

Setup the Google Colab file

To keep things simple, I have supplied the code for you to run in this Google Colab file. You will need to make a copy of the file and add your own API key in order for it to work.

Adapt the code as required

In the example, I am generating content for two pages on the Uber Car Share website, one for ‘car hire bondi beach’, and another for ‘car hire melbourne’, but the function could be applied to a much larger list of locations.

First I define a function that contains the following:

A conversation prompt

Within this there are two sections:

  • A ‘system’ prompt. This is where we provide context to chatGPT.
  • A ‘user’ prompt, where we provide the location that we want ChatGPT to write about.

It took some time to get my head around system and user prompts. I recommend reading the examples contained in the best practices guide on the chatgpt website if you want to understand more about how they work.

Prompt design and engineering is a whole field in itself, and one that I am still learning. The prompt below led to good results for me for this task, but they could almost certainly be improved with experimentation. A few key best practices that I’ve learnt and used in the example below are:

  • Providing context by telling ChatGPT to adopt a persona (‘you are an SEO specialist…’)
  • Using short and concise language – it’s easier for the model to understand, and cheaper!
  • Provide examples
  • Use formatting such as numbered lists, line breaks, and delimiting longer pieces of content (I used double square brackets to contain the examples)
def gpt_copy_generation(location):

    conversation = [
    {"role": "system", 
     "content":
    """ 
    You are an SEO specialist writing content for an australian company that provides vehicle hire services called Uber Car Share. 
     
    You will be provided with the name of a location within australia.
     
    You should return some copy to be used as a page introduction. It should help improve the page's SEO performance for keywords like 'car hire in sydney', van rental in melbourne'. Follow these guidelines:
     
    1. The text should be between 1000-1400 characters
    2. The text should make specific references to the location and places or landmarks that are there. 
    3. The text should contain a variety of terms that relate to the topic, for example 'car share', 'car rental', 'car hire' etc.
     
    Here are two examples, enclosed within double square brackets:

    [[ 
    location: Parramatta 
     
    "Looking for a car hire in Parramatta? Uber Carshare is your answer. We offer a range of vehicles to suit your needs, whether you're planning a family trip to the Parramatta Park, need a spacious car for a bike ride along the Parramatta River, or a compact car for quick errands around Westfield Parramatta.

    Uber Carshare stands out from traditional car hire services. With us, you can skip the queues and the paperwork. Simply find cars near you using our app, view prices by the hour or by the day, and get almost instant access to your chosen vehicle. It's free to become a member, and the keys are always at the car when you book an Instant Keys car, ready for your journey.

    Our car hire service in Parramatta is not only convenient but also cost-effective. With Uber Carshare, you only pay for what you need. Rent by the hour, day, week, or longer. All trips include fuel, damage cover, and 24/7 roadside assistance.

    Renting a car in Parramatta has never been easier. With hundreds of cars owned by locals, you can find a vehicle parked on your street or near the Parramatta Train Station. So why wait? Join us for free today and experience the convenience and affordability of Uber Carshare's car hire in Parramatta."
    ]] 
    [[
    location: south melbourne
          
    "Are you in need of a car hire in South Melbourne? Uber Carshare is here to assist. We offer a variety of vehicles to cater to your specific needs, whether you're planning a family trip to the nearby Royal Botanic Gardens, need a spacious car for a bike ride along the Yarra River, or a compact car for quick errands around the South Melbourne Market.

    Uber Carshare offers a more convenient alternative to traditional car hire services. With us, you can bypass the queues and the paperwork. Simply locate cars near you using our app, view prices by the hour or by the day, and gain almost instant access to your chosen vehicle. It's free to become a member, and the keys are always at the car when you book an Instant Keys car, ready for your journey.

    Our car hire service in South Melbourne is not only convenient but also cost-effective. With Uber Carshare, you only pay for what you need. Rent by the hour, day, week, or longer. All trips include fuel, damage cover, and 24/7 roadside assistance.

    Renting a car in South Melbourne has never been easier. With hundreds of cars owned by locals, you can find a vehicle parked on your street or near the South Melbourne Tram Station. So why wait? Join us for free today and experience the convenience and affordability of Uber Carshare's car hire in South Melbourne."

    ]]"""},

    {"role": "user", "content": f"location: {location}"}
    
]

The part where I build the request

In this section, I compile a few parameters and the prompt that I created above into a request that I will submit to the API. More about each of these parameters can be found on the Open AI website. The most useful thing to know here is that the temperature parameter ranges from 0 to 2 and that higher temperatures will make the output more random (leading to a more ‘quirky’ writing style’).

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=conversation,
        max_tokens=1000,
        temperature=1
    )
   
    return response['choices'][0]['message']['content']

I am returning only the content part of the response in this example, but the response does contain other useful information, such as the number of tokens used (which determines the cost). If you want to see this additional data, you would adjust the final line to look like this:

return response

Finally, run the code!

Run each of the cells. You should then be prompted to save the file named ‘output.csv’.

Conclusion

In this article you have learnt about the importance of long form content for SEO landing pages and how you can start to generate content programatically for large website using the ChatGPT API. This is a fast-evolving area, and it’s likely that updates to the API could cause the Colab file to break in the future.

If you require help with this, or any other SEO project, then please don’t hesitate to reach out to me at samficek@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *