|
1 | 1 | import os
|
2 | 2 | import sys
|
3 |
| -import webbrowser |
4 | 3 |
|
5 |
| -import pyperclip |
6 | 4 | import rich
|
7 | 5 | import typer
|
8 | 6 | from rich.console import Console
|
9 | 7 | from yt_dlp.cookies import SUPPORTED_BROWSERS
|
10 | 8 |
|
11 | 9 | from aishell.adapters.openai_cookie_adapter import OpenAICookieAdapter
|
12 |
| -from aishell.exceptions import UnauthorizedAccessError |
| 10 | +from aishell.models import RevChatGPTChatbotConfigModel |
| 11 | +from aishell.models.aishell_config_model import AiShellConfigModel |
13 | 12 | from aishell.models.language_model import LanguageModel
|
14 | 13 | from aishell.query_clients import GPT3Client, OfficialChatGPTClient, QueryClient, ReverseEngineeredChatGPTClient
|
| 14 | +from aishell.utils import AiShellConfigManager |
15 | 15 |
|
16 | 16 | cli_app = typer.Typer()
|
17 | 17 |
|
18 | 18 |
|
19 |
| -def _open_chatgpt_browser(): |
20 |
| - CHATGPT_LOGIN_URL = 'https://chat.openai.com/auth/login?next=/chat' |
21 |
| - webbrowser.open(CHATGPT_LOGIN_URL) |
| 19 | +def config_aishell(): |
| 20 | + rich.print('''Hi! 🙌 I am [bold blue]AiShell[/bold blue], [yellow]your powerful terminal assistant[/yellow] 🔥 |
| 21 | +I am here to assist you with configuring AiShell. 💪 |
22 | 22 |
|
| 23 | +Please make sure that you have logged into chat.openai.com on your browser before we continue. 🗝️ |
23 | 24 |
|
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 |
| - ) |
| 25 | +''') |
| 26 | + typer.confirm('Are you ready to proceed? 🚀', abort=True) |
| 27 | + |
| 28 | + rich.print(f'''Which browser did you use to log in to chat.openai.com? |
| 29 | +
|
| 30 | +We support the following browsers: [{SUPPORTED_BROWSERS}]''') |
| 31 | + browser_name = typer.prompt('Please enter your choice here: ') |
| 32 | + if browser_name not in SUPPORTED_BROWSERS: |
| 33 | + rich.print(f'Browser {browser_name} is not supported. Supported browsers are: {SUPPORTED_BROWSERS}') |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | + adapter = OpenAICookieAdapter(browser_name) |
| 37 | + session_token = adapter.get_openai_session_token() |
| 38 | + if not session_token: |
| 39 | + rich.print('Failed to get session token. 😓 Can you check if you are logged in to https://chat.openai.com?') |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + is_paid = typer.confirm("It's my last question! 🤩 Are you a PLUS user?") |
| 43 | + |
| 44 | + chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token, paid=is_paid) |
| 45 | + aishell_config = AiShellConfigModel(chatgpt_config=chatgpt_config) |
| 46 | + config_manager = AiShellConfigManager(config_model=aishell_config) |
| 47 | + config_manager.save_config() |
| 48 | + |
| 49 | + rich.print(f'''[green bold]Excellent![/green bold] You are now ready to use [bold blue]AiShell[/bold blue] 🚀 |
| 50 | +
|
| 51 | +Enjoy your AI powered terminal assistant! 🎉 |
| 52 | +
|
| 53 | +[dim]To check your settings file, it's at: {config_manager.config_path}[/dim] |
| 54 | +
|
| 55 | +''') |
| 56 | + return config_manager |
31 | 57 |
|
32 | 58 |
|
33 | 59 | @cli_app.command()
|
34 | 60 | def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENGINEERED_CHATGPT):
|
| 61 | + is_config_file_available = AiShellConfigManager.is_config_file_available(AiShellConfigManager.DEFAULT_CONFIG_PATH) |
| 62 | + config_manager: AiShellConfigManager |
| 63 | + if is_config_file_available: |
| 64 | + config_manager = AiShellConfigManager(load_config=True) |
| 65 | + else: |
| 66 | + config_manager = config_aishell() |
| 67 | + |
35 | 68 | query_client: QueryClient
|
36 |
| - if language_model == LanguageModel.GPT3: |
| 69 | + if language_model == LanguageModel.REVERSE_ENGINEERED_CHATGPT: |
| 70 | + query_client = ReverseEngineeredChatGPTClient(config=config_manager.config_model.chatgpt_config) |
| 71 | + elif language_model == LanguageModel.GPT3: |
37 | 72 | query_client = GPT3Client()
|
38 | 73 | elif language_model == LanguageModel.OFFICIAL_CHATGPT:
|
39 | 74 | query_client = OfficialChatGPTClient()
|
40 |
| - elif language_model == LanguageModel.REVERSE_ENGINEERED_CHATGPT: |
41 |
| - try: |
42 |
| - query_client = ReverseEngineeredChatGPTClient() |
43 |
| - except UnauthorizedAccessError: |
44 |
| - print('You are not logged in to OpenAI, attempting to log you in...') |
45 |
| - _open_chatgpt_browser() |
46 |
| - BROWSER_NAME = typer.prompt(f'Which browser did you use to log in? [{SUPPORTED_BROWSERS}]') |
47 |
| - adapter = OpenAICookieAdapter(BROWSER_NAME) |
48 |
| - session_token = adapter.get_openai_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() |
56 | 75 |
|
57 | 76 | query_client.query(question)
|
58 | 77 |
|
|
0 commit comments