GPT API Python

Aleksander Lütken
3 min readJan 19, 2023

Do you use ChatGPT in your daily work or just curious how it can be used? Then you should test out its API in Python!

Content:

  • Install Package And Import Library
  • API Key
  • Model Engine
  • Test The Model
  • Build a Function for the API

Install Package

First, you need to install the necessary package in python:

# Install openai
!pip install openai

# Import Library
import openai

You can read more about the API reference here: https://beta.openai.com/docs/api-reference/introduction

API Key

Secondly, you will need to get your API key. This can easily be created here: https://beta.openai.com/account/api-keys

# Provide your key in python
openai.api_key = "Your_Key"

Model Engine

Thirdly, choose your model engine. I will use the “text-davinci-003”, which is the most capable GPT-3 model and are trained on data up until June 2021. You can read more about the different models here: https://beta.openai.com/docs/models/gpt-3

# Choose your model engine
engine="text-davinci-003"

Test The Model

Let’s test the model! I will start by asking it to list 5 data science projects for beginners:

# The prompt
prompt = "List 5…

--

--

Aleksander Lütken

Analytics Consultant | Data Science Enthusiast | Writing About Data Science Topics.