> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/chat-history-api.md).

# 聊天历史 API

与会话或角色聊天历史交互所需的所有相关 API。

{% hint style="danger" %}
此 API 仅适用于 Scale 套餐及更高级别。
{% endhint %}

## 列出某个角色的会话

<mark style="color:绿色;">`POST`</mark> `https://api.convai.com/character/chatHistory/list`

列出该角色的所有会话。该 API 接受 limit 参数，可用于限制要获取的会话数量。如果该值设置为 "-1"，则返回所有会话。

#### 标头

| 名称                                              | 类型  | 说明                                          |
| ----------------------------------------------- | --- | ------------------------------------------- |
| CONVAI-API-KEY<mark style="color:红色;">\*</mark> | 字符串 | 每个用户提供的唯一 api-key。在登录 Convai 账户后，可在钥匙图标下找到。 |

#### 请求体

| 名称     | 类型  | 说明       |
| ------ | --- | -------- |
| charID | 字符串 | 您角色的 ID。 |
| 限制     | 字符串 | 返回的会话数量  |

{% tabs %}
{% tab title="200：OK 返回会话列表" %}

```json
[
    {
        "sessionID": "c1234567890aba1111222233334447862",
        "date": "25-09-2024",
        "time": "20:52:57",
        "is_added_to_memory": false
    },
    {
        "sessionID": "8c123456789011111222223333344455",
        "date": "25-09-2024",
        "time": "19:54:56",
        "is_added_to_memory": false
    }
]

```

{% endtab %}

{% tab title="401 API 密钥验证失败" %}

```json
{
    "ERROR": "提供的 API 密钥无效。"
}
```

{% endtab %}
{% endtabs %}

以下是一些示例代码，用于演示该端点的请求格式 -->

{% tabs %}
{% tab title="Python" %}
{% code overflow="wrap" %}

```python
import requests
import json

url = "https://api.convai.com/character/chatHistory/list"

headers = { 
    'CONVAI-API-KEY': '<Your-API-Key>',
    'Content-Type': 'application/json'
}

# 为 JSON 负载创建一个字典
payload = { 
    "charID": "<Your-Character-Id>",
    "limit": "-1"
}

# 将负载转换为 JSON
json_payload = json.dumps(payload)

response = requests.post(url, headers=headers, data=json_payload)

print(response.text)
```

{% endcode %}
{% endtab %}

{% tab title="cURL" %}
{% code overflow="wrap" %}

```shell
curl -X POST "https://api.convai.com/character/chatHistory/list" \
     -H "CONVAI-API-KEY: <Your-API-Key>" \
     -H "Content-Type: application/json" \
     -d '{"charID": "<Your-Character-Id>", "limit": "-1"}'
```

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

## 某个会话的聊天历史

<mark style="color:绿色;">`POST`</mark> `https://api.convai.com/character/chatHistory/details`

列出给定 session-id 的所有交互。输出中还包含一个布尔参数，用于指示用户输入是否为触发器。如果你在与角色互动时曾发送过触发器，此参数将设为 true。

#### 标头

| 名称                                              | 类型  | 说明                                          |
| ----------------------------------------------- | --- | ------------------------------------------- |
| CONVAI-API-KEY<mark style="color:红色;">\*</mark> | 字符串 | 每个用户提供的唯一 api-key。在登录 Convai 账户后，可在钥匙图标下找到。 |

#### 请求体

| 名称        | 类型  | 说明                |
| --------- | --- | ----------------- |
| charID    | 字符串 | 您角色的 ID。          |
| sessionID | 字符串 | 你要获取交互的该角色的会话 ID。 |

{% tabs %}
{% tab title="200：OK 返回会话交互记录" %}

```json
[
    {
        "timestamp": "2024-09-25 19:54:59.124733",
        "interaction": [
            {
                "speaker": "用户",
                "message": "你好"
            },
            {
                "speaker": "角色",
                "message": "你好，欢迎来到历史博物馆。我们这里有迄今发现的最古老化石。你有兴趣参观一下吗？"
            }
         ],
        "is_trigger_input": false
    }
]
```

{% endtab %}

{% tab title="401 API 密钥验证失败" %}

```json
{
    "ERROR": "提供的 API 密钥无效。"
}
```

{% endtab %}
{% endtabs %}

以下是一些示例代码，用于演示该端点的请求格式 -->

{% tabs %}
{% tab title="Python" %}
{% code overflow="wrap" %}

```python
import requests
import json

url = "https://api.convai.com/character/chatHistory/details"

headers = { 
    'CONVAI-API-KEY': '<Your-API-Key>',
    'Content-Type': 'application/json'
}

# 为 JSON 负载创建一个字典
payload = { 
    "charID": "<Your-Character-Id>",
    "sessionID": "<Your-Session-ID>"
}

# 将负载转换为 JSON
json_payload = json.dumps(payload)

response = requests.post(url, headers=headers, data=json_payload)

print(response.text)
```

{% endcode %}
{% endtab %}

{% tab title="cURL" %}
{% code overflow="wrap" %}

```shell
curl -X POST "https://api.convai.com/character/chatHistory/list" \
     -H "CONVAI-API-KEY: <Your-API-Key>" \
     -H "Content-Type: application/json" \
     -d '{"charID": "<Your-Character-Id>", "sessionID": "<Your-Session-ID>"}'
```

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/chat-history-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
