ChatGPT API Tutorial: How to make the next big thing using OpenAI

DTD-123
4 min readMar 7, 2023

--

Photo by Zac Wolff on Unsplash

Unless you’re living under a rock, you’ve definitely noticed the progress made by OpenAI recently, with their large language model (LLM), ChatGPT. Just last week, they’ve released a new API that allows developers to use the model for many different purposes, with very reasonable pricing.

Pricing

The API can be used for free, however if you don’t have a billing method there is a rate limit that can be easily reached. For developers with a billing method added, the API is priced at $0.002 per 1,000 tokens, which is 10x cheaper than previous GPT models, like text-davinci-003.

Use cases

In the blog post introducing the API, multiple different companies using the new API were mentioned. For example, Quizlet used the API to make a chatbot to help students learn using a virtual AI tutor. Also, Snapchat is using the API to make a chatbot for users to use, which can make creative work like haiku poems. So, clearly, this technology has a lot of potential.

“Hello World”

First, you need to get the API key for your OpenAI account. Log in to your OpenAI dashboard, and click on your profile, then “View API keys”.

OpenAI API Key manager

You should see a screen like the one above. Click on “Create new secret key” to get your API key. Make sure to keep it stored somewhere on your computer, like a password manager, because you can’t copy it again after you’ve created it.

Now, let’s open the terminal and start coding. We need to install the OpenAI library like this:

pip install openai

Now, let’s open a new Python file. Let’s import the OpenAI library and make our first request to the API.

import openai

openai.api_key = "YOUR_API_KEY_HERE"

completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Write a story about a cat."}]
)

print(completion)

You should get an output with a story about a cat. Congratulations! You’ve just built your first app using the ChatGPT API. Now, let’s make it a chat interface, by putting it in an infinite loop and getting user input.

import openai

openai.api_key = "YOUR_API_KEY_HERE"

while True:
userinput = input("Prompt: ")

completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": userinput}]
)

# Print the message content only rather than the whole object
print(completion.choices[0].message.content)

Now we have a working chat interface, kind of like the ChatGPT website. However, you may notice, the API is stateless, meaning it has no memory of previous messages in the conversation. We can fix this by building a conversation history and injecting it into each prompt.

import openai

openai.api_key = "YOUR_API_KEY_HERE"

conversation_history = ""

while True:
userinput = input("Prompt: ")

conversation_history += "User: " + userinput + "\n"

completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": conversation_history + userinput}]
)

conversation_history += "AI: " + completion.choices[0].message.content + "\n"

# Print the message content only rather than the whole object
print(completion.choices[0].message.content)

Alright, now we have an AI-powered chatbot, but it is basically the same as regular ChatGPT, since we have not customized it for our own purpose. To solve this, we can use a “system” role in the request to give it information about how it should act. This allows us to make it work for our own use cases.

import openai

openai.api_key = "YOUR_API_KEY_HERE"

# Customized prompt
system_prompt = "Hello, from now on you are FactBot, a bot that can tell its users about many different facts and test them on it."

conversation_history = ""

while True:
userinput = input("Prompt: ")

conversation_history += "User: " + userinput + "\n"

completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": conversation_history + userinput}]
)

conversation_history += "AI: " + completion.choices[0].message.content + "\n"

# Print the message content only rather than the whole object
print(completion.choices[0].message.content)

Perfect! Now we have a fully working AI chatbot that is custom-made for your use case.

Further reading

See also

https://openai.com/research/whisper

--

--