2023-09-26 08:03:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-07 17:00:45 +00:00
|
|
|
import sys
|
2023-10-20 23:52:19 +00:00
|
|
|
import asyncio
|
|
|
|
import webbrowser
|
|
|
|
|
|
|
|
from os import path
|
|
|
|
from asyncio import AbstractEventLoop
|
2023-10-22 18:01:14 +00:00
|
|
|
from platformdirs import user_config_dir
|
|
|
|
|
2023-10-20 23:52:19 +00:00
|
|
|
from ..typing import Dict, Messages
|
|
|
|
from browser_cookie3 import chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox, BrowserCookieError
|
2023-10-22 21:53:18 +00:00
|
|
|
from .. import debug
|
2023-09-26 08:03:37 +00:00
|
|
|
|
2023-10-01 18:29:57 +00:00
|
|
|
# Change event loop policy on windows
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
if isinstance(
|
|
|
|
asyncio.get_event_loop_policy(), asyncio.WindowsProactorEventLoopPolicy
|
|
|
|
):
|
|
|
|
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
|
|
|
|
|
|
|
# Local Cookie Storage
|
2023-10-07 17:00:45 +00:00
|
|
|
_cookies: Dict[str, Dict[str, str]] = {}
|
2023-09-26 08:03:37 +00:00
|
|
|
|
|
|
|
# If event loop is already running, handle nested event loops
|
|
|
|
# If "nest_asyncio" is installed, patch the event loop.
|
|
|
|
def get_event_loop() -> AbstractEventLoop:
|
|
|
|
try:
|
|
|
|
asyncio.get_running_loop()
|
|
|
|
except RuntimeError:
|
2023-10-02 04:47:07 +00:00
|
|
|
try:
|
|
|
|
return asyncio.get_event_loop()
|
|
|
|
except RuntimeError:
|
|
|
|
asyncio.set_event_loop(asyncio.new_event_loop())
|
|
|
|
return asyncio.get_event_loop()
|
2023-09-26 08:03:37 +00:00
|
|
|
try:
|
2023-10-01 04:38:11 +00:00
|
|
|
event_loop = asyncio.get_event_loop()
|
2023-09-26 08:03:37 +00:00
|
|
|
if not hasattr(event_loop.__class__, "_nest_patched"):
|
|
|
|
import nest_asyncio
|
|
|
|
nest_asyncio.apply(event_loop)
|
|
|
|
return event_loop
|
|
|
|
except ImportError:
|
|
|
|
raise RuntimeError(
|
2023-10-07 17:00:45 +00:00
|
|
|
'Use "create_async" instead of "create" function in a running event loop. Or install the "nest_asyncio" package.'
|
|
|
|
)
|
|
|
|
|
2023-10-20 23:52:19 +00:00
|
|
|
def init_cookies():
|
|
|
|
urls = [
|
|
|
|
'https://chat-gpt.org',
|
|
|
|
'https://www.aitianhu.com',
|
|
|
|
'https://chatgptfree.ai',
|
|
|
|
'https://gptchatly.com',
|
|
|
|
'https://bard.google.com',
|
|
|
|
'https://huggingface.co/chat',
|
|
|
|
'https://open-assistant.io/chat'
|
|
|
|
]
|
|
|
|
|
|
|
|
browsers = ['google-chrome', 'chrome', 'firefox', 'safari']
|
|
|
|
|
|
|
|
def open_urls_in_browser(browser):
|
|
|
|
b = webbrowser.get(browser)
|
|
|
|
for url in urls:
|
|
|
|
b.open(url, new=0, autoraise=True)
|
|
|
|
|
|
|
|
for browser in browsers:
|
|
|
|
try:
|
|
|
|
open_urls_in_browser(browser)
|
|
|
|
break
|
|
|
|
except webbrowser.Error:
|
|
|
|
continue
|
2023-09-26 08:03:37 +00:00
|
|
|
|
2023-10-07 17:00:45 +00:00
|
|
|
# Load cookies for a domain from all supported browsers.
|
|
|
|
# Cache the results in the "_cookies" variable.
|
2023-10-20 23:52:19 +00:00
|
|
|
def get_cookies(domain_name=''):
|
2023-10-22 21:53:18 +00:00
|
|
|
if domain_name in _cookies:
|
|
|
|
return _cookies[domain_name]
|
2023-10-22 18:01:14 +00:00
|
|
|
def g4f(domain_name):
|
|
|
|
user_data_dir = user_config_dir("g4f")
|
|
|
|
cookie_file = path.join(user_data_dir, "Default", "Cookies")
|
2023-10-23 07:46:25 +00:00
|
|
|
return [] if not path.exists(cookie_file) else chrome(cookie_file, domain_name)
|
|
|
|
|
2023-10-22 21:53:18 +00:00
|
|
|
cookies = {}
|
2023-10-22 18:01:14 +00:00
|
|
|
for cookie_fn in [g4f, chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]:
|
2023-09-26 08:03:37 +00:00
|
|
|
try:
|
2023-10-22 18:01:14 +00:00
|
|
|
cookie_jar = cookie_fn(domain_name=domain_name)
|
2023-10-22 21:53:18 +00:00
|
|
|
if len(cookie_jar) and debug.logging:
|
|
|
|
print(f"Read cookies from {cookie_fn.__name__} for {domain_name}")
|
|
|
|
for cookie in cookie_jar:
|
|
|
|
if cookie.name not in cookies:
|
|
|
|
cookies[cookie.name] = cookie.value
|
|
|
|
except BrowserCookieError as e:
|
2023-09-26 08:03:37 +00:00
|
|
|
pass
|
2023-10-22 21:53:18 +00:00
|
|
|
_cookies[domain_name] = cookies
|
2023-10-20 23:52:19 +00:00
|
|
|
return _cookies[domain_name]
|
2023-09-26 08:03:37 +00:00
|
|
|
|
|
|
|
|
2023-10-10 07:49:29 +00:00
|
|
|
def format_prompt(messages: Messages, add_special_tokens=False) -> str:
|
2023-10-23 07:46:25 +00:00
|
|
|
if not add_special_tokens and len(messages) <= 1:
|
2023-10-03 20:12:56 +00:00
|
|
|
return messages[0]["content"]
|
2023-10-23 07:46:25 +00:00
|
|
|
formatted = "\n".join(
|
|
|
|
[
|
|
|
|
f'{message["role"].capitalize()}: {message["content"]}'
|
|
|
|
for message in messages
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return f"{formatted}\nAssistant:"
|
2023-10-07 17:00:45 +00:00
|
|
|
|
2023-10-03 20:12:56 +00:00
|
|
|
|
|
|
|
def get_browser(user_data_dir: str = None):
|
2023-10-04 01:15:17 +00:00
|
|
|
from undetected_chromedriver import Chrome
|
2023-10-03 20:12:56 +00:00
|
|
|
|
|
|
|
if not user_data_dir:
|
2023-10-04 01:15:17 +00:00
|
|
|
user_data_dir = user_config_dir("g4f")
|
2023-10-03 20:12:56 +00:00
|
|
|
|
|
|
|
return Chrome(user_data_dir=user_data_dir)
|