kramcat / CharacterAI

Unofficial Python API for character.ai
https://docs.kram.cat
MIT License
379 stars 52 forks source link

Cannot chat to characters because of JSON error? #79

Open jynn12 opened 5 months ago

jynn12 commented 5 months ago

When I enter the character URL, I get this error

Traceback (most recent call last):
  File "/home/pandesal/chatbot/char.py", line 7, in <module>
    chat = client.chat.get_chat(char)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/characterai/characterai.py", line 531, in get_chat
    return PyCAI.request(
           ^^^^^^^^^^^^^^                                    File "/usr/lib/python3.11/site-packages/characterai/characterai.py", line 69, in request
    data = response.json()
           ^^^^^^^^^^^^^^^                                   File "/usr/lib/python3.11/site-packages/tls_client/response.py", line 39, in json
    return json.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 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
unhingedsoul commented 5 months ago

This error only occurs with chat2. All you have to do is go to the beta.character.ai website and open the chat with a character you want to chat with. Then you go to the link and change chat2 to chat like this:

https://beta.character.ai/chat2?char=xxx https://beta.character.ai/chat?char=xxx

That's it. You don't have to change anything in the code.

jynn12 commented 5 months ago

@unhingedsoul Thanks, it works now:)

Karvp commented 5 months ago

It looks like there are some misunderstood things here, so I think I should note something. The error occurred because the server's response to the API request wasn't JSON; probably it is returning: there is no history between user and character in this case.

jynn12 commented 5 months ago

@Karvp Nope, It has a chat history...like a really REALLY long chat history...

Its been around 2 weeks since I've been chatting with this bot using this code

jynn12 commented 5 months ago

Also, it seems that CharacterAI is using CloudFlare now? Could that be interfering with the code?

Karvp commented 5 months ago

Also, it seems that CharacterAI is using CloudFlare now? Could that be interfering with the code?

@jynn12 As long as you don't spam requests, imo :v

jynn12 commented 5 months ago

@Karvp Nah, the same code works on other characters, but not on the one I am using. Its chat and not chat2 btw

Karvp commented 5 months ago

@Karvp Nah, the same code works on other characters, but not on the one I am using. Its chat and not chat2 btw

Seems tricky. Try inspecting the code 😄

Btw, srr for the unclear reply

jynn12 commented 5 months ago

@Karvp Well, when I wrote this code, only me and God knows how it works. Now only God knows...

I'll try stripping the other functions from the code and will update here...

RodnoiD1m0n commented 4 months ago

@Karvp Nah, the same code works on other characters, but not on the one I am using. Its chat and not chat2 btw

im also using chat instead of chat2, but in my case, its works on one history of a character but not on other. oh, also it gives me this on send_message instead of get_chat. i tried debugging it, and it seems that characterai website returns 500 status code(internal server error) and api on line 67 tries parsing it to json which raises the error

RodnoiD1m0n commented 4 months ago

oh nvm, i did not notice that second history was using chat2, but for anyone wondering this fix from above should do:

This error only occurs with chat2. All you have to do is go to the beta.character.ai website and open the chat with a character you want to chat with. Then you go to the link and change chat2 to chat like this:

https://beta.character.ai/chat2?char=xxx https://beta.character.ai/chat?char=xxx

That's it. You don't have to change anything in the code.

(i get other error tho, but i think thats a separate issue)

edit: the other error was from pprint

AaronJohnson02 commented 4 months ago

This error only occurs with chat2. All you have to do is go to the beta.character.ai website and open the chat with a character you want to chat with. Then you go to the link and change chat2 to chat like this:

https://beta.character.ai/chat2?char=xxx https://beta.character.ai/chat?char=xxx

That's it. You don't have to change anything in the code.

Hey, So I've tried to use the input the following link: https://beta.character.ai/chat?char=4WOVrCApi4JYwfYwU2e5eDeFalLOkGBw6IfUZPX1XVQ&source=recent-chats

But i received the following error


File /opt/conda/lib/python3.10/site-packages/characterai/characterai.py:74, in PyCAI.request(url, session, token, method, data, split, neo)
     72     raise errors.ServerError(data['comment'])
     73 elif str(data).startswith("{'detail': 'Auth"):
---> 74     raise errors.AuthError('Invalid token')
     75 elif str(data).startswith("{'status': 'Error"):
     76     raise errors.ServerError(data['status'])

AuthError: Invalid token```
Karvp commented 4 months ago

@AaronJohnson02 In your case, just check ur token

kramcat commented 2 months ago

check with the new version