An AI-powered blog

Category: The random blog

A present with blog posts

Discover the world of AI blogging, as ChatGPT creates unique and random content nearly full automatically in our random blog:

Automation of the “Random blog” with the OpenAI API

In the article “AI blogs independently about random topics: The rise of ChatGPT“, we showed how to generate blog posts with random topics using ChatGPT. However, to actually publish the post, a lot of copy-pasting and editing is still required. The good news is that with the OpenAI API (and in our case, the WordPress REST API), much of this can be automated and better structured.

An overview and documentation of the OpenAI API can be found here. To use the API, you also need an account with OpenAI. The API is generally paid and is billed based on tokens- but you can currently use a trial version to test it. OpenAI provides you with $18 for the test period for free. If you want to know what a prompt or response corresponds to in tokens, you can test that with the tokenizer tool.

Approach:

The workflow in our Python script looks something like this:

1. Generate a random topic for the post

This involves generating the random topic along with metadata using a suitable prompt. To ensure that the output is structured and repeatable for repeated requests, the output of ChatGPT should be in JSON format. We need the following information:

  • Post title
  • Focus keyphrase
  • SEO title
  • Meta description
  • Image prompt for the post image

Additionally, we want to generate posts in English and German, so the above-mentioned content must be generated in both languages.

Finally, we need to write the JSON content to a file so that we can access it later. We will also add the number of tokens used for the request and reply to the file name.

2. Generate the content of the post

After the topic has been chosen by ChatGPT, we will generate a suitable prompt to generate the English-language content. Some readability criteria should be provided:

  • Writing style
  • Text length
  • Paragraph length
  • Maximum number of words per paragraph
  • The focus keyphrase should be included in the introductory sentence

In addition, ChatGPT should insert HTML tags for subheadings and paragraphs.

This response from ChatGPT should also be written to a file along with the tokens used.

3. Translate the generated content

Once the English-language content has been generated, it must be translated into German by ChatGPT.

As usual, the translated text will be stored in a file along with the tokens used.

4. Generate the drafts with the WordPress REST API

To save ourselves a lot of work, we want to send the generated content directly to our blog if possible. This is possible with the WordPress REST API. You only need a user configured for this with at least author rights and a corresponding application password:

After that, you can send HTTP requests to your blog to read content or create posts. Here is the API documentation. But you can also let ChatGPT or Bing Chat generate the Python code for the requests – like I did.

5. Create a topic blacklist

It can happen that ChatGPT repeatedly generates content for already generated topics. For this reason, we want to create a blacklist of already generated topics that we can consider directly in step 1.

Preparing the prompts

In the first step, we want to implement some helper functions to generate the prompts.

import openai
import os
import json
import requests

openai.api_key = "<YOUR API KEY>"
openai.organization = "<YOUR ORGANISATION>"

def generate_text(prompt, system_hint):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": system_hint},
            {"role": "user", "content": prompt},
        ]
    )

    return response

def get_topic_prompt(topic=None, blacklist=None):
    prompt = "Generate a "
    
    if topic is None:
         prompt += "random topic for a blog post "
    else:
         prompt += f"blog post for the topic ""{topic}"" "

    prompt += "in english and german. \
        Write an appropriate focus keyphrase with 4 words or less. \
        Write a short SEO title and a post title. \
        Write a prompt for an image creator. \
        Write a meta description with a maximum length of 156 characters."
    
    if not (blacklist is None):
        prompt += f" Do not use a topic from the following list: {blacklist}. "

    prompt += "Use the following json format as output: \
        { \
        \"focus_keyphrase_english\": \"\", \
        \"seo_title_english\": \"\", \
        \"post_title_english\": \"\", \
        \"image_prompt_english\": \"\", \
        \"meta_description_english\": \"\", \
        \"focus_keyphrase_german\": \"\", \
        \"seo_title_german\": \"\", \
        \"post_title_german\": \"\", \
        \"meta_description_german\": \"\" \
        }"
    
    system_hint = "You are a helpful assistant that writes blog posts."
    
    return prompt, system_hint
    
