From 307f6e50adc00aca6638df5a4b472da5b43601f8 Mon Sep 17 00:00:00 2001 From: Abdul Qoyyuum Date: Sat, 24 Jun 2023 17:19:01 +0800 Subject: [PATCH] :sparkle: Improved OpenAI API Key Insert to Env (#2486) Co-authored-by: Luke K (pr-0f3t) <2609441+lc0rp@users.noreply.github.com> Co-authored-by: Reinier van der Leer --- autogpt/config/config.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/autogpt/config/config.py b/autogpt/config/config.py index d032f822..f4dff28d 100644 --- a/autogpt/config/config.py +++ b/autogpt/config/config.py @@ -1,5 +1,6 @@ """Configuration class to store the state of bools for different scripts access.""" import os +import re from typing import List import openai @@ -310,4 +311,22 @@ def check_openai_api_key(config: Config) -> None: + Fore.RESET ) print("You can get your key from https://platform.openai.com/account/api-keys") - exit(1) + openai_api_key = input( + "If you do have the key, please enter your OpenAI API key now:\n" + ) + key_pattern = r"^sk-\w{48}" + openai_api_key = openai_api_key.strip() + if re.search(key_pattern, openai_api_key): + os.environ["OPENAI_API_KEY"] = openai_api_key + cfg.set_openai_api_key(openai_api_key) + print( + Fore.GREEN + + "OpenAI API key successfully set!\n" + + Fore.ORANGE + + "NOTE: The API key you've set is only temporary.\n" + + "For longer sessions, please set it in .env file" + + Fore.RESET + ) + else: + print("Invalid OpenAI API key!") + exit(1)