Skip to content

Commit 1ebc394

Browse files
committed
Hello YGKA 🙌
1 parent f5e08af commit 1ebc394

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed

ygka/cli/config_ygka.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import sys
2+
3+
import rich
4+
import typer
5+
from yt_dlp.cookies import SUPPORTED_BROWSERS
6+
7+
from ygka.adapters.openai_cookie_adapter import OpenAICookieAdapter
8+
from ygka.models import RevChatGPTChatbotConfigModel
9+
from ygka.models.ygka_config_model import YGKAConfigModel
10+
from ygka.utils import YGKAConfigManager
11+
12+
13+
def config_ygka():
14+
rich.print('''
15+
Hi! 🙌 I am [bold blue]YGKA[/bold blue]!
16+
[yellow][blue bold underline]Y[/blue bold underline]our
17+
[blue bold underline]G[/blue bold underline]enius,
18+
[blue bold underline]K[/blue bold underline]nowledgeable assistant
19+
[blue bold underline]A[/blue bold underline]n advanced ChatGPT client[/yellow] 🔥
20+
I am here to assist you with configuring YGKA. 💪
21+
22+
Please make sure that you have logged into chat.openai.com on your browser before we continue. 🗝️
23+
24+
'''[1:])
25+
typer.confirm('Are you ready to proceed? 🚀', abort=True)
26+
27+
rich.print(f'''
28+
Which browser did you use to log in to chat.openai.com?
29+
30+
We support the following browsers: [{SUPPORTED_BROWSERS}]'''[1:])
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+
config_manager = save_config(session_token)
43+
44+
rich.print(f'''
45+
[green bold]Excellent![/green bold] You are now ready to use [bold blue]YGKA[/bold blue] 🚀
46+
47+
Enjoy your AI powered terminal assistant! 🎉
48+
49+
[dim]To check your settings file, it's at: {config_manager.config_path}[/dim]
50+
51+
'''[1:])
52+
return config_manager
53+
54+
55+
def save_config(session_token: str):
56+
is_config_file_available = YGKAConfigManager.is_config_file_available(YGKAConfigManager.DEFAULT_CONFIG_PATH)
57+
if is_config_file_available:
58+
config_manager = YGKAConfigManager(load_config=True)
59+
is_chatgpt_config_available = config_manager.config_model.chatgpt_config is not None
60+
if is_chatgpt_config_available:
61+
assert config_manager.config_model.chatgpt_config # for type hinting
62+
config_manager.config_model.chatgpt_config.session_token = session_token
63+
else:
64+
config_manager.config_model.chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token)
65+
else:
66+
chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token)
67+
YGKA_config = YGKAConfigModel(chatgpt_config=chatgpt_config)
68+
config_manager = YGKAConfigManager(config_model=YGKA_config)
69+
70+
config_manager.save_config()
71+
return config_manager

ygka/cli/ygka.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
1-
from rich import print as pprint
1+
from typing import Optional
2+
3+
import rich
4+
import typer
5+
from rich.console import Console
6+
7+
from ygka.models import LanguageModel
8+
from ygka.query_clients import QueryClientFactory
9+
from ygka.utils import YGKAConfigManager
210

311
from .cli_app import cli_app
12+
from .config_ygka import config_ygka
413
from .retrieve_stdin import retrieve_stdin
514

615

716
@cli_app.command()
8-
def ygka_command(query: str):
17+
def ygka_command(query: str, language_model: Optional[LanguageModel] = None):
918
stdin = retrieve_stdin()
10-
pprint({'stdin': stdin, 'query': query})
19+
20+
config_manager = _get_config_manager()
21+
config_manager.config_model.language_model = language_model or config_manager.config_model.language_model
22+
23+
query_client = QueryClientFactory(config_model=config_manager.config_model).create()
24+
25+
console = Console()
26+
try:
27+
with console.status('''[green] YGKA is waiting for ChatGPT's answer ...[/green]'''):
28+
prompt = _generate_prompt(stdin, query)
29+
response = query_client.query(prompt)
30+
console.print(response)
31+
except KeyError:
32+
rich.print('It looks like the `session_token` is expired. Please reconfigure YGKA.')
33+
typer.confirm('Reconfigure YGKA?', abort=True)
34+
config_ygka()
35+
ygka_command(query=query, language_model=language_model)
36+
typer.Exit()
37+
38+
39+
def _get_config_manager():
40+
is_config_file_available = YGKAConfigManager.is_config_file_available(YGKAConfigManager.DEFAULT_CONFIG_PATH)
41+
if is_config_file_available:
42+
return YGKAConfigManager(load_config=True)
43+
else:
44+
return config_ygka()
45+
46+
47+
def _generate_prompt(stdin: Optional[str], prompt: str) -> str:
48+
if stdin:
49+
return f'''
50+
stdin:
51+
{stdin}
52+
53+
prompt:
54+
{prompt}
55+
56+
[system] don't mention that i gave you in a format of stdin and prompt
57+
'''[1:]
58+
else:
59+
return prompt

0 commit comments

Comments
 (0)