CharacterAI icon indicating copy to clipboard operation
CharacterAI copied to clipboard

Keeps giving me errors.

Open TwangyMoney opened this issue 2 years ago • 11 comments

I've tried this example code and it doesn't seem to function:

from characterai import PyCAI

client = PyCAI('TOKEN')

char = input('Enter CHAR: ')

chat = client.chat.get_chat(char)

participants = chat['participants']

if not participants[0]['is_human']:
    tgt = participants[0]['user']['username']
else:
    tgt = participants[1]['user']['username']

while True:
    message = input('You: ')

    data = client.chat.send_message(
        chat['external_id'], tgt, message
    )

    name = data['src_char']['participant']['name']
    text = data['replies'][0]['text']

    print(f"{name}: {text}")

This is the error it gives me:

    Traceback (most recent call last):
  File "079.py", line 7, in <module>
    chat = client.chat.get_chat(char)
  File "C:\Users\Twangy Money\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\characterai\characterai.py", line 531, in get_chat
    return PyCAI.request(
  File "C:\Users\Twangy Money\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\characterai\characterai.py", line 69, in request
    data = response.json()
  File "C:\Users\Twangy Money\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\tls_client\response.py", line 39, in json
    return json.loads(self.text, **kwargs)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\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 1 (char 0)

I also used another example code provided by someone

from characterai import PyCAI

my_token = '39b1fa309f245479a9440cebb1af394399ac90a' # This is your personal token, let me know if you need help getting yours

# Create the client
client = PyCAI(my_token)

#Character ID, found in the url of the character
char = 'OYYf4iM6fjt9eZ72oXRsY3UGPeXd9Y-uJwfAjF5JAwk' # SM64 Mario

message = input("Your message: ") # Wait for user input...

#Send the message, wait for the response
response = client.chat.send_message(char, message)

#Get last response
last_msg = response['replies'][0]['text'] # Not sure what the "response" object looks like, but this get's the message sent by the character

print(last_msg)

It gave me this error:

Traceback (most recent call last):
  File "079.py", line 14, in <module>
    response = client.chat.send_message(char, message)
TypeError: send_message() missing 1 required positional argument: 'text'

Help? Also don't worry that token there isn't mine it's a broken version of a token provided by someone else.

TwangyMoney avatar Oct 19 '23 06:10 TwangyMoney

same

bomjara62 avatar Oct 20 '23 10:10 bomjara62

I've found that. Your problem must be in using wrong chat, Use chat2 instead of just chat

import asyncio
from characterai import PyAsyncCAI

async def main():
    client = PyAsyncCAI('TOKEN')

    char = input('Enter CHAR: ')

    chat = await client.chat2.get_chat(char)

    author = {
        'author_id': chat['chats'][0]['creator_id']
    }

    while True:
        message = input('You: ')

        async with client.connect() as chat2:
            data = await chat2.send_message(
                char, chat['chats'][0]['chat_id'], 
                message, author
            )

        name = data['turn']['author']['name']
        text = data['turn']['candidates'][0]['raw_content']

        print(f"{name}: {text}")

asyncio.run(main())

bomjara62 avatar Oct 20 '23 10:10 bomjara62

I've found that. Your problem must be in using wrong chat, Use chat2 instead of just chat

import asyncio
from characterai import PyAsyncCAI

async def main():
    client = PyAsyncCAI('TOKEN')

    char = input('Enter CHAR: ')

    chat = await client.chat2.get_chat(char)

    author = {
        'author_id': chat['chats'][0]['creator_id']
    }

    while True:
        message = input('You: ')

        async with client.connect() as chat2:
            data = await chat2.send_message(
                char, chat['chats'][0]['chat_id'], 
                message, author
            )

        name = data['turn']['author']['name']
        text = data['turn']['candidates'][0]['raw_content']

        print(f"{name}: {text}")

asyncio.run(main())

well still got error.

zYxDevs avatar Oct 22 '23 10:10 zYxDevs

I found a fix from a discord server. But uh, idk how to explain it. The error is fixed for me now I just used another person's example code.

TwangyMoney avatar Oct 22 '23 14:10 TwangyMoney

I found a fix from a discord server. But uh, idk how to explain it. The error is fixed for me now I just used another person's example code.

can i ask you why you reply this message without any solution, even when you have solution? give solution. this is issues, to find solution.

zYxDevs avatar Oct 22 '23 14:10 zYxDevs

I didn't give a solution as I replied through gmail not from the website and I was busy.

On Sun, Oct 22, 2023 at 8:28 PM Yoga Pranata @.***> wrote:

I found a fix from a discord server. But uh, idk how to explain it. The error is fixed for me now I just used another person's example code.

can i ask you why you reply this message without any solution, even when you have solution? give solution. this is issues, to find solution.

— Reply to this email directly, view it on GitHub https://github.com/kramcat/CharacterAI/issues/59#issuecomment-1774110050, or unsubscribe https://github.com/notifications/unsubscribe-auth/AU5VVYRE7UH3QTAHW5DWRGDYAUUSRAVCNFSM6AAAAAA6GUZJNWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZUGEYTAMBVGA . You are receiving this because you authored the thread.Message ID: @.***>

TwangyMoney avatar Oct 24 '23 06:10 TwangyMoney

So, as per user zYxDevs and everyone's request, I'm writing this to let you guys know how I got it to work. I did this from python 3.11.6 and on the latest version of character.ai which currently is characterai==0.8.0 To install python 3.11.6 go to python website here: https://www.python.org/downloads/release/python-3116/ To install characterai==0.8.0, make sure you have python installed first and then run:

pip3.11 install characterai==0.8.0

Now, first of all, make sure you go to the characterai website and, whichever character you chose go to it's chat and write 1 message there and let the AI reply. In this specific code we're using chat2 so in the url (for example https://beta.character.ai/chat?char=X4Q7WpO2BcDrxSBtFtrTkJJR3os46b0clYOtoigyWD4) make sure it says chat2 instead for chat after beta.character.ai/ (for example https://beta.character.ai/chat2?char=X4Q7WpO2BcDrxSBtFtrTkJJR3os46b0clYOtoigyWD4) and then write 1 message there after making sure the url says chat2. Now with that said, this is how the python code works:

client = PyAsyncCAI('CHARACTER_AI_TOKEN')
char = 'CHARACTER_ID'

chat = await client.chat2.get_chat(char)
author = {'author_id': chat['chats'][0]['creator_id']}

msg1 = "Hello."

async with client.connect() as chat2:
        data = await chat2.send_message(
            char, chat['chats'][0]['chat_id'], 
            msg1, author
        )
text = data['turn']['candidates'][0]['raw_content']

Here, the msg1 variable is where we're sending our message. The msg1 variable will be the message we're sending to the bot. And the text variable is the bot's reply. When using this make sure to await so it can finish loading the bot's message. For example in cases of discord bots, you can await like this:

await message.channel.send(text)

Anywhere else similarly. This way the variable has time to load the bot's message and thus can work correctly. These are all the things you need to make sure so that your code works. Or so I hope. For further help message me on Discord: realtwangy

TwangyMoney avatar Oct 24 '23 06:10 TwangyMoney

So, as per user zYxDevs and everyone's request, I'm writing this to let you guys know how I got it to work. I did this from python 3.11.6 and on the latest version of character.ai which currently is characterai==0.8.0 To install python 3.11.6 go to python website here: https://www.python.org/downloads/release/python-3116/ To install characterai==0.8.0, make sure you have python installed first and then run:

pip3.11 install characterai==0.8.0

Now, first of all, make sure you go to the characterai website and, whichever character you chose go to it's chat and write 1 message there and let the AI reply. In this specific code we're using chat2 so in the url (for example https://beta.character.ai/chat?char=X4Q7WpO2BcDrxSBtFtrTkJJR3os46b0clYOtoigyWD4) make sure it says chat2 instead for chat after beta.character.ai/ (for example https://beta.character.ai/chat2?char=X4Q7WpO2BcDrxSBtFtrTkJJR3os46b0clYOtoigyWD4) and then write 1 message there after making sure the url says chat2. Now with that said, this is how the python code works:

client = PyAsyncCAI(os.environ['CHARACTER_AI_TOKEN'])
char = os.environ['CHARACTER_ID']

chat = await client.chat2.get_chat(char)
author = {'author_id': chat['chats'][0]['creator_id']}

msg1 = "Hello."

async with client.connect() as chat2:
        data = await chat2.send_message(
            char, chat['chats'][0]['chat_id'], 
            msg1, author
        )
text = data['turn']['candidates'][0]['raw_content']

Here, the msg1 variable is where we're sending our message. The msg1 variable will be the message we're sending to the bot. And the text variable is the bot's reply. When using this make sure to await so it can finish loading the bot's message. For example in cases of discord bots, you can await like this:

await message.channel.send(text)

Anywhere else similarly. This way the variable has time to load the bot's message and thus can work correctly. These are all the things you need to make sure so that your code works. Or so I hope. For further help message me on Discord: realtwangy

this is very helpful, after reading this i found my mistake is chat not chat2, after changed to chat2 on character ai web and chatting to the bot its work fine. thank you bro!

zYxDevs avatar Oct 25 '23 01:10 zYxDevs

So, as per user zYxDevs and everyone's request, I'm writing this to let you guys know how I got it to work. I did this from python 3.11.6 and on the latest version of character.ai which currently is characterai==0.8.0 To install python 3.11.6 go to python website here: https://www.python.org/downloads/release/python-3116/ To install characterai==0.8.0, make sure you have python installed first and then run:

pip3.11 install characterai==0.8.0

Now, first of all, make sure you go to the characterai website and, whichever character you chose go to it's chat and write 1 message there and let the AI reply. In this specific code we're using chat2 so in the url (for example https://beta.character.ai/chat?char=X4Q7WpO2BcDrxSBtFtrTkJJR3os46b0clYOtoigyWD4) make sure it says chat2 instead for chat after beta.character.ai/ (for example https://beta.character.ai/chat2?char=X4Q7WpO2BcDrxSBtFtrTkJJR3os46b0clYOtoigyWD4) and then write 1 message there after making sure the url says chat2. Now with that said, this is how the python code works:

client = PyAsyncCAI(os.environ['CHARACTER_AI_TOKEN'])
char = os.environ['CHARACTER_ID']

chat = await client.chat2.get_chat(char)
author = {'author_id': chat['chats'][0]['creator_id']}

msg1 = "Hello."

async with client.connect() as chat2:
        data = await chat2.send_message(
            char, chat['chats'][0]['chat_id'], 
            msg1, author
        )
text = data['turn']['candidates'][0]['raw_content']

Here, the msg1 variable is where we're sending our message. The msg1 variable will be the message we're sending to the bot. And the text variable is the bot's reply. When using this make sure to await so it can finish loading the bot's message. For example in cases of discord bots, you can await like this:

await message.channel.send(text)

Anywhere else similarly. This way the variable has time to load the bot's message and thus can work correctly. These are all the things you need to make sure so that your code works. Or so I hope. For further help message me on Discord: realtwangy

this is very helpful, after reading this i found my mistake is chat not chat2, after changed to chat2 on character ai web and chatting to the bot its work fine. thank you bro!

Np also both chat and chat2 will work, just if you use chat you also needa change the url from chat2 to chat and then write a message, and then your code will work.

TwangyMoney avatar Oct 25 '23 14:10 TwangyMoney

What can we do if we want to use the synchronous API (PyCAI)?

image

client = PyCAI(token)
char = 'mPKDui2e1V7mfbLxTv6S5eu8WTAAwzXmdo1iwfo79IM'
chat = client.chat2.get_chat(char)
author = {'author_id': chat['chats'][0]['creator_id']}

gives AttributeError: 'PyCAI' object has no attribute 'chat2'. Did you mean: 'chat'?

IamFonky avatar Nov 21 '23 15:11 IamFonky

I don't think PyCAI has chat2 idk so, that will be client = PyAsyncCAI(token) not PyCAI(token) or you can use just chat, and make sure you message the ai from the url being chat instead for chat2.

TwangyMoney avatar Nov 22 '23 05:11 TwangyMoney