How to Integrate the ChatGPT API into your Python Application
Time to read: 4 minutes
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:
For those of you following the tutorial on Windows, enter the following commands in a command prompt window:
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:
- The OpenAI Python client library, to send requests to the OpenAI ChatGPT engine.
- The python-dotenv package, to read a configuration file.
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:
You will learn how to work with this file in the next section.
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:
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:
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.
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!
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.