# Duy trì kiến ​​thức qua Api

> Xác thực, phương thức gọi và API dịch vụ ứng dụng vẫn nhất quán. Sự khác biệt là mã thông báo API kiến ​​thức có thể hoạt động trên tất cả các cơ sở kiến ​​thức.

## Lợi ích của việc Sử dụng API Kiến Thức

* Đồng bộ hóa hệ thống dữ liệu của bạn với ChatX để tạo ra các quy trình làm việc mạnh mẽ.
* Cung cấp API danh sách kiến thức và danh sách tài liệu cũng như giao diện truy vấn chi tiết, để hỗ trợ xây dựng trang quản lý dữ liệu riêng của bạn.
* Hỗ trợ tải lên/cập nhật tài liệu dưới dạng văn bản thuần và tệp, cũng như thêm và sửa đổi hàng loạt, để đơn giản hóa quá trình đồng bộ hóa của bạn.
* Giảm thời gian xử lý và đồng bộ hóa tài liệu thủ công, cải thiện khả năng hiển thị phần mềm và dịch vụ của ChatX.

## Cách sử dụng

Hãy truy cập trang kiến thức, bạn có thể chuyển đổi tab sang trang API trong thanh điều hướng bên trái. Trên trang này, bạn có thể xem tài liệu API được cung cấp bởi ChatX và quản lý thông tin xác thực để truy cập API Kiến Thức.

<figure><img src="https://content.gitbook.com/content/SK9x6AT8FGi77omZphkL/blobs/4j8ya4qbMXL3WBlNuUre/Capture.JPG" alt=""><figcaption><p>Tài liệu API Kiến Thức</p></figcaption></figure>

## **Tạo Kiến Thức Trống**

**`POST /datasets`**

{% hint style="info" %}
Dùng để tạo một tập dữ liệu trống.
{% endhint %}

```
curl --location --request POST 'https://api.dify.ai/v1/datasets' \
--header 'Authorization: Bearer {api_key}' \
--header 'Content-Type: application/json' \
--data-raw '{"name": "name"}'
```

#### Danh sách kiến ​​thức

```
curl --location --request GET 'https://api.dify.ai/v1/datasets?page=1&limit=20' \
--header 'Authorization: Bearer {api_key}'
```

#### Tạo tài liệu từ văn bản

```
curl --location --request POST '<https://api.dify.ai/v1/datasets/<uuid:dataset_id>/document/create_by_text>' \\
--header 'Authorization: Bearer {api_key}' \\
--header 'Content-Type: application/json' \\
--data-raw '{
    "name": "Dify",
    "text": "Dify means Do it for you...",
    "indexing_technique": "high_quality",
    "process_rule": {
        "rules": {
                "pre_processing_rules": [{
                        "id": "remove_extra_spaces",
                        "enabled": true
                }, {
                        "id": "remove_urls_emails",
                        "enabled": true
                }],
                "segmentation": {
                        "separator": "###",
                        "max_tokens": 500
                }
        },
        "mode": "custom"
    }
}'
```

#### Tạo một tài liệu từ tập tin

```
curl --location POST 'https://api.dify.ai/v1/datasets/{dataset_id}/document/create_by_file' \
--header 'Authorization: Bearer {api_key}' \
--form 'data="{
	"name": "Dify",
	"indexing_technique": "high_quality",
	"process_rule": {
		"rules": {
			"pre_processing_rules": [{
				"id": "remove_extra_spaces",
				"enabled": true
			}, {
				"id": "remove_urls_emails",
				"enabled": true
			}],
			"segmentation": {
				"separator": "###",
				"max_tokens": 500
			}
		},
		"mode": "custom"
	}
    }";
    type=text/plain' \
--form 'file=@"/path/to/file"'
```

#### Lấy trạng thái nhúng tài liệu

```
curl --location --request GET 'https://api.dify.ai/v1/datasets/{dataset_id}/documents/{batch}/indexing-status' \
--header 'Authorization: Bearer {api_key}'
```

#### Xóa tài liệu

```
curl --location --request DELETE 'https://api.dify.ai/v1/datasets/{dataset_id}/documents/{document_id}' \
--header 'Authorization: Bearer {api_key}'
```

#### Nhận danh sách tài liệu

```
curl --location --request GET 'https://api.dify.ai/v1/datasets/{dataset_id}/documents' \
--header 'Authorization: Bearer {api_key}'
```

#### Thêm phân đoạn mới

```
curl 'https://api.dify.ai/v1/datasets/aac47674-31a8-4f12-aab2-9603964c4789/documents/2034e0c1-1b75-4532-849e-24e72666595b/segment' \
  --header 'Authorization: Bearer {api_key}' \
  --header 'Content-Type: application/json' \
  --data-raw $'"segments":[
  {"content":"Dify means Do it for you",
  "keywords":["Dify","Do"]
  }
  ]'
  --compressed
```

### Thông báo lỗi

* `document_indexing`，tài liệu đang ở trạng thái lập chỉ mục
* `provider_not_initialize`， Mô hình nhúng chưa được định cấu hình
* `not_found`，tài liệu không tồn tại
* `dataset_name_duplicate` ，tên kiến ​​​​thức đã tồn tại
* `provider_quota_exceeded`，mô hình quota đã vượt quá giới hạn
* `dataset_not_initialized`，Kiến thức chưa được khởi tạo
* `unsupported_file_type`，Loại tập tin không được hỗ trợ
  * support file type：txt, markdown, md, pdf, html, htm, xlsx, docx, csv
* `too_many_files`，Số lượng tệp quá lớn và chỉ hỗ trợ tải lên một tệp tạm thời
* `file_too_large`，File quá lớn, hỗ trợ file dưới 15M
