Tutorial: How to use ChatGPT API with Python

After offering a free version of ChatGPT and then a premium version for $20 per month, OpenAI now offers ChatGPT in the form of an API.

Considering that GPT-3 has allowed for the creation of very powerful applications, imagine the power of ChatGPT integrated into any application!

In this article, I will explain how to integrate the ChatGPT API into your applications using Python.

How to use the ChatGPT API with Python?

The Python code to do this is very simple.

You need to do three things:

1. Create an OpenAI account

2. Access your API key

3. Launch the Python code using your key

Create an OpenAI account to access the ChatGPT API

If you haven't already done so, you need to start by creating an account on OpenAI.

Once logged in, go to "Personal" in the upper right corner, then "View API keys".

Openai platform

Click on "New secret token" to generate your API key.

Keep it secret and make sure to update it regularly, as it provides full access to your account.

Calling the ChatGPT API from Python

Start by installing the OpenAI library:

pip install openai

If you are on a notebook:

!pip install openai
import openai

openai.api_key = VOTRE_API_KEY

prompt = "Write me a conclusion from an article that talks about using the ChatGPT API""

completion = openai.ChatCompletion.create(
  messages=[{"role": "user", "content": prompt}]


Now you can change the prompt as you wish to generate the expected output.

This function can take other parameters that may be interesting, such as max_tokens, which gives the maximum number of tokens in the response, or temperature, which gives the degree of unpredictability of the agent.

If the temperature is set to 0, the model will always give the same response without taking any risks. On the other hand, if it is set to 1, the responses will be more original each time:

prompt = "Write me a conclusion from an article that talks about using the ChatGPT API"
response = openai.Completion.create(

One of the most interesting features of ChatGPT was its ability to remember the ongoing conversation and reuse what you said to it before.

We can reproduce this behavior in the following way:

        {"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?"}

By specifying the entire conversation, the model will take into account everything that has been said to generate the output.

We can imagine a conversation exchange in this form:

for k in range(3):
    messages=[{"role": "system", "content": "Tu es un prof de Python"}]
    msg = input()
    messages.append({"role": "user", "content": msg})
    reply = openai.ChatCompletion.create(
    messages.append({"role": "assistant", "content":completion['choices'][0]['message']['content']})

In this exchange, I initialize ChatGPT by telling it that I am a Python teacher and my first request. I manually enter my first message, add it to the list of conversation messages, and then generate ChatGPT's response.

Similarly, I add this response to the list of messages. Then, I ask the user to enter a message to continue the discussion.

Pricing and billing for the ChatGPT API

In this section, I will address an important topic: money.

You need to know two things:

  1. The ChatGPT API is cheaper.
  2. You can manage your monthly budget to avoid disasters.

How much does the ChatGPT API cost?

The ChatGPT API is cheaper than previous models' APIs.

It is offered at $0.002 per 1000 tokens.

I refer you to the documentation to find out exactly what a token represents.

In English, 1000 tokens represent approximately 750 words.

So if you need to generate a 750-word article in English by writing a 250-word prompt, it will cost you approximately $0.0026.

How to limit the budget?

To avoid surprises on your bank account at the end of the month, I recommend setting up a budget limit for your account.

It's easy to do.

Go to "billing" on the left, then "Usage limits":

Openai Billing

You can then set a monthly "soft limit" and "hard limit". Once the soft limit is reached, you will receive a notification email. Once the hard limit is reached, API calls will be blocked.


As I asked ChatGPT to generate this conclusion from Python and it cost me money, I'll include it even though I don't like it too much ahah.

In conclusion, the ChatGPT API represents a remarkable advancement in the field of chatbots and artificial intelligence.

With its advanced natural language processing capabilities, it allows for the creation of highly personalized and effective chatbots to meet the needs of each business.

Moreover, it also opens up new horizons for automating customer communication processes, making the task easier for entrepreneurs.

In summary, the use of the ChatGPT API offers interesting perspectives for companies looking to improve their customer relations while reducing costs.

Next Post Previous Post