mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2026-02-11 09:14:19 +01:00
❇️ 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 <github@pwuts.nl>
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user