> 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/knowledge-bank-api.md).

# 知识库 API

{% hint style="danger" %}
此 API 仅在企业版计划中可用。
{% endhint %}

## 上传新的 KB 文件

<mark style="color:绿色;">`POST`</mark> `https://api.convai.com/character/knowledge-bank/upload`

用户可以上传一个新的 KB 文件。文件成功上传后，他们可以将其连接到角色。调用 API 后，文件只会被上传以供处理，在处理完成之前将无法使用。上传 API 将返回分配给已上传文件的唯一 ID，之后对该文件的所有交互都必须使用该 ID。

#### 请求头

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

#### 请求体

| 名称         | 类型  | 说明     |
| ---------- | --- | ------ |
| file\_name | 字符串 | 文件名    |
| file       | 字节  | 原始文件字节 |

{% tabs %}
{% tab title="200：OK 文件已成功上传" %}

```json
{
   "id": "<已上传文件的 UUID>",
   "file_name": "<file_name>",
   "is_available": false,
   "status": "inactive",
   "timestamp": "2024-09-17 20:51:09.216522",
   "file_size": "72374"
}

```

{% endtab %}

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

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

{% endtab %}
{% endtabs %}

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

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

```python
import requests
import json

url = "https://api.convai.com/character/knowledge-bank/upload"


headers = { 
  'CONVAI-API-KEY': '<Your-API-Key>',
}

# 要上传的文件路径
file_path = "photosynthesis.txt"

# 以二进制模式打开文件
with open(file_path, "rb") as file:
    # 为表单数据创建一个字典
    form_data = { 
        "file_name": file.name,
        "file": file
    }   

    # 发送带有 multipart/form-data 的 POST 请求
    response = requests.post(url, headers=headers, files=form_data)


print(response.text)
```

{% endcode %}
{% endtab %}

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

```shell
curl -X POST \\
  https://api.convai.com/character/knowledge-bank/upload \\
  -H 'CONVAI-API-KEY: <Your-API-Key>' \\
  -F 'file_name=photosynthesis.txt' \\
  -F 'file=@photosynthesis.txt'
```

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

## 更新现有 KB 文件

<mark style="color:绿色;">`POST`</mark> `https://api.convai.com/character/knowledge-bank/update`

用户可以通过上传新版本来更新现有的 KB 文件。文件成功上传后，他们可以将其连接到角色。调用 API 后，文件只会被上传以供处理，在处理完成之前将无法使用。

#### 请求头

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

#### 请求体

| 名称           | 类型  | 说明            |
| ------------ | --- | ------------- |
| document\_id | 字符串 | 需要更新的现有文档的 ID |
| file         | 字节  | 原始文件字节        |

{% tabs %}
{% tab title="200：OK 文件已成功上传" %}

```json
{
   "id": "<已上传文件的 UUID>",
   "file_name": "<file_name>",
   "is_available": false,
   "status": "inactive",
   "timestamp": "2024-09-17 20:51:09.216522",
   "file_size": "72374"
}

```

{% endtab %}

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

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

{% endtab %}
{% endtabs %}

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

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

```python
import requests
import json

url = "https://api.convai.com/character/knowledge-bank/update"


headers = { 
  'CONVAI-API-KEY': '<Your-API-Key>',
}

# 要上传的文件路径
file_path = "photosynthesis.txt"

# 以二进制模式打开文件
with open(file_path, "rb") as file:
    # 为表单数据创建一个字典
    files = {"file": ("photosynthesis-filename.txt", file, "text/plain")}
    form_data = {"document_id": "<document_id"}

    # 发送带有 multipart/form-data 的 POST 请求
    response = requests.post(url, headers=headers, files=files, data=form_data)


print(response.text)
```

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

## 列出 KB 文件状态

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

列出 KB 文件的状态。使用此 API 检查先前上传的 KB 文件的状态。一旦此 API 返回 "*is\_available*" 作为 *true* 你的 UUID，你可以放心地认为先前上传的文件已处理完毕，并可连接到你的角色。

请注意，list API 需要将 character\_id 作为输入。因此，它会返回一个 "*status*" 字段，其取值可以是 "*active*" 或 "*inactive*。" 此字段表示某个特定文件是否已连接到角色。如果其 "*status*" 为 "*active*"，则视为已连接。

