Getting an error when trying to run the notebook

I am getting this error when trying to run the notebook ModuleNotFoundError: No module named ‘dotenv’.

I copied the code exactly as it s in the course’s notebook. Do I need to install/import any other libraries in order to run the code?

Thanks for helping Attila. I did add the new cell but now I get an authentication error.
See screenhot


I’m confused because The course instructor says that we don’t need an API key - since that s provided.

Because you do not have a valid API key to use. You need to register one on the OpenAI website.

For use on the course website you do not need your own API key, but for external use you do. Registration for a personal use API key is currently free, but its use is limited. Perfect for trial, not for commercial use. In addition, there are other paid options.

Thank you for clarifying. I’m using a paid version now.

I am trying to use jupyternotebook on my own laptop and having the same issue with No valid API key. My code as below:

!pip install openai
!pip install python-dotenv
import openai
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

openai.api_key = os.getenv(‘the key I got from openAI’)

def get_completion(prompt, model=“gpt-3.5-turbo”):
messages = [{“role”: “user”, “content”: prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message[“content”]

text = f"“”
You should express what you want a model to do by \
providing instructions that are as clear and \
specific as you can possibly make them. \
This will guide the model towards the desired output, \
and reduce the chances of receiving irrelevant \
or incorrect responses. Don’t confuse writing a \
clear prompt with writing a short prompt. \
In many cases, longer prompts provide more clarity \
and context for the model, which can lead to \
more detailed and relevant outputs.
“”"
prompt = f"“”
Summarize the text delimited by triple backticks \
into a single sentence.
{text}
“”"
response = get_completion(prompt)
print(response)

openai.api_key = os.getenv(‘the key I got from openAI’)

Replace the above line in your code with this:

openai.api_key =“YOUR_API_KEY”

If you do not have an API key you can register on the following website:

1 Like

@Attila_Ambrus has it right

This is a much simpler solution than modifying the environment and loading the environment variable. Using this approach you don’t need to install packages other than openai and later redlines and panel, and don’t need to import or use os at all

Thanks a lot. it works though I got another error about message. Will try to solve by myself first. Thanks again

1 Like