Unverified Commit 9235f347 authored by jinyuan sun's avatar jinyuan sun Committed by GitHub
Browse files

Merge pull request #3 from JarrettSJohnson/api_key_envvar

parents f7ab80a4 a08f4336
Loading
Loading
Loading
Loading
+11 −7
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ conversation_history = " "

PLUGIN_DIR = os.path.dirname(os.path.abspath(__file__))
API_KEY_FILE = os.path.join(PLUGIN_DIR, "apikey.txt")
OPENAI_KEY_ENV = "OPENAI_API_KEY"

def set_api_key(api_key):
    api_key = api_key.strip()
@@ -16,13 +17,16 @@ def set_api_key(api_key):
    print("API key set and saved to file successfully.")

def load_api_key():
    api_key = os.getenv(OPENAI_KEY_ENV)
    if not api_key:
        try:
            with open(API_KEY_FILE, "r") as api_key_file:
                api_key = api_key_file.read().strip()
                openai.api_key = api_key
                print("API key loaded from file.")
        except FileNotFoundError:
        print("API key file not found. Please set your API key using 'set_api_key your_api_key_here' command.")
            print("API key file not found. Please set your API key using 'set_api_key your_api_key_here' command" +
                  f" or by environment variable '{OPENAI_KEY_ENV}'.")

load_api_key()