PuppyCoding

Friendly Python & AI tutorials for beginner & intermediate programmers.


The Simplest Way to Use ChatGPT with Python

Two creatures dressed as butlers, ready to respond to requests, just like ChatGPT.

A simple example of writing a Python program to use ChatGPT has the following three steps:

  1. Prepare a prompt
  2. Send the prompt to ChatGPT
  3. Print the response

Fortunately it’s pretty much as easy as it sounds. The only preparation required is to:

  • Get your OpenAI API key from platform.openai.com
  • Install the openai Python module. In your terminal (called command prompt on Windows), type:
    • pip install openai

Then you’re ready to code!

Basic program (not recommended)

import openai

openai.api_key = "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"

prompt = input("Please enter a question or request: ")

result = openai.chat.completions.create(
model = "gpt-3.5-turbo",
messages = [
{
"role": "user",
"content": prompt
}
]
)

print(result.choices[0].message.content)

This is not the best way, because our API key is exposed. It should be kept secret, and to do that we can use an “environment variable”. I’ve explained how to hide API keys in Python in another blog post, and so using that method, below is the revised, better code. Note that you will need to prepare a hidden .env file for it to work.

Basic program (recommended)

import os
from dotenv import load_dotenv
import openai

load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")

prompt = input("Please enter a question or request: ")

result = openai.chat.completions.create(
model = "gpt-3.5-turbo",
messages = [
{
"role": "user",
"content": prompt
}
]
)

print(result.choices[0].message.content)

In detail: Sending the request to ChatGPT

The openai.chat.completions.create() function is used to create the request. It can take multiple parameters but two that are required are model and messages.

model

This is simply the name of the OpenAI chat model you’d like to use. There are two recommended models you can choose from:

  • gpt-4 (newer but more expensive)
  • gpt-3.5-turbo (a bit older but cheaper)

messages

This is actually an array (list) of objects (dictionaries), so you could send a history of previous chat messages in your request. However I’m concentrating on a simple example, so I’ve just used a single message. Each message needs at least a role and ideally some content.

  • role: Use “user”. Additional roles are “system” and “assistant” which can be used to send background information.
  • content: Use this to send the prompt.

That’s it for the ChatGPT request!

In detail: Dealing with the ChatGPT response

The response from ChatGPT contains multiple levels of data, but usually we’re just interested in the answer to our prompt, which is a property called “content”. We can drill down into it as follows:

  • “choices” array (list)
    • First item (index 0)
      • “message”
        • “content”

And that’s it for the ChatGPT response!

Hopefully this example code gives you a good foundation to build on. The OpenAI API documentation is quite readable so I recommend taking a look, and finally here’s a video guide if you’re more of a visual learner.



Leave a comment