Skip to content

Commit

Permalink
from docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kramcat authored Jun 8, 2023
1 parent 04bc75f commit e8316bf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/async/chatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import asyncio
from characterai import PyAsyncCAI

async def main():
client = PyAsyncCAI('TOKEN')
await client.start()

char = input('Enter CHAR: ')

# Save tgt and history_external_id
# to avoid making a lot of requests
chat = await client.chat.get_chat(char)

history_id = chat['external_id']
participants = chat['participants']

# In the list of "participants",
# a character can be at zero or in the first place
if not participants[0]['is_human']:
tgt = participants[0]['user']['username']
else:
tgt = participants[1]['user']['username']

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

data = await client.chat.send_message(
char, message, history_external_id=history_id, tgt=tgt
)

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

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

asyncio.run(main())
16 changes: 16 additions & 0 deletions examples/async/parse_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import asyncio
from characterai import PyAsyncCAI

async def main():
client = PyAsyncCAI('TOKEN')
await client.start()

history = await client.chat.get_history('CHAR')

for h in history['messages']:
name = h['src_char']['participant']['name']
text = h['text']

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

asyncio.run(main())
31 changes: 31 additions & 0 deletions examples/sync/chatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from characterai import PyCAI

client = PyCAI('TOKEN')

char = input('Enter CHAR: ')

# Save tgt and history_external_id
# to avoid making a lot of requests
chat = client.chat.get_chat(char)

history_id = chat['external_id']
participants = chat['participants']

# In the list of "participants",
# a character can be at zero or in the first place
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(
char, message, history_external_id=history_id, tgt=tgt
)

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

print(f"{name}: {text}")
11 changes: 11 additions & 0 deletions examples/sync/parse_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from characterai import PyCAI

client = PyCAI('TOKEN')

history = client.chat.get_history('CHAR')

for h in history['messages']:
name = h['src_char']['participant']['name']
text = h['text']

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

0 comments on commit e8316bf

Please sign in to comment.