CharacterAI
CharacterAI copied to clipboard
Cannot create a new chat
Hello,
When I'm trying to create a new chat, I got this error : AttributeError: 'NoneType' object has no attribute 'send'
We can see in pyaisynccai.py:31 that the ws attribute is defined as None.
Am I doing something wrong or is it just a lack of implementation in the code?
~~Nope, there is no way to get the token anymore according to the documentation~~
Disregard - if you are getting that error ^ you have to be logged in.
Hi, try this:
client = PyAsyncCAI('CHARACTER_AI_TOKEN')
async with client.connect() as chat2:
#create your new chat here...
See the source for more details
@tominal I dont understand what you mean. I got the char_token and it's working great to send and receive messages, the problem here is that idk how to use the new_chat method because the websocket property is never defined :/
@Karvp Thank you for your answer.
Now I have another error : characterai.errors.ServerError: Unauthorized access for user_id: **********
The id here is not the char_token but just a number
Could you share your code and goal so we can understand your issue better?
Sure! The aim is to create a new chat everytime i run the code and then to speak with the character. Here is the simplified code
async def test():
client = PyAsyncCAI(token)
async with client.connect() as chat2:
chat = await chat2.new_chat(char_id,"idk",user_id)
author = {"author_id": chat["chats"][0]["creator_id"]}
while True:
message = input("You: ")
data = await chat2.send_message(char_id, chat["chats"][0]["chat_id"], message, author)
print(json.dumps(data))
name = data["turn"]["author"]["name"]
text = data["turn"]["candidates"][0]["raw_content"]
print(f"{name}: {text}")
asyncio.run(test())
Edit : I forgot to put the call to the client sry
Did you use the correct user id? Are you the owner of the character? If not, is the character public?
Edit: I have fixed a few mistake in your code:
import uuid
import characterai, asyncio, json
def print_response(data: dict):
name, text = data["turn"]["author"]["name"], data["turn"]["candidates"][0]["raw_content"]
print(f'{name}: {text}')
async def test():
char_id = "CHARACTER_ID"
user_id = "USER_ID"
client = characterai.PyAsyncCAI("TOKEN")
async with client.connect() as chat2:
# generate the chat_id
chat_id = str(uuid.uuid4())
# The new_chat returns a tuple
chat, greeting = await chat2.new_chat(char_id,chat_id,user_id)
# This is the correct way to get creator_id
author = {"author_id": chat["chat"]["creator_id"]}
# Correct way to get chat_id
# chat_id = chat["chat"]["chat_id"]
while True:
message = input("You: ")
data = await chat2.send_message(char_id, chat_id, message, author)
print(json.dumps(data))
print_response(data)
asyncio.run(test())
I have added the chat_id creator, see uuid.uuid4(). Please note that you can only create 1 chat with an id.
P/s: This is the dictionary structure of the chat variable, if needed:
{
"chat": {
"chat_id": string,
"create_time": string,
"creator_id": string,
"character_id": string,
"state": string,
"type": string,
"visibility": string
},
"command": string
}
check on new version