# Chatting

{% tabs %}
{% tab title="Sync" %}
{% code lineNumbers="true" %}

```python
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)

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(
        chat['external_id'], tgt, message
    )

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

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

{% endcode %}
{% endtab %}

{% tab title="Async" %}
{% code lineNumbers="true" %}

```python
import asyncio
from characterai import PyAsyncCAI

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

    char = input('Enter CHAR: ')

    # Getting chat info
    chat = await client.chat.get_chat(char)

    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(
            chat['external_id'], tgt, message
        )

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

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

asyncio.run(main())
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pycai.gitbook.io/welcome/examples/chatting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
