OpenaiChat.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. from __future__ import annotations
  2. import uuid, json, asyncio, os
  3. from py_arkose_generator.arkose import get_values_for_request
  4. from asyncstdlib.itertools import tee
  5. from async_property import async_cached_property
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.support.ui import WebDriverWait
  8. from selenium.webdriver.support import expected_conditions as EC
  9. from ..base_provider import AsyncGeneratorProvider
  10. from ..helper import get_event_loop, format_prompt, get_cookies
  11. from ...webdriver import get_browser
  12. from ...typing import AsyncResult, Messages
  13. from ...requests import StreamSession
  14. models = {
  15. "gpt-3.5": "text-davinci-002-render-sha",
  16. "gpt-3.5-turbo": "text-davinci-002-render-sha",
  17. "gpt-4": "gpt-4",
  18. "gpt-4-gizmo": "gpt-4-gizmo"
  19. }
  20. class OpenaiChat(AsyncGeneratorProvider):
  21. url = "https://chat.openai.com"
  22. working = True
  23. needs_auth = True
  24. supports_gpt_35_turbo = True
  25. supports_gpt_4 = True
  26. _cookies: dict = {}
  27. @classmethod
  28. async def create(
  29. cls,
  30. prompt: str = None,
  31. model: str = "",
  32. messages: Messages = [],
  33. history_disabled: bool = False,
  34. action: str = "next",
  35. conversation_id: str = None,
  36. parent_id: str = None,
  37. **kwargs
  38. ) -> Response:
  39. if prompt:
  40. messages.append({
  41. "role": "user",
  42. "content": prompt
  43. })
  44. generator = cls.create_async_generator(
  45. model,
  46. messages,
  47. history_disabled=history_disabled,
  48. action=action,
  49. conversation_id=conversation_id,
  50. parent_id=parent_id,
  51. response_fields=True,
  52. **kwargs
  53. )
  54. return Response(
  55. generator,
  56. await anext(generator),
  57. action,
  58. messages,
  59. kwargs
  60. )
  61. @classmethod
  62. async def create_async_generator(
  63. cls,
  64. model: str,
  65. messages: Messages,
  66. proxy: str = None,
  67. timeout: int = 120,
  68. access_token: str = None,
  69. cookies: dict = None,
  70. auto_continue: bool = False,
  71. history_disabled: bool = True,
  72. action: str = "next",
  73. conversation_id: str = None,
  74. parent_id: str = None,
  75. response_fields: bool = False,
  76. **kwargs
  77. ) -> AsyncResult:
  78. if not model:
  79. model = "gpt-3.5"
  80. elif model not in models:
  81. raise ValueError(f"Model are not supported: {model}")
  82. if not parent_id:
  83. parent_id = str(uuid.uuid4())
  84. if not cookies:
  85. cookies = cls._cookies
  86. if not access_token:
  87. if not cookies:
  88. cls._cookies = cookies = get_cookies("chat.openai.com")
  89. if "access_token" in cookies:
  90. access_token = cookies["access_token"]
  91. if not access_token:
  92. login_url = os.environ.get("G4F_LOGIN_URL")
  93. if login_url:
  94. yield f"Please login: [ChatGPT]({login_url})\n\n"
  95. cls._cookies["access_token"] = access_token = await cls.browse_access_token(proxy)
  96. headers = {
  97. "Accept": "text/event-stream",
  98. "Authorization": f"Bearer {access_token}",
  99. }
  100. async with StreamSession(
  101. proxies={"https": proxy},
  102. impersonate="chrome110",
  103. headers=headers,
  104. timeout=timeout,
  105. cookies=dict([(name, value) for name, value in cookies.items() if name == "_puid"])
  106. ) as session:
  107. end_turn = EndTurn()
  108. while not end_turn.is_end:
  109. data = {
  110. "action": action,
  111. "arkose_token": await get_arkose_token(proxy, timeout),
  112. "conversation_id": conversation_id,
  113. "parent_message_id": parent_id,
  114. "model": models[model],
  115. "history_and_training_disabled": history_disabled and not auto_continue,
  116. }
  117. if action != "continue":
  118. prompt = format_prompt(messages) if not conversation_id else messages[-1]["content"]
  119. data["messages"] = [{
  120. "id": str(uuid.uuid4()),
  121. "author": {"role": "user"},
  122. "content": {"content_type": "text", "parts": [prompt]},
  123. }]
  124. async with session.post(f"{cls.url}/backend-api/conversation", json=data) as response:
  125. try:
  126. response.raise_for_status()
  127. except:
  128. raise RuntimeError(f"Error {response.status_code}: {await response.text()}")
  129. last_message = 0
  130. async for line in response.iter_lines():
  131. if not line.startswith(b"data: "):
  132. continue
  133. line = line[6:]
  134. if line == b"[DONE]":
  135. break
  136. try:
  137. line = json.loads(line)
  138. except:
  139. continue
  140. if "message" not in line:
  141. continue
  142. if "error" in line and line["error"]:
  143. raise RuntimeError(line["error"])
  144. if "message_type" not in line["message"]["metadata"]:
  145. continue
  146. if line["message"]["author"]["role"] != "assistant":
  147. continue
  148. if line["message"]["metadata"]["message_type"] in ("next", "continue", "variant"):
  149. conversation_id = line["conversation_id"]
  150. parent_id = line["message"]["id"]
  151. if response_fields:
  152. response_fields = False
  153. yield ResponseFields(conversation_id, parent_id, end_turn)
  154. new_message = line["message"]["content"]["parts"][0]
  155. yield new_message[last_message:]
  156. last_message = len(new_message)
  157. if "finish_details" in line["message"]["metadata"]:
  158. if line["message"]["metadata"]["finish_details"]["type"] == "stop":
  159. end_turn.end()
  160. if not auto_continue:
  161. break
  162. action = "continue"
  163. await asyncio.sleep(5)
  164. @classmethod
  165. async def browse_access_token(cls, proxy: str = None) -> str:
  166. def browse() -> str:
  167. driver = get_browser(proxy=proxy)
  168. try:
  169. driver.get(f"{cls.url}/")
  170. WebDriverWait(driver, 1200).until(
  171. EC.presence_of_element_located((By.ID, "prompt-textarea"))
  172. )
  173. javascript = """
  174. access_token = (await (await fetch('/api/auth/session')).json())['accessToken'];
  175. expires = new Date(); expires.setTime(expires.getTime() + 60 * 60 * 24 * 7); // One week
  176. document.cookie = 'access_token=' + access_token + ';expires=' + expires.toUTCString() + ';path=/';
  177. return access_token;
  178. """
  179. return driver.execute_script(javascript)
  180. finally:
  181. driver.quit()
  182. loop = get_event_loop()
  183. return await loop.run_in_executor(
  184. None,
  185. browse
  186. )
  187. async def get_arkose_token(proxy: str = None, timeout: int = None) -> str:
  188. config = {
  189. "pkey": "3D86FBBA-9D22-402A-B512-3420086BA6CC",
  190. "surl": "https://tcr9i.chat.openai.com",
  191. "headers": {
  192. "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
  193. },
  194. "site": "https://chat.openai.com",
  195. }
  196. args_for_request = get_values_for_request(config)
  197. async with StreamSession(
  198. proxies={"https": proxy},
  199. impersonate="chrome107",
  200. timeout=timeout
  201. ) as session:
  202. async with session.post(**args_for_request) as response:
  203. response.raise_for_status()
  204. decoded_json = await response.json()
  205. if "token" in decoded_json:
  206. return decoded_json["token"]
  207. raise RuntimeError(f"Response: {decoded_json}")
  208. class EndTurn():
  209. def __init__(self):
  210. self.is_end = False
  211. def end(self):
  212. self.is_end = True
  213. class ResponseFields():
  214. def __init__(
  215. self,
  216. conversation_id: str,
  217. message_id: str,
  218. end_turn: EndTurn
  219. ):
  220. self.conversation_id = conversation_id
  221. self.message_id = message_id
  222. self._end_turn = end_turn
  223. class Response():
  224. def __init__(
  225. self,
  226. generator: AsyncResult,
  227. fields: ResponseFields,
  228. action: str,
  229. messages: Messages,
  230. options: dict
  231. ):
  232. self.aiter, self.copy = tee(generator)
  233. self.fields = fields
  234. self.action = action
  235. self._messages = messages
  236. self._options = options
  237. def __aiter__(self):
  238. return self.aiter
  239. @async_cached_property
  240. async def message(self) -> str:
  241. return "".join([chunk async for chunk in self.copy])
  242. async def next(self, prompt: str, **kwargs) -> Response:
  243. return await OpenaiChat.create(
  244. **self._options,
  245. prompt=prompt,
  246. messages=await self.messages,
  247. action="next",
  248. conversation_id=self.fields.conversation_id,
  249. parent_id=self.fields.message_id,
  250. **kwargs
  251. )
  252. async def do_continue(self, **kwargs) -> Response:
  253. if self.end_turn:
  254. raise RuntimeError("Can't continue message. Message already finished.")
  255. return await OpenaiChat.create(
  256. **self._options,
  257. messages=await self.messages,
  258. action="continue",
  259. conversation_id=self.fields.conversation_id,
  260. parent_id=self.fields.message_id,
  261. **kwargs
  262. )
  263. async def variant(self, **kwargs) -> Response:
  264. if self.action != "next":
  265. raise RuntimeError("Can't create variant from continue or variant request.")
  266. return await OpenaiChat.create(
  267. **self._options,
  268. messages=self._messages,
  269. action="variant",
  270. conversation_id=self.fields.conversation_id,
  271. parent_id=self.fields.message_id,
  272. **kwargs
  273. )
  274. @async_cached_property
  275. async def messages(self):
  276. messages = self._messages
  277. messages.append({
  278. "role": "assistant", "content": await self.message
  279. })
  280. return messages
  281. @property
  282. def end_turn(self):
  283. return self.fields._end_turn.is_end