Python: Using the OpenAI API with InstructGPT

How to query Generative Pre-trained Transformers programmatically.

Daniel Ellis Research
3 min readMar 25


Photo by Gertrūda Valasevičiūtė on Unsplash

In recent months (article written Nov 22), Generative Pre-trained Transformers have taken the world by storm. They seem to be the solution to many tasks, but also the epoch of many many headaches. In this quick and simple tutorial, I talk through how to use the OpenAI API in Python and couple it with the text-davinci-003 model.


We begin by installing the API

!pip install --upgrade openai


Once we have installed our libraries, we now need to generate an openAI key for our application. We do this by visiting:

This links any requests you make to your user account. It is important you make a note of the key and save it somewhere secure since this will only be shown once.

We now load this into our python program:

import openai
openai.api_key = '<key goes here>'

If you want to be that extra bit secure, you may additionally choose to encrypt your key, as per my previous article:

Building Our Query Function

Since the API returns a string of different values, we write a wrapper function to simplify the question progress. This can be seen below:

def ask(gpt_prompt, engine = "text-davinci-002",max_tokens=256 ):

response = openai.Completion.create(
# temperature is the randomness where 1 is the most random
# maximum numbers of tokens to generate a prompt for
# a token is approximately 4 characters of text

return response['choices'][0]['text']



Daniel Ellis Research

Research Software Engineer specialising in High-Performance Computing and Data Visualisation. — PhD in Atmospheric Chemistry and Masters in Theoretical Physics.