Skip to content

Commit 2cacc5a

Browse files
committed
Make cli to ask copy session token or not
1 parent 5751c2a commit 2cacc5a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

aishell/cli.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
2+
import sys
23
import webbrowser
34

5+
import pyperclip
6+
import rich
47
import typer
58
from rich.console import Console
69
from yt_dlp.cookies import SUPPORTED_BROWSERS
@@ -18,6 +21,15 @@ def _open_chatgpt_browser():
1821
webbrowser.open(CHATGPT_LOGIN_URL)
1922

2023

24+
def _ask_user_copy_session_token_to_clipboard(session_token: str) -> None:
25+
copy_session_token = typer.confirm('Do you want to copy the session token to your clipboard?')
26+
if copy_session_token:
27+
pyperclip.copy(session_token)
28+
rich.print(
29+
'Session token copied to clipboard. [bold]`export CHATGPT_SESSION_TOKEN=<session_token>`[/bold] to set it.'
30+
)
31+
32+
2133
@cli_app.command()
2234
def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENGINEERED_CHATGPT):
2335
query_client: QueryClient
@@ -34,7 +46,13 @@ def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENG
3446
BROWSER_NAME = typer.prompt(f'Which browser did you use to log in? [{SUPPORTED_BROWSERS}]')
3547
adapter = OpenAICookieAdapter(BROWSER_NAME)
3648
session_token = adapter.get_openai_session_token()
37-
query_client = ReverseEngineeredChatGPTClient(session_token=session_token)
49+
if session_token is not None:
50+
os.environ['CHATGPT_SESSION_TOKEN'] = session_token
51+
_ask_user_copy_session_token_to_clipboard(session_token)
52+
ask(question, language_model)
53+
else:
54+
print('Failed to log in.')
55+
sys.exit()
3856

3957
query_client.query(question)
4058

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

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

0 commit comments

Comments
 (0)