Api key problem

i am can’t able slove error , need help!!

import openai
import os

openai.api_key = "sk-....vbJmk"


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

openai.api_key  = os.getenv('sk-...mk')


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)

AuthenticationError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_7420/1836459946.py in
39 {text}
40 “”"
—> 41 response = get_completion(prompt)
42 print(response)

~\AppData\Local\Temp/ipykernel_7420/1836459946.py in get_completion(prompt, model)
13 def get_completion(prompt, model=“gpt-3.5-turbo”):
14 messages = [{“role”: “user”, “content”: prompt}]
—> 15 response = openai.ChatCompletion.create(
16 model=model,
17 messages=messages,

~\AppData\Local\Programs\Python\Python39\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:

~\AppData\Local\Programs\Python\Python39\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 )

~\AppData\Local\Programs\Python\Python39\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,

~\AppData\Local\Programs\Python\Python39\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)

~\AppData\Local\Programs\Python\Python39\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 OpenAI Platform 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 OpenAI Platform for details.

Are you using your key or it is the lab’s key?

Repeated assignments to variable openai.api_key,you should comment the following statement in your program:

openai.api_key = os.getenv(‘sk-…mk’)

Then, your program is as follows:

import openai
import os

openai.api_key = "sk-....vbJmk"

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

# openai.api_key  = os.getenv('sk-...mk')

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)
1 Like

i using my key but getting rate limit error
please help with this

Create another key or buy it…

if we not buy this what is alternate solution ?