Simatwa / WebChatGPT

Python SDK/API for ChatGPT Web-Version
GNU General Public License v3.0
86 stars 21 forks source link

RequestsJSONDecodeError on ChatGPT(...) #2

Closed Almas-Ali closed 6 months ago

Almas-Ali commented 6 months ago
from WebChatGPT import ChatGPT

bot = ChatGPT(
    "chat.openai.com.cookies.json",
)

response = bot.ask('Hello, how are you?')
print(response)
Traceback (most recent call last):
  File "**/.venv/lib/python3.11/site-packages/requests/models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 2 (char 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "**/main.py", line 4, in <module>
    bot = ChatGPT(
          ^^^^^^^^
  File "**/.venv/lib/python3.11/site-packages/WebChatGPT/main.py", line 35, in __init__
    self.session.headers.update(utils.get_request_headers_and_append_auth(self))
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "**/.venv/lib/python3.11/site-packages/WebChatGPT/utils.py", line 64, in get_request_headers_and_append_auth
    self.auth = resp.json()
                ^^^^^^^^^^^
  File "**/.venv/lib/python3.11/site-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

I think you need to use pydantic to easily handle those JSON responses as a Python model. I am highly interested in this project and would like to test it out. I did think about this month but didn't get much time to do so.

Simatwa commented 6 months ago

Thanks for that. Could you possibly give it a PR?

Almas-Ali commented 6 months ago

Will try.

Almas-Ali commented 6 months ago

@Simatwa do you use discord or telegram? We may talk there. Drop your account handle here for sometime then delete it.

Simatwa commented 6 months ago

I prefer not to share bruh.

tobwen commented 6 months ago

That's easy to fix: https://github.com/Simatwa/WebChatGPT/blob/d6a124a957fe70bee50e74635cdf0d889c4a1cd7/WebChatGPT/utils.py#L15

You're requesting a compressed response, but you're not decompressing it. Either comment this out (which might be detected by OpenAI) or decompress the response.

Simatwa commented 6 months ago

B

That's easy to fix:

https://github.com/Simatwa/WebChatGPT/blob/d6a124a957fe70bee50e74635cdf0d889c4a1cd7/WebChatGPT/utils.py#L15

You're requesting a compressed response, but you're not decompressing it. Either comment this out (which might be detected by OpenAI) or decompress the response.

Commenting it out might cause detection especially through user-agent leaks. Brotli auto-handles that if it's installed. I'll add it as one of the dependencies asap.

Almas-Ali commented 6 months ago

I tried brotli last night but couldn’t make it work. Is it working now?

Simatwa commented 6 months ago

On this side. It's a yes.