My api key is not working?

i am learning to how to make prompts with open AI, but my API key is not working i tried several API keys.
here is my code:-
import openai
from dotenv import load_dotenv, find_dotenv

import openai
from dotenv import load_dotenv,find_dotenv
import os

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

openai.api_key = os.getenv(‘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,

)
return response.choiuces[0].messages['content']

text = f"““Robotics is an interdisciplinary field that combines various branches of science
and engineering to create intelligent machines capable of performing tasks autonomously or
with minimal human intervention. It involves the design, construction, operation, and use of robots,
which are mechanical devices programmed to
interact with the physical world.””"

prompt = f"“” Summarize the text delimited by triple backticks
into a single sentence.
‘’‘{text}’‘’
“”"
response = get_completion(prompt)
print(response)

error:----------------------------------------------------------------------------
AuthenticationError Traceback (most recent call last)
Cell In[3], line 18
1 text = f"“”
2 You should express what you want a model to do by \
3 providing instructions that are as clear and \
(…)
11 more detailed and relevant outputs.
12 “”"
13 prompt = f"“”
14 Summarize the text delimited by triple backticks \
15 into a single sentence.
16 {text}
17 “”"
—> 18 response = get_completion(prompt)
19 print(response)

Cell In[2], line 3, 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,
6 temperature=0, # this is the degree of randomness of the model’s output
7 )
8 return response.choices[0].message[“content”]

File /usr/local/lib/python3.9/site-packages/openai/api_resources/chat_completion.py:25, in ChatCompletion.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:

File /usr/local/lib/python3.9/site-packages/openai/api_resources/abstract/engine_api_resource.py:149, in EngineAPIResource.create(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)
127 @classmethod
128 def create(
129 cls,
(…)
136 **params,
137 ):
138 (
139 deployment_id,
140 engine,
141 timeout,
142 stream,
143 headers,
144 request_timeout,
145 typed_api_type,
146 requestor,
147 url,
148 params,
→ 149 ) = cls.__prepare_create_request(
150 api_key, api_base, api_type, api_version, organization, **params
151 )
153 response, _, api_key = requestor.request(
154 “post”,
155 url,
(…)
160 request_timeout=request_timeout,
161 )
163 if stream:
164 # must be an iterator

File /usr/local/lib/python3.9/site-packages/openai/api_resources/abstract/engine_api_resource.py:106, in EngineAPIResource.__prepare_create_request(cls, api_key, api_base, api_type, api_version, organization, **params)
103 elif timeout == 0:
104 params[“timeout”] = MAX_TIMEOUT
→ 106 requestor = api_requestor.APIRequestor(
107 api_key,
108 api_base=api_base,
109 api_type=api_type,
110 api_version=api_version,
111 organization=organization,
112 )
113 url = cls.class_url(engine, api_type, api_version)
114 return (
115 deployment_id,
116 engine,
(…)
124 params,
125 )

File /usr/local/lib/python3.9/site-packages/openai/api_requestor.py:130, in APIRequestor.init(self, key, api_base, api_type, api_version, organization)
121 def init(
122 self,
123 key=None,
(…)
127 organization=None,
128 ):
129 self.api_base = api_base or openai.api_base
→ 130 self.api_key = key or util.default_api_key()
131 self.api_type = (
132 ApiType.from_str(api_type)
133 if api_type
134 else ApiType.from_str(openai.api_type)
135 )
136 self.api_version = api_version or openai.api_version

File /usr/local/lib/python3.9/site-packages/openai/util.py:186, 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://onboard.openai.com for details, or email support@openai.com if you have any questions.”
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://onboard.openai.com for details, or email support@openai.com if you have any questions.

I’ll move your message to the ChatGPT course discussion area. You posted it in General Discussions.

Try searching the forum for “API”, there are a lot of posts about it. Maybe you’ll find an answer.

Well, now you should generate another key, because this one is no longer secret. This is a unique identifier that allows your account to be charged for usage of the chat gpt model service. Bad idea to publish it.

okay i removed it my key is not working this is the error i am seeing :----------------------------------------------------------------------------
AuthenticationError Traceback (most recent call last)
Cell In[3], line 18
1 text = f"“”
2 You should express what you want a model to do by \
3 providing instructions that are as clear and \
(…)
11 more detailed and relevant outputs.
12 “”"
13 prompt = f"“”
14 Summarize the text delimited by triple backticks \
15 into a single sentence.
16 {text}
17 “”"
—> 18 response = get_completion(prompt)
19 print(response)

Cell In[2], line 3, 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,
6 temperature=0, # this is the degree of randomness of the model’s output
7 )
8 return response.choices[0].message[“content”]

File /usr/local/lib/python3.9/site-packages/openai/api_resources/chat_completion.py:25, in ChatCompletion.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:

File /usr/local/lib/python3.9/site-packages/openai/api_resources/abstract/engine_api_resource.py:149, in EngineAPIResource.create(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)
127 @classmethod
128 def create(
129 cls,
(…)
136 **params,
137 ):
138 (
139 deployment_id,
140 engine,
141 timeout,
142 stream,
143 headers,
144 request_timeout,
145 typed_api_type,
146 requestor,
147 url,
148 params,
→ 149 ) = cls.__prepare_create_request(
150 api_key, api_base, api_type, api_version, organization, **params
151 )
153 response, _, api_key = requestor.request(
154 “post”,
155 url,
(…)
160 request_timeout=request_timeout,
161 )
163 if stream:
164 # must be an iterator

File /usr/local/lib/python3.9/site-packages/openai/api_resources/abstract/engine_api_resource.py:106, in EngineAPIResource.__prepare_create_request(cls, api_key, api_base, api_type, api_version, organization, **params)
103 elif timeout == 0:
104 params[“timeout”] = MAX_TIMEOUT
→ 106 requestor = api_requestor.APIRequestor(
107 api_key,
108 api_base=api_base,
109 api_type=api_type,
110 api_version=api_version,
111 organization=organization,
112 )
113 url = cls.class_url(engine, api_type, api_version)
114 return (
115 deployment_id,
116 engine,
(…)
124 params,
125 )

File /usr/local/lib/python3.9/site-packages/openai/api_requestor.py:130, in APIRequestor.init(self, key, api_base, api_type, api_version, organization)
121 def init(
122 self,
123 key=None,
(…)
127 organization=None,
128 ):
129 self.api_base = api_base or openai.api_base
→ 130 self.api_key = key or util.default_api_key()
131 self.api_type = (
132 ApiType.from_str(api_type)
133 if api_type
134 else ApiType.from_str(openai.api_type)
135 )
136 self.api_version = api_version or openai.api_version

File /usr/local/lib/python3.9/site-packages/openai/util.py:186, 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://onboard.openai.com for details, or email support@openai.com if you have any questions.”
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://onboard.openai.com for details, or email support@openai.com if you have any questions.

okay i removed it my key is not working this is the error i am seeing :----------------------------------------------------------------------------

As detailed in several other threads, there are three options.

  1. store your API key in a local file, then read it back in at the beginning of a chat-gpt program
  2. set the API key into your local environment, then read it back in at the beginning of a chat-gpt program
  3. assign the API key as a string directly in your chat-gpt program

You can’t just try to read it either from a file or from the environment if you didn’t explicitly put it there first. If you directly assign it from a string, you don’t need the dotenv stuff at all. Below I’ll link a thread with more detail that could have been found using forum search on this error.

Here’s one, there are many