diff --git a/sgpt/client.py b/sgpt/client.py index 74ec9eef..355b255e 100644 --- a/sgpt/client.py +++ b/sgpt/client.py @@ -3,9 +3,10 @@ from typing import Dict, Generator, List import requests +import typer from .cache import Cache -from .config import cfg +from .config import SHELL_GPT_CONFIG_PATH, cfg CACHE_LENGTH = int(cfg.get("CACHE_LENGTH")) CACHE_PATH = Path(cfg.get("CACHE_PATH")) @@ -58,6 +59,12 @@ def _request( timeout=REQUEST_TIMEOUT, stream=stream, ) + # Check if OPENAI_API_KEY is valid + if response.status_code == 401 or response.status_code == 403: + typer.secho( + f"Invalid OpenAI API key, update your config file: {SHELL_GPT_CONFIG_PATH}", + fg="red", + ) response.raise_for_status() # TODO: Optimise. # https://github.com/openai/openai-python/blob/237448dc072a2c062698da3f9f512fae38300c1c/openai/api_requestor.py#L98