Exploring the OpenAI API: A Developer's Journey to Consistent Outputs

Exploring the OpenAI API: A Developer's Journey to Consistent Outputs
Play this article

Stepping into the realm of AI applications can be a daunting endeavor, especially if you're a beginner. My first foray into the OpenAI API was a fascinating journey, albeit fraught with a few stumbling blocks. Today, I want to share my experience, highlighting the challenges I faced, and more importantly, the solutions I discovered.

Before we delve into my personal journey, let's take a step back and understand how to make a basic interaction with the OpenAI API. This will give us a solid foundation to build upon.

Setting the Foundation - Understanding OpenAI API Interaction

If you're just starting with the OpenAI API, a simple example to begin with could be asking the model a question, such as "Who won the world cup in 2006?". The OpenAI API allows you to interact with its models using HTTP requests. Here's how you can do this:

curl --location 'https://api.openai.com/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_OPENAI_API_KEY' \
--data '{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "Who won the world cup in 2006?"
        }
    ]
}'

This command sends a request to the 'https://api.openai.com/v1/chat/completions' endpoint. The 'Content-Type' header indicates that we're sending JSON data, while the 'Authorization' header should contain your personal OpenAI API key. The data sent is a JSON object, specifying the 'model' used and an array of 'messages'.

In response, you'll receive a JSON object:

{
    "id": "chatcmpl-7UvV60XY00C0NGsOG9Ul0gcVX2GmD",
    "object": "chat.completion",
    "created": 1687605228,
    "model": "gpt-3.5-turbo-0301",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "Italy won the World Cup in 2006."
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 18,
        "completion_tokens": 10,
        "total_tokens": 28
    }
}

The 'choices' array contains the responses from the model. In this case, the assistant correctly answered that "Italy won the World Cup in 2006." The 'usage' object provides information about token usage, which is useful for tracking and understanding billing.

Now that we have a basic understanding of interacting with the OpenAI API, let's dive into my personal journey of using it to create engaging blog titles.

Starting Simple - Generating a Single Blog Title

Initially, I aimed to use the OpenAI API to generate an engaging title for a blog article. I started with this prompt:

"Create an engaging and attention-grabbing title that accurately represents the blog content, includes relevant SEO keywords, remains under 70 characters, is specific to the content, conveys the reader's value, uses numbers or lists when applicable, and experiments with emotive words for the following text: {text}"

While it worked quite well, I noticed some inconsistent outputs. Occasionally, the API would return the title surrounded by quotation marks. It wasn't a significant issue since it could be dealt with programmatically, but it was something I noted.

Striving for More - Generating Multiple Titles

Realizing that a single title suggestion may not suffice, I decided to modify my prompt to generate three title suggestions:

"Create 3 engaging and attention-grabbing titles that accurately represent the blog content, includes relevant SEO keywords, remains under 70 characters, is specific to the content, conveys the reader's value, uses numbers or lists when applicable, and experiments with emotive words for the following text: {text}"

However, this adjustment led to further inconsistencies in the output. The output format varied, alternating between a list format or titles prefixed with numerical identifiers, e.g.,:

"1. Title1\n2. Title2\n3. Title3"
"1.) Title1\n2.) Title2\n3.) Title3"
"- Title1\n- Title2\n- Title3"

Navigating Through The Noise - Seeking a Consistent Structure

My quest for a consistent structure led me to several suggestions on how to write a better prompt. Despite these efforts, the result still varied. That's when I decided to revisit the OpenAI documentation, hoping to glean some new insights.

And indeed, there it was. An option that I had overlooked - n.

The n parameter allowed me to specify the number of generated responses. It was exactly what I needed!

Eureka - Finally Achieving Consistent Outputs

I reverted back to my initial prompt, aiming for a single title:

"Create an engaging and attention-grabbing title that accurately represents the blog content, includes relevant SEO keywords, remains under 70 characters, is specific to the content, conveys the reader's value, uses numbers or lists when applicable, and experiments with emotive words for the following text: {text}"

And in the request body, I set n to 3.

This simple tweak was transformative. Now, the API consistently returned an array within the 'choices' section containing three titles. Mission accomplished!

Wrapping Up

This journey into the OpenAI API was not only a lesson in problem-solving but also underscored the importance of thorough documentation exploration. It's easy to get caught up in the troubleshooting cycle, forgetting that the answers might already be in front of you.

Remember, understanding and correctly utilizing the available parameters is a vital step towards mastering any API, including OpenAI's.