OpenaiChat.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. from __future__ import annotations
  2. import os
  3. import re
  4. import asyncio
  5. import uuid
  6. import json
  7. import base64
  8. import time
  9. import random
  10. from typing import AsyncIterator, Iterator, Optional, Generator, Dict, List
  11. from copy import copy
  12. try:
  13. import nodriver
  14. has_nodriver = True
  15. except ImportError:
  16. has_nodriver = False
  17. from ..base_provider import AsyncAuthedProvider, ProviderModelMixin
  18. from ...typing import AsyncResult, Messages, Cookies, ImagesType
  19. from ...requests.raise_for_status import raise_for_status
  20. from ...requests import StreamSession
  21. from ...requests import get_nodriver
  22. from ...image import ImageResponse, ImageRequest, to_image, to_bytes, is_accepted_format
  23. from ...errors import MissingAuthError, NoValidHarFileError
  24. from ...providers.response import JsonConversation, FinishReason, SynthesizeData, AuthResult
  25. from ...providers.response import Sources, TitleGeneration, RequestLogin, Parameters
  26. from ..helper import format_cookies
  27. from ..openai.har_file import get_request_config
  28. from ..openai.har_file import RequestConfig, arkReq, arkose_url, start_url, conversation_url, backend_url, backend_anon_url
  29. from ..openai.proofofwork import generate_proof_token
  30. from ..openai.new import get_requirements_token, get_config
  31. from ... import debug
  32. DEFAULT_HEADERS = {
  33. "accept": "*/*",
  34. "accept-encoding": "gzip, deflate, br, zstd",
  35. 'accept-language': 'en-US,en;q=0.8',
  36. "referer": "https://chatgpt.com/",
  37. "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
  38. "sec-ch-ua-mobile": "?0",
  39. "sec-ch-ua-platform": "\"Windows\"",
  40. "sec-fetch-dest": "empty",
  41. "sec-fetch-mode": "cors",
  42. "sec-fetch-site": "same-origin",
  43. "sec-gpc": "1",
  44. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
  45. }
  46. INIT_HEADERS = {
  47. 'accept': '*/*',
  48. 'accept-language': 'en-US,en;q=0.8',
  49. 'cache-control': 'no-cache',
  50. 'pragma': 'no-cache',
  51. 'priority': 'u=0, i',
  52. "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
  53. 'sec-ch-ua-arch': '"arm"',
  54. 'sec-ch-ua-bitness': '"64"',
  55. 'sec-ch-ua-mobile': '?0',
  56. 'sec-ch-ua-model': '""',
  57. "sec-ch-ua-platform": "\"Windows\"",
  58. 'sec-ch-ua-platform-version': '"14.4.0"',
  59. 'sec-fetch-dest': 'document',
  60. 'sec-fetch-mode': 'navigate',
  61. 'sec-fetch-site': 'none',
  62. 'sec-fetch-user': '?1',
  63. 'upgrade-insecure-requests': '1',
  64. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
  65. }
  66. UPLOAD_HEADERS = {
  67. "accept": "application/json, text/plain, */*",
  68. 'accept-language': 'en-US,en;q=0.8',
  69. "referer": "https://chatgpt.com/",
  70. "priority": "u=1, i",
  71. "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
  72. "sec-ch-ua-mobile": "?0",
  73. 'sec-ch-ua-platform': '"macOS"',
  74. "sec-fetch-dest": "empty",
  75. "sec-fetch-mode": "cors",
  76. "sec-fetch-site": "cross-site",
  77. "x-ms-blob-type": "BlockBlob",
  78. "x-ms-version": "2020-04-08",
  79. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
  80. }
  81. class OpenaiChat(AsyncAuthedProvider, ProviderModelMixin):
  82. """A class for creating and managing conversations with OpenAI chat service"""
  83. label = "OpenAI ChatGPT"
  84. url = "https://chatgpt.com"
  85. working = True
  86. supports_gpt_4 = True
  87. supports_message_history = True
  88. supports_system_message = True
  89. default_model = "auto"
  90. fallback_models = [default_model, "gpt-4", "gpt-4o", "gpt-4o-mini", "gpt-4o-canmore", "o1", "o1-preview", "o1-mini"]
  91. vision_models = fallback_models
  92. synthesize_content_type = "audio/mpeg"
  93. _api_key: str = None
  94. _headers: dict = None
  95. _cookies: Cookies = None
  96. _expires: int = None
  97. @classmethod
  98. async def on_auth_async(cls, **kwargs) -> AsyncIterator:
  99. async for chunk in cls.login():
  100. yield chunk
  101. yield AuthResult(
  102. api_key=cls._api_key,
  103. cookies=cls._cookies or RequestConfig.cookies or {},
  104. headers=cls._headers or RequestConfig.headers or cls.get_default_headers(),
  105. expires=cls._expires,
  106. proof_token=RequestConfig.proof_token,
  107. turnstile_token=RequestConfig.turnstile_token
  108. )
  109. @classmethod
  110. def get_models(cls, proxy: str = None, timeout: int = 180) -> List[str]:
  111. if not cls.models:
  112. # try:
  113. # headers = {
  114. # **(cls.get_default_headers() if cls._headers is None else cls._headers),
  115. # "accept": "application/json",
  116. # }
  117. # with Session(
  118. # proxy=proxy,
  119. # impersonate="chrome",
  120. # timeout=timeout,
  121. # headers=headers
  122. # ) as session:
  123. # response = session.get(
  124. # f"{cls.url}/backend-anon/models"
  125. # if cls._api_key is None else
  126. # f"{cls.url}/backend-api/models"
  127. # )
  128. # raise_for_status(response)
  129. # data = response.json()
  130. # cls.models = [model.get("slug") for model in data.get("models")]
  131. # except Exception as e:
  132. # debug.log(f"OpenaiChat: Failed to get models: {type(e).__name__}: {e}")
  133. cls.models = cls.fallback_models
  134. return cls.models
  135. @classmethod
  136. async def upload_images(
  137. cls,
  138. session: StreamSession,
  139. auth_result: AuthResult,
  140. images: ImagesType,
  141. ) -> ImageRequest:
  142. """
  143. Upload an image to the service and get the download URL
  144. Args:
  145. session: The StreamSession object to use for requests
  146. headers: The headers to include in the requests
  147. images: The images to upload, either a PIL Image object or a bytes object
  148. Returns:
  149. An ImageRequest object that contains the download URL, file name, and other data
  150. """
  151. async def upload_image(image, image_name):
  152. # Convert the image to a PIL Image object and get the extension
  153. data_bytes = to_bytes(image)
  154. image = to_image(data_bytes)
  155. extension = image.format.lower()
  156. data = {
  157. "file_name": "" if image_name is None else image_name,
  158. "file_size": len(data_bytes),
  159. "use_case": "multimodal"
  160. }
  161. # Post the image data to the service and get the image data
  162. headers = auth_result.headers if hasattr(auth_result, "headers") else None
  163. async with session.post(f"{cls.url}/backend-api/files", json=data, headers=headers) as response:
  164. cls._update_request_args(auth_result, session)
  165. await raise_for_status(response, "Create file failed")
  166. image_data = {
  167. **data,
  168. **await response.json(),
  169. "mime_type": is_accepted_format(data_bytes),
  170. "extension": extension,
  171. "height": image.height,
  172. "width": image.width
  173. }
  174. # Put the image bytes to the upload URL and check the status
  175. await asyncio.sleep(1)
  176. async with session.put(
  177. image_data["upload_url"],
  178. data=data_bytes,
  179. headers={
  180. **UPLOAD_HEADERS,
  181. "Content-Type": image_data["mime_type"],
  182. "x-ms-blob-type": "BlockBlob",
  183. "x-ms-version": "2020-04-08",
  184. "Origin": "https://chatgpt.com",
  185. }
  186. ) as response:
  187. await raise_for_status(response)
  188. # Post the file ID to the service and get the download URL
  189. async with session.post(
  190. f"{cls.url}/backend-api/files/{image_data['file_id']}/uploaded",
  191. json={},
  192. headers=auth_result.headers
  193. ) as response:
  194. cls._update_request_args(auth_result, session)
  195. await raise_for_status(response, "Get download url failed")
  196. image_data["download_url"] = (await response.json())["download_url"]
  197. return ImageRequest(image_data)
  198. if not images:
  199. return
  200. return [await upload_image(image, image_name) for image, image_name in images]
  201. @classmethod
  202. def create_messages(cls, messages: Messages, image_requests: ImageRequest = None, system_hints: list = None):
  203. """
  204. Create a list of messages for the user input
  205. Args:
  206. prompt: The user input as a string
  207. image_response: The image response object, if any
  208. Returns:
  209. A list of messages with the user input and the image, if any
  210. """
  211. # Create a message object with the user role and the content
  212. messages = [{
  213. "id": str(uuid.uuid4()),
  214. "author": {"role": message["role"]},
  215. "content": {"content_type": "text", "parts": [message["content"]]},
  216. "metadata": {"serialization_metadata": {"custom_symbol_offsets": []}, **({"system_hints": system_hints} if system_hints else {})},
  217. "create_time": time.time(),
  218. } for message in messages]
  219. # Check if there is an image response
  220. if image_requests:
  221. # Change content in last user message
  222. messages[-1]["content"] = {
  223. "content_type": "multimodal_text",
  224. "parts": [*[{
  225. "asset_pointer": f"file-service://{image_request.get('file_id')}",
  226. "height": image_request.get("height"),
  227. "size_bytes": image_request.get("file_size"),
  228. "width": image_request.get("width"),
  229. }
  230. for image_request in image_requests],
  231. messages[-1]["content"]["parts"][0]]
  232. }
  233. # Add the metadata object with the attachments
  234. messages[-1]["metadata"] = {
  235. "attachments": [{
  236. "height": image_request.get("height"),
  237. "id": image_request.get("file_id"),
  238. "mimeType": image_request.get("mime_type"),
  239. "name": image_request.get("file_name"),
  240. "size": image_request.get("file_size"),
  241. "width": image_request.get("width"),
  242. }
  243. for image_request in image_requests]
  244. }
  245. return messages
  246. @classmethod
  247. async def get_generated_image(cls, auth_result: AuthResult, session: StreamSession, element: dict, prompt: str = None) -> ImageResponse:
  248. try:
  249. prompt = element["metadata"]["dalle"]["prompt"]
  250. file_id = element["asset_pointer"].split("file-service://", 1)[1]
  251. except TypeError:
  252. return
  253. except Exception as e:
  254. raise RuntimeError(f"No Image: {e.__class__.__name__}: {e}")
  255. try:
  256. async with session.get(f"{cls.url}/backend-api/files/{file_id}/download", headers=auth_result.headers) as response:
  257. cls._update_request_args(auth_result, session)
  258. await raise_for_status(response)
  259. download_url = (await response.json())["download_url"]
  260. return ImageResponse(download_url, prompt)
  261. except Exception as e:
  262. raise RuntimeError(f"Error in downloading image: {e}")
  263. @classmethod
  264. async def create_authed(
  265. cls,
  266. model: str,
  267. messages: Messages,
  268. auth_result: AuthResult,
  269. proxy: str = None,
  270. timeout: int = 180,
  271. auto_continue: bool = False,
  272. history_disabled: bool = False,
  273. action: str = "next",
  274. conversation_id: str = None,
  275. conversation: Conversation = None,
  276. images: ImagesType = None,
  277. return_conversation: bool = False,
  278. max_retries: int = 0,
  279. web_search: bool = False,
  280. **kwargs
  281. ) -> AsyncResult:
  282. """
  283. Create an asynchronous generator for the conversation.
  284. Args:
  285. model (str): The model name.
  286. messages (Messages): The list of previous messages.
  287. proxy (str): Proxy to use for requests.
  288. timeout (int): Timeout for requests.
  289. api_key (str): Access token for authentication.
  290. auto_continue (bool): Flag to automatically continue the conversation.
  291. history_disabled (bool): Flag to disable history and training.
  292. action (str): Type of action ('next', 'continue', 'variant').
  293. conversation_id (str): ID of the conversation.
  294. images (ImagesType): Images to include in the conversation.
  295. return_conversation (bool): Flag to include response fields in the output.
  296. **kwargs: Additional keyword arguments.
  297. Yields:
  298. AsyncResult: Asynchronous results from the generator.
  299. Raises:
  300. RuntimeError: If an error occurs during processing.
  301. """
  302. async with StreamSession(
  303. proxy=proxy,
  304. impersonate="chrome",
  305. timeout=timeout
  306. ) as session:
  307. image_requests = None
  308. if not cls.needs_auth:
  309. if cls._headers is None:
  310. cls._create_request_args(cls._cookies)
  311. async with session.get(cls.url, headers=INIT_HEADERS) as response:
  312. cls._update_request_args(auth_result, session)
  313. await raise_for_status(response)
  314. else:
  315. if cls._headers is None and getattr(auth_result, "cookies", None):
  316. cls._create_request_args(auth_result.cookies, auth_result.headers)
  317. if not cls._set_api_key(getattr(auth_result, "api_key", None)):
  318. raise MissingAuthError("Access token is not valid")
  319. async with session.get(cls.url, headers=cls._headers) as response:
  320. cls._update_request_args(auth_result, session)
  321. await raise_for_status(response)
  322. try:
  323. image_requests = await cls.upload_images(session, auth_result, images) if images else None
  324. except Exception as e:
  325. debug.log("OpenaiChat: Upload image failed")
  326. debug.log(f"{e.__class__.__name__}: {e}")
  327. model = cls.get_model(model)
  328. if conversation is None:
  329. conversation = Conversation(conversation_id, str(uuid.uuid4()), getattr(auth_result, "cookies", {}).get("oai-did"))
  330. else:
  331. conversation = copy(conversation)
  332. if getattr(auth_result, "cookies", {}).get("oai-did") != getattr(conversation, "user_id", None):
  333. conversation = Conversation(None, str(uuid.uuid4()))
  334. if cls._api_key is None:
  335. auto_continue = False
  336. conversation.finish_reason = None
  337. sources = Sources([])
  338. while conversation.finish_reason is None:
  339. async with session.post(
  340. f"{cls.url}/backend-anon/sentinel/chat-requirements"
  341. if cls._api_key is None else
  342. f"{cls.url}/backend-api/sentinel/chat-requirements",
  343. json={"p": None if not getattr(auth_result, "proof_token", None) else get_requirements_token(getattr(auth_result, "proof_token", None))},
  344. headers=cls._headers
  345. ) as response:
  346. if response.status in (401, 403):
  347. auth_result.reset()
  348. else:
  349. cls._update_request_args(auth_result, session)
  350. await raise_for_status(response)
  351. chat_requirements = await response.json()
  352. need_turnstile = chat_requirements.get("turnstile", {}).get("required", False)
  353. need_arkose = chat_requirements.get("arkose", {}).get("required", False)
  354. chat_token = chat_requirements.get("token")
  355. # if need_arkose and RequestConfig.arkose_token is None:
  356. # await get_request_config(proxy)
  357. # cls._create_request_args(auth_result.cookies, auth_result.headers)
  358. # cls._set_api_key(auth_result.access_token)
  359. # if auth_result.arkose_token is None:
  360. # raise MissingAuthError("No arkose token found in .har file")
  361. if "proofofwork" in chat_requirements:
  362. if getattr(auth_result, "proof_token", None) is None:
  363. auth_result.proof_token = get_config(auth_result.headers.get("user-agent"))
  364. proofofwork = generate_proof_token(
  365. **chat_requirements["proofofwork"],
  366. user_agent=getattr(auth_result, "headers", {}).get("user-agent"),
  367. proof_token=getattr(auth_result, "proof_token", None)
  368. )
  369. [debug.log(text) for text in (
  370. #f"Arkose: {'False' if not need_arkose else auth_result.arkose_token[:12]+'...'}",
  371. f"Proofofwork: {'False' if proofofwork is None else proofofwork[:12]+'...'}",
  372. f"AccessToken: {'False' if cls._api_key is None else cls._api_key[:12]+'...'}",
  373. )]
  374. data = {
  375. "action": action,
  376. "parent_message_id": conversation.message_id,
  377. "model": model,
  378. "timezone_offset_min":-60,
  379. "timezone":"Europe/Berlin",
  380. "suggestions":[],
  381. "history_and_training_disabled": history_disabled and not auto_continue and not return_conversation or not cls.needs_auth,
  382. "conversation_mode":{"kind":"primary_assistant","plugin_ids":None},
  383. "force_paragen":False,
  384. "force_paragen_model_slug":"",
  385. "force_rate_limit":False,
  386. "reset_rate_limits":False,
  387. "websocket_request_id": str(uuid.uuid4()),
  388. "system_hints": ["search"] if web_search else None,
  389. "supported_encodings":["v1"],
  390. "conversation_origin":None,
  391. "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},
  392. "paragen_stream_type_override":None,
  393. "paragen_cot_summary_display_override":"allow",
  394. "supports_buffering":True
  395. }
  396. if conversation.conversation_id is not None:
  397. data["conversation_id"] = conversation.conversation_id
  398. debug.log(f"OpenaiChat: Use conversation: {conversation.conversation_id}")
  399. if action != "continue":
  400. data["parent_message_id"] = getattr(conversation, "parent_message_id", conversation.message_id)
  401. conversation.parent_message_id = None
  402. messages = messages if conversation_id is None else [messages[-1]]
  403. data["messages"] = cls.create_messages(messages, image_requests, ["search"] if web_search else None)
  404. headers = {
  405. **cls._headers,
  406. "accept": "text/event-stream",
  407. "content-type": "application/json",
  408. "openai-sentinel-chat-requirements-token": chat_token,
  409. }
  410. #if RequestConfig.arkose_token:
  411. # headers["openai-sentinel-arkose-token"] = RequestConfig.arkose_token
  412. if proofofwork is not None:
  413. headers["openai-sentinel-proof-token"] = proofofwork
  414. if need_turnstile and getattr(auth_result, "turnstile_token", None) is not None:
  415. headers['openai-sentinel-turnstile-token'] = auth_result.turnstile_token
  416. async with session.post(
  417. f"{cls.url}/backend-anon/conversation"
  418. if cls._api_key is None else
  419. f"{cls.url}/backend-api/conversation",
  420. json=data,
  421. headers=headers
  422. ) as response:
  423. cls._update_request_args(auth_result, session)
  424. if response.status == 403:
  425. auth_result.proof_token = None
  426. RequestConfig.proof_token = None
  427. await raise_for_status(response)
  428. buffer = u""
  429. async for line in response.iter_lines():
  430. async for chunk in cls.iter_messages_line(session, line, conversation, sources):
  431. if isinstance(chunk, str):
  432. chunk = chunk.replace("\ue203", "").replace("\ue204", "").replace("\ue206", "")
  433. buffer += chunk
  434. if buffer.find(u"\ue200") != -1:
  435. if buffer.find(u"\ue201") != -1:
  436. buffer = buffer.replace("\ue200", "").replace("\ue202", "\n").replace("\ue201", "")
  437. buffer = buffer.replace("navlist\n", "#### ")
  438. def replacer(match):
  439. link = None
  440. if len(sources.list) > int(match.group(1)):
  441. link = sources.list[int(match.group(1))]["url"]
  442. return f"[[{int(match.group(1))+1}]]({link})"
  443. return f" [{int(match.group(1))+1}]"
  444. buffer = re.sub(r'(?:cite\nturn0search|cite\nturn0news|turn0news)(\d+)', replacer, buffer)
  445. else:
  446. continue
  447. yield buffer
  448. buffer = ""
  449. else:
  450. yield chunk
  451. if sources.list:
  452. yield sources
  453. if return_conversation:
  454. yield conversation
  455. if not history_disabled and auth_result.api_key is not None:
  456. yield SynthesizeData(cls.__name__, {
  457. "conversation_id": conversation.conversation_id,
  458. "message_id": conversation.message_id,
  459. "voice": "maple",
  460. })
  461. if auto_continue and conversation.finish_reason == "max_tokens":
  462. conversation.finish_reason = None
  463. action = "continue"
  464. await asyncio.sleep(5)
  465. else:
  466. break
  467. yield Parameters(**{
  468. "action": "continue" if conversation.finish_reason == "max_tokens" else "variant",
  469. "conversation": conversation.get_dict(),
  470. "proof_token": RequestConfig.proof_token,
  471. "cookies": cls._cookies,
  472. "headers": cls._headers,
  473. "web_search": web_search,
  474. })
  475. actions = ["variant", "continue"] if conversation.finish_reason == "max_tokens" else ["variant"]
  476. yield FinishReason(conversation.finish_reason, actions=actions)
  477. @classmethod
  478. async def iter_messages_line(cls, session: StreamSession, line: bytes, fields: Conversation, sources: Sources) -> AsyncIterator:
  479. if not line.startswith(b"data: "):
  480. return
  481. elif line.startswith(b"data: [DONE]"):
  482. if fields.finish_reason is None:
  483. fields.finish_reason = "error"
  484. return
  485. try:
  486. line = json.loads(line[6:])
  487. except:
  488. return
  489. if not isinstance(line, dict):
  490. return
  491. if "type" in line:
  492. if line["type"] == "title_generation":
  493. yield TitleGeneration(line["title"])
  494. if "v" in line:
  495. v = line.get("v")
  496. if isinstance(v, str) and fields.is_recipient:
  497. if "p" not in line or line.get("p") == "/message/content/parts/0":
  498. yield v
  499. elif isinstance(v, list):
  500. for m in v:
  501. if m.get("p") == "/message/content/parts/0" and fields.is_recipient:
  502. yield m.get("v")
  503. elif m.get("p") == "/message/metadata/search_result_groups":
  504. for entry in [p.get("entries") for p in m.get("v")]:
  505. for link in entry:
  506. sources.add_source(link)
  507. elif re.match(r"^/message/metadata/content_references/\d+$", m.get("p")):
  508. sources.add_source(m.get("v"))
  509. elif m.get("p") == "/message/metadata":
  510. fields.finish_reason = m.get("v", {}).get("finish_details", {}).get("type")
  511. break
  512. elif isinstance(v, dict):
  513. if fields.conversation_id is None:
  514. fields.conversation_id = v.get("conversation_id")
  515. debug.log(f"OpenaiChat: New conversation: {fields.conversation_id}")
  516. m = v.get("message", {})
  517. fields.is_recipient = m.get("recipient", "all") == "all"
  518. if fields.is_recipient:
  519. c = m.get("content", {})
  520. if c.get("content_type") == "multimodal_text":
  521. generated_images = []
  522. for element in c.get("parts"):
  523. if isinstance(element, dict) and element.get("content_type") == "image_asset_pointer":
  524. image = cls.get_generated_image(session, cls._headers, element)
  525. generated_images.append(image)
  526. for image_response in await asyncio.gather(*generated_images):
  527. if image_response is not None:
  528. yield image_response
  529. if m.get("author", {}).get("role") == "assistant":
  530. if fields.parent_message_id is None:
  531. fields.parent_message_id = v.get("message", {}).get("id")
  532. fields.message_id = v.get("message", {}).get("id")
  533. return
  534. if "error" in line and line.get("error"):
  535. raise RuntimeError(line.get("error"))
  536. @classmethod
  537. async def synthesize(cls, params: dict) -> AsyncIterator[bytes]:
  538. async for _ in cls.login():
  539. pass
  540. async with StreamSession(
  541. impersonate="chrome",
  542. timeout=0
  543. ) as session:
  544. async with session.get(
  545. f"{cls.url}/backend-api/synthesize",
  546. params=params,
  547. headers=cls._headers
  548. ) as response:
  549. await raise_for_status(response)
  550. async for chunk in response.iter_content():
  551. yield chunk
  552. @classmethod
  553. async def login(
  554. cls,
  555. proxy: str = None,
  556. api_key: str = None,
  557. proof_token: str = None,
  558. cookies: Cookies = None,
  559. headers: dict = None,
  560. **kwargs
  561. ) -> AsyncIterator:
  562. if cls._expires is not None and (cls._expires - 60*10) < time.time():
  563. cls._headers = cls._api_key = None
  564. if cls._headers is None or headers is not None:
  565. cls._headers = {} if headers is None else headers
  566. if proof_token is not None:
  567. RequestConfig.proof_token = proof_token
  568. if cookies is not None:
  569. RequestConfig.cookies = cookies
  570. if api_key is not None:
  571. cls._create_request_args(RequestConfig.cookies, RequestConfig.headers)
  572. cls._set_api_key(api_key)
  573. else:
  574. try:
  575. await get_request_config(proxy)
  576. cls._create_request_args(RequestConfig.cookies, RequestConfig.headers)
  577. if RequestConfig.access_token is not None or cls.needs_auth:
  578. if not cls._set_api_key(RequestConfig.access_token):
  579. raise NoValidHarFileError(f"Access token is not valid: {RequestConfig.access_token}")
  580. except NoValidHarFileError:
  581. if has_nodriver:
  582. if cls._api_key is None:
  583. login_url = os.environ.get("G4F_LOGIN_URL")
  584. if login_url:
  585. yield RequestLogin(cls.label, login_url)
  586. await cls.nodriver_auth(proxy)
  587. else:
  588. raise
  589. yield Parameters(**{
  590. "api_key": cls._api_key,
  591. "proof_token": RequestConfig.proof_token,
  592. "cookies": RequestConfig.cookies,
  593. })
  594. @classmethod
  595. async def nodriver_auth(cls, proxy: str = None):
  596. browser = await get_nodriver(proxy=proxy)
  597. page = browser.main_tab
  598. def on_request(event: nodriver.cdp.network.RequestWillBeSent):
  599. if event.request.url == start_url or event.request.url.startswith(conversation_url):
  600. RequestConfig.headers = event.request.headers
  601. elif event.request.url in (backend_url, backend_anon_url):
  602. if "OpenAI-Sentinel-Proof-Token" in event.request.headers:
  603. RequestConfig.proof_token = json.loads(base64.b64decode(
  604. event.request.headers["OpenAI-Sentinel-Proof-Token"].split("gAAAAAB", 1)[-1].encode()
  605. ).decode())
  606. if "OpenAI-Sentinel-Turnstile-Token" in event.request.headers:
  607. RequestConfig.turnstile_token = event.request.headers["OpenAI-Sentinel-Turnstile-Token"]
  608. if "Authorization" in event.request.headers:
  609. cls._api_key = event.request.headers["Authorization"].split()[-1]
  610. elif event.request.url == arkose_url:
  611. RequestConfig.arkose_request = arkReq(
  612. arkURL=event.request.url,
  613. arkBx=None,
  614. arkHeader=event.request.headers,
  615. arkBody=event.request.post_data,
  616. userAgent=event.request.headers.get("User-Agent")
  617. )
  618. await page.send(nodriver.cdp.network.enable())
  619. page.add_handler(nodriver.cdp.network.RequestWillBeSent, on_request)
  620. page = await browser.get(cls.url)
  621. user_agent = await page.evaluate("window.navigator.userAgent")
  622. await page.select("#prompt-textarea", 240)
  623. await page.evaluate("document.getElementById('prompt-textarea').innerText = 'Hello'")
  624. await page.evaluate("document.querySelector('[data-testid=\"send-button\"]').click()")
  625. while True:
  626. if cls._api_key is not None or not cls.needs_auth:
  627. break
  628. body = await page.evaluate("JSON.stringify(window.__remixContext)")
  629. if body:
  630. match = re.search(r'"accessToken":"(.*?)"', body)
  631. if match:
  632. cls._api_key = match.group(1)
  633. break
  634. await asyncio.sleep(1)
  635. while True:
  636. if RequestConfig.proof_token:
  637. break
  638. await asyncio.sleep(1)
  639. RequestConfig.data_build = await page.evaluate("document.documentElement.getAttribute('data-build')")
  640. RequestConfig.cookies = await page.send(get_cookies([cls.url]))
  641. await page.close()
  642. cls._create_request_args(RequestConfig.cookies, RequestConfig.headers, user_agent=user_agent)
  643. cls._set_api_key(cls._api_key)
  644. @staticmethod
  645. def get_default_headers() -> Dict[str, str]:
  646. return {
  647. **DEFAULT_HEADERS,
  648. "content-type": "application/json",
  649. }
  650. @classmethod
  651. def _create_request_args(cls, cookies: Cookies = None, headers: dict = None, user_agent: str = None):
  652. cls._headers = cls.get_default_headers() if headers is None else headers
  653. if user_agent is not None:
  654. cls._headers["user-agent"] = user_agent
  655. cls._cookies = {} if cookies is None else cookies
  656. cls._update_cookie_header()
  657. @classmethod
  658. def _update_request_args(cls, auth_result: AuthResult, session: StreamSession):
  659. if hasattr(auth_result, "cookies"):
  660. for c in session.cookie_jar if hasattr(session, "cookie_jar") else session.cookies.jar:
  661. auth_result.cookies[getattr(c, "key", getattr(c, "name", ""))] = c.value
  662. cls._cookies = auth_result.cookies
  663. cls._update_cookie_header()
  664. @classmethod
  665. def _set_api_key(cls, api_key: str):
  666. if api_key:
  667. exp = api_key.split(".")[1]
  668. exp = (exp + "=" * (4 - len(exp) % 4)).encode()
  669. cls._expires = json.loads(base64.b64decode(exp)).get("exp")
  670. debug.log(f"OpenaiChat: API key expires at\n {cls._expires} we have:\n {time.time()}")
  671. if time.time() > cls._expires:
  672. debug.log(f"OpenaiChat: API key is expired")
  673. else:
  674. cls._api_key = api_key
  675. cls._headers["authorization"] = f"Bearer {api_key}"
  676. return True
  677. return False
  678. @classmethod
  679. def _update_cookie_header(cls):
  680. if cls._cookies:
  681. cls._headers["cookie"] = format_cookies(cls._cookies)
  682. class Conversation(JsonConversation):
  683. """
  684. Class to encapsulate response fields.
  685. """
  686. def __init__(self, conversation_id: str = None, message_id: str = None, user_id: str = None, finish_reason: str = None, parent_message_id: str = None):
  687. self.conversation_id = conversation_id
  688. self.message_id = message_id
  689. self.finish_reason = finish_reason
  690. self.is_recipient = False
  691. self.parent_message_id = message_id if parent_message_id is None else parent_message_id
  692. self.user_id = user_id
  693. def get_cookies(
  694. urls: Optional[Iterator[str]] = None
  695. ) -> Generator[Dict, Dict, Dict[str, str]]:
  696. params = {}
  697. if urls is not None:
  698. params['urls'] = [i for i in urls]
  699. cmd_dict = {
  700. 'method': 'Network.getCookies',
  701. 'params': params,
  702. }
  703. json = yield cmd_dict
  704. return {c["name"]: c["value"] for c in json['cookies']} if 'cookies' in json else {}