Hi,
I am new to openai and trying to run the example code to run a bot.
import os
import openai
openai.api_key = os.getenv(“APIKEY”)
response = openai.Completion.create(
engine=“text-davinci-001”,
prompt=“Marv is a chatbot that reluctantly answers questions with sarcastic responses:\n\nYou: How many pounds are in a kilogram?\nMarv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.\nYou: What does HTML stand for?\nMarv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.\nYou: When did the first airplane fly?\nMarv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they’d come and take me away.\nYou: What is the meaning of life?\nMarv: I’m not sure. I’ll ask my friend Google.\nYou: What time is it?\nMarv:”,
temperature=0.5,
max_tokens=60,
top_p=0.3,
frequency_penalty=0.5,
presence_penalty=0.0
)
This code is taken from one of the examples: I have given the key values. But still getting error
as " 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."
You need to check to see if the environment variable named ‘APIKEY’ actually exists. When I originally installed the Python module, I believe the environment variable was named ‘OPENAI_API_KEY’, not ‘APIKEY’ like you have it in the os.getenv() function.
If changing that string to ‘OPENAI_API_KEY’ doesn’t work either, then do this on your machine:
Launch “Control Panel”
“System”
“Advanced system settings”
Switch to “Advanced” tab
“Environment variables”
Choose “User Variable” (for just you since any other accounts on your system could access System Variables)
To add a new environment variable: Choose “New”
Enter the variable name “OPENAI_API_KEY” and the actual API string then save it.
You need to RE-START the Python interactive session if you have it running on your system for the new setting to take effect.
I thought you couldn’t initialize a variable in Python like that, but I wrong! Learn something new every day.
Anyways, it’s best practice not to hard-code your API key into your script like that and simply reference environment variables to reduce the likelihood of exposing sensitive information to others and to increase the security of your code.
Side-Note: I know that your whole API key isn’t visible, but I’d still take down the screenshot if I were you. The API key in the screenshot is now much easier to bruteforce given that most of the key is exposed. That is unless you already regenerated another API key.
Thank you. Adding the environment variable generated the following errors after import openai
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai_init_.py”, line 8, in
from openai.api_resources import (
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources_init_.py”, line 1, in
from openai.api_resources.answer import Answer # noqa: F401
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources\answer.py”, line 1, in
from openai.openai_object import OpenAIObject
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\openai_object.py”, line 6, in
from openai import api_requestor, util
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py”, line 13, in
from openai import error, util, version
File “C:\Users\B\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\util.py”, line 6, in
response = openai.Completion.create(
AttributeError: partially initialized module ‘openai’ has no attribute ‘Completion’ (most likely due to a circular import)
Thank you. Removing os.getenv runs on Python command prompt but does nothing. Getting errors in pycharm " AttributeError: partially initialized module ‘openai’ has no attribute ‘Completion’ (most likely due to a circular import)"