From f836488266e06539ab2fbeebc6ecc4f941a3dcfb Mon Sep 17 00:00:00 2001 From: zai <122824602+Zai-Kun@users.noreply.github.com> Date: Thu, 28 Dec 2023 19:11:45 +0500 Subject: [PATCH 1/5] Add feature for async --- re_gpt/async_chatgpt.py | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/re_gpt/async_chatgpt.py b/re_gpt/async_chatgpt.py index dec54cd..7d3c85c 100644 --- a/re_gpt/async_chatgpt.py +++ b/re_gpt/async_chatgpt.py @@ -144,7 +144,7 @@ def content_callback(chunk): response_queue.put_nowait(chunk) url = CHATGPT_API.format("conversation") - response = await self.chatgpt.session.post( + await self.chatgpt.session.post( url=url, headers=self.chatgpt.build_request_headers(), json=payload, @@ -152,7 +152,7 @@ def content_callback(chunk): ) await response_queue.put(None) - stream_task = asyncio.create_task(perform_request()) + asyncio.create_task(perform_request()) while True: chunk = await response_queue.get() @@ -174,7 +174,9 @@ async def build_message_payload(self, user_input: str) -> dict: "conversation_mode": {"conversation_mode": {"kind": "primary_assistant"}}, "conversation_id": self.conversation_id, "action": "next", - "arkose_token": await self.arkose_token_generator(), + "arkose_token": await self.arkose_token_generator() + if self.chatgpt.generate_arkose_token + else None, "force_paragen": False, "history_and_training_disabled": False, "messages": [ @@ -203,7 +205,9 @@ async def build_message_continuation_payload(self) -> dict: payload = { "conversation_mode": {"conversation_mode": {"kind": "primary_assistant"}}, "action": "continue", - "arkose_token": await self.arkose_token_generator(), + "arkose_token": await self.arkose_token_generator() + if self.chatgpt.generate_arkose_token + else None, "conversation_id": self.conversation_id, "force_paragen": False, "history_and_training_disabled": False, @@ -221,6 +225,15 @@ async def arkose_token_generator(self) -> str: Returns: str: Arkose token. """ + if not self.chatgpt.tried_downloading_binary: + self.chatgpt.binary_path = await async_get_binary_path(self.chatgpt.session) + + if self.chatgpt.binary_path: + self.chatgpt.arkose = ctypes.CDLL(self.chatgpt.binary_path) + self.chatgpt.arkose.GetToken.restype = ctypes.c_char_p + + self.chatgpt.tried_downloading_binary = True + if self.chatgpt.binary_path: try: result = self.chatgpt.arkose.GetToken() @@ -285,6 +298,7 @@ def __init__( session_token: Optional[str] = None, exit_callback_function: Optional[Callable] = None, auth_token: Optional[str] = None, + generate_arkose_token: Optional[bool] = False, ): """ Initializes an instance of the class. @@ -294,10 +308,16 @@ def __init__( session_token (Optional[str]): A session token. Defaults to None. exit_callback_function (Optional[callable]): A function to be called on exit. Defaults to None. auth_token (Optional[str]): An authentication token. Defaults to None. + generate_arkose_token (Optional[bool]): Toggle whether to generate and send arkose-token in the payload. Defaults to False. """ self.proxies = proxies self.exit_callback_function = exit_callback_function + self.arkose = None + self.binary_path = None + self.tried_downloading_binary = False + self.generate_arkose_token = generate_arkose_token + self.session_token = session_token self.auth_token = auth_token self.session = None @@ -306,11 +326,14 @@ async def __aenter__(self): self.session = AsyncSession( impersonate="chrome110", timeout=99999, proxies=self.proxies ) - self.binary_path = await async_get_binary_path(self.session) + if self.generate_arkose_token: + self.binary_path = await async_get_binary_path(self.session) + + if self.binary_path: + self.arkose = ctypes.CDLL(self.binary_path) + self.arkose.GetToken.restype = ctypes.c_char_p - if self.binary_path: - self.arkose = ctypes.CDLL(self.binary_path) - self.arkose.GetToken.restype = ctypes.c_char_p + self.tried_downloading_binary = True if not self.auth_token: if self.session_token is None: @@ -319,7 +342,7 @@ async def __aenter__(self): return self - async def __aexit__(self, *args): + async def __aexit__(self, *_): try: if self.exit_callback_function and callable(self.exit_callback_function): if not inspect.iscoroutinefunction(self.exit_callback_function): From 1489655f355308b6888f005181b39317221f2509 Mon Sep 17 00:00:00 2001 From: zai <122824602+Zai-Kun@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:43:17 +0500 Subject: [PATCH 2/5] Add feature for sync gpt --- re_gpt/sync_chatgpt.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/re_gpt/sync_chatgpt.py b/re_gpt/sync_chatgpt.py index 9762e0b..c28a03c 100644 --- a/re_gpt/sync_chatgpt.py +++ b/re_gpt/sync_chatgpt.py @@ -176,7 +176,9 @@ def build_message_payload(self, user_input: str) -> dict: "conversation_mode": {"conversation_mode": {"kind": "primary_assistant"}}, "conversation_id": self.conversation_id, "action": "next", - "arkose_token": self.arkose_token_generator(), + "arkose_token": self.arkose_token_generator() + if self.chatgpt.generate_arkose_token + else None, "force_paragen": False, "history_and_training_disabled": False, "messages": [ @@ -205,7 +207,9 @@ def build_message_continuation_payload(self) -> dict: payload = { "conversation_mode": {"conversation_mode": {"kind": "primary_assistant"}}, "action": "continue", - "arkose_token": self.arkose_token_generator(), + "arkose_token": self.arkose_token_generator() + if self.chatgpt.generate_arkose_token + else None, "conversation_id": self.conversation_id, "force_paragen": False, "history_and_training_disabled": False, @@ -223,6 +227,15 @@ def arkose_token_generator(self) -> str: Returns: str: Arkose token. """ + if not self.chatgpt.tried_downloading_binary: + self.chatgpt.binary_path = sync_get_binary_path(self.chatgpt.session) + + if self.chatgpt.binary_path: + self.chatgpt.arkose = ctypes.CDLL(self.chatgpt.binary_path) + self.chatgpt.arkose.GetToken.restype = ctypes.c_char_p + + self.chatgpt.tried_downloading_binary = True + if self.chatgpt.binary_path: try: result = self.chatgpt.arkose.GetToken() @@ -280,11 +293,15 @@ def __enter__(self): self.session = Session( impersonate="chrome110", timeout=99999, proxies=self.proxies ) - self.binary_path = sync_get_binary_path(self.session) - if self.binary_path: - self.arkose = ctypes.CDLL(self.binary_path) - self.arkose.GetToken.restype = ctypes.c_char_p + if self.generate_arkose_token: + self.binary_path = sync_get_binary_path(self.session) + + if self.binary_path: + self.arkose = ctypes.CDLL(self.binary_path) + self.arkose.GetToken.restype = ctypes.c_char_p + + self.tried_downloading_binary = True if not self.auth_token: if self.session_token is None: From 91187040cec073088755c43c06c1190673a1b014 Mon Sep 17 00:00:00 2001 From: zai <122824602+Zai-Kun@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:45:39 +0500 Subject: [PATCH 3/5] Tiny improvement --- examples/async_complex_example.py | 2 +- examples/complex_example.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/async_complex_example.py b/examples/async_complex_example.py index 5407812..fc7d2ba 100644 --- a/examples/async_complex_example.py +++ b/examples/async_complex_example.py @@ -26,7 +26,7 @@ def print_chat(chat): Args: chat (dict): The chat data. """ - for key, message in chat.get("mapping", {}).items(): + for _, message in chat.get("mapping", {}).items(): if "message" in message and message["message"]["content"]["parts"][0]: role = message["message"]["author"]["role"] content = message["message"]["content"]["parts"][0] diff --git a/examples/complex_example.py b/examples/complex_example.py index 5ec5cd5..05af46e 100644 --- a/examples/complex_example.py +++ b/examples/complex_example.py @@ -20,7 +20,7 @@ def print_chat(chat): Args: chat (dict): The chat data. """ - for key, message in chat.get("mapping", {}).items(): + for _, message in chat.get("mapping", {}).items(): if "message" in message and message["message"]["content"]["parts"][0]: role = message["message"]["author"]["role"] content = message["message"]["content"]["parts"][0] From 0bb21e040fd8a2e5503a39795fccd866a10205af Mon Sep 17 00:00:00 2001 From: zai <122824602+Zai-Kun@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:48:07 +0500 Subject: [PATCH 4/5] Remove unnecessary libs --- requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 983216c..16f4c5e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1 @@ -cryptography curl_cffi==0.5.9 -pycryptodome From 3f4b867e4cd74ce938f95414528eac814d858282 Mon Sep 17 00:00:00 2001 From: zai <122824602+Zai-Kun@users.noreply.github.com> Date: Fri, 29 Dec 2023 02:51:16 +0500 Subject: [PATCH 5/5] Update version from 2.8.11 to 2.9.5 and remove unnecessary libs --- setup.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index e7aa9c1..c6e71fd 100644 --- a/setup.py +++ b/setup.py @@ -2,9 +2,9 @@ setup( name="re_gpt", - version="2.8.11", + version="2.9.5", author="Zai-Kun", - description="ChatGPT web version in Python. Basically, you can use the ChatGPT API for free without any limitations, just as in the web version.", + description="Unofficial reverse-engineered ChatGPT API in Python.", long_description=open("README.md").read(), long_description_content_type="text/markdown", url="https://github.com/Zai-Kun/reverse-engineered-chatgpt", @@ -12,9 +12,5 @@ "Bug Tracker": "https://github.com/Zai-Kun/reverse-engineered-chatgpt/issues", }, packages=find_packages(), - install_requires=[ - "curl_cffi==0.5.9", - "pycryptodome", - "cryptography", - ], + install_requires=["curl_cffi==0.5.9"], )