Getting Started with the ChatGPT API and Python

Getting Started with the ChatGPT API and Python

OpenAI launched the ChatGPT API today which makes it so easy to create chatbots in Python. Here's an implementation of a command line chatbot powered by Python and ChatGPT API in 16 lines of code.

import openai

messages = []
system_msg = input("What type of chatbot would you like to create? ")
messages.append({"role": "system", "content": system_msg})

print("Say hello to your new assistant!")
while input != "quit()": 
    message = input()
    messages.append({"role": "user", "content": message})
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages)
    reply = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": reply})
    print("\n" + reply + "\n")

And here's what it looks like when you run it:

OpenAI Python Library

This code assumes you've installed the OpenAI Python Library. That said, if you run into this error:

openai.ChatCompletion.create(
   ^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'openai' has no attribute 'ChatCompletion'. Did you mean: 'Completion'?

It's because you didn't upgrade your OpenAI package to the most recent version that includes the ChatCompletion() endpoint. Best just to run this to ensure you've got the most recent version:

pip install --upgrade openai

Create OpenAI API Keys

Once you've got the latest version of openai installed, you'll need to set your API key. You can find/create API keys on your OpenAI console.

0:00
/

There's two different ways to set your API key once you have it.

OpenAI's Python library does a nifty piece of magic where it looks for an OPEN_API_KEY environment variable, and if it finds it, you're good to go. You can set the environment variable from the command line like this:

export OPENAI_API_KEY=sk-...Acjwe

Alternatively, you can hardcode the API key into your code, just don't check it into any repos:

import openai

openai.api_key = "sk-...Acjwe"

TextCompletion() vs. ChatCompletion()

If you've used TextCompletion endpoint in the past, you'll notice four differences about working with the the new ChatCompletion() endpoint:

  1. You use openai.ChatCompletion.create() instead of openai.textCompletion.create()
  2. You use the gpt-3.5-turbo model instead of davinci-3 or others.
  3. You find your response from OpenAI in response['choices'][0]['message']['content']. I found that VS Code's Copilot still wants to autocomplete the old-school response["choices"][0]["text"] – which will throw a  KeyError: 'text'.
  4. You pass in a list of messages, but that difference deserves its own subsection.

The ChatComplete() messages parameter

The official ChatGPT API docs give this example as a messages list:

messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]

When an API version of ChatGPT was announced, a big question was, "how are they going to represent state?" The OpenAI team found an elegant solution.

You pass in a list of messages which contains chronological entries representing the prompt that created the chatbot, and the chat history. Each dict in the list takes the form of a dictionary with role and message attributes like this:

{"role": "system|assistant|user", "message":"hello"}

role takes one of three values:

  • system in which case the message attribute contains the description of the chatbot ("a helpful chatbot that rhymes")
  • user, in which case message contains a message sent by the user
  • assistant, in which case message contains a response received from ChatGPT

It's your responsibility to keep the messages list up to date, with all the messages sent back-and-forth between the user and the assistant (ChatGPT). This is easy to do with messages.append().

What's Next?

Oh my goodness. The possibilities have never felt more endless.

First I'm going to explore some of the packages for creating more beautiful Python based CLIs like Cement and Click.

Then I'm going to hook up file IO so I can do things like "write ____ and save it to {filename}."

The opportunities for requests + ChatGPT API feel literally infinite.  

In 35+ years of being a developer, I don't remember a more exciting time to write code.

Subscribe to HaiHai Labs

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe