I am getting this error in jupyter even though i copied as it is in the guidlines


AuthenticationError Traceback (most recent call last)
in
16 {text}
17 “”"
—> 18 response = get_completion(prompt)
19 print(response)

in get_completion(prompt, model)
1 def get_completion(prompt, model=“gpt-3.5-turbo”):
2 messages = [{“role”: “user”, “content”: prompt}]
----> 3 response = openai.ChatCompletion.create(
4 model=model,
5 messages=messages,

~\anaconda3\lib\site-packages\openai\api_resources\chat_completion.py in create(cls, *args, **kwargs)
23 while True:
24 try:
—> 25 return super().create(*args, **kwargs)
26 except TryAgain as e:
27 if timeout is not None and time.time() > start + timeout:

~\anaconda3\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py in create(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)
147 url,
148 params,
→ 149 ) = cls.__prepare_create_request(
150 api_key, api_base, api_type, api_version, organization, **params
151 )

~\anaconda3\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py in __prepare_create_request(cls, api_key, api_base, api_type, api_version, organization, **params)
104 params[“timeout”] = MAX_TIMEOUT
105
→ 106 requestor = api_requestor.APIRequestor(
107 api_key,
108 api_base=api_base,

~\anaconda3\lib\site-packages\openai\api_requestor.py in init(self, key, api_base, api_type, api_version, organization)
136 ):
137 self.api_base = api_base or openai.api_base
→ 138 self.api_key = key or util.default_api_key()
139 self.api_type = (
140 ApiType.from_str(api_type)

~\anaconda3\lib\site-packages\openai\util.py in default_api_key()
184 return openai.api_key
185 else:
→ 186 raise openai.error.AuthenticationError(
187 “No API key provided. You can set your API key in code using ‘openai.api_key = ’, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with ‘openai.api_key_path = ’. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.”
188 )

AuthenticationError: No API key provided. You can set your API key in code using ‘openai.api_key = ’, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with ‘openai.api_key_path = ’. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.

1 Like

This is my code:
import openai
import os

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

openai.api_key = os.getenv(‘OPENAI_API_KEY’)

def get_completion(prompt, model=“gpt-3.5-turbo”):
messages = [{“role”: “user”, “content”: prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model’s output
)
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)

The reason is as follows in your error info:

You need set your API key in code.

2 Likes