#### 请求头

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

#### 请求体

| 名称            | 类型  | 说明       |
| ------------- | --- | -------- |
| character\_id | 字符串 | 你的角色 ID。 |

{% tabs %}
{% tab title="200：OK 文件已成功上传" %}

```json
{
  'docs': [
    '{
        "id": "<文件 UUID>",
        "file_name": "photosynthesis.txt",
        "is_available": true,
        "status": "inactive",
        "timestamp": "2024-09-17 21:44:53.201385",
        "file_size": "1404"
    }',
    '{
        "id": "<文件 UUID>",
        "file_name": "FAQ.txt",
        "is_available": true,
        "status": "inactive",
        "timestamp": "2024-09-17 21:21:35.566677",
        "file_size": "736"
    }']
}

```

{% endtab %}

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

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

{% endtab %}
{% endtabs %}

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

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

```python
import requests
import json

url = "https://api.convai.com/character/knowledge-bank/list"


headers = { 
  'CONVAI-API-KEY': '<Your-API-Key>',
}

# 为表单数据创建一个字典
form_data = { 
    'character_id': '<Your-CharacterId>',
}

# 发送带有 multipart/form-data 的 POST 请求
response = requests.post(url, headers=headers, data=form_data)

print(response.text)

```

{% endcode %}
{% endtab %}

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

```shell
curl -X POST 'https://api.convai.com/character/knowledge-bank/list' \\
  -H 'CONVAI-API-KEY: <Your-API-Key>' \\
  -F 'character_id=<Your-CharacterId>'
```

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

## 将 KB 文件连接到角色（或断开连接）

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

Update API 可用于将 KB 文件附加到你的角色，或将其移除。文件成功连接后，之后与该角色的所有交互都将按需从所附加的 KB 中获取知识。

#### 请求头

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

#### 请求体

| 名称     | 类型  | 说明                                      |
| ------ | --- | --------------------------------------- |
| charID | 字符串 | 你的角色 ID。                                |
| docs   | 列表  | JSON 列表。每个条目对应一个文件。字段为 "id" 和 "status"。 |

{% tabs %}
{% tab title="200：OK 文件已成功上传" %}

```json
{"STATUS": "成功"}
```

{% endtab %}

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

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

{% endtab %}
{% endtabs %}

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

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

```python
import requests
import json

url = "https://api.convai.com/character/update"

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

# 为 JSON 负载创建一个字典
payload = { 
    "charID": "<Your-Character-Id>",
    "docs": [
        {   
            "id": "<File-UUID>",
            "status": "active"  # 或根据需要设为 "inactive"
        }   
    ]   
}

# 将负载转换为 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/update' \\
-H 'CONVAI-API-KEY: <Your-API-Key>' \\
-H 'Content-Type: application/json' \\
-d '{
  "charID": "<Your-Character-Id>",
  "docs": [
    {
      "id": "<File-UUID>",
      "status": "active"
    }
  ]
}'
```

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

## 删除 KB 文件

<mark style="color:绿色;">`POST`</mark> `https://api.convai.com/character/knowledge-bank/delete`

KB 删除 API 可用于永久移除用户账户中的文档。请注意，删除文档后，它会从所有与之关联的角色中移除。

#### 请求头

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

#### 请求体

| 名称           | 类型  | 说明         |
| ------------ | --- | ---------- |
| document\_id | 字符串 | 应删除文档的 ID。 |

{% tabs %}
{% tab title="200：OK 文件已成功删除" %}

```json
{"文档已成功删除"}
```

{% endtab %}

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

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

{% endtab %}
{% endtabs %}

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

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

```python
import requests
import json

url = "https://api.convai.com/character/knowledge-bank/delete"

headers = { 
    'CONVAI-API-KEY': '<Your-API-Key>',
}

# 为 JSON 负载创建一个字典
form_data = { 
    "document_id": "<File-UUID>"   
}

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

print(response.text)

```

{% endcode %}
{% endtab %}

{% tab title="cURL" %}

```bash
curl -X POST "https://api.convai.com/character/knowledge-bank/delete" \\
-H "CONVAI-API-KEY: <Your-API-Key>" \\
-d "document_id=<File-UUID>"
```

{% 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 dynamically by asking a question.

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

```
GET https://docs.convai.com/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/knowledge-bank-api.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.
