Skip to content

Commit

Permalink
Merge pull request #18 from code-yeongyu/feature/replace-yt-dlp
Browse files Browse the repository at this point in the history
  • Loading branch information
code-yeongyu authored Apr 15, 2023
2 parents e9dce9f + 837ffd1 commit 7857790
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 325 deletions.
315 changes: 76 additions & 239 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typer = { extras = ["all"], version = "^0.7.0" }
rich = "<13.0.0"
revchatgpt = "^4.2.2"
pydantic = "^1.10.7"
yt-dlp = "^2023.3.4"
revchatgptauth = "^2023.4.16"

[tool.poetry.scripts]
ygka = "ygka:main"
Expand Down
1 change: 0 additions & 1 deletion ygka/adapters/__init__.py

This file was deleted.

27 changes: 0 additions & 27 deletions ygka/adapters/openai_cookie_adapter.py

This file was deleted.

17 changes: 0 additions & 17 deletions ygka/adapters/test/test_openai_cookie_adapter.py

This file was deleted.

27 changes: 8 additions & 19 deletions ygka/cli/config_ygka.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import rich
import typer
from yt_dlp.cookies import SUPPORTED_BROWSERS
from revChatGPTAuth import SupportedBrowser

from ygka.adapters.openai_cookie_adapter import OpenAICookieAdapter
from ygka.models import RevChatGPTChatbotConfigModel
from ygka.models.ygka_config_model import YGKAConfigModel
from ygka.utils import YGKAConfigManager


def config_ygka():
SUPPORTED_BROWSERS = [browser.value for browser in SupportedBrowser]
rich.print('''
Hi! 🙌 I am [bold blue]YGKA[/bold blue]!
[yellow][blue bold underline]Y[/blue bold underline]our
Expand All @@ -33,13 +32,7 @@ def config_ygka():
rich.print(f'Browser {browser_name} is not supported. Supported browsers are: {SUPPORTED_BROWSERS}')
sys.exit(1)

adapter = OpenAICookieAdapter(browser_name)
session_token = adapter.get_openai_session_token()
if not session_token:
rich.print('Failed to get session token. 😓 Can you check if you are logged in to https://chat.openai.com?')
sys.exit(1)

config_manager = save_config(session_token)
config_manager = save_config(browser_name)

rich.print(f'''
[green bold]Excellent![/green bold] You are now ready to use [bold blue]YGKA[/bold blue] 🚀
Expand All @@ -52,19 +45,15 @@ def config_ygka():
return config_manager


def save_config(session_token: str):
def save_config(browser_name: str):
config_manager: YGKAConfigManager = YGKAConfigManager()

is_config_file_available = YGKAConfigManager.is_config_file_available(YGKAConfigManager.DEFAULT_CONFIG_PATH)
if is_config_file_available:
config_manager = YGKAConfigManager(load_config=True)
is_chatgpt_config_available = config_manager.config_model.chatgpt_config is not None
if is_chatgpt_config_available:
assert config_manager.config_model.chatgpt_config # for type hinting
config_manager.config_model.chatgpt_config.session_token = session_token
else:
config_manager.config_model.chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token)
config_manager.config_model.browser_name = browser_name
else:
chatgpt_config = RevChatGPTChatbotConfigModel(session_token=session_token)
YGKA_config = YGKAConfigModel(chatgpt_config=chatgpt_config)
YGKA_config = YGKAConfigModel(browser_name=browser_name)
config_manager = YGKAConfigManager(config_model=YGKA_config)

config_manager.save_config()
Expand Down
17 changes: 4 additions & 13 deletions ygka/cli/ygka.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Optional

import rich
import typer
from rich.console import Console

from ygka.models import LanguageModel
Expand All @@ -23,17 +21,10 @@ def ygka_command(query: str, language_model: Optional[LanguageModel] = None):
query_client = QueryClientFactory(config_model=config_manager.config_model).create()

