Member-only story
A Simple Guide to OpenAI API with Python
ChatGPT API is coming soon, but you can learn how to use OpenAI API today!

Recently OpenAI announced that ChatGPT will be coming to their API soon.
While we don’t know how long that will take, we can familiarize ourselves with the OpenAI API today!
By learning the OpenAI API today, you’ll be able to access OpenAI’s powerful models such as GPT-3 for natural language tasks, Codex to translate natural language to code, and DALL-E to create and edit original images.
In this guide, we’ll learn how to use OpenAI API with Python.
First Things First — Generate Your API Key
Before we start working with the OpenAI API, we need to login into our OpenAI account and generate our API keys.

Remember that OpenAI won’t display your secret API key again after you generate it, so copy your API key and save it.
I’ll create an environment variable named OPENAI_API_KEY
that will contain my API key and will be used in the next sections.
Exploring the OpenAI API with Python
To interact with OpenAI API, we need to install the official Python bindings by running the following command.
pip install openai
There are many things we can do with this API. In this guide, we’ll do text completion, code completion, and image generation.
1. Text Completion
Text completion can be used for classification, text generation, conversations, transformation, conversion, summarization, etc. To work with it, we have to use the completion endpoint and give the model a prompt. Then the model will generate text that attempts to match the context/pattern given.
Say we want to classify the following text.
Decide whether a Tweet's sentiment is positive, neutral, or negative.
Tweet: I didn't like the new Batman movie!
Sentiment: