I am completely new, so apologies in advance.
I have Python 3.11.4 installed on my Mac.
According to the OpenAI Quickstart guide, I must install the OpenAI Python library by running 'pip install --upgrade openai' in my terminal. (Side note, I am using the Terminal on Mac. When I launch Jupyter Notebook via Anaconda, I get another terminal that pops up, I haven't been touching that, not sure if I need to.) That went well.
Next it says to set up your API Key (for all projects or just a single project). I know my API Key. But I get stuck here. It states to Edit Bash Profile and Add Environmental Variable (export OPENAI_API_KEY='my_api_goes_here'). I am not able to do the 'add environmental variable' part.
When I try to run their example notebook:
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)
I get the following error: OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable.
I am assuming this is because I didn't complete the API Key set-up process.
I tried to add the environmental variable in the terminal but couldn't figure it out. I just pasted the line (with my API key) and nothing ran.
Here is a picture of the terminal where I am unsure of where to paste 'export OPENAI_API_KEY='my_api_goes_here'': terminal
You have a couple options in most recommended to least:
Running
then pasting in the line:
with your API key replacing the right hand side.
Open your terminal and run:
This will set the environment variable however does not persist it. For repeated use, adding to a file such as 1 is a good idea (although please evaluate your security posture)
oslibrary in your script [NOT RECOMMEND]Within your script, you can set the environment variable