Prepare your local machine to experiment with the OpenAI API by installing Python, setting up a virtual environment, and verifying the setup.
Prepare your local machine to experiment with the OpenAI API. We’ll cover installing Python, setting up a virtual environment, installing required packages, obtaining your API key, exporting it as an environment variable, and verifying everything with curl and Jupyter Notebook.Workflow Overview
Download and install Python 3.10+ for your operating system from python.org/downloads. After installation, verify your setup:
Copy
Ask AI
python -V# Expected output:# Python 3.11.4
Launch the REPL to confirm everything works:
Copy
Ask AI
>>> print("Hello, I'm Python!")Hello, I'm Python!>>> name = input("What is your name?\n")# What is your name?# Alice>>> print(f"Hi, {name}.")Hi, Alice.
Using Python 3.10 or later ensures compatibility with the latest OpenAI Python client.
Send a sample request to the Chat Completions endpoint:
Copy
Ask AI
curl https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-3.5-turbo", "messages": [ {"role": "system", "content": "You are an AI assistant for timezones."}, {"role": "user", "content": "If it is 9AM in London, what time is it in Hyderabad?"} ] }'
A successful JSON response will include fields like id, choices, and usage.
Create a new notebook (for example, test_openai.ipynb) and enter:
Copy
Ask AI
import osimport openaiopenai.api_key = os.getenv("OPENAI_API_KEY")response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are an AI assistant for timezones."}, {"role": "user", "content": "If it is 9AM in London, what time is it in Hyderabad?"} ])print(response.choices[0].message.content)
If the notebook returns the expected answer, your development environment is correctly configured!