Below is example python code and the expected results that you can use to confirm an OpenAI’s API key is working.
Code Example: Python
!pip install openai
!pip install python-dotenv
import os
import openai
from dotenv import load_dotenv
load_dotenv()
# replace 'sk-abc ... xyz' with you secret key and don't forget to enclose using single quotes
os.environ['OPENAI_KEY'] = 'sk-abc ... YOUR SECRET KEY GOES HERE ... xyz'
api_key = os.getenv('OPENAI_KEY')
openai.api_key = api_key
# returns a list of all OpenAI models
# models = openai.Model.list()
# print(models)
for _ in range(5):
text = openai.Completion.create(
model="text-davinci-003",
prompt="France is famous for its",
max_tokens=15,
temperature=1
)
print(text['choices'][0]['text'])
print("==============================")
Results
Requirement already satisfied: openai in /usr/local/lib/python3.10/dist-packages (0.27.8)
Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai) (2.31.0)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai) (4.66.1)
Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from openai) (3.8.5)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2023.7.22)
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (23.1.0)
Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (6.0.4)
Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (4.0.3)
Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (1.9.2)
Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (1.4.0)
Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (1.3.1)
Requirement already satisfied: python-dotenv in /usr/local/lib/python3.10/dist-packages (1.0.0)
wine
France is well-known for its extensive history and culture,
==============================
food. French cuisine is considered one of the finest in the world and is
==============================
food
France is renowned for its rich and varied cuisine. Popular dishes
==============================
wines
France is well known for its many types of wines. It
==============================
food
French cuisine is renowned for its rich flavors and use of quality
==============================
Sources
Resources that explain OpenAI’s API.
OpenAI API Reference: here.
Is it possible to set environemnt variables in Googles Colaboratory: here.
OpenAI Python API: How to Use & Examples (August 2023): here.
Important
Do not share your API key with others, or expose it in the browser, or other client-side code. In order to protect the security of your account delete any API key after testing.