> 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 密钥。登录 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": "Invalid API key provided.
}
```

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

# Path to the file you want to upload
file_path = "photosynthesis.txt"

# Open the file in binary mode
with open(file_path, "rb") as file:
    # Create a dictionary for the form data
    form_data = { 
        "file_name": file.name,
        "file": file
    }   

    # Send the POST request with multipart/form-data
    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 密钥。登录 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": "Invalid API key provided.
}
```

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

# Path to the file you want to upload
file_path = "photosynthesis.txt"

# Open the file in binary mode
with open(file_path, "rb") as file:
    # Create a dictionary for the form data
    files = {"file": ("photosynthesis-filename.txt", file, "text/plain")}
    form_data = {"document_id": "<document_id"}

    # Send the POST request with multipart/form-data
    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 密钥。登录 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": "Invalid API key provided.
}
```

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

# Create a dictionary for the form data
form_data = { 
    'character_id': '<Your-CharacterId>',
}

# Send the POST request with multipart/form-data
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`

可以使用 Update API 将知识库文件附加到角色（或从角色中移除）。文件成功关联后，与角色的所有后续交互都会根据需要从关联的知识库中获取信息。

#### 请求头

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

#### 请求正文

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

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

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

{% endtab %}

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

```json
{
    "API_ERROR": "Invalid API key provided.
}
```

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

# 将 payload 转换为 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 密钥。登录 Convai 账户后，可在密钥图标下找到。 |

#### 请求正文

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

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

```json
{"Successfully deleted document"}
```

{% endtab %}

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

```
{
    "API_ERROR": "Invalid API key provided.
}
```

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