Getting started with the ChatGPT API

Unless you’ve been living under a rock, you’ll have heard of ChatGPT. OpenAI has opened up the capabilities of their AIs for everyone to use! Now anyone can use it to generate drafts of emails or blog posts, for generating code examples or for explaining and summarising.

It’s even pretty good at translating between languages, so let’s try that out by building a translation app!

Building a translation app

You can have fun carefully crafting some ChatGPT prompts for whatever task you like! For this example, we’re going to ask ChatGPT to translate from English into German.

In this program you can type any sentence or more of English text in and chatGPT will translate it.

Step 1: Generate your OpenAI API Key

You’ll need an OpenAI account, if you’ve tried out ChatGPT before you probably already have one.

Once you’re logged in, just open up the user settings page and find API keys! It’s free to create an API key, but there is a limit on how many requests you can send for free.

Click “Create new secret key” and copy the key.

An API key is like a password, you don’t want someone else finding it and using up all your free requests. In a new SplootCode project, paste your API key into the Environment Variables so that it’s stored securely and won’t be accidentally shared with anyone else.

To use that API Key in our project, we can access that environment variable like this. You don’t need to type it out, just drag and drop the example usage from below the environment variables.

Step 2: Getting Authorized

In order to use the ChatGPT API, we need to send our API key with every single request. This API uses Bearer Authentication, which means we must include a special header with the API key in each request.

When you run this code, you’ll be able to check if the Authorisation is set up correctly – because the API response will tell us if it’s not.

Checking if you’re Authorized

If something is wrong with your API key, or wrong with the Authorization header then you’ll see a message like this:

{'error': {'message': "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from <https://platform.openai.com/account/api-keys.">, 'type': 'invalid_request_error', 'param': None, 'code': None}}
{'error': {'message': 'you must provide a model parameter', 'type': 'invalid_request_error', 'param': None, 'code': None}}

If the Authoriziation header is working correctly, you’ll see a different error message:

{'error': {'message': 'you must provide a model parameter', 'type': 'invalid_request_error', 'param': None, 'code': None}}

This is great! The code does not include the other parameters yet, but at least we know that it passed the Authorization step!

Step 3: Building the chat context

The ChatGPT API needs to be given at least a short history of chat messages to use as context to start generating the next message in the chat.

The API expects a list of messages, where each message is a dictionary (JSON object) which has a role name and some content.

Here’s an example chat history which instructs the model to translate English to German:

Now, we want this to be customisable, so when we run the program we can enter any English text and our program will translate it.

Last step! We just need to tell ChatGPT which model we want it to use, and send the messages as part of the request. The ChatGPT Chat completion guide recommends using the gpt-3.5-turbo model which is one of their most advanced models and also the one used by the official ChatGPT app.

If you’re still printing the response data to the console, you should be able to see the response.

Enter English text: Which way to the train station please?
{'id': 'chatcmpl-6rI5gJaYGsPV4Afsz1MFlo22lkR50', 'object': 'chat.completion', 'created': 1678158944, 'model': 'gpt-3.5-turbo-0301', 'usage': {'prompt_tokens': 37, 'completion_tokens': 12, 'total_tokens': 49}, 'choices': [{'message': {'role': 'assistant', 'content': 'Wie komme ich bitte zum Bahnhof?'}, 'finish_reason': 'stop', 'index': 0}]}

Step 4: Interpreting the response

There’s a lot of information in this response, but there’s only one part we care about – the translated text!

Look carefully at the whole response, and find the translated German text.

{
	'id': 'chatcmpl-6rI5gJaYGsPV4Afsz1MFlo22lkR50',
	'object': 'chat.completion',
	'created': 1678158944,
	'model': 'gpt-3.5-turbo-0301',
	'usage': {'prompt_tokens': 37, 'completion_tokens': 12, 'total_tokens': 49},
	'choices': [
		{
			'message': {
				'role': 'assistant',
				'content': 'Wie komme ich bitte zum Bahnhof?'
			},
			'finish_reason': 'stop',
			'index': 0
		}
	]
}

The information we want is under the “choices” key, and the set of choices is a list. The API can return more than one option for the response if you ask it to, but the default is to return only one.

We need to get the first choice from the list (of one) and then from that choice, we drill down to the message and more specifically, the content of that message.

And we’re done!

Now when we run our app, we can enter as much English text as we like and see only the resulting translated text!

Enter English text: Which way to the train station please?
"Wie komme ich bitte zum Bahnhof?"

Open the full code example and add your own API key to the Environment Variables to start using it.

Where to go from here

Now that we’ve got the building blocks to use the Chat Completion API, you can use it in your own apps!

  • A Slack command or Discord command for fast translations, while you’re chatting.
  • Automatically creating first drafts of blog posts based on the title.
  • Add instructions to play-act as a particular character and then chat back and forth to them!