2023-09-26 11:03:37 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-21 02:52:19 +03:00
|
|
|
import asyncio
|
|
|
|
import webbrowser
|
2023-11-18 06:38:31 +03:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
import secrets
|
2023-12-06 11:35:36 +03:00
|
|
|
import os
|
2024-01-01 04:09:06 +03:00
|
|
|
from os import path
|
2024-01-01 19:48:57 +03:00
|
|
|
from asyncio import AbstractEventLoop, BaseEventLoop
|
2024-01-01 04:09:06 +03:00
|
|
|
from platformdirs import user_config_dir
|
2023-11-16 18:56:23 +03:00
|
|
|
from browser_cookie3 import (
|
|
|
|
chrome,
|
|
|
|
chromium,
|
|
|
|
opera,
|
|
|
|
opera_gx,
|
|
|
|
brave,
|
|
|
|
edge,
|
|
|
|
vivaldi,
|
|
|
|
firefox,
|
2023-12-06 11:35:36 +03:00
|
|
|
_LinuxPasswordManager
|
2023-11-16 18:56:23 +03:00
|
|
|
)
|
2023-10-22 21:01:14 +03:00
|
|
|
|
2023-11-20 16:00:40 +03:00
|
|
|
from ..typing import Dict, Messages
|
2023-10-23 00:53:18 +03:00
|
|
|
from .. import debug
|
2023-09-26 11:03:37 +03:00
|
|
|
|
2023-10-01 21:29:57 +03:00
|
|
|
# Local Cookie Storage
|
2023-10-07 20:00:45 +03:00
|
|
|
_cookies: Dict[str, Dict[str, str]] = {}
|
2023-09-26 11:03:37 +03:00
|
|
|
|
2024-01-01 04:09:06 +03:00
|
|
|
# If loop closed or not set, create new event loop.
|
|
|
|
# If event loop is already running, handle nested event loops.
|
2023-09-26 11:03:37 +03:00
|
|
|
# If "nest_asyncio" is installed, patch the event loop.
|
|
|
|
def get_event_loop() -> AbstractEventLoop:
|
|
|
|
try:
|
2024-01-01 04:09:06 +03:00
|
|
|
loop = asyncio.get_event_loop()
|
2024-01-01 19:48:57 +03:00
|
|
|
if isinstance(loop, BaseEventLoop):
|
|
|
|
loop._check_closed()
|
2023-09-26 11:03:37 +03:00
|
|
|
except RuntimeError:
|
2024-01-01 04:09:06 +03:00
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
2023-09-26 11:03:37 +03:00
|
|
|
try:
|
2024-01-01 04:09:06 +03:00
|
|
|
# Is running event loop
|
|
|
|
asyncio.get_running_loop()
|
|
|
|
if not hasattr(loop.__class__, "_nest_patched"):
|
2023-09-26 11:03:37 +03:00
|
|
|
import nest_asyncio
|
2024-01-01 04:09:06 +03:00
|
|
|
nest_asyncio.apply(loop)
|
|
|
|
except RuntimeError:
|
|
|
|
# No running event loop
|
|
|
|
pass
|
2023-09-26 11:03:37 +03:00
|
|
|
except ImportError:
|
|
|
|
raise RuntimeError(
|
2023-10-07 20:00:45 +03:00
|
|
|
'Use "create_async" instead of "create" function in a running event loop. Or install the "nest_asyncio" package.'
|
|
|
|
)
|
2024-01-01 04:09:06 +03:00
|
|
|
return loop
|
2023-10-07 20:00:45 +03:00
|
|
|
|
2023-10-21 02:52:19 +03: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 11:03:37 +03:00
|
|
|
|
2023-12-06 11:35:36 +03:00
|
|
|
# Check for broken dbus address in docker image
|
|
|
|
if os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null":
|
|
|
|
_LinuxPasswordManager.get_password = lambda a, b: b"secret"
|
|
|
|
|
2023-10-07 20:00:45 +03:00
|
|
|
# Load cookies for a domain from all supported browsers.
|
|
|
|
# Cache the results in the "_cookies" variable.
|
2023-10-21 02:52:19 +03:00
|
|
|
def get_cookies(domain_name=''):
|
2023-10-23 00:53:18 +03:00
|
|
|
if domain_name in _cookies:
|
|
|
|
return _cookies[domain_name]
|
2023-10-22 21:01:14 +03:00
|
|
|
def g4f(domain_name):
|
|
|
|
user_data_dir = user_config_dir("g4f")
|
|
|
|
cookie_file = path.join(user_data_dir, "Default", "Cookies")
|
2023-10-23 10:46:25 +03:00
|
|
|
return [] if not path.exists(cookie_file) else chrome(cookie_file, domain_name)
|
|
|
|
|
2023-10-23 00:53:18 +03:00
|
|
|
cookies = {}
|
2023-10-22 21:01:14 +03:00
|
|
|
for cookie_fn in [g4f, chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]:
|
2023-09-26 11:03:37 +03:00
|
|
|
try:
|
2023-10-22 21:01:14 +03:00
|
|
|
cookie_jar = cookie_fn(domain_name=domain_name)
|
2023-10-23 00:53:18 +03: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
|
2023-12-06 11:35:36 +03:00
|
|
|
except:
|
2023-09-26 11:03:37 +03:00
|
|
|
pass
|
2023-10-23 00:53:18 +03:00
|
|
|
_cookies[domain_name] = cookies
|
2023-10-21 02:52:19 +03:00
|
|
|
return _cookies[domain_name]
|
2023-09-26 11:03:37 +03:00
|
|
|
|
|
|
|
|
2023-10-10 10:49:29 +03:00
|
|
|
def format_prompt(messages: Messages, add_special_tokens=False) -> str:
|
2023-10-23 10:46:25 +03:00
|
|
|
if not add_special_tokens and len(messages) <= 1:
|
2023-10-03 23:12:56 +03:00
|
|
|
return messages[0]["content"]
|
2023-11-18 06:38:31 +03:00
|
|
|
formatted = "\n".join([
|
|
|
|
f'{message["role"].capitalize()}: {message["content"]}'
|
|
|
|
for message in messages
|
|
|
|
])
|
2023-10-23 10:46:25 +03:00
|
|
|
return f"{formatted}\nAssistant:"
|
2023-10-07 20:00:45 +03:00
|
|
|
|
2023-10-03 23:12:56 +03:00
|
|
|
|
2023-11-18 06:38:31 +03:00
|
|
|
def get_random_string(length: int = 10) -> str:
|
|
|
|
return ''.join(
|
|
|
|
random.choice(string.ascii_lowercase + string.digits)
|
|
|
|
for _ in range(length)
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_random_hex() -> str:
|
|
|
|
return secrets.token_hex(16).zfill(32)
|