setx 명령어는 환경 변수를 영구적으로 저장하지만, 현재 터미널 세션에는 바로 적용되지 않습니다.
따라서, 환경 변수를 확인하려면 새 명령 프롬프트 창을 열고 아래 명령어로 확인해 보세요:

 

cmd에서

setx OPENAI_API_KEY sk-프로젝트-키-여기에-입력

 

PowerShell 에서

$env:OPENAI_API_KEY = "sk-프로젝트-키-여기에-입력"

 

cmd
 
echo %OPENAI_API_KEY%

현재 세션에 적용하고 싶다면, set 명령어를 사용하세요:

cmd
 
set OPENAI_API_KEY=sk-proj-6xf4H8M9FLyIgqCeJQU0U84EeZ9.......
 

이렇게 하면 현재 터미널에서 바로 사용할 수 있습니다.

 

 

터미널 환경

 

$env:OPENAI_API_KEY

 

echo $env:OPENAI_API_KEY

 

로 확인할수 있다 

 

 

파이썬 코드로 확인하는 방법 API 값

 

import os
from openai import OpenAI

print(os.environ.get("OPENAI_API_KEY"))

 

 

 

테스트 코드 

import os
from openai import OpenAI

#print(os.environ.get("OPENAI_API_KEY"))

client = OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY"),  # This is the default and can be omitted
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="gpt-4o",
)

 

 

이런 용량이 부족하단 에러가 나와야 한다

 

Exception has occurred: RateLimitError
  •  
Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}
httpx.HTTPStatusError: Client error '429 Too Many Requests' for url 'https://api.openai.com/v1/chat/completions' For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429

 

 

 

 

 

결제하고 난후 다음 코드로 테스트 해보면 다음 결과처럼 보이게 된다

 

 

import os
from openai import OpenAI

#print(os.environ.get("OPENAI_API_KEY"))

client = OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY"),  # This is the default and can be omitted
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="gpt-4o",
)

response_message =  chat_completion.choices[0].message.content
print(response_message)

 

 

결과 : 

This is a test.

 

 

반응형

+ Recent posts