I’ve been having a headache with the configuration the KEY API for a week now.
I have a MAC and I am using JupyterLab and I charged my Pay as you go to 20$ (Billing Settings)
I did the following steps:
1)Install Python
pip install --upgrade openai
I don’t have any virtual environment
My Python version is 3.8.18
2) Setup my API key on my MAC by Terminal
export OPENAI_API_KEY=‘sk-*******’
I verify than that I have the API KEY environment variable correctly configured on my MAC.
echo $OPENAI_API_KEY
sk-**************** (It works fine)
**3) Sending my first API request
I make a “openai-test.py” by Terminal and Copy and Paste the next code:
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
]
)
print(completion.choices[0].message)
Now, the following line is executed via Terminal
python openai-test.py and it works fine
**But on my JupiterLab script it doesn’t work when I execute this sentence,
import os
api_key = os.getenv('OPENAI_API_KEY')
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
]
)
print(completion.choices[0].message)
it doesn’t founds my environment variable OPENAI_API_KEY Why? What it is happening?