From 0a6fbe561cc86bf6c7a8dd2ce51c2068917b8ea7 Mon Sep 17 00:00:00 2001 From: Ismail Bennani <48138437+Ismail-Ben@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:37:37 -0500 Subject: [PATCH] Handle API authenticating response (#395) --- sgpt/client.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sgpt/client.py b/sgpt/client.py index 74ec9ee..355b255 100644 --- a/sgpt/client.py +++ b/sgpt/client.py @@ -3,9 +3,10 @@ from pathlib import Path 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 @@ class OpenAIClient: 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