2023-09-03 11:26:26 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-09-05 18:27:24 +03:00
|
|
|
from curl_cffi.requests import AsyncSession
|
|
|
|
import uuid
|
2023-08-28 02:43:45 +03:00
|
|
|
import json
|
|
|
|
|
2023-09-05 18:27:24 +03:00
|
|
|
from .base_provider import AsyncProvider, get_cookies, format_prompt
|
2023-09-03 11:26:26 +03:00
|
|
|
from ..typing import AsyncGenerator
|
|
|
|
|
2023-08-25 07:41:32 +03:00
|
|
|
|
2023-09-05 18:27:24 +03:00
|
|
|
class OpenaiChat(AsyncProvider):
|
2023-08-27 18:37:44 +03:00
|
|
|
url = "https://chat.openai.com"
|
|
|
|
needs_auth = True
|
2023-09-05 18:27:24 +03:00
|
|
|
working = True
|
2023-08-25 07:41:32 +03:00
|
|
|
supports_gpt_35_turbo = True
|
2023-08-28 02:43:45 +03:00
|
|
|
_access_token = None
|
2023-08-25 07:41:32 +03:00
|
|
|
|
|
|
|
@classmethod
|
2023-09-05 18:27:24 +03:00
|
|
|
async def create_async(
|
2023-08-25 07:41:32 +03:00
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: list[dict[str, str]],
|
|
|
|
proxy: str = None,
|
2023-09-05 18:27:24 +03:00
|
|
|
access_token: str = None,
|
2023-08-25 07:41:32 +03:00
|
|
|
cookies: dict = None,
|
2023-08-28 02:43:45 +03:00
|
|
|
**kwargs: dict
|
2023-08-25 07:41:32 +03:00
|
|
|
) -> AsyncGenerator:
|
2023-09-05 18:27:24 +03:00
|
|
|
proxies = None
|
2023-08-25 07:41:32 +03:00
|
|
|
if proxy:
|
|
|
|
if "://" not in proxy:
|
|
|
|
proxy = f"http://{proxy}"
|
2023-09-05 18:27:24 +03:00
|
|
|
proxies = {
|
|
|
|
"http": proxy,
|
|
|
|
"https": proxy
|
|
|
|
}
|
|
|
|
if not access_token:
|
|
|
|
access_token = await cls.get_access_token(cookies)
|
|
|
|
headers = {
|
|
|
|
"Accept": "text/event-stream",
|
|
|
|
"Authorization": f"Bearer {access_token}",
|
|
|
|
}
|
|
|
|
async with AsyncSession(proxies=proxies, headers=headers, impersonate="chrome107") as session:
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
response = await session.post("https://chat.openai.com/backend-api/conversation", json=data)
|
|
|
|
response.raise_for_status()
|
|
|
|
last_message = None
|
|
|
|
for line in response.content.decode().splitlines():
|
|
|
|
if line.startswith("data: "):
|
|
|
|
line = line[6:]
|
|
|
|
if line != "[DONE]":
|
|
|
|
line = json.loads(line)
|
|
|
|
if "message" in line:
|
|
|
|
last_message = line["message"]["content"]["parts"][0]
|
|
|
|
return last_message
|
2023-08-25 07:41:32 +03:00
|
|
|
|
|
|
|
|
2023-09-05 18:27:24 +03:00
|
|
|
@classmethod
|
|
|
|
async def get_access_token(cls, cookies: dict = None, proxies: dict = None):
|
|
|
|
if not cls._access_token:
|
2023-08-28 02:43:45 +03:00
|
|
|
cookies = cookies if cookies else get_cookies("chat.openai.com")
|
2023-09-05 18:27:24 +03:00
|
|
|
async with AsyncSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session:
|
|
|
|
response = await session.get("https://chat.openai.com/api/auth/session")
|
|
|
|
response.raise_for_status()
|
|
|
|
cls._access_token = response.json()["accessToken"]
|
|
|
|
return cls._access_token
|
2023-08-28 02:43:45 +03:00
|
|
|
|
2023-08-25 07:41:32 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@property
|
|
|
|
def params(cls):
|
|
|
|
params = [
|
|
|
|
("model", "str"),
|
|
|
|
("messages", "list[dict[str, str]]"),
|
|
|
|
("stream", "bool"),
|
|
|
|
("proxy", "str"),
|
2023-09-05 18:27:24 +03:00
|
|
|
("access_token", "str"),
|
|
|
|
("cookies", "dict[str, str]")
|
2023-08-25 07:41:32 +03:00
|
|
|
]
|
|
|
|
param = ", ".join([": ".join(p) for p in params])
|
2023-09-05 18:27:24 +03:00
|
|
|
return f"g4f.provider.{cls.__name__} supports: ({param})"
|