How to Integrate the ChatGPT API into your Python Application

March 17, 2023
Written by
Reviewed by

If you are in tech and are active on social media, chances are you have heard about ChatGPT, the popular language model from OpenAI, that can have a fluent conversation in a way that is often indistinguishable from a human.

OpenAI provides an interactive website in which you can chat with ChatGPT at https://chat.openai.com, but it has also made an API available to allow developers to integrate it into their applications. In this tutorial you will learn how to integrate ChatGPT with a Python application.

Tutorial requirements

To follow this tutorial you need the following items:

  • Python 3.7 or newer. If your operating system does not provide a Python interpreter, you can go to python.org to download an installer.
  • An OpenAI API key. Request access here.

Create a Python virtual environment

Following Python best practices, as a first step in creating the chatbot you are going to create a separate directory for the project, and inside it you are going to create a virtual environment. Then you are going to install the Python packages that are needed for the chatbot.

If you are using a Unix or macOS system, open a terminal and enter the following commands to do the tasks described above:

mkdir python-chatgpt
cd python-chatgpt
python3 -m venv venv
source venv/bin/activate
pip install openai python-dotenv

For those of you following the tutorial on Windows, enter the following commands in a command prompt window:

md python-chatgpt
cd python-chatgpt
python -m venv venv
venv\Scripts\activate
pip install openai python-dotenv

The last command uses pip, the Python package installer, to install the two packages that we are going to use in this project, which are:

Configuration

As mentioned above, this project requires an API key from OpenAI. The Python application will need to have access to this key, so you are going to create a .env file and store it there. The application will then import it from this file as an environment variable.

Create a .env file in your project directory (note the leading dot) and paste the following configuration into the file, replacing your-openai-api-key-here with your API key:

OPENAI_KEY=your-openai-api-key-here

You will learn how to work with this file in the next section.

If you plan on putting your project under source control, make sure this file is excluded, as you would not want to accidentally share your OpenAI key.

Using ChatGPT from Python

In this section you are going to create the support code that works with the ChatGPT engine. The code will be stored in a file called chatgpt.py. Below you can see the contents of this file:

import os
from dotenv import load_dotenv
import openai

load_dotenv()
openai.api_key = os.environ.get('OPENAI_KEY')
completion = openai.ChatCompletion()

def askgpt(question, chat_log=None):
    if chat_log is None:
        chat_log = [{
            'role': 'system',
            'content': 'You are a helpful, upbeat and funny assistant.',
        }]
    chat_log.append({'role': 'user', 'content': question})
    response = completion.create(model='gpt-3.5-turbo', messages=chat_log)
    answer = response.choices[0]['message']['content']
    chat_log.append({'role': 'assistant', 'content': answer})
    return answer, chat_log

The load_dotenv() function imports data stored in the .env file as environment variables, which in this case is done to read the OpenAI key you stored in this file earlier. Note how the OPENAI_KEY variable is used in the following line to initialize OpenAI with the key. The completion variable holds the actual client that connects to the ChatGPT engine. This is the object that will interact with the OpenAI service.

The askgpt() function is where all the magic happens. The function takes the question or prompt from the user as a first argument, followed by an optional chat log. If the chat log is not provided then the function initializes it with a single element.

A chat log contains all the exchanges between the user and ChatGPT. When a conversation is started, the chat log is initialized with only one entry, which has the purpose of preparing the chat engine and setting the tone for it.

Each message in the chat log is assigned a role. The three available roles are:

  • system: only used in the first prompt to give the chat engine some guidelines on its personality and how to respond and interact with the user.
  • user: the questions or prompts entered by the user.
  • assistant: the responses returned by the ChatGPT engine for previous questions.

The initial system message can be changed to create different styles of chatbots. I encourage you to try different system prompts to see how the responses change accordingly.

The function appends the question from the user to the chat_log list with the user role, and then it sends the list to the completion.create() function, which returns the chat engine’s response. The model argument that is passed along with the chat log is used to specify which language model to use. The gpt-3.5-turbo model is designed to work with ChatGPT at the time I’m writing this, but make sure to check the ChatGPT API documentation as this model might have been superseded by a newer one by the time you read this.

The response from ChatGPT is an object that has a choices attribute, which is a list of possible responses. At this time, this list always comes back with a single response. The message element in this response has the text of the answer in the content sub-element. Before it ends, the function extracts the text returned by ChaptGPT from the response objects and adds it to the chat log using the assistant role. It then returns both the response and the updated chat log.

Start a Python shell to test the askgpt() function:

>>> from chatgpt import askgpt
>>> answer, log = askgpt('Why did the chicken cross the road?')
>>> print(answer)
To get to the other side! Classic joke, but it still makes me chuckle every time. Do you have a favorite joke?
>>> answer, log = askgpt('Knock, knock.', log)
>>> print(answer)
Who's there?
>>> answer, log = askgpt('Goat.', log)
>>> print(answer)
Goat who?
>>> answer, log = askgpt('Goat to the door and find out!', log)
>>> print(answer)
Haha, that's a good one! You got me with that punchline. Do you know any more knock-knock jokes?

Awesome, right? Note how by passing the chat log starting from the second question the engine knows the context of the discussion and can maintain the thread of the conversation over several messages, just like a human would do.

Note that ChatGPT at times might be too busy and you may get error responses. In that case, wait some time and retry.

Conclusion

This was a nice little project, don’t you think? Now it is your turn to be creative and incorporate the askgpt() function I showed above into your Python project.

I’d love to see what you build with Python and ChatGPT!

Miguel Grinberg is a Principal Software Engineer for Technical Content at Twilio. Reach out to him at mgrinberg@twilio.com if you have a cool project you’d like to share on this blog!