def get_content_prompt(topic):
    prompt = f"Write a journalistic blog post on the following topic: \
        {topic} \
        Leave out the title and begin with an introduction. Use appropriate subtitles. \
        Use html tags for subtitles and paragraphs. \
        The focus keyphrase should appear in the introduction. \
        A paragraph should not be longer than 200 words. \
        No more than 25 percent of the sentences should have more than 20 words. \
        The post should contain a maximum of 10 percent passive sentences. \
        The post should contain at least 600 words and have enough transition words."
    
    system_hint = "You are a helpful assistant that writes blog posts."

    return prompt, system_hint

def get_translation_prompt(target_language, content):
     prompt = f"Translate the following into {target_language} and keep existing html tags: {content}"

     system_hint = "You are a helpful assistant that translates texts from english to german."

     return prompt, system_hint

The code defines four functions that perform tasks related to writing blog posts.

The function generate_text() uses the OpenAI GPT-3 API to generate text based on a prompt and system cues.

The function get_topic_prompt() generates a prompt for generating a topic for a blog post, while get_content_prompt() generates a prompt for actually writing the blog post with corresponding style cues.

Finally, the function get_translation_prompt() generates a prompt for translating text from English into another language while retaining existing HTML tags.

Read blacklist and create topic

blacklist_filename = "blacklist.txt"

if not os.path.isfile(blacklist_filename):
    with open(blacklist_filename, 'w') as f:
        f.write('')

with open("blacklist.txt", "r") as f:
    blacklist = f.read()

if(len(blacklist) > 0):
    prompt_topic = get_topic_prompt(blacklist=blacklist)
else:
    prompt_topic = get_topic_prompt()

topic_response = generate_text(prompt_topic[0], prompt_topic[1])

topic = topic_response.choices[0].message.content
topic_tokens = topic_response.usage.total_tokens

topic_json = json.loads(topic)
focus_keyphrase = topic_json["focus_keyphrase_english"]

if not os.path.exists(focus_keyphrase):
    os.makedirs(focus_keyphrase)

topic_filename = f"TOPIC_{topic_tokens}.json"
topic_file_path = os.path.join(focus_keyphrase, topic_filename)

with open(topic_file_path, "w") as f:
        f.write(json.dumps(topic_json, indent=4))

with open("blacklist.txt", "a") as f:
        f.writelines(focus_keyphrase + ";")

The code first checks if a file named blacklist.txt exists. If not, an empty file with that name is created. Then, the contents of the blacklist.txt file are read and stored in a variable. If the blacklist is not empty, an instruction to generate a prompt using that blacklist is generated. Otherwise, an instruction without a blacklist is generated.

Next, the generated prompt text is sent to GPT-3 using the generate_text() function, and the first generated response is used to extract the topic and focus keyword. Then, a folder with the name of the focus keyword is created, if it doesn’t already exist. A file with the generated topic is saved in JSON format in the newly created folder. Finally, the focus keyword is added to the blacklist by appending it to the end of the blacklist.txt file.

Generate the content

shortened_topic_json = {
    'focus_keyphrase_english': topic_json['focus_keyphrase_english'],
    'post_title_english': topic_json['post_title_english'],
    'meta_description_english': topic_json['meta_description_english']
}

prompt_content = get_content_prompt(json.dumps(shortened_topic_json))

content_response = generate_text(prompt_content[0], prompt_content[1])

content = content_response.choices[0].message.content
content_tokens = content_response.usage.total_tokens

content_filename = f"CONTENT_EN_{content_tokens}.txt"
content_file_path = os.path.join(focus_keyphrase, content_filename)

with open(content_file_path, "w") as f:
        f.write(content)     

The following code generates content for a blog post and saves it to a text file. First, a shortened JSON object is created from the original topic, containing only the keyword phrase, post title, and meta description. Then a prompt for content generation is created using this reduced topic. The generated content is saved in a text file named CONTENT_EN_<Token Count>.txt, where the token count is derived from the response of the generated content.

