Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
Automatically detect if official can reach
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed May 28, 2023
1 parent 55dff85 commit 2c0cbaa
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/revChatGPT/V1.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,21 @@ def __init__(
self.conversation_id_prev_queue = []
self.parent_id_prev_queue = []
self.lazy_loading = lazy_loading
self.base_url = base_url or BASE_URL
self.recipients = RecipientManager()
self.disable_history = config.get("disable_history", False)

self.__check_credentials()
# Check if chat.openai.com is reachable
if not base_url:
response = self.session.get("https://chat.openai.com/backend-api/accounts/check", impersonate='safari15_5')
if response.status_code != 200:
print(f"Using bypass.churchless.tech backend due to status code {response.status_code}")
self.base_url = BASE_URL
else:
print("Using chat.openai.com backend")
self.base_url = "https://chat.openai.com/backend-api/"
else:
self.base_url = base_url

@logger(is_timed=True)
def __check_credentials(self) -> None:
Expand Down Expand Up @@ -345,7 +355,7 @@ def __send_request(
url=f"{self.base_url}conversation",
data=json.dumps(data),
timeout=timeout,
impersonate='chrome110',
impersonate='safari15_5',
content_callback=response_file.write, # a hack around curl_cffi not supporting stream=True
)
self.__check_response(response)
Expand Down Expand Up @@ -693,7 +703,7 @@ def get_conversations(
:param limit: Integer
"""
url = f"{self.base_url}conversations?offset={offset}&limit={limit}"
response = self.session.get(url, impersonate='chrome110')
response = self.session.get(url, impersonate='safari15_5')
self.__check_response(response)
if encoding is not None:
response.encoding = encoding
Expand All @@ -708,7 +718,7 @@ def get_msg_history(self, convo_id: str, encoding: str | None = None) -> list:
:param encoding: String
"""
url = f"{self.base_url}conversation/{convo_id}"
response = self.session.get(url, impersonate='chrome110')
response = self.session.get(url, impersonate='safari15_5')
self.__check_response(response)
if encoding is not None:
response.encoding = encoding
Expand All @@ -724,7 +734,7 @@ def gen_title(self, convo_id: str, message_id: str) -> str:
data=json.dumps(
{"message_id": message_id, "model": "text-davinci-002-render"},
),
impersonate='chrome110'
impersonate='safari15_5'
)
self.__check_response(response)
return response.json().get("title", "Error generating title")
Expand All @@ -737,7 +747,7 @@ def change_title(self, convo_id: str, title: str) -> None:
:param title: String
"""
url = f"{self.base_url}conversation/{convo_id}"
response = self.session.patch(url, data=json.dumps({"title": title}), impersonate='chrome110')
response = self.session.patch(url, data=json.dumps({"title": title}), impersonate='safari15_5')
self.__check_response(response)

@logger(is_timed=True)
Expand All @@ -747,7 +757,7 @@ def delete_conversation(self, convo_id: str) -> None:
:param id: UUID of conversation
"""
url = f"{self.base_url}conversation/{convo_id}"
response = self.session.patch(url, data='{"is_visible": false}', impersonate='chrome110')
response = self.session.patch(url, data='{"is_visible": false}', impersonate='safari15_5')
self.__check_response(response)

@logger(is_timed=True)
Expand All @@ -756,7 +766,7 @@ def clear_conversations(self) -> None:
Delete all conversations
"""
url = f"{self.base_url}conversations"
response = self.session.patch(url, data='{"is_visible": false}', impersonate='chrome110')
response = self.session.patch(url, data='{"is_visible": false}', impersonate='safari15_5')
self.__check_response(response)

@logger(is_timed=False)
Expand Down Expand Up @@ -790,7 +800,7 @@ def rollback_conversation(self, num: int = 1) -> None:
@logger(is_timed=True)
def get_plugins(self, offset: int = 0, limit: int = 250, status: str = "approved"):
url = f"{self.base_url}aip/p?offset={offset}&limit={limit}&statuses={status}"
response = self.session.get(url, impersonate='chrome110')
response = self.session.get(url, impersonate='safari15_5')
self.__check_response(response)
# Parse as JSON
return json.loads(response.text)
Expand All @@ -799,7 +809,7 @@ def get_plugins(self, offset: int = 0, limit: int = 250, status: str = "approved
def install_plugin(self, plugin_id: str):
url = f"{self.base_url}aip/p/{plugin_id}/user-settings"
payload = {"is_installed": True}
response = self.session.patch(url, data=json.dumps(payload), impersonate='chrome110')
response = self.session.patch(url, data=json.dumps(payload), impersonate='safari15_5')
self.__check_response(response)

@logger(is_timed=False)
Expand Down Expand Up @@ -861,7 +871,7 @@ async def __send_request(
url=f"{self.base_url}conversation",
data=json.dumps(data),
timeout=timeout,
impersonate='chrome110',
impersonate='safari15_5',
content_callback=response_file.write,
)
await self.__check_response(response)
Expand Down Expand Up @@ -1138,7 +1148,7 @@ async def get_conversations(self, offset: int = 0, limit: int = 20) -> list:
:param limit: Integer
"""
url = f"{self.base_url}conversations?offset={offset}&limit={limit}"
response = await self.session.get(url, impersonate='chrome110')
response = await self.session.get(url, impersonate='safari15_5')
await self.__check_response(response)
data = json.loads(response.text)
return data["items"]
Expand All @@ -1153,7 +1163,7 @@ async def get_msg_history(
:param id: UUID of conversation
"""
url = f"{self.base_url}conversation/{convo_id}"
response = await self.session.get(url, impersonate='chrome110')
response = await self.session.get(url, impersonate='safari15_5')
if encoding is not None:
response.encoding = encoding
await self.__check_response(response)
Expand All @@ -1169,7 +1179,7 @@ async def gen_title(self, convo_id: str, message_id: str) -> None:
url,
data=json.dumps(
{"message_id": message_id, "model": "text-davinci-002-render"},
), impersonate='chrome110'
), impersonate='safari15_5'
)
await self.__check_response(response)

Expand All @@ -1180,7 +1190,7 @@ async def change_title(self, convo_id: str, title: str) -> None:
:param title: String
"""
url = f"{self.base_url}conversation/{convo_id}"
response = await self.session.patch(url, data=f'{{"title": "{title}"}}', impersonate='chrome110')
response = await self.session.patch(url, data=f'{{"title": "{title}"}}', impersonate='safari15_5')
await self.__check_response(response)

async def delete_conversation(self, convo_id: str) -> None:
Expand All @@ -1189,15 +1199,15 @@ async def delete_conversation(self, convo_id: str) -> None:
:param convo_id: UUID of conversation
"""
url = f"{self.base_url}conversation/{convo_id}"
response = await self.session.patch(url, data='{"is_visible": false}', impersonate='chrome110')
response = await self.session.patch(url, data='{"is_visible": false}', impersonate='safari15_5')
await self.__check_response(response)

async def clear_conversations(self) -> None:
"""
Delete all conversations
"""
url = f"{self.base_url}conversations"
response = await self.session.patch(url, data='{"is_visible": false}', impersonate='chrome110')
response = await self.session.patch(url, data='{"is_visible": false}', impersonate='safari15_5')
await self.__check_response(response)

async def __map_conversations(self) -> None:
Expand Down

0 comments on commit 2c0cbaa

Please sign in to comment.