Skip to content

Commit 0ab9cf0

Browse files
committed
Seperate cli.py into multiple files
1 parent cd782ab commit 0ab9cf0

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

aishell/cli/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import typer
2+
3+
cli_app = typer.Typer()

aishell/cli/ask.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
3+
import typer
4+
from rich.console import Console
5+
6+
from aishell.models.language_model import LanguageModel
7+
from aishell.query_clients import GPT3Client, OfficialChatGPTClient, QueryClient, ReverseEngineeredChatGPTClient
8+
from aishell.utils import AiShellConfigManager
9+
10+
from . import cli_app
11+
from .config_aishell import config_aishell
12+
13+
14+
@cli_app.command()
15+
def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENGINEERED_CHATGPT):
16+
is_config_file_available = AiShellConfigManager.is_config_file_available(AiShellConfigManager.DEFAULT_CONFIG_PATH)
17+
config_manager: AiShellConfigManager
18+
if is_config_file_available:
19+
config_manager = AiShellConfigManager(load_config=True)
20+
else:
21+
config_manager = config_aishell()
22+
config_manager.config_model.language_model = language_model
23+
configured_language_model = config_manager.config_model.language_model
24+
25+
query_client: QueryClient
26+
if configured_language_model == LanguageModel.REVERSE_ENGINEERED_CHATGPT:
27+
query_client = ReverseEngineeredChatGPTClient(config=config_manager.config_model.chatgpt_config)
28+
elif configured_language_model == LanguageModel.GPT3:
29+
query_client = GPT3Client()
30+
elif configured_language_model == LanguageModel.OFFICIAL_CHATGPT:
31+
query_client = OfficialChatGPTClient()
32+
else:
33+
raise NotImplementedError(f'Language model {configured_language_model} is not implemented yet.')
34+
35+
query_client.query(question)
36+
37+
console = Console()
38+
with console.status(f'''
39+
[green] AiShell is thinking of `{question}` ...[/green]
40+
41+
[dim]AiShell is not responsible for any damage caused by the command executed by the user.[/dim]'''.strip()):
42+
response = query_client.query(question)
43+
console.print(f'AiShell: {response}\n')
44+
45+
will_execute = typer.confirm('Execute this command?')
46+
47+
if will_execute:
48+
os.system(response)
Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import os
21
import sys
32

43
import rich
54
import typer
6-
from rich.console import Console
75
from yt_dlp.cookies import SUPPORTED_BROWSERS
86

97
from aishell.adapters.openai_cookie_adapter import OpenAICookieAdapter
108
from aishell.models import RevChatGPTChatbotConfigModel
119
from aishell.models.aishell_config_model import AiShellConfigModel
12-
from aishell.models.language_model import LanguageModel
13-
from aishell.query_clients import GPT3Client, OfficialChatGPTClient, QueryClient, ReverseEngineeredChatGPTClient
1410
from aishell.utils import AiShellConfigManager
1511

16-
cli_app = typer.Typer()
17-
1812

1913
def config_aishell():
2014
rich.print('''Hi! 🙌 I am [bold blue]AiShell[/bold blue], [yellow]your powerful terminal assistant[/yellow] 🔥
@@ -54,41 +48,3 @@ def config_aishell():
5448
5549
''')
5650
return config_manager
57-
58-
59-
@cli_app.command()
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-
config_manager.config_model.language_model = language_model
68-
configured_language_model = config_manager.config_model.language_model
69-
70-
query_client: QueryClient
71-
if configured_language_model == LanguageModel.REVERSE_ENGINEERED_CHATGPT:
72-
query_client = ReverseEngineeredChatGPTClient(config=config_manager.config_model.chatgpt_config)
73-
elif configured_language_model == LanguageModel.GPT3:
74-
query_client = GPT3Client()
75-
elif configured_language_model == LanguageModel.OFFICIAL_CHATGPT:
76-
query_client = OfficialChatGPTClient()
77-
else:
78-
raise NotImplementedError(f'Language model {configured_language_model} is not implemented yet.')
79-
80-
query_client.query(question)
81-
82-
console = Console()
83-
with console.status(
84-
f'''
85-
[green] AiShell is thinking of `{question}` ...[/green]
86-
87-
[dim]AiShell is not responsible for any damage caused by the command executed by the user.[/dim]'''.strip(), ):
88-
response = query_client.query(question)
89-
console.print(f'AiShell: {response}\n')
90-
91-
will_execute = typer.confirm('Execute this command?')
92-
93-
if will_execute:
94-
os.system(response)

0 commit comments

Comments
 (0)