2023-09-03 04:26:26 -04:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-03 21:15:17 -04:00
|
|
|
import uuid, json, time
|
2023-08-27 19:43:45 -04:00
|
|
|
|
2023-10-04 01:20:51 -04:00
|
|
|
from ..base_provider import AsyncGeneratorProvider
|
|
|
|
from ..helper import get_browser, get_cookies, format_prompt
|
|
|
|
from ...typing import AsyncGenerator
|
|
|
|
from ...requests import StreamSession
|
2023-09-03 04:26:26 -04:00
|
|
|
|
2023-10-03 16:12:56 -04:00
|
|
|
class OpenaiChat(AsyncGeneratorProvider):
|
2023-08-27 11:37:44 -04:00
|
|
|
url = "https://chat.openai.com"
|
|
|
|
needs_auth = True
|
2023-09-05 11:27:24 -04:00
|
|
|
working = True
|
2023-08-25 00:41:32 -04:00
|
|
|
supports_gpt_35_turbo = True
|
2023-08-27 19:43:45 -04:00
|
|
|
_access_token = None
|
2023-08-25 00:41:32 -04:00
|
|
|
|
|
|
|
@classmethod
|
2023-10-03 16:12:56 -04:00
|
|
|
async def create_async_generator(
|
2023-08-25 00:41:32 -04:00
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: list[dict[str, str]],
|
|
|
|
proxy: str = None,
|
2023-09-05 11:27:24 -04:00
|
|
|
access_token: str = None,
|
2023-08-25 00:41:32 -04:00
|
|
|
cookies: dict = None,
|
2023-10-04 23:13:37 -04:00
|
|
|
timeout: int = 30,
|
2023-08-27 19:43:45 -04:00
|
|
|
**kwargs: dict
|
2023-08-25 00:41:32 -04:00
|
|
|
) -> AsyncGenerator:
|
2023-09-25 09:52:19 -04:00
|
|
|
proxies = {"https": proxy}
|
2023-09-05 11:27:24 -04:00
|
|
|
if not access_token:
|
2023-09-09 17:07:00 -04:00
|
|
|
access_token = await cls.get_access_token(cookies, proxies)
|
2023-09-05 11:27:24 -04:00
|
|
|
headers = {
|
|
|
|
"Accept": "text/event-stream",
|
|
|
|
"Authorization": f"Bearer {access_token}",
|
|
|
|
}
|
2023-10-04 23:13:37 -04:00
|
|
|
async with StreamSession(proxies=proxies, headers=headers, impersonate="chrome107", timeout=timeout) as session:
|
2023-09-05 11:27:24 -04:00
|
|
|
messages = [
|
|
|
|
{
|
|
|
|
"id": str(uuid.uuid4()),
|
|
|
|
"author": {"role": "user"},
|
|
|
|
"content": {"content_type": "text", "parts": [format_prompt(messages)]},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
data = {
|
|
|
|
"action": "next",
|
|
|
|
"messages": messages,
|
|
|
|
"conversation_id": None,
|
|
|
|
"parent_message_id": str(uuid.uuid4()),
|
|
|
|
"model": "text-davinci-002-render-sha",
|
|
|
|
"history_and_training_disabled": True,
|
|
|
|
}
|
2023-10-03 16:12:56 -04:00
|
|
|
async with session.post(f"{cls.url}/backend-api/conversation", json=data) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
last_message = ""
|
|
|
|
async for line in response.iter_lines():
|
|
|
|
if line.startswith(b"data: "):
|
|
|
|
line = line[6:]
|
|
|
|
if line == b"[DONE]":
|
|
|
|
break
|
2023-10-03 21:15:17 -04:00
|
|
|
try:
|
|
|
|
line = json.loads(line)
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
if "message" not in line or "message_type" not in line["message"]["metadata"]:
|
|
|
|
continue
|
|
|
|
if line["message"]["metadata"]["message_type"] == "next":
|
2023-10-03 16:12:56 -04:00
|
|
|
new_message = line["message"]["content"]["parts"][0]
|
|
|
|
yield new_message[len(last_message):]
|
|
|
|
last_message = new_message
|
|
|
|
|
|
|
|
@classmethod
|
2023-10-03 17:53:17 -04:00
|
|
|
def browse_access_token(cls) -> str:
|
2023-10-03 16:12:56 -04:00
|
|
|
try:
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
2023-08-25 00:41:32 -04:00
|
|
|
|
2023-10-03 21:15:17 -04:00
|
|
|
driver = get_browser()
|
|
|
|
except ImportError:
|
2023-10-03 16:12:56 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
driver.get(f"{cls.url}/")
|
|
|
|
try:
|
|
|
|
WebDriverWait(driver, 1200).until(
|
|
|
|
EC.presence_of_element_located((By.ID, "prompt-textarea"))
|
|
|
|
)
|
|
|
|
javascript = "return (await (await fetch('/api/auth/session')).json())['accessToken']"
|
|
|
|
return driver.execute_script(javascript)
|
|
|
|
finally:
|
2023-10-03 21:15:17 -04:00
|
|
|
time.sleep(1)
|
2023-10-03 16:12:56 -04:00
|
|
|
driver.quit()
|
2023-08-25 00:41:32 -04:00
|
|
|
|
2023-10-03 17:53:17 -04:00
|
|
|
@classmethod
|
|
|
|
async def fetch_access_token(cls, cookies: dict, proxies: dict = None) -> str:
|
|
|
|
async with StreamSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session:
|
|
|
|
async with session.get(f"{cls.url}/api/auth/session") as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
auth = await response.json()
|
|
|
|
if "accessToken" in auth:
|
|
|
|
return auth["accessToken"]
|
|
|
|
|
2023-09-05 11:27:24 -04:00
|
|
|
@classmethod
|
2023-09-25 09:52:19 -04:00
|
|
|
async def get_access_token(cls, cookies: dict = None, proxies: dict = None) -> str:
|
2023-09-05 11:27:24 -04:00
|
|
|
if not cls._access_token:
|
2023-08-27 19:43:45 -04:00
|
|
|
cookies = cookies if cookies else get_cookies("chat.openai.com")
|
2023-10-03 17:53:17 -04:00
|
|
|
if cookies:
|
|
|
|
cls._access_token = await cls.fetch_access_token(cookies, proxies)
|
|
|
|
if not cls._access_token:
|
|
|
|
cls._access_token = cls.browse_access_token()
|
|
|
|
if not cls._access_token:
|
|
|
|
raise RuntimeError("Read access token failed")
|
2023-09-05 11:27:24 -04:00
|
|
|
return cls._access_token
|
2023-08-27 19:43:45 -04:00
|
|
|
|
2023-08-25 00:41:32 -04:00
|
|
|
@classmethod
|
|
|
|
@property
|
|
|
|
def params(cls):
|
|
|
|
params = [
|
|
|
|
("model", "str"),
|
|
|
|
("messages", "list[dict[str, str]]"),
|
|
|
|
("stream", "bool"),
|
|
|
|
("proxy", "str"),
|
2023-09-05 11:27:24 -04:00
|
|
|
("access_token", "str"),
|
|
|
|
("cookies", "dict[str, str]")
|
2023-08-25 00:41:32 -04:00
|
|
|
]
|
|
|
|
param = ", ".join([": ".join(p) for p in params])
|
2023-09-17 17:23:54 -04:00
|
|
|
return f"g4f.provider.{cls.__name__} supports: ({param})"
|