Replace GPT-3 by ChatGPT, the API is 10 times cheaper!

CHAT GPT API

March 1st, 2023. This is such an exciting day for the AI enthusiasts! 🥂

OpenAI has officially released the ChatGPT API to all developers (+ access to the Whisper Speech-To-Text API).

As you may already know, ChatGPT leverages a language model which is much more effective than GPT-3.

It’s based on gpt-3.5-turbo. If you’ve played both with GPT-3 (in the playground or via the API) and with ChatGPT, you’ve probably noticed a significant difference in the quality of the output. ChatGPT is much more conversational and answers more questions with a better accuracy.

The wait is over: you can now leverage the power of ChatGPT in your own app via the API.

I’ve just recorded a quick demo of the implementation in a Kaggle Notebook. It took me 30 seconds.

👉 You can get access to the notebook itself, clone it and use it with your API Key.

How much does it cost to use the ChatGPT API?


The ChatGPT API is 10 times cheaper than the best legacy GPT model (GPT-3, Davinci-003).

You’ll pay $0.002 for 1000 tokens (roughly 750 words) instead of $0.02 for GPT-3. So you know what you have to do asap: repurpose your Python scripts with the brand new API.

How to use the ChatGPT API in Python?

Here’s the boilerplate of the full Python code to start playing with the ChatGPT API.

You can create a function with variables to integrate this call in your AI-powered scripts.

import openai

#build the query
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = [{"role":"system","content":"Your are my coding tutor"},{"role":"system", "content":"Can you please give me the boilerplate code for the py file of a Flask app with one HTML page? Thanks."}]
)

#get the answer & the usage in tokens (to calculate your cost)
answer = response["choices"][0]["message"]["content"]
usage = response["usage"]["total_tokens"]

print(answer) 

cost = usage/1000*0.002
print(f"Cost = ${cost} for {usage} tokens")

NOTE: for “role” you have the choice between “system”, “assistant” or “user”.
Here’s how Openai defines the meaning of those terms in their documentation.

The system message helps set the behavior of the assistant. In the example above, the assistant was instructed with “You are a helpful assistant.”

The user messages help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction.

The assistant messages help store prior responses. They can also be written by a developer to help give examples of desired behavior.

#example of a function

def chatgptapi(role, message):

   openai.api_key = "YOUR API KEY"

   response = openai.ChatCompletion.create(
   model = "gpt-3.5-turbo",
   messages = [{"role": f'{role}', "content": f'{message}'}],
)
   answer = response["choices"][0]["message"]["content"]
   usage = response["usage"]["total_tokens"]

   return answer, usage
#you can then access the function this way, from any other .py file in your Python environment 

from name_of_py_file_where_you_have_the_function import chatgptapi

output = chatgptapi("system","your message to the API") #it returns a tuple (answer, usage)

reply = output[0] #the first element of the tuple returned by the function
usage = output[1] #the second element of the tuple returned by the function

If you need help to implement the API in your workflow, don’t hesitate to contact me.

🚀 Subscribe to my weekly newsletter packed with tips & tricks around AI, SEO, coding and smart automations

☕️ If you found this piece helpful, you can buy me coffee