OPENAI_API_KEY environment variable

* feat: get api key from OPENAI_API_KEY environment variable
* doc(readme): add OPENAI_API_KEY instructions
This commit is contained in:
Loïc Coyle
2023-03-13 23:00:42 +01:00
committed by GitHub
parent 9c883fcd19
commit 02e5b619c9
2 changed files with 7 additions and 1 deletions
+3 -1
View File
@@ -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.
+4
View File
@@ -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)