Translating the generated content

prompt_translation = get_translation_prompt(target_language="german", content=content)

translation_response = generate_text(prompt_translation[0], prompt_translation[1])

translation = translation_response.choices[0].message.content
translation_tokens = translation_response.usage.total_tokens

translation_filename = f"CONTENT_DE_{translation_tokens}.txt"
translation_file_path = os.path.join(focus_keyphrase, translation_filename)

with open(translation_file_path, "w") as f:
        f.write(translation)  

This code section creates a translation of the previously generated content from English to German. To do this, a text prompt is created and sent to the OpenAI service to obtain a machine translation. The result is saved in a file in the same directory as the original English version.

Creating the drafts

url = 'https://<YOUR DOMAIN>/wp-json/wp/v2/posts'
headers = {'Content-Type': 'application/json'}
auth = ('<YOUR USER>', '<YOUR PASSWORD>')

data = {
    'title': topic_json["post_title_english"],
    'content': content,
    'status': 'draft',
    'categories': '<YOUR CATEGORY ID OR LIST OF IDs>',
    'lang': 'en'
}

requests.post(url, headers=headers, auth=auth, data=json.dumps(data))

data = {
    'title': topic_json["post_title_german"],
    'content': translation,
    'status': 'draft',
    'categories': '<YOUR CATEGORY ID OR LIST OF IDs>',
    'lang': 'de'
}

requests.post(url, headers=headers, auth=auth, data=json.dumps(data))

This code section sends two POST requests to a WordPress REST API. The variable url contains the URL to the REST API. The HTTP headers to be sent in the request are defined in the variable headers, and the user’s login credentials are specified in the variable auth.

The data to be sent in the POST requests is defined in the variables data. In the first request, the English title and content of the post, the status as a draft, a category ID, and the language en are specified. In the second request, the German title and content of the post, the status as a draft, a category ID, and the language de are specified.

Conclusion

With this code, we saved a lot of work while demonstrating how to use the OpenAI API with GPT models to programmatically process ChatGPT content in a dialog-based manner.

Although we’re not quite finished with this script yet, as we still need to manually add the metadata for SEO optimization and link the translations in WordPress. In addition, the featured image still needs to be generated.

However, that’s exactly why the posts were created as drafts. This allows for fine-tuning and most importantly, the ability to review the generated content before publishing. After all, we still want to retain some control over the AI.

All in all, the OpenAI API offers a variety of use cases for automating processes with the help of artificial intelligence. With this post, we were hopefully able to show you one of many possible use cases and get you excited about the possibilities that arise from API-based access!

Sustainable fashion: A guide to a more conscious wardrobe

As the world becomes increasingly aware of the environmental impact of the fashion industry, sustainable fashion has emerged as a growing trend. But what exactly is sustainable fashion, and how can consumers make more conscious choices when it comes to their wardrobe?

Note: This post is part of The random blog. The content of the associated posts is randomly generated by an AI.

What is sustainable fashion?

Sustainable fashion refers to clothing that is made in a way that is environmentally and socially responsible. This can include using sustainable materials, reducing waste and emissions during production, and ensuring fair labor practices throughout the supply chain.

Why is sustainable fashion important?

The fashion industry is one of the largest contributors to environmental pollution, with the production and transportation of clothing emitting large amounts of greenhouse gases and depleting natural resources. Additionally, many workers in the fashion industry face exploitative and unsafe working conditions. By choosing sustainable fashion, consumers can reduce their environmental impact and support ethical labor practices.

How can you create a sustainable wardrobe?

