AI/OpenAI API

OpenAI API - API KEY Error (Windows - Python)

강서버 2025. 1. 22. 19:01
728x90
반응형

[ 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)

 
from openai import OpenAI

client = OpenAI(
  api_key="sk-proj-GNBxSl-kdZB-S9RreOmCzYl4ZfAp-W472LoTzZkFXCiPEwb75QiwhfpAVnBH7gJ2dfsbkofPNIT3BlbkFJyIkrH58B6WLV_ORxmSVIj2iklcR0zyYQ1gwdheHekSxGkkWBdN2xYmj4KfmrwxSY_f8MA"
)

completion = client.chat.completions.create(
  model="gpt-4o-mini",
  store=True,
  messages=[
    {"role": "user", "content": "write a haiku about ai"}
  ]
)

print(completion.choices[0].message);
 

 

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)

 
import openai
 
openai.api_key = "sk-proj-GNBxSl-kdZB-S9RreOmCzYl4ZfAp-W472LoTzZkFXCiPEwb75QiwhfpAVnBH7gJ2dfsbkofPNIT3BlbkFJyIkrH58B6WLV_ORxmSVIj2iklcR0zyYQ1gwdheHekSxGkkWBdN2xYmj4KfmrwxSY_f8MA"

# create a chat completion
chat_completion = openai.ChatCompletion.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello world"}])

# print the chat completion
print(chat_completion.choices[0].message.content)
 

 

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"

 
(Register)
 
PS C:\Users\user> setx OPENAI_API_KEY "sk-proj-GNBxSl-kdZB-S9RreOmCzYl4ZfAp-W472LoTzZkFXCiPEwb75QiwhfpAVnBH7gJ2dfsbkofPNIT3BlbkFJyIkrH58B6WLV_ORxmSVIj2iklcR0zyYQ1gwdheHekSxGkkWBdN2xYmj4KfmrwxSY_f8MA"

성공: 지정한 값을 저장했습니다.
 
 
(Reload)
 
PS C:\Users\user> $env:OPENAI_API_KEY = [System.Environment]::GetEnvironmentVariable("OPENAI_API_KEY", "User")
 
 
(Lookup)
 
PS C:\Users\user> Write-Output "$env:OPENAI_API_KEY"
sk-proj-GNBxSl-kdZB-S9RreOmCzYl4ZfAp-W472LoTzZkFXCiPEwb75QiwhfpAVnBH7gJ2dfsbkofPNIT3BlbkFJyIkrH58B6WLV_ORxmSVIj2iklcR0zyYQ1gwdheHekSxGkkWBdN2xYmj4KfmrwxSY_f8MA
 
 

 

( Capture screen )

 

Environment variables ] (Windows 10)

 

 

[ openai version : 1.59.9 ]

> pip install openai==1.59.9

 

Request code :

 
from openai import OpenAI

client = OpenAI()

completion = client.chat.completions.create(
  model="gpt-4o-mini",
  store=True,
  messages=[
    {"role": "user", "content": "write a haiku about ai"}
  ]
)

print(completion.choices[0].message);
 

 

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 :

 
import openai

# create a chat completion
chat_completion = openai.ChatCompletion.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello world"}])

# print the chat completion
print(chat_completion.choices[0].message.content)
 

 

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/

 

openai

Python client library for the OpenAI API

pypi.org

 

728x90
반응형