Handle API authenticating response (#395)

This commit is contained in:
Ismail Bennani
2023-12-18 21:37:37 -05:00
committed by GitHub
parent f50d544086
commit 0a6fbe561c
+8 -1
View File
@@ -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