kramcat / CharacterAI

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

Error when attempting to get response from character #11

Closed alfuwu closed 1 year ago

alfuwu commented 1 year ago
File "C:\Users\alfred\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861, in callit
    func(*args)
  File "C:\dat\Python\testing\c.ai-py.py", line 20, in chat
    data = client.chat.send_message(cid, message)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alfred\AppData\Local\Programs\Python\Python311\Lib\site-packages\characterai\characterai.py", line 144, in send_message
    return json.loads('{"replies": ' + response.split(r'{"replies": ')[-1])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alfred\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alfred\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alfred\AppData\Local\Programs\Python\Python311\Lib\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 13 (char 12)

code:

from characterai import pyCAI
import sys
import time

def log(string, instant_text="", new_line=True, pause_at_periods=True, pause_at_commas=True, sleep=0.025):
    string = str(string)
    sys.stdout.write(instant_text)
    for i, c in enumerate(string):
        sys.stdout.write(c)
        sys.stdout.flush()
        if i + 1 < len(string):
            if c not in [".", "!", "?"] and pause_at_periods == True and (string[i + 1] != " " or i + 2 < len(string)):
                time.sleep(sleep*5)
            elif c == "," and pause_at_commas == True:
                time.sleep(sleep*2.25)
            else:
                time.sleep(sleep)
        elif c == "," and pause_at_commas == True:
            time.sleep(sleep*2.25)
        else:
            time.sleep(sleep)
    if new_line == True: sys.stdout.write("\n")

token = '[token removed]'
character = '[id removed]'

client = pyCAI(token)

while True:
    message = input('> ')
    data = client.chat.send_message(character, message)
    log(data["replies"][0]["text"], f"{data['src_char']['participant']['name']}: ")

strangely, this error doesn't occur when the message has to wait for Cloudflare or when its the first message sent to the library.

EDIT: I set the library to print the output so that I could attempt to find out what was wrong. When the error was thrown, this was the output, which is definitely not JSON:

<html>
  <head>
    <title>Internal Server Error</title>
  </head>
  <body>
    <h1><p>Internal Server Error</p></h1>

  <script defer src="https://static.cloudflareinsights.com/beacon.min.js/v52afc6f149f6479b8c77fa569edb01181681764108816" integrity="sha512-jGCTpDpBAYDGNYR5ztKt4BQPGef1P0giN6ZGVUi835kFF88FOmmn8jBQWNgrNd8g/Yu421NdgWhwQoaOPFflDw==" data-cf-beacon='{"rayId":"7ca3fd09cb924126","token":"de6c8a8324c04a50b30bdeb96bc8d410","version":"2023.4.0","si":100}' crossorigin="anonymous"></script>
</body>
</html>

From the above output, I assume that something is wrong with either the way the library fetches data or just server errors on c.ai's side, although I can use the site normally so I'm leaning towards the former.

kramcat commented 1 year ago

Sometimes there are server-side errors when sending a message

I do not know if my library is the source of the problem, but it seems to me that it is not

I partially fixed this in v0.5.0. Now, instead of an error, the source code of the site with an error will be returned

kramcat commented 1 year ago

JSON error and error 500 fixed on v0.6.0