2023-09-03 11:26:26 +03:00
|
|
|
from __future__ import annotations
|
2024-01-26 09:54:13 +03:00
|
|
|
|
2024-11-18 04:53:50 +03:00
|
|
|
import re
|
2024-01-14 09:45:41 +03:00
|
|
|
import asyncio
|
|
|
|
import uuid
|
|
|
|
import json
|
2024-02-28 09:36:43 +03:00
|
|
|
import base64
|
2024-03-08 13:01:38 +03:00
|
|
|
import time
|
2024-11-18 17:41:45 +03:00
|
|
|
import requests
|
2024-12-07 07:06:24 +03:00
|
|
|
import random
|
2024-03-26 08:42:47 +03:00
|
|
|
from copy import copy
|
2023-09-03 11:26:26 +03:00
|
|
|
|
2024-03-14 15:53:57 +03:00
|
|
|
try:
|
2024-11-17 21:51:26 +03:00
|
|
|
import nodriver
|
2024-11-18 04:53:50 +03:00
|
|
|
from nodriver.cdp.network import get_response_body
|
2024-11-17 21:51:26 +03:00
|
|
|
has_nodriver = True
|
2024-03-14 15:53:57 +03:00
|
|
|
except ImportError:
|
2024-11-17 21:51:26 +03:00
|
|
|
has_nodriver = False
|
2024-01-01 03:51:49 +03:00
|
|
|
|
2024-01-23 21:44:48 +03:00
|
|
|
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
2024-04-18 21:18:51 +03:00
|
|
|
from ...typing import AsyncResult, Messages, Cookies, ImageType, AsyncIterator
|
2024-11-17 21:51:26 +03:00
|
|
|
from ...requests.raise_for_status import raise_for_status
|
2024-11-21 16:05:50 +03:00
|
|
|
from ...requests import StreamSession
|
|
|
|
from ...requests import get_nodriver
|
2024-05-19 00:13:57 +03:00
|
|
|
from ...image import ImageResponse, ImageRequest, to_image, to_bytes, is_accepted_format
|
2024-11-29 15:56:11 +03:00
|
|
|
from ...errors import MissingAuthError, NoValidHarFileError
|
2024-11-21 07:00:08 +03:00
|
|
|
from ...providers.response import BaseConversation, FinishReason, SynthesizeData
|
2024-04-21 16:15:55 +03:00
|
|
|
from ..helper import format_cookies
|
2024-11-29 15:56:11 +03:00
|
|
|
from ..openai.har_file import get_request_config
|
2024-11-18 04:53:50 +03:00
|
|
|
from ..openai.har_file import RequestConfig, arkReq, arkose_url, start_url, conversation_url, backend_url, backend_anon_url
|
2024-04-28 23:19:51 +03:00
|
|
|
from ..openai.proofofwork import generate_proof_token
|
2024-12-06 03:43:49 +03:00
|
|
|
from ..openai.new import get_requirements_token, get_config
|
2024-02-23 13:33:38 +03:00
|
|
|
from ... import debug
|
2023-09-03 11:26:26 +03:00
|
|
|
|
2024-04-28 23:19:51 +03:00
|
|
|
DEFAULT_HEADERS = {
|
|
|
|
"accept": "*/*",
|
|
|
|
"accept-encoding": "gzip, deflate, br, zstd",
|
2024-12-06 03:43:49 +03:00
|
|
|
'accept-language': 'en-US,en;q=0.8',
|
2024-05-15 03:27:51 +03:00
|
|
|
"referer": "https://chatgpt.com/",
|
2024-04-28 23:19:51 +03:00
|
|
|
"sec-ch-ua": "\"Brave\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"",
|
|
|
|
"sec-ch-ua-mobile": "?0",
|
|
|
|
"sec-ch-ua-platform": "\"Windows\"",
|
|
|
|
"sec-fetch-dest": "empty",
|
|
|
|
"sec-fetch-mode": "cors",
|
|
|
|
"sec-fetch-site": "same-origin",
|
|
|
|
"sec-gpc": "1",
|
|
|
|
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
|
|
|
|
}
|
|
|
|
|
2024-12-06 03:43:49 +03:00
|
|
|
INIT_HEADERS = {
|
|
|
|
'accept': '*/*',
|
|
|
|
'accept-language': 'en-US,en;q=0.8',
|
|
|
|
'cache-control': 'no-cache',
|
|
|
|
'pragma': 'no-cache',
|
|
|
|
'priority': 'u=0, i',
|
|
|
|
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
|
|
|
|
'sec-ch-ua-arch': '"arm"',
|
|
|
|
'sec-ch-ua-bitness': '"64"',
|
|
|
|
'sec-ch-ua-mobile': '?0',
|
|
|
|
'sec-ch-ua-model': '""',
|
|
|
|
'sec-ch-ua-platform': '"macOS"',
|
|
|
|
'sec-ch-ua-platform-version': '"14.4.0"',
|
|
|
|
'sec-fetch-dest': 'document',
|
|
|
|
'sec-fetch-mode': 'navigate',
|
|
|
|
'sec-fetch-site': 'none',
|
|
|
|
'sec-fetch-user': '?1',
|
|
|
|
'upgrade-insecure-requests': '1',
|
|
|
|
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
|
|
|
|
}
|
|
|
|
|
2024-01-23 21:44:48 +03:00
|
|
|
class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-01-14 09:45:41 +03:00
|
|
|
"""A class for creating and managing conversations with OpenAI chat service"""
|
2024-03-09 05:10:59 +03:00
|
|
|
|
2024-04-18 21:18:51 +03:00
|
|
|
label = "OpenAI ChatGPT"
|
2024-05-15 03:27:51 +03:00
|
|
|
url = "https://chatgpt.com"
|
2024-01-14 09:45:41 +03:00
|
|
|
working = True
|
|
|
|
supports_gpt_4 = True
|
2024-02-27 01:41:06 +03:00
|
|
|
supports_message_history = True
|
2024-02-28 09:36:43 +03:00
|
|
|
supports_system_message = True
|
2024-11-18 04:53:50 +03:00
|
|
|
default_model = "auto"
|
2024-12-07 07:06:24 +03:00
|
|
|
fallback_models = [default_model, "gpt-4", "gpt-4o", "gpt-4o-mini", "gpt-4o-canmore", "o1-preview", "o1-mini"]
|
2024-11-18 17:41:45 +03:00
|
|
|
vision_models = fallback_models
|
2024-11-22 03:50:48 +03:00
|
|
|
synthesize_content_type = "audio/mpeg"
|
2024-11-17 21:51:26 +03:00
|
|
|
|
2024-02-28 09:36:43 +03:00
|
|
|
_api_key: str = None
|
|
|
|
_headers: dict = None
|
|
|
|
_cookies: Cookies = None
|
2024-03-08 13:01:38 +03:00
|
|
|
_expires: int = None
|
2023-11-19 07:36:04 +03:00
|
|
|
|
2024-11-18 17:41:45 +03:00
|
|
|
@classmethod
|
|
|
|
def get_models(cls):
|
|
|
|
if not cls.models:
|
|
|
|
try:
|
|
|
|
response = requests.get(f"{cls.url}/backend-anon/models")
|
|
|
|
response.raise_for_status()
|
|
|
|
data = response.json()
|
|
|
|
cls.models = [model.get("slug") for model in data.get("models")]
|
|
|
|
except Exception:
|
|
|
|
cls.models = cls.fallback_models
|
|
|
|
return cls.models
|
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
@classmethod
|
2024-01-23 21:44:48 +03:00
|
|
|
async def upload_image(
|
2024-01-13 17:37:36 +03:00
|
|
|
cls,
|
|
|
|
session: StreamSession,
|
|
|
|
headers: dict,
|
2024-02-09 00:02:52 +03:00
|
|
|
image: ImageType,
|
|
|
|
image_name: str = None
|
2024-01-26 09:54:13 +03:00
|
|
|
) -> ImageRequest:
|
2024-01-14 17:35:58 +03:00
|
|
|
"""
|
|
|
|
Upload an image to the service and get the download URL
|
2024-01-14 09:45:41 +03:00
|
|
|
|
|
|
|
Args:
|
|
|
|
session: The StreamSession object to use for requests
|
|
|
|
headers: The headers to include in the requests
|
|
|
|
image: The image to upload, either a PIL Image object or a bytes object
|
|
|
|
|
|
|
|
Returns:
|
2024-01-26 09:54:13 +03:00
|
|
|
An ImageRequest object that contains the download URL, file name, and other data
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
# Convert the image to a PIL Image object and get the extension
|
2024-01-13 17:37:36 +03:00
|
|
|
data_bytes = to_bytes(image)
|
2024-05-19 00:13:57 +03:00
|
|
|
image = to_image(data_bytes)
|
|
|
|
extension = image.format.lower()
|
2024-01-13 17:37:36 +03:00
|
|
|
data = {
|
2024-05-19 00:13:57 +03:00
|
|
|
"file_name": "" if image_name is None else image_name,
|
2024-01-13 17:37:36 +03:00
|
|
|
"file_size": len(data_bytes),
|
|
|
|
"use_case": "multimodal"
|
|
|
|
}
|
2024-01-14 09:45:41 +03:00
|
|
|
# Post the image data to the service and get the image data
|
2024-01-13 17:37:36 +03:00
|
|
|
async with session.post(f"{cls.url}/backend-api/files", json=data, headers=headers) as response:
|
2024-05-19 00:13:57 +03:00
|
|
|
cls._update_request_args(session)
|
2024-11-23 02:17:35 +03:00
|
|
|
await raise_for_status(response, "Create file failed")
|
2024-01-13 17:37:36 +03:00
|
|
|
image_data = {
|
|
|
|
**data,
|
|
|
|
**await response.json(),
|
2024-05-19 00:13:57 +03:00
|
|
|
"mime_type": is_accepted_format(data_bytes),
|
2024-01-13 17:37:36 +03:00
|
|
|
"extension": extension,
|
|
|
|
"height": image.height,
|
|
|
|
"width": image.width
|
|
|
|
}
|
2024-01-14 09:45:41 +03:00
|
|
|
# Put the image bytes to the upload URL and check the status
|
2024-01-13 17:37:36 +03:00
|
|
|
async with session.put(
|
|
|
|
image_data["upload_url"],
|
|
|
|
data=data_bytes,
|
|
|
|
headers={
|
2024-12-06 03:43:49 +03:00
|
|
|
**DEFAULT_HEADERS,
|
2024-01-13 17:37:36 +03:00
|
|
|
"Content-Type": image_data["mime_type"],
|
2024-12-01 00:11:18 +03:00
|
|
|
"x-ms-blob-type": "BlockBlob",
|
|
|
|
"x-ms-version": "2020-04-08",
|
|
|
|
"Origin": "https://chatgpt.com",
|
2024-01-13 17:37:36 +03:00
|
|
|
}
|
|
|
|
) as response:
|
2024-12-01 00:11:18 +03:00
|
|
|
await raise_for_status(response)
|
2024-01-14 09:45:41 +03:00
|
|
|
# Post the file ID to the service and get the download URL
|
2024-01-13 17:37:36 +03:00
|
|
|
async with session.post(
|
|
|
|
f"{cls.url}/backend-api/files/{image_data['file_id']}/uploaded",
|
|
|
|
json={},
|
|
|
|
headers=headers
|
|
|
|
) as response:
|
2024-03-14 15:53:57 +03:00
|
|
|
cls._update_request_args(session)
|
2024-11-23 02:17:35 +03:00
|
|
|
await raise_for_status(response, "Get download url failed")
|
2024-01-29 20:14:46 +03:00
|
|
|
image_data["download_url"] = (await response.json())["download_url"]
|
|
|
|
return ImageRequest(image_data)
|
2024-03-09 05:10:59 +03:00
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
@classmethod
|
2024-11-23 02:17:35 +03:00
|
|
|
def create_messages(cls, messages: Messages, image_request: ImageRequest = None, system_hints: list = None):
|
2024-01-14 17:35:58 +03:00
|
|
|
"""
|
|
|
|
Create a list of messages for the user input
|
2024-01-14 09:45:41 +03:00
|
|
|
|
|
|
|
Args:
|
|
|
|
prompt: The user input as a string
|
|
|
|
image_response: The image response object, if any
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of messages with the user input and the image, if any
|
|
|
|
"""
|
2024-02-27 01:41:06 +03:00
|
|
|
# Create a message object with the user role and the content
|
|
|
|
messages = [{
|
|
|
|
"author": {"role": message["role"]},
|
|
|
|
"content": {"content_type": "text", "parts": [message["content"]]},
|
2024-11-17 21:51:26 +03:00
|
|
|
"id": str(uuid.uuid4()),
|
|
|
|
"create_time": int(time.time()),
|
2024-12-01 00:11:18 +03:00
|
|
|
"metadata": {"serialization_metadata": {"custom_symbol_offsets": []}, "system_hints": system_hints},
|
2024-02-27 01:41:06 +03:00
|
|
|
} for message in messages]
|
|
|
|
|
2024-01-14 09:45:41 +03:00
|
|
|
# Check if there is an image response
|
2024-03-26 08:42:47 +03:00
|
|
|
if image_request is not None:
|
2024-02-27 01:41:06 +03:00
|
|
|
# Change content in last user message
|
|
|
|
messages[-1]["content"] = {
|
2024-01-13 17:37:36 +03:00
|
|
|
"content_type": "multimodal_text",
|
|
|
|
"parts": [{
|
2024-01-29 20:14:46 +03:00
|
|
|
"asset_pointer": f"file-service://{image_request.get('file_id')}",
|
|
|
|
"height": image_request.get("height"),
|
|
|
|
"size_bytes": image_request.get("file_size"),
|
|
|
|
"width": image_request.get("width"),
|
2024-02-27 01:41:06 +03:00
|
|
|
}, messages[-1]["content"]["parts"][0]]
|
2024-01-13 17:37:36 +03:00
|
|
|
}
|
2024-01-14 09:45:41 +03:00
|
|
|
# Add the metadata object with the attachments
|
2024-02-27 01:41:06 +03:00
|
|
|
messages[-1]["metadata"] = {
|
2024-01-13 17:37:36 +03:00
|
|
|
"attachments": [{
|
2024-01-29 20:14:46 +03:00
|
|
|
"height": image_request.get("height"),
|
|
|
|
"id": image_request.get("file_id"),
|
|
|
|
"mimeType": image_request.get("mime_type"),
|
|
|
|
"name": image_request.get("file_name"),
|
|
|
|
"size": image_request.get("file_size"),
|
|
|
|
"width": image_request.get("width"),
|
2024-01-13 17:37:36 +03:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
return messages
|
2024-02-27 01:41:06 +03:00
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
@classmethod
|
2024-11-23 02:17:35 +03:00
|
|
|
async def get_generated_image(cls, session: StreamSession, headers: dict, element: dict, prompt: str = None) -> ImageResponse:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Retrieves the image response based on the message content.
|
|
|
|
|
2024-01-14 17:35:58 +03:00
|
|
|
This method processes the message content to extract image information and retrieves the
|
|
|
|
corresponding image from the backend API. It then returns an ImageResponse object containing
|
|
|
|
the image URL and the prompt used to generate the image.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (StreamSession): The StreamSession object used for making HTTP requests.
|
|
|
|
headers (dict): HTTP headers to be used for the request.
|
|
|
|
line (dict): A dictionary representing the line of response that contains image information.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
ImageResponse: An object containing the image URL and the prompt, or None if no image is found.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
RuntimeError: If there'san error in downloading the image, including issues with the HTTP request or response.
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
2024-11-19 17:26:03 +03:00
|
|
|
try:
|
|
|
|
prompt = element["metadata"]["dalle"]["prompt"]
|
|
|
|
file_id = element["asset_pointer"].split("file-service://", 1)[1]
|
2024-11-23 02:17:35 +03:00
|
|
|
except TypeError:
|
|
|
|
return
|
2024-11-19 17:26:03 +03:00
|
|
|
except Exception as e:
|
|
|
|
raise RuntimeError(f"No Image: {e.__class__.__name__}: {e}")
|
2024-01-14 09:45:41 +03:00
|
|
|
try:
|
|
|
|
async with session.get(f"{cls.url}/backend-api/files/{file_id}/download", headers=headers) as response:
|
2024-03-14 15:53:57 +03:00
|
|
|
cls._update_request_args(session)
|
|
|
|
await raise_for_status(response)
|
2024-01-14 09:45:41 +03:00
|
|
|
download_url = (await response.json())["download_url"]
|
|
|
|
return ImageResponse(download_url, prompt)
|
|
|
|
except Exception as e:
|
|
|
|
raise RuntimeError(f"Error in downloading image: {e}")
|
|
|
|
|
2023-08-25 07:41:32 +03:00
|
|
|
@classmethod
|
2023-10-03 23:12:56 +03:00
|
|
|
async def create_async_generator(
|
2023-08-25 07:41:32 +03:00
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-09 14:33:20 +03:00
|
|
|
messages: Messages,
|
2023-08-25 07:41:32 +03:00
|
|
|
proxy: str = None,
|
2024-04-18 21:18:51 +03:00
|
|
|
timeout: int = 180,
|
2024-01-26 09:54:13 +03:00
|
|
|
cookies: Cookies = None,
|
2023-11-12 12:12:05 +03:00
|
|
|
auto_continue: bool = False,
|
2024-11-20 11:52:38 +03:00
|
|
|
history_disabled: bool = False,
|
2023-11-19 07:36:04 +03:00
|
|
|
action: str = "next",
|
|
|
|
conversation_id: str = None,
|
2024-03-26 08:42:47 +03:00
|
|
|
conversation: Conversation = None,
|
2023-11-19 07:36:04 +03:00
|
|
|
parent_id: str = None,
|
2024-01-13 17:37:36 +03:00
|
|
|
image: ImageType = None,
|
2024-02-28 09:36:43 +03:00
|
|
|
image_name: str = None,
|
2024-03-26 08:42:47 +03:00
|
|
|
return_conversation: bool = False,
|
2024-05-18 16:37:46 +03:00
|
|
|
max_retries: int = 3,
|
2024-11-23 02:17:35 +03:00
|
|
|
web_search: bool = False,
|
2023-10-09 14:33:20 +03:00
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Create an asynchronous generator for the conversation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
model (str): The model name.
|
|
|
|
messages (Messages): The list of previous messages.
|
|
|
|
proxy (str): Proxy to use for requests.
|
|
|
|
timeout (int): Timeout for requests.
|
2024-02-23 13:33:38 +03:00
|
|
|
api_key (str): Access token for authentication.
|
2024-01-14 09:45:41 +03:00
|
|
|
cookies (dict): Cookies to use for authentication.
|
|
|
|
auto_continue (bool): Flag to automatically continue the conversation.
|
|
|
|
history_disabled (bool): Flag to disable history and training.
|
|
|
|
action (str): Type of action ('next', 'continue', 'variant').
|
|
|
|
conversation_id (str): ID of the conversation.
|
2024-01-14 17:35:58 +03:00
|
|
|
parent_id (str): ID of the parent message.
|
2024-01-14 09:45:41 +03:00
|
|
|
image (ImageType): Image to include in the conversation.
|
2024-03-26 08:42:47 +03:00
|
|
|
return_conversation (bool): Flag to include response fields in the output.
|
2024-01-14 09:45:41 +03:00
|
|
|
**kwargs: Additional keyword arguments.
|
|
|
|
|
|
|
|
Yields:
|
|
|
|
AsyncResult: Asynchronous results from the generator.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
RuntimeError: If an error occurs during processing.
|
|
|
|
"""
|
2024-12-06 03:43:49 +03:00
|
|
|
if cls.needs_auth:
|
|
|
|
await cls.login(proxy)
|
2023-11-19 07:36:04 +03:00
|
|
|
async with StreamSession(
|
2024-04-21 23:39:00 +03:00
|
|
|
proxy=proxy,
|
2024-02-23 13:33:38 +03:00
|
|
|
impersonate="chrome",
|
2024-02-27 01:41:06 +03:00
|
|
|
timeout=timeout
|
2023-11-19 07:36:04 +03:00
|
|
|
) as session:
|
2024-12-07 07:06:24 +03:00
|
|
|
image_request = None
|
2024-12-06 03:43:49 +03:00
|
|
|
if not cls.needs_auth:
|
2024-12-07 07:06:24 +03:00
|
|
|
if cls._headers is None:
|
|
|
|
cls._create_request_args(cookies)
|
|
|
|
async with session.get(cls.url, headers=INIT_HEADERS) as response:
|
|
|
|
cls._update_request_args(session)
|
|
|
|
await raise_for_status(response)
|
|
|
|
else:
|
|
|
|
async with session.get(cls.url, headers=cls._headers) as response:
|
|
|
|
cls._update_request_args(session)
|
|
|
|
await raise_for_status(response)
|
|
|
|
try:
|
|
|
|
image_request = await cls.upload_image(session, cls._headers, image, image_name) if image else None
|
|
|
|
except Exception as e:
|
|
|
|
debug.log("OpenaiChat: Upload image failed")
|
|
|
|
debug.log(f"{e.__class__.__name__}: {e}")
|
2024-04-21 23:39:00 +03:00
|
|
|
model = cls.get_model(model)
|
2024-04-05 22:00:35 +03:00
|
|
|
if conversation is None:
|
|
|
|
conversation = Conversation(conversation_id, str(uuid.uuid4()) if parent_id is None else parent_id)
|
|
|
|
else:
|
|
|
|
conversation = copy(conversation)
|
|
|
|
if cls._api_key is None:
|
|
|
|
auto_continue = False
|
|
|
|
conversation.finish_reason = None
|
|
|
|
while conversation.finish_reason is None:
|
2024-05-18 16:37:46 +03:00
|
|
|
async with session.post(
|
|
|
|
f"{cls.url}/backend-anon/sentinel/chat-requirements"
|
|
|
|
if cls._api_key is None else
|
|
|
|
f"{cls.url}/backend-api/sentinel/chat-requirements",
|
2024-11-19 12:23:24 +03:00
|
|
|
json={"p": get_requirements_token(RequestConfig.proof_token) if RequestConfig.proof_token else None},
|
2024-05-18 16:37:46 +03:00
|
|
|
headers=cls._headers
|
|
|
|
) as response:
|
|
|
|
cls._update_request_args(session)
|
|
|
|
await raise_for_status(response)
|
2024-11-17 21:51:26 +03:00
|
|
|
chat_requirements = await response.json()
|
|
|
|
need_turnstile = chat_requirements.get("turnstile", {}).get("required", False)
|
|
|
|
need_arkose = chat_requirements.get("arkose", {}).get("required", False)
|
|
|
|
chat_token = chat_requirements.get("token")
|
2024-05-18 16:37:46 +03:00
|
|
|
|
2024-11-18 04:53:50 +03:00
|
|
|
if need_arkose and RequestConfig.arkose_token is None:
|
|
|
|
await get_request_config(proxy)
|
|
|
|
cls._create_request_args(RequestConfig,cookies, RequestConfig.headers)
|
|
|
|
cls._set_api_key(RequestConfig.access_token)
|
|
|
|
if RequestConfig.arkose_token is None:
|
2024-05-18 16:37:46 +03:00
|
|
|
raise MissingAuthError("No arkose token found in .har file")
|
|
|
|
|
2024-11-17 21:51:26 +03:00
|
|
|
if "proofofwork" in chat_requirements:
|
2024-12-07 07:06:24 +03:00
|
|
|
if RequestConfig.proof_token is None:
|
|
|
|
RequestConfig.proof_token = get_config(cls._headers.get("user-agent"))
|
2024-05-18 16:37:46 +03:00
|
|
|
proofofwork = generate_proof_token(
|
2024-11-17 21:51:26 +03:00
|
|
|
**chat_requirements["proofofwork"],
|
2024-11-21 10:22:48 +03:00
|
|
|
user_agent=cls._headers.get("user-agent"),
|
2024-11-18 04:53:50 +03:00
|
|
|
proof_token=RequestConfig.proof_token
|
2024-05-19 06:09:55 +03:00
|
|
|
)
|
2024-11-19 17:26:03 +03:00
|
|
|
[debug.log(text) for text in (
|
|
|
|
f"Arkose: {'False' if not need_arkose else RequestConfig.arkose_token[:12]+'...'}",
|
|
|
|
f"Proofofwork: {'False' if proofofwork is None else proofofwork[:12]+'...'}",
|
2024-11-21 09:14:36 +03:00
|
|
|
f"AccessToken: {'False' if cls._api_key is None else cls._api_key[:12]+'...'}",
|
2024-11-19 17:26:03 +03:00
|
|
|
)]
|
2023-11-20 16:00:40 +03:00
|
|
|
data = {
|
|
|
|
"action": action,
|
2024-11-17 21:51:26 +03:00
|
|
|
"messages": None,
|
2024-04-05 22:00:35 +03:00
|
|
|
"parent_message_id": conversation.message_id,
|
2024-01-26 09:54:13 +03:00
|
|
|
"model": model,
|
2024-12-06 23:54:13 +03:00
|
|
|
"timezone_offset_min":-60,
|
|
|
|
"timezone":"Europe/Berlin",
|
2024-12-07 07:06:24 +03:00
|
|
|
"history_and_training_disabled": history_disabled and not auto_continue and not return_conversation or not cls.needs_auth,
|
2024-12-06 23:54:13 +03:00
|
|
|
"conversation_mode":{"kind":"primary_assistant","plugin_ids":None},
|
|
|
|
"force_paragen":False,
|
|
|
|
"force_paragen_model_slug":"",
|
|
|
|
"force_rate_limit":False,
|
|
|
|
"reset_rate_limits":False,
|
2024-11-18 04:53:50 +03:00
|
|
|
"websocket_request_id": str(uuid.uuid4()),
|
2024-12-06 23:54:13 +03:00
|
|
|
"system_hints": ["search"] if web_search else None,
|
|
|
|
"supported_encodings":["v1"],
|
|
|
|
"conversation_origin":None,
|
2024-12-07 07:06:24 +03:00
|
|
|
"client_contextual_info":{"is_dark_mode":False,"time_since_loaded":random.randint(20, 500),"page_height":578,"page_width":1850,"pixel_ratio":1,"screen_height":1080,"screen_width":1920},
|
2024-12-06 23:54:13 +03:00
|
|
|
"paragen_stream_type_override":None,
|
|
|
|
"paragen_cot_summary_display_override":"allow",
|
|
|
|
"supports_buffering":True
|
2023-11-20 16:00:40 +03:00
|
|
|
}
|
2024-11-17 21:51:26 +03:00
|
|
|
if conversation.conversation_id is not None:
|
|
|
|
data["conversation_id"] = conversation.conversation_id
|
2024-11-19 17:26:03 +03:00
|
|
|
debug.log(f"OpenaiChat: Use conversation: {conversation.conversation_id}")
|
2023-11-20 16:00:40 +03:00
|
|
|
if action != "continue":
|
2024-02-28 09:36:43 +03:00
|
|
|
messages = messages if conversation_id is None else [messages[-1]]
|
2024-11-23 02:17:35 +03:00
|
|
|
data["messages"] = cls.create_messages(messages, image_request, ["search"] if web_search else None)
|
2024-03-14 15:53:57 +03:00
|
|
|
headers = {
|
2024-11-21 10:22:48 +03:00
|
|
|
**cls._headers,
|
|
|
|
"accept": "text/event-stream",
|
|
|
|
"content-type": "application/json",
|
|
|
|
"openai-sentinel-chat-requirements-token": chat_token,
|
2024-03-14 15:53:57 +03:00
|
|
|
}
|
2024-11-18 04:53:50 +03:00
|
|
|
if RequestConfig.arkose_token:
|
2024-11-21 10:22:48 +03:00
|
|
|
headers["openai-sentinel-arkose-token"] = RequestConfig.arkose_token
|
2024-04-28 23:19:51 +03:00
|
|
|
if proofofwork is not None:
|
2024-11-21 10:22:48 +03:00
|
|
|
headers["openai-sentinel-proof-token"] = proofofwork
|
2024-11-18 04:53:50 +03:00
|
|
|
if need_turnstile and RequestConfig.turnstile_token is not None:
|
|
|
|
headers['openai-sentinel-turnstile-token'] = RequestConfig.turnstile_token
|
2024-01-13 17:37:36 +03:00
|
|
|
async with session.post(
|
2024-05-19 00:13:57 +03:00
|
|
|
f"{cls.url}/backend-anon/conversation"
|
|
|
|
if cls._api_key is None else
|
2024-01-13 17:37:36 +03:00
|
|
|
f"{cls.url}/backend-api/conversation",
|
|
|
|
json=data,
|
2024-03-14 15:53:57 +03:00
|
|
|
headers=headers
|
2024-01-13 17:37:36 +03:00
|
|
|
) as response:
|
2024-02-27 01:41:06 +03:00
|
|
|
cls._update_request_args(session)
|
2024-05-18 16:37:46 +03:00
|
|
|
if response.status == 403 and max_retries > 0:
|
|
|
|
max_retries -= 1
|
2024-11-19 17:26:03 +03:00
|
|
|
debug.log(f"Retry: Error {response.status}: {await response.text()}")
|
2024-05-18 16:37:46 +03:00
|
|
|
await asyncio.sleep(5)
|
|
|
|
continue
|
2024-03-14 15:53:57 +03:00
|
|
|
await raise_for_status(response)
|
2024-11-21 07:00:08 +03:00
|
|
|
if return_conversation:
|
|
|
|
yield conversation
|
|
|
|
async for line in response.iter_lines():
|
|
|
|
async for chunk in cls.iter_messages_line(session, line, conversation):
|
|
|
|
yield chunk
|
2024-12-06 23:54:13 +03:00
|
|
|
if not history_disabled and RequestConfig.access_token is not None:
|
2024-11-21 07:00:08 +03:00
|
|
|
yield SynthesizeData(cls.__name__, {
|
|
|
|
"conversation_id": conversation.conversation_id,
|
|
|
|
"message_id": conversation.message_id,
|
|
|
|
"voice": "maple",
|
|
|
|
})
|
2024-04-05 22:00:35 +03:00
|
|
|
if auto_continue and conversation.finish_reason == "max_tokens":
|
|
|
|
conversation.finish_reason = None
|
|
|
|
action = "continue"
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
else:
|
2023-11-20 16:00:40 +03:00
|
|
|
break
|
2024-11-21 07:00:08 +03:00
|
|
|
yield FinishReason(conversation.finish_reason)
|
2024-02-28 09:36:43 +03:00
|
|
|
|
|
|
|
@classmethod
|
2024-03-26 08:42:47 +03:00
|
|
|
async def iter_messages_line(cls, session: StreamSession, line: bytes, fields: Conversation) -> AsyncIterator:
|
2024-02-28 09:36:43 +03:00
|
|
|
if not line.startswith(b"data: "):
|
|
|
|
return
|
|
|
|
elif line.startswith(b"data: [DONE]"):
|
2024-03-09 05:10:59 +03:00
|
|
|
if fields.finish_reason is None:
|
|
|
|
fields.finish_reason = "error"
|
2024-02-28 09:36:43 +03:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
line = json.loads(line[6:])
|
|
|
|
except:
|
|
|
|
return
|
2024-11-17 21:51:26 +03:00
|
|
|
if isinstance(line, dict) and "v" in line:
|
|
|
|
v = line.get("v")
|
2024-11-20 11:52:38 +03:00
|
|
|
if isinstance(v, str) and fields.is_recipient:
|
2024-12-01 00:11:18 +03:00
|
|
|
if "p" not in line or line.get("p") == "/message/content/parts/0":
|
|
|
|
yield v
|
2024-11-20 11:52:38 +03:00
|
|
|
elif isinstance(v, list) and fields.is_recipient:
|
2024-11-17 21:51:26 +03:00
|
|
|
for m in v:
|
|
|
|
if m.get("p") == "/message/content/parts/0":
|
|
|
|
yield m.get("v")
|
|
|
|
elif m.get("p") == "/message/metadata":
|
|
|
|
fields.finish_reason = m.get("v", {}).get("finish_details", {}).get("type")
|
|
|
|
break
|
|
|
|
elif isinstance(v, dict):
|
|
|
|
if fields.conversation_id is None:
|
|
|
|
fields.conversation_id = v.get("conversation_id")
|
2024-11-19 17:26:03 +03:00
|
|
|
debug.log(f"OpenaiChat: New conversation: {fields.conversation_id}")
|
|
|
|
m = v.get("message", {})
|
2024-12-01 00:11:18 +03:00
|
|
|
fields.is_recipient = m.get("recipient", "all") == "all"
|
2024-11-20 11:52:38 +03:00
|
|
|
if fields.is_recipient:
|
2024-11-19 17:26:03 +03:00
|
|
|
c = m.get("content", {})
|
|
|
|
if c.get("content_type") == "multimodal_text":
|
|
|
|
generated_images = []
|
|
|
|
for element in c.get("parts"):
|
2024-11-20 11:52:38 +03:00
|
|
|
if isinstance(element, dict) and element.get("content_type") == "image_asset_pointer":
|
2024-11-23 02:17:35 +03:00
|
|
|
image = cls.get_generated_image(session, cls._headers, element)
|
2024-11-26 21:28:41 +03:00
|
|
|
generated_images.append(image)
|
2024-11-19 17:26:03 +03:00
|
|
|
for image_response in await asyncio.gather(*generated_images):
|
2024-11-26 21:28:41 +03:00
|
|
|
if image_response is not None:
|
|
|
|
yield image_response
|
2024-11-20 11:52:38 +03:00
|
|
|
if m.get("author", {}).get("role") == "assistant":
|
|
|
|
fields.message_id = v.get("message", {}).get("id")
|
2024-02-28 09:36:43 +03:00
|
|
|
return
|
2024-11-17 21:51:26 +03:00
|
|
|
if "error" in line and line.get("error"):
|
|
|
|
raise RuntimeError(line.get("error"))
|
2024-03-14 15:53:57 +03:00
|
|
|
|
2024-11-21 07:00:08 +03:00
|
|
|
@classmethod
|
|
|
|
async def synthesize(cls, params: dict) -> AsyncIterator[bytes]:
|
|
|
|
await cls.login()
|
|
|
|
async with StreamSession(
|
|
|
|
impersonate="chrome",
|
|
|
|
timeout=900
|
|
|
|
) as session:
|
|
|
|
async with session.get(
|
|
|
|
f"{cls.url}/backend-api/synthesize",
|
|
|
|
params=params,
|
|
|
|
headers=cls._headers
|
|
|
|
) as response:
|
|
|
|
await raise_for_status(response)
|
|
|
|
async for chunk in response.iter_content():
|
|
|
|
yield chunk
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def login(cls, proxy: str = None):
|
|
|
|
if cls._expires is not None and cls._expires < time.time():
|
|
|
|
cls._headers = cls._api_key = None
|
|
|
|
try:
|
|
|
|
await get_request_config(proxy)
|
|
|
|
cls._create_request_args(RequestConfig.cookies, RequestConfig.headers)
|
|
|
|
cls._set_api_key(RequestConfig.access_token)
|
|
|
|
except NoValidHarFileError:
|
|
|
|
if has_nodriver:
|
2024-12-06 23:54:13 +03:00
|
|
|
if RequestConfig.access_token is None:
|
|
|
|
await cls.nodriver_auth(proxy)
|
2024-11-21 07:00:08 +03:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2024-04-07 17:02:49 +03:00
|
|
|
@classmethod
|
2024-11-18 04:53:50 +03:00
|
|
|
async def nodriver_auth(cls, proxy: str = None):
|
2024-12-07 21:38:04 +03:00
|
|
|
browser = await get_nodriver(proxy=proxy, user_data_dir="chatgpt")
|
2024-11-18 04:53:50 +03:00
|
|
|
page = browser.main_tab
|
|
|
|
def on_request(event: nodriver.cdp.network.RequestWillBeSent):
|
|
|
|
if event.request.url == start_url or event.request.url.startswith(conversation_url):
|
|
|
|
RequestConfig.headers = event.request.headers
|
|
|
|
elif event.request.url in (backend_url, backend_anon_url):
|
|
|
|
if "OpenAI-Sentinel-Proof-Token" in event.request.headers:
|
|
|
|
RequestConfig.proof_token = json.loads(base64.b64decode(
|
|
|
|
event.request.headers["OpenAI-Sentinel-Proof-Token"].split("gAAAAAB", 1)[-1].encode()
|
|
|
|
).decode())
|
|
|
|
if "OpenAI-Sentinel-Turnstile-Token" in event.request.headers:
|
|
|
|
RequestConfig.turnstile_token = event.request.headers["OpenAI-Sentinel-Turnstile-Token"]
|
|
|
|
if "Authorization" in event.request.headers:
|
|
|
|
RequestConfig.access_token = event.request.headers["Authorization"].split()[-1]
|
|
|
|
elif event.request.url == arkose_url:
|
|
|
|
RequestConfig.arkose_request = arkReq(
|
|
|
|
arkURL=event.request.url,
|
|
|
|
arkBx=None,
|
|
|
|
arkHeader=event.request.headers,
|
|
|
|
arkBody=event.request.post_data,
|
|
|
|
userAgent=event.request.headers.get("user-agent")
|
|
|
|
)
|
|
|
|
await page.send(nodriver.cdp.network.enable())
|
|
|
|
page.add_handler(nodriver.cdp.network.RequestWillBeSent, on_request)
|
|
|
|
page = await browser.get(cls.url)
|
2024-11-21 16:05:50 +03:00
|
|
|
user_agent = await page.evaluate("window.navigator.userAgent")
|
2024-11-18 04:53:50 +03:00
|
|
|
await page.select("#prompt-textarea", 240)
|
|
|
|
while True:
|
2024-12-06 23:54:13 +03:00
|
|
|
if RequestConfig.access_token:
|
2024-11-18 04:53:50 +03:00
|
|
|
break
|
2024-12-07 07:06:24 +03:00
|
|
|
body = await page.evaluate("JSON.stringify(window.__remixContext)")
|
|
|
|
if body:
|
|
|
|
match = re.search(r'"accessToken":"(.*?)"', body)
|
|
|
|
if match:
|
|
|
|
RequestConfig.access_token = match.group(1)
|
|
|
|
break
|
2024-11-18 04:53:50 +03:00
|
|
|
await asyncio.sleep(1)
|
2024-12-07 07:06:24 +03:00
|
|
|
while True:
|
|
|
|
if RequestConfig.proof_token:
|
|
|
|
break
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
RequestConfig.data_build = await page.evaluate("document.documentElement.getAttribute('data-build')")
|
|
|
|
for c in await page.send(nodriver.cdp.network.get_cookies([cls.url])):
|
|
|
|
RequestConfig.cookies[c.name] = c.value
|
2024-04-07 17:02:49 +03:00
|
|
|
await page.close()
|
2024-11-21 16:05:50 +03:00
|
|
|
cls._create_request_args(RequestConfig.cookies, RequestConfig.headers, user_agent=user_agent)
|
2024-11-18 04:53:50 +03:00
|
|
|
cls._set_api_key(RequestConfig.access_token)
|
2024-04-07 17:02:49 +03:00
|
|
|
|
2024-04-05 22:00:35 +03:00
|
|
|
@staticmethod
|
|
|
|
def get_default_headers() -> dict:
|
|
|
|
return {
|
2024-04-21 08:22:59 +03:00
|
|
|
**DEFAULT_HEADERS,
|
2024-04-05 22:00:35 +03:00
|
|
|
"content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
2024-02-27 01:41:06 +03:00
|
|
|
@classmethod
|
2024-04-21 16:15:55 +03:00
|
|
|
def _create_request_args(cls, cookies: Cookies = None, headers: dict = None, user_agent: str = None):
|
|
|
|
cls._headers = cls.get_default_headers() if headers is None else headers
|
2024-04-18 21:18:51 +03:00
|
|
|
if user_agent is not None:
|
|
|
|
cls._headers["user-agent"] = user_agent
|
2024-11-20 11:52:38 +03:00
|
|
|
cls._cookies = {} if cookies is None else cookies
|
2024-02-28 09:36:43 +03:00
|
|
|
cls._update_cookie_header()
|
2024-02-27 01:41:06 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _update_request_args(cls, session: StreamSession):
|
|
|
|
for c in session.cookie_jar if hasattr(session, "cookie_jar") else session.cookies.jar:
|
2024-03-26 08:42:47 +03:00
|
|
|
cls._cookies[c.key if hasattr(c, "key") else c.name] = c.value
|
2024-02-28 09:36:43 +03:00
|
|
|
cls._update_cookie_header()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _set_api_key(cls, api_key: str):
|
|
|
|
cls._api_key = api_key
|
2024-03-08 13:01:38 +03:00
|
|
|
cls._expires = int(time.time()) + 60 * 60 * 4
|
2024-11-18 04:53:50 +03:00
|
|
|
if api_key:
|
|
|
|
cls._headers["authorization"] = f"Bearer {api_key}"
|
2024-02-28 09:36:43 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _update_cookie_header(cls):
|
2024-12-06 03:43:49 +03:00
|
|
|
if cls._cookies:
|
|
|
|
cls._headers["cookie"] = format_cookies(cls._cookies)
|
2024-02-27 01:41:06 +03:00
|
|
|
|
2024-03-26 08:42:47 +03:00
|
|
|
class Conversation(BaseConversation):
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Class to encapsulate response fields.
|
|
|
|
"""
|
2024-02-28 09:36:43 +03:00
|
|
|
def __init__(self, conversation_id: str = None, message_id: str = None, finish_reason: str = None):
|
2023-11-19 07:36:04 +03:00
|
|
|
self.conversation_id = conversation_id
|
|
|
|
self.message_id = message_id
|
2024-02-28 09:36:43 +03:00
|
|
|
self.finish_reason = finish_reason
|
2024-11-21 16:05:50 +03:00
|
|
|
self.is_recipient = False
|