console = Console()
try:
with console.status('''[green] YGKA is waiting for ChatGPT's answer ...[/green]'''):
prompt = _generate_prompt(stdin, query)
response = query_client.query(prompt)
console.print(response)
except KeyError:
rich.print('It looks like the `session_token` is expired. Please reconfigure YGKA.')
typer.confirm('Reconfigure YGKA?', abort=True)
config_ygka()
ygka_command(query=query, language_model=language_model)
typer.Exit()
with console.status('''[green] YGKA is waiting for ChatGPT's answer ...[/green]'''):
prompt = _generate_prompt(stdin, query)
response = query_client.query(prompt)
console.print(response)


def _get_config_manager():
Expand Down
7 changes: 3 additions & 4 deletions ygka/models/ygka_config_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
from pydantic import BaseModel, root_validator

from .language_model import LanguageModel
from .revchatgpt_chatbot_config_model import RevChatGPTChatbotConfigModel


class YGKAConfigModel(BaseModel):
language_model: LanguageModel = LanguageModel.REVERSE_ENGINEERED_CHATGPT
chatgpt_config: Optional[RevChatGPTChatbotConfigModel] = None
browser_name: Optional[str] = None
openai_api_key: Optional[str] = None

@root_validator
def check_required_info_provided(cls, values: dict[str, Optional[str]]):
language_model = values.get('language_model')
if language_model == LanguageModel.REVERSE_ENGINEERED_CHATGPT:
if not values.get('chatgpt_config'):
raise ValueError(f'chatgpt_config should not be none if language_model is {language_model}')
if not values.get('browser_name'):
raise ValueError(f'browser_name should not be none if language_model is {language_model}')
elif language_model == LanguageModel.OFFICIAL_CHATGPT:
if not values.get('openai_api_key'):
raise ValueError(f'openai_api_key should not be none if language_model is {language_model}')
Expand Down
12 changes: 10 additions & 2 deletions ygka/query_clients/query_client_factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from ygka.models import LanguageModel, YGKAConfigModel
from typing import cast

from revChatGPTAuth import get_access_token

from ygka.models import LanguageModel, RevChatGPTChatbotConfigModel, YGKAConfigModel


class QueryClientFactory:
Expand All @@ -11,7 +15,11 @@ def create(self):
'''Create a query client.'''
if self.config_model.language_model == LanguageModel.REVERSE_ENGINEERED_CHATGPT:
from .reverse_engineered_chatgpt_client import ReverseEngineeredChatGPTClient
return ReverseEngineeredChatGPTClient(config=self.config_model.chatgpt_config)
browser_name: str = \
cast(str, self.config_model.browser_name) # REVERSE_ENGINEERED_CHATGPT means browser_name is not None
access_token = get_access_token(browser_name)
rev_chatgpt_config_model = RevChatGPTChatbotConfigModel(access_token=access_token)
return ReverseEngineeredChatGPTClient(config=rev_chatgpt_config_model)
elif self.config_model.language_model == LanguageModel.OFFICIAL_CHATGPT:
from .official_chatgpt_client import OfficialChatGPTClient
return OfficialChatGPTClient(openai_api_key=self.config_model.openai_api_key)
Expand Down
4 changes: 2 additions & 2 deletions ygka/utils/ygka_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class YGKAConfigManager:
DEFAULT_CONFIG_PATH = os.path.expanduser('~/.ygka_config.json')
DEFAULT_CONFIG_PATH = os.path.expanduser('~/.ygka_openai_config.json')

def __init__(
self,
Expand All @@ -22,7 +22,7 @@ def __init__(
An instance of YGKAConfigManager to use as the configuration.\
If None and load_config is True, loads the configuration from the configuration file.
config_path: Path to the configuration file. \
If None, uses the default path "~/.ygka_config.json".
If None, uses the default path "~/.ygka_openai_config.json".
load_config: If True and config_model is None, loads the configuration from the configuration file.
'''
self.config_path = config_path or YGKAConfigManager.DEFAULT_CONFIG_PATH
Expand Down

0 comments on commit 7857790

Please sign in to comment.