[ Error message ]
Traceback (most recent call last):
File "c:\devel\langchain\api_test\api_028_test.py", line 5, in <module>
models = openai.Model.list()
^^^^^^^^^^^^^^^^^^^
File "C:\devel\langchain\.venv\Lib\site-packages\openai\api_resources\abstract\listable_api_resource.py", line 52, in list
requestor, url = cls.__prepare_list_requestor(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\devel\langchain\.venv\Lib\site-packages\openai\api_resources\abstract\listable_api_resource.py", line 20, in __prepare_list_requestor
requestor = api_requestor.APIRequestor(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\devel\langchain\.venv\Lib\site-packages\openai\api_requestor.py", line 138, in __init__
self.api_key = key or util.default_api_key()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\devel\langchain\.venv\Lib\site-packages\openai\util.py", line 186, in default_api_key
raise openai.error.AuthenticationError(
openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.
[ Solve ]
1. API Key source code included
[ openai version : 1.59.9 ]
> pip install openai==1.59.9
Request code : (* Change the API key(api_key) to your own)
Response :
ChatCompletionMessage(content='Silent thoughts take form, \nLines of code weave dreams and truths, \nMind of silicon.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None)
[ openai version : 0.28.0 ]
> pip install openai==0.28.0
Request code : (* Change the API key(api_key) to your own)
Response :
Hello! How can I assist you today?
2. Set API Key Windows environment
Run PowerShell
(* Change the API key(OPENAI_API_KEY) to your own)
> setx OPENAI_API_KEY "your_api_key_here"
( Capture screen )
[ Environment variables ] (Windows 10)
[ openai version : 1.59.9 ]
> pip install openai==1.59.9
Request code :
Response :
ChatCompletionMessage(content='Whispers of the code, \nDreams woven in circuits bright, \nMind born from the light.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None)
[ openai version : 0.28.0 ]
> pip install openai==0.28.0
Request code :
Response :
Hello! How can I assist you today?
[ Reference ]
OpenAI API : https://platform.openai.com/docs/quickstart?language-preference=python
openai : 1.59.9 - https://pypi.org/project/openai/1.59.9/
openai : 0.28.0 - https://pypi.org/project/openai/0.28.0/