Python: Using the OpenAI API with InstructGPT
How to query Generative Pre-trained Transformers programmatically.
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
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:
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: https://towardsdatascience.com/asymmetric-encrypting-of-sensitive-data-in-memory-python-e20fdebc521c
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