There are a number of ways to make more sustainable choices when it comes to your wardrobe. Here are a few tips:

  1. Choose sustainable materials: Look for clothing made from sustainable materials like organic cotton, linen, or recycled polyester.
  2. Buy second-hand: Shopping second-hand is a great way to reduce your environmental impact and save money. Check out thrift stores, consignment shops, or online marketplaces for gently used clothing.
  3. Invest in quality pieces: Rather than buying cheap, fast fashion items that will quickly fall apart, invest in high-quality pieces that will last for years.
  4. Support ethical brands: Look for brands that prioritize ethical labor practices and use sustainable materials. Do your research to ensure that the brands you support align with your values.
  5. Take care of your clothes: Proper care can extend the life of your clothing, reducing the need to replace them. Follow care instructions carefully, and consider using eco-friendly laundry detergents.

The future of sustainable fashion

As consumers continue to demand more sustainable options, the fashion industry is beginning to shift towards more environmentally and socially responsible practices. However, there is still much work to be done to create a truly sustainable fashion industry.

By making conscious choices when it comes to our wardrobe, we can all contribute to a more sustainable future.

AI blogs independently about random topics: The Rise of ChatGPT

Welcome to our new blog-series The random blog: The world of blogging is constantly evolving, and now there’s a new kid on the block: AI-generated content. With the rise of language models like ChatGPT, we’re seeing an exciting new era of writing unfold. In our case, ChatGPT generates unique blog posts on random topics. From music to politics, science to art, you never know what you’re going to get. Join us as we explore the fascinating world of AI blogging.

The Rise of ChatGPT

ChatGPT is a language model developed by OpenAI, which stands for “Generative Pre-trained Transformer.” Essentially, it uses machine learning to generate text that resembles human writing. And it’s getting really good at it. ChatGPT is now one of the most advanced language models in the world, with the ability to generate paragraphs, essays, and even books. Its capacity for language is truly remarkable, and it’s revolutionizing the world of writing.

AI Blogging: The Surprising World of Randomly Generated Content

ChatGPT’s blog series is a great example of the potential for AI-generated content. The topics it generates are completely random, which means you never know what you’re going to get. One day, it might be an essay on the history of jazz. The next, a review of the latest superhero movie. And because it’s generated by an AI, it’s always completely original. It’s a fascinating experiment in the possibilities of language models.

Here is the prompt that we will use for the randomly generated posts – including SEO optimizations, hints for improved readability and a prompt for a suitable image for the post:

Write a journalistic blog post on a random topic.
Start with an appropriate focus keyphrase, a short SEO title, a post title, and a meta description with a maximum of 156 characters.
The focus keyphrase should appear in the introduction.
Write an appropriate prompt for an image creator.
Use appropriate subheadings for each section.
A paragraph should not be longer than 200 words.
No more than 25 percent of the sentences should have more than 20 words.
The post should contain a maximum of 10 percent passive sentences.
Finally translate everything into german.
The english version should contain at least 600 words and have enough transition words.

Why AI Blogging Matters

AI blogging has the potential to change the way we think about writing and content creation. By automating the writing process, we can generate more content faster than ever before. This means that blogs and websites can publish more content in less time, which can be great for building an audience.

The Future of AI Blogging

As language models like ChatGPT continue to improve, we can expect to see more and more AI-generated content in the future. While it might not replace human writers entirely, it will certainly have a role to play in content creation. And as AI becomes more advanced, it might even be able to create content that’s indistinguishable from human writing. The possibilities are truly exciting.

Conclusion

The world of AI blogging is still in its early stages, but it already shows great potential. ChatGPT’s blog series is a great example of the potential of AI-generated content and how it can change the way we think about writing and content. Although there are still challenges, such as the need to check content for plagiarism and errors, the possibilities that AI-generated content offers are exciting. In the future, AI blogging will certainly play a role in content production and may even help produce content faster and more efficiently.

It’s important to remember that AI blogging will not replace human writers, but will only serve as a supplement to content production. But who knows what the future holds? Perhaps AI-generated content will someday be so advanced that it can surpass human writers. Until then, we will continue to explore the fascinating world of AI blogging and see where it takes us.

In another post on this topic, we will explore the automation of blog posts using the OpenAI API and the WordPress REST API.