Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from Zai-Kun/toggle-on-or-off-arkose-token
Browse files Browse the repository at this point in the history
Added option to toggle arkose token generation on or off
  • Loading branch information
Zai-Kun authored Dec 28, 2023
2 parents 97dca5c + 3f4b867 commit ac2c3b7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/async_complex_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion examples/complex_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
41 changes: 32 additions & 9 deletions re_gpt/async_chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ 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,
content_callback=content_callback,
)
await response_queue.put(None)

stream_task = asyncio.create_task(perform_request())
asyncio.create_task(perform_request())

while True:
chunk = await response_queue.get()
Expand All @@ -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": [
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand Down
29 changes: 23 additions & 6 deletions re_gpt/sync_chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
cryptography
curl_cffi==0.5.9
pycryptodome
10 changes: 3 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

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",
project_urls={
"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"],
)

0 comments on commit ac2c3b7

Please sign in to comment.