Skip to content

Commit

Permalink
fix: no caching of device id and sentinel token
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrasev committed Apr 11, 2024
1 parent 7929566 commit 6764127
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions chatrapper/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import json
import typing
from uuid import uuid4
import expiringdict
import httpx
import websockets
import logging
from websockets.exceptions import ConnectionClosedError, ConnectionClosedOK

BASE_URL = "https://chat.openai.com"
EXPDICT = expiringdict.ExpiringDict(max_len=100, max_age_seconds=120)


class RefreshTokenException(Exception):
Expand Down Expand Up @@ -80,24 +78,18 @@ def __init__(self,
self.device_id = str(uuid.uuid4())

async def get_new_session_id(self) -> str:
try:
return EXPDICT[self.device_id]
except KeyError:
self.device_id = str(uuid.uuid4())
headers = dict(ReqHeader(**{"oai-device-id": self.device_id}))

async with httpx.AsyncClient(timeout=30) as client:
response = await client.post(
f"{BASE_URL}/backend-anon/sentinel/chat-requirements",
headers=headers)
if response.status_code == 200:
data = response.json()
token = data.get('token')
EXPDICT[self.device_id] = token
return token
else:
logging.error(response.text)
logging.error("Failed to refresh session ID and token")
headers = dict(ReqHeader(**{"oai-device-id": self.device_id}))
async with httpx.AsyncClient(timeout=30) as client:
response = await client.post(
f"{BASE_URL}/backend-anon/sentinel/chat-requirements",
headers=headers)
if response.status_code == 200:
data = response.json()
token = data.get('token')
return token
else:
logging.error(response.text)
logging.error("Failed to refresh session ID and token")

def get_body(self, inputx: typing.Union[str, typing.List[typing.Dict]]) -> typing.Dict:
body = {
Expand Down

0 comments on commit 6764127

Please sign in to comment.