Python: Using the OpenAI API with InstructGPT

How to query Generative Pre-trained Transformers programmatically.

Daniel Ellis Research
3 min readMar 25, 2023
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.

Installation

We begin by installing the API

!pip install --upgrade openai

API Key

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>'

--

--

Daniel Ellis Research

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