> 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

使用 Convai 与你的 AI 角色的知识库交互所需的所有相关 API。

{% hint style="danger" %}
此 API 仅适用于企业版方案。
{% endhint %}

## 上传新的知识库文件

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

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

#### 标头

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

#### 请求体

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

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

```json
{
   "id": "<uuid of the uploaded file>",
   "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 %}

## 更新现有知识库文件

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

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

#### 标头

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

#### 请求体

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

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

```json
{
   "id": "<uuid of the uploaded file>",
   "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 %}

## 列出知识库文件状态

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

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

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

#### 标头

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

#### 请求体

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

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

```json
{
  'docs': [
    '{
        "id": "<File UUID>",
        "file_name": "photosynthesis.txt",
        "is_available": true,
        "status": "inactive",
        "timestamp": "2024-09-17 21:44:53.201385",
        "file_size": "1404"
    }',
    '{
        "id": "<File 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 %}

## 将知识库文件连接到（或断开与）角色

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

可使用更新 API 将知识库文件附加到您的角色，或将其移除。文件成功连接后，之后与角色的所有交互将按需从所附加的知识库中获取知识。

#### 标头

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

#### 请求体

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

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

```json
{"STATUS": "SUCCESS"}
```

{% 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 %}

## 删除知识库文件

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

知识库删除 API 可用于永久从用户账户中移除文档。请注意，删除文档会将其从所有关联的角色中移除。

#### 标头

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

#### 请求体

| 名称           | 类型  | 说明         |
| ------------ | --- | ---------- |
| 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 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/knowledge-bank-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.
