Beginner, I am stuck on setting up to use the GPT 3.5 model in Jupyter Notebook?

46 views Asked by At

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

1

There are 1 answers

2
AbeCoull On

You have a couple options in most recommended to least:

  1. Setting this within your zshrc, bshrc, etc:

Running

vim ~/.zshrc # Or replace with whatever file you are utiling such as bshrc for bash

then pasting in the line:

export OPENAI_API_KEY='my_api_goes_here'

with your API key replacing the right hand side.

  1. Executing this in your terminal:

Open your terminal and run:

export OPENAI_API_KEY='my_api_goes_here'

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)

  1. Using the os library in your script [NOT RECOMMEND]

Within your script, you can set the environment variable

import os

os.environ["OPENAI_API_KEY"] = 'my_api_goes_here'