Skip to content

Commit

Permalink
Merge pull request #30 from code-yeongyu/feature/ask-user-copy
Browse files Browse the repository at this point in the history
  • Loading branch information
code-yeongyu authored Mar 3, 2023
2 parents bad5858 + 2cacc5a commit 5dd2fa8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
24 changes: 21 additions & 3 deletions aishell/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
import sys
import webbrowser

import pyperclip
import rich
import typer
from rich.console import Console
from yt_dlp.cookies import SUPPORTED_BROWSERS
Expand All @@ -18,6 +21,15 @@ def _open_chatgpt_browser():
webbrowser.open(CHATGPT_LOGIN_URL)


def _ask_user_copy_session_token_to_clipboard(session_token: str) -> None:
copy_session_token = typer.confirm('Do you want to copy the session token to your clipboard?')
if copy_session_token:
pyperclip.copy(session_token)
rich.print(
'Session token copied to clipboard. [bold]`export CHATGPT_SESSION_TOKEN=<session_token>`[/bold] to set it.'
)


@cli_app.command()
def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENGINEERED_CHATGPT):
query_client: QueryClient
Expand All @@ -34,7 +46,13 @@ def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENG
BROWSER_NAME = typer.prompt(f'Which browser did you use to log in? [{SUPPORTED_BROWSERS}]')
adapter = OpenAICookieAdapter(BROWSER_NAME)
session_token = adapter.get_openai_session_token()
query_client = ReverseEngineeredChatGPTClient(session_token=session_token)
if session_token is not None:
os.environ['CHATGPT_SESSION_TOKEN'] = session_token
_ask_user_copy_session_token_to_clipboard(session_token)
ask(question, language_model)
else:
print('Failed to log in.')
sys.exit()

query_client.query(question)

Expand All @@ -43,9 +61,9 @@ def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENG
f'''
[green] AiShell is thinking of `{question}` ...[/green]
[italic]AiShell is not responsible for any damage caused by the command executed by the user.[/italic]'''.strip(), ):
[dim]AiShell is not responsible for any damage caused by the command executed by the user.[/dim]'''.strip(), ):
response = query_client.query(question)
console.print(f'[italic]ai$hell: {response}\n')
console.print(f'AiShell: {response}\n')

will_execute = typer.confirm('Execute this command?')

Expand Down
50 changes: 13 additions & 37 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ openai = "^0.26.5"
pydantic = "^1.10.4"
pyright = "^1.1.294"
yt-dlp = "^2023.2.17"
pyperclip = "^1.8.2"

[tool.poetry.scripts]
aishell = "aishell:main"
Expand Down

0 comments on commit 5dd2fa8

Please sign in to comment.