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

Added keyring usage #22

Merged
merged 12 commits into from
Sep 16, 2024
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,9 @@ pip install git+http://github.com/lnxpy/hey.git
</details>

<details>
<summary><h4>2. Set the <code>HEY_TOKEN</code> environment variable</h4></summary>
<summary><h4>2. Set the <code>HEY_TOKEN</code></h4></summary>

Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into either the `.bashrc` (or `.zshrc`) file.

- If you use the default bash shell
```sh
echo "export HEY_TOKEN=<TOKEN>" >> ~/.bashrc
```
- If you use ZSH
```sh
echo "export HEY_TOKEN=<TOKEN>" >> ~/.zshrc
```
Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into hey, use `hey token [HEY_TOKEN]` to add your token.

</details>

Expand Down
10 changes: 10 additions & 0 deletions hey/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Annotated, Optional

import typer
import keyring
from rich.markdown import Markdown
from rich.panel import Panel

Expand Down Expand Up @@ -74,3 +75,12 @@ def ask(

result = answer(user_input, no_style)
console.print(result)

@app.command()
def token(
sepehrrasooli marked this conversation as resolved.
Show resolved Hide resolved
user_input: str,
):
"""
Take HEY_TOKEN from user.
"""
keyring.set_password("system","HEY_TOKEN",user_input)
10 changes: 5 additions & 5 deletions hey/openai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import time
import keyring

from openai import OpenAI, OpenAIError
from rich.console import Console
Expand All @@ -16,14 +17,13 @@ def __init__(self) -> None:
self.is_valid = False

def validate(self) -> str:
token = os.environ.get("HEY_TOKEN", None)
token = keyring.get_password("system","HEY_TOKEN")
if token:
self.is_valid = True
return token
else:
raise TokenIsNotDefined(
"make sure the `HEY_TOKEN` is defined in the .bashrc/.zshrc file."
)
raise TokenIsNotDefined(
"Token is not defined, Use `hey token [token]` to set token."
sepehrrasooli marked this conversation as resolved.
Show resolved Hide resolved
)


class ChatGPT:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ dependencies = [
"typer >= 0.12.3",
"rich >= 13.7.1",
"openai >= 1.30.1",
"platformdirs >= 4.2.2"
"platformdirs >= 4.2.2",
"keyring >= 25.3.0"
]
readme = { file = "README.md", content-type = "text/markdown" }
license = { file = "LICENSE" }
Expand Down