From 02e5b619c9e63dffee7e0a4dc50cad0158befaa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Coyle?= <33181239+loiccoyle@users.noreply.github.com> Date: Mon, 13 Mar 2023 23:00:42 +0100 Subject: [PATCH] OPENAI_API_KEY environment variable * feat: get api key from OPENAI_API_KEY environment variable * doc(readme): add OPENAI_API_KEY instructions --- README.md | 4 +++- sgpt/app.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3e09ceb..2fab4ad 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,9 @@ A command-line productivity tool powered by OpenAI's ChatGPT (GPT-3.5). As devel ```shell pip install shell-gpt --user ``` -On first start you would need to generate and provide your API key, get one [here](https://beta.openai.com/account/api-keys). +You'll need an OpenAI API key, you can generate one [here](https://beta.openai.com/account/api-keys). + +If the`$OPENAI_API_KEY` environment variable is set it will be used, otherwise, you will be prompted for your key which will then be stored in `~/.config/shell-gpt/api-key.txt`. ## Usage `sgpt` has a variety of use cases, including simple queries, shell queries, and code queries. diff --git a/sgpt/app.py b/sgpt/app.py index 4fe5dbe..4c66140 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -40,6 +40,10 @@ def get_api_key() -> str: :return: String API key for OpenAI API requests. """ + + if "OPENAI_API_KEY" in os.environ: + return os.environ["OPENAI_API_KEY"] + if not KEY_FILE.exists(): api_key = getpass(prompt="Please enter your API secret key") KEY_FILE.parent.mkdir(parents=True, exist_ok=True)