Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created --reset-key flag to update OpenAI key #395

Merged
merged 6 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sgpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,19 @@ def main(
callback=install_shell_integration,
hidden=True, # Hiding since should be used only once.
),
reset_key : bool = typer.Option(
False,
"--reset-key",
help="Resets OpenAI API key in config file. Note: the OPENAI_API_KEY environment variable will still be prioritized over the key in config file.",
),
) -> None:
if reset_key:
if cfg.reset_OPENAI_API_key():
typer.secho("Successfully reset OpenAI API key", fg="green")
else:
typer.secho("Reset OpenAI API key in config file. However, the existing OPENAI_API_KEY environement variable will still be used.", fg="yellow")
return

stdin_passed = not sys.stdin.isatty()

if stdin_passed and not repl:
Expand Down
11 changes: 10 additions & 1 deletion sgpt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict, Generator, List

import requests
import typer

from .cache import Cache
from .config import cfg
Expand Down Expand Up @@ -58,7 +59,15 @@ def _request(
timeout=REQUEST_TIMEOUT,
stream=stream,
)
response.raise_for_status()
try:
TheR1D marked this conversation as resolved.
Show resolved Hide resolved
response.raise_for_status()
except requests.exceptions.HTTPError as http_err:
# Check if the status code is 403 (Forbidden) or 401 (Unauthorized)
if http_err.response.status_code == 403 or http_err.response.status_code == 401:
typer.secho("Invalid OpenAI API key.", fg="red")
TheR1D marked this conversation as resolved.
Show resolved Hide resolved
else:
typer.secho(f"HTTPError occurred: {http_err}", fg="red")
TheR1D marked this conversation as resolved.
Show resolved Hide resolved
raise typer.Exit(code=1)
# TODO: Optimise.
# https://github.com/openai/openai-python/blob/237448dc072a2c062698da3f9f512fae38300c1c/openai/api_requestor.py#L98
if not stream:
Expand Down
9 changes: 9 additions & 0 deletions sgpt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,14 @@ def get(self, key: str) -> str: # type: ignore
raise UsageError(f"Missing config key: {key}")
return value

def reset_OPENAI_API_key(self):
__api_key = getpass(prompt="Please enter your new OpenAI API key: ")
self["OPENAI_API_KEY"] = __api_key
self._write()
#if an env variable exists it will still be used
if os.getenv("OPENAI_API_KEY"):
return False
else:
return True

cfg = Config(SHELL_GPT_CONFIG_PATH, **DEFAULT_CONFIG)