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

Hide API key from Rich tracebacks #221

Merged
merged 3 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
]
Expand Down
4 changes: 1 addition & 3 deletions sgpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 10 additions & 6 deletions sgpt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class OpenAIClient:
cache = Cache(CACHE_LENGTH, CACHE_PATH)

def __init__(self, api_host: str, api_key: str) -> None:
self.api_key = api_key
self.__api_key = api_key
self.api_host = api_host

@cache
Expand All @@ -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": f"Bearer {self.api_key}",
}
data = {
"messages": messages,
"model": model,
Expand All @@ -50,7 +46,15 @@ def _request(
}
endpoint = f"{self.api_host}/v1/chat/completions"
response = requests.post(
endpoint, headers=headers, 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.
Expand Down
4 changes: 2 additions & 2 deletions sgpt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down