kramcat / CharacterAI

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

Can you make the syntax more understandable #66

Closed azarelpro closed 5 months ago

azarelpro commented 10 months ago

I just don't understand the example code

ghost commented 10 months ago

I can break it down:

from characterai import PyCAI #imports PyCAI

client = PyCAI('TOKEN') #put the value of the char_token from dev tools here

char = input('Enter CHAR: ') #asks for the character id (after char= and before &source)

chat = client.chat.get_chat(char) 

participants = chat['participants']

if not participants[0]['is_human']:#finds which participant is human
    tgt = participants[0]['user']['username']
else:
    tgt = participants[1]['user']['username']

while True: #loops the conversation forever
    message = input('You: ') #your message

    data = client.chat.send_message(
        chat['external_id'], tgt, message
    )#sends your message to the bot

    name = data['src_char']['participant']['name']#finds the name of the bot
    text = data['replies'][0]['text']#finds the text from the bot's response

    print(f"{name}: {text}")#prints the bot's response

Here is an example of it filled out

from characterai import PyCAI

client = PyCAI('jhad9vjuvar8ckitj7tg2nsfwkwa3mwc795e2y4g')
#from value: ">>jhad9vjuvar8ckitj7tg2nsfwkwa3mwc795e2y4g<<"

char = 'OYYf4iM6fjt9eZ72oXRsY3UGPeXd9Y-uJwfAjF5JAwk'
#from https://beta.character.ai/chat?char=>>OYYf4iM6fjt9eZ72oXRsY3UGPeXd9Y-uJwfAjF5JAwk<<&source=recent-chats

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}")

I hope this helps 😁

kramcat commented 5 months ago

check new I hope it's better a bit