Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create a new chat #70

Open
IamFonky opened this issue Nov 25, 2023 · 7 comments
Open

Cannot create a new chat #70

IamFonky opened this issue Nov 25, 2023 · 7 comments

Comments

@IamFonky
Copy link

Hello,

When I'm trying to create a new chat, I got this error : AttributeError: 'NoneType' object has no attribute 'send'

image

We can see in pyaisynccai.py:31 that the ws attribute is defined as None.

image

Am I doing something wrong or is it just a lack of implementation in the code?

@tominal
Copy link

tominal commented Nov 27, 2023

Nope, there is no way to get the token anymore according to the documentation

image

Disregard - if you are getting that error ^ you have to be logged in.

@Karvp
Copy link

Karvp commented Nov 30, 2023

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

@IamFonky
Copy link
Author

IamFonky commented Dec 1, 2023

@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

@Karvp
Copy link

Karvp commented Dec 1, 2023

Could you share your code and goal so we can understand your issue better?

@IamFonky
Copy link
Author

IamFonky commented Dec 1, 2023

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

@Karvp
Copy link

Karvp commented Dec 1, 2023

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
}

@kramcat
Copy link
Owner

kramcat commented Apr 12, 2024

check on new version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants