[Python] Character AI API closes connection when it shouldn't

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I've tried breeding speech recognition and character.AI (because it has nice free api). When I try to send a message the connection suddenly drops.

    I've tested "Simple Example" from github.com/kramcat/CharacterAI and it works.

    Here's the code:

    async def main():
    char = "GddGh5qJnuDMc_60Ldh-KhoRZy4HSu4jD6e66kp8Htg"
    client = aiocai.Client('b311ec5d7a71f09ec30bdec1b377672bea63bcdf')
    me = await client.get_me()
    async with await client.connect() as chat:
    new, answer = await chat.new_chat(char, me.id)
    speak(answer.text)

    while True:

    if os.path.exists("recorded_speech.wav"):
    os.remove("recorded_speech.wav")
    text = record()
    print(text)

    message = await chat.send_message(char, new.chat_id, text)
    speak(f'{message.text}')


    speak() is just a SAM tts. It works fine.
    record() is simple stt, it works too.
    text contains string with recorded voice

    But when I get to message = await chat.send_message(char, new.chat_id, text) it gives me this exception:

    Traceback (most recent call last):
    File "C:\Users\Admin\Desktop\python\tts\brain.py", line 97, in <module>
    print(asyncio.run(main()))
    File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
    File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
    File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 685, in run_until_complete
    return future.result()
    File "C:\Users\Admin\Desktop\python\tts\brain.py", line 93, in main
    message = await chat.send_message(char, new.chat_id, text)
    File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\characterai\aiocai\methods\chat2.py", line 360, in send_message
    await self.ws.send(json.dumps(message))
    File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\legacy\protocol.py", line 628, in send
    await self.ensure_open()
    File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\legacy\protocol.py", line 929, in ensure_open
    raise self.connection_closed_exc()
    websockets.exceptions.ConnectionClosedOK: sent 1000 (OK); then received 1000 (OK)


    basically, it has to write CAI response into message and then voice it with SAM. in fact I didn't change "Simple Example" code at all, just swapped out text = input('YOU: ') for simple voice recognition.

    Continue reading...

Compartilhe esta Página