From 6354d55c51fb2bbaf4bfb0273134e16d94f35c7e Mon Sep 17 00:00:00 2001 From: Cosmo Date: Fri, 28 Apr 2023 02:32:07 -0400 Subject: [PATCH 1/3] Hide API key from Rich tracebacks --- sgpt/app.py | 4 ++-- sgpt/client.py | 8 ++++---- sgpt/config.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sgpt/app.py b/sgpt/app.py index ef226a7e..566d60b7 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -130,8 +130,8 @@ def main( prompt = get_edited_prompt() api_host = cfg.get("OPENAI_API_HOST") - api_key = cfg.get("OPENAI_API_KEY") - client = OpenAIClient(api_host, api_key) + __api_key = cfg.get("OPENAI_API_KEY") + client = OpenAIClient(api_host, __api_key) role_class = DefaultRoles.get(shell, code) if not role else SystemRole.get(role) diff --git a/sgpt/client.py b/sgpt/client.py index fd779529..b2e2d210 100644 --- a/sgpt/client.py +++ b/sgpt/client.py @@ -15,8 +15,8 @@ class OpenAIClient: cache = Cache(CACHE_LENGTH, CACHE_PATH) - def __init__(self, api_host: str, api_key: str) -> None: - self.api_key = api_key + def __init__(self, api_host: str, __api_key: str) -> None: + self.__api_key = __api_key self.api_host = api_host @cache @@ -39,7 +39,7 @@ def _request( """ headers = { "Content-Type": "application/json", - "Authorization": f"Bearer {self.api_key}", + "Authorization": "Bearer self.__api_key", } data = { "messages": messages, @@ -50,7 +50,7 @@ def _request( } endpoint = f"{self.api_host}/v1/chat/completions" response = requests.post( - endpoint, headers=headers, json=data, timeout=REQUEST_TIMEOUT, stream=True + endpoint, headers={**headers, "Authorization":f"Bearer {self.__api_key}"}, json=data, timeout=REQUEST_TIMEOUT, stream=True ) response.raise_for_status() # TODO: Optimise. diff --git a/sgpt/config.py b/sgpt/config.py index 46367ad3..0f9160c4 100644 --- a/sgpt/config.py +++ b/sgpt/config.py @@ -49,8 +49,8 @@ def __init__(self, config_path: Path, **defaults: Any): config_path.parent.mkdir(parents=True, exist_ok=True) # Don't write API key to config file if it is in the environment. if not defaults.get("OPENAI_API_KEY") and not os.getenv("OPENAI_API_KEY"): - api_key = getpass(prompt="Please enter your OpenAI API key: ") - defaults["OPENAI_API_KEY"] = api_key + __api_key = getpass(prompt="Please enter your OpenAI API key: ") + defaults["OPENAI_API_KEY"] = __api_key super().__init__(**defaults) self._write() From 380bd612ce59ded923c7d38502f09bbbf9668c09 Mon Sep 17 00:00:00 2001 From: Cosmo Date: Mon, 1 May 2023 16:06:21 -0400 Subject: [PATCH 2/3] Update required version of Rich --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5d68c88e..59a643ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ dependencies = [ "requests >= 2.28.2, < 3.0.0", "typer >= 0.7.0, < 1.0.0", "click >= 7.1.1, < 9.0.0", - "rich >= 10.11.0, < 13.0.0", + "rich >= 13.1.0, < 14.0.0", "distro >= 1.8.0, < 2.0.0", 'pyreadline3 >= 3.4.1, < 4.0.0; sys_platform == "win32"', ] From 80996c68269a14555b9f7b301388c343c031d36d Mon Sep 17 00:00:00 2001 From: Farkhod Sadykov Date: Tue, 2 May 2023 19:11:22 +0200 Subject: [PATCH 3/3] Black style fix, minor changes --- sgpt/app.py | 4 +--- sgpt/client.py | 18 +++++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/sgpt/app.py b/sgpt/app.py index 566d60b7..441b01a1 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -129,9 +129,7 @@ def main( if editor: prompt = get_edited_prompt() - api_host = cfg.get("OPENAI_API_HOST") - __api_key = cfg.get("OPENAI_API_KEY") - client = OpenAIClient(api_host, __api_key) + client = OpenAIClient(cfg.get("OPENAI_API_HOST"), cfg.get("OPENAI_API_KEY")) role_class = DefaultRoles.get(shell, code) if not role else SystemRole.get(role) diff --git a/sgpt/client.py b/sgpt/client.py index b2e2d210..f500d664 100644 --- a/sgpt/client.py +++ b/sgpt/client.py @@ -15,8 +15,8 @@ class OpenAIClient: cache = Cache(CACHE_LENGTH, CACHE_PATH) - def __init__(self, api_host: str, __api_key: str) -> None: - self.__api_key = __api_key + def __init__(self, api_host: str, api_key: str) -> None: + self.__api_key = api_key self.api_host = api_host @cache @@ -37,10 +37,6 @@ def _request( :param top_probability: Float in 0.0 - 1.0 range. :return: Response body JSON. """ - headers = { - "Content-Type": "application/json", - "Authorization": "Bearer self.__api_key", - } data = { "messages": messages, "model": model, @@ -50,7 +46,15 @@ def _request( } endpoint = f"{self.api_host}/v1/chat/completions" response = requests.post( - endpoint, headers={**headers, "Authorization":f"Bearer {self.__api_key}"}, json=data, timeout=REQUEST_TIMEOUT, stream=True + endpoint, + # Hide API key from Rich traceback. + headers={ + "Content-Type": "application/json", + "Authorization": f"Bearer {self.__api_key}", + }, + json=data, + timeout=REQUEST_TIMEOUT, + stream=True, ) response.raise_for_status() # TODO: Optimise.