> For the complete documentation index, see [llms.txt](https://docs.clickai.vn/clickai-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clickai.vn/clickai-docs/co-so-du-lieu/dac-ta-api-kien-thuc-ben-ngoai.md).

# Đặc tả API Kiến thức Bên ngoài

## Mục lục

·       \[Authentication]\(#authentication)

·       \[Request]\(#request)

·       \[Response]\(#response)

·       \[Error Handling]\(#error-handling)

&#x20;

## Authentication

ClickAI xác thực với external knowledge service bằng Bearer token trong Authorization header:

Authorization: Bearer {API\_KEY}

API Key là credential bạn cung cấp khi đăng ký External Knowledge API trong ClickAI.

&#x20;

## Request

### Endpoint

POST {your-endpoint}/retrieval\
Content-Type: application/json\
Authorization: Bearer {API\_KEY}

ClickAI tự động thêm /retrieval vào API Endpoint đã đăng ký. Ví dụ nếu bạn cấu hình endpoint là <https://your-service.com>, ClickAI sẽ gửi request đến <https://your-service.com/retrieval>.

### Body

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Trường</td><td valign="top">Kiểu</td><td valign="top">Bắt buộc</td><td valign="top">Mô tả</td></tr><tr><td valign="top">knowledge_id</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">ID của knowledge source trong hệ thống bên ngoài</td></tr><tr><td valign="top">query</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">Câu hỏi tìm kiếm từ người dùng</td></tr><tr><td valign="top">retrieval_setting</td><td valign="top">object</td><td valign="top">✅</td><td valign="top">Cấu hình retrieval (xem chi tiết bên dưới)</td></tr><tr><td valign="top">metadata_condition</td><td valign="top">object</td><td valign="top">❌</td><td valign="top">Điều kiện lọc metadata (xem chi tiết bên dưới)</td></tr></tbody></table>

&#x20;

### retrieval\_setting

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Trường</td><td valign="top">Kiểu</td><td valign="top">Mô tả</td></tr><tr><td valign="top">top_k</td><td valign="top">integer</td><td valign="top">Số chunks tối đa trả về</td></tr><tr><td valign="top">score_threshold</td><td valign="top">float</td><td valign="top">Điểm similarity tối thiểu (0.0 - 1.0). Mặc định 0.0 (không lọc)</td></tr></tbody></table>

&#x20;

### metadata\_condition

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Trường</td><td valign="top">Kiểu</td><td valign="top">Mô tả</td></tr><tr><td valign="top">logical_operator</td><td valign="top">string</td><td valign="top">Toán tử logic: and hoặc or. Mặc định and</td></tr><tr><td valign="top">conditions</td><td valign="top">array</td><td valign="top">Danh sách điều kiện lọc</td></tr></tbody></table>

&#x20;

Mỗi condition gồm:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Trường</td><td valign="top">Kiểu</td><td valign="top">Mô tả</td></tr><tr><td valign="top">name</td><td valign="top">string</td><td valign="top">Tên trường metadata</td></tr><tr><td valign="top">comparison_operator</td><td valign="top">string</td><td valign="top">Toán tử so sánh</td></tr><tr><td valign="top">value</td><td valign="top">any</td><td valign="top">Giá trị so sánh (không bắt buộc cho empty, not empty)</td></tr></tbody></table>

&#x20;

Supported Comparison Operators:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Loại dữ liệu</td><td valign="top">Operators</td></tr><tr><td valign="top">String</td><td valign="top">contains, not contains, start with, end with, is, is not, in, not in, empty, not empty</td></tr><tr><td valign="top">Number</td><td valign="top">=, ≠, >, &#x3C;, ≥, ≤, empty, not empty</td></tr><tr><td valign="top">Date</td><td valign="top">before, after, is, empty, not empty</td></tr></tbody></table>

&#x20;

### Example Request

{\
&#x20; "knowledge\_id": "your-knowledge-id",\
&#x20; "query": "ClickAI là gì?",\
&#x20; "retrieval\_setting": {\
&#x20;   "top\_k": 3,\
&#x20;   "score\_threshold": 0.5\
&#x20; }\
}

Example Request với Metadata Filtering:

{\
&#x20; "knowledge\_id": "your-knowledge-id",\
&#x20; "query": "Chính sách bảo hành",\
&#x20; "retrieval\_setting": {\
&#x20;   "top\_k": 5,\
&#x20;   "score\_threshold": 0.4\
&#x20; },\
&#x20; "metadata\_condition": {\
&#x20;   "logical\_operator": "and",\
&#x20;   "conditions": \[\
&#x20;     {\
&#x20;       "name": "category",\
&#x20;       "comparison\_operator": "is",\
&#x20;       "value": "policy"\
&#x20;     },\
&#x20;     {\
&#x20;       "name": "language",\
&#x20;       "comparison\_operator": "is",\
&#x20;       "value": "vi"\
&#x20;     }\
&#x20;   ]\
&#x20; }\
}

&#x20;

## Response

Response phải chứa trường records dạng array. Nếu không có kết quả, trả về array rỗng:

{"records": \[]}

### records

Mỗi record trong array records gồm:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Trường</td><td valign="top">Kiểu</td><td valign="top">Bắt buộc</td><td valign="top">Mô tả</td></tr><tr><td valign="top">content</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">Nội dung text của chunk</td></tr><tr><td valign="top">score</td><td valign="top">float</td><td valign="top">✅</td><td valign="top">Điểm relevance/similarity (0.0 - 1.0)</td></tr><tr><td valign="top">title</td><td valign="top">string</td><td valign="top">❌</td><td valign="top">Tiêu đề tài liệu nguồn</td></tr><tr><td valign="top">metadata</td><td valign="top">object</td><td valign="top">✅</td><td valign="top">Metadata bổ sung. Phải là object {}, KHÔNG được là null</td></tr></tbody></table>

&#x20;

### Example Response

{\
&#x20; "records": \[\
&#x20;   {\
&#x20;     "content": "ClickAI là nền tảng AI toàn diện cho doanh nghiệp.",\
&#x20;     "score": 0.98,\
&#x20;     "title": "gioi-thieu.txt",\
&#x20;     "metadata": {\
&#x20;       "path": "s3://clickai/gioi-thieu.txt",\
&#x20;       "description": "Tài liệu giới thiệu ClickAI"\
&#x20;     }\
&#x20;   },\
&#x20;   {\
&#x20;     "content": "ClickAI cho phép xây dựng ứng dụng AI không cần code.",\
&#x20;     "score": 0.85,\
&#x20;     "title": "features.txt",\
&#x20;     "metadata": {}\
&#x20;   }\
&#x20; ]\
}

&#x20;

## Error Handling

Khi xảy ra lỗi, API nên trả về object chứa error\_code và error\_msg:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Trường</td><td valign="top">Kiểu</td><td valign="top">Mô tả</td></tr><tr><td valign="top">error_code</td><td valign="top">integer</td><td valign="top">Mã lỗi</td></tr><tr><td valign="top">error_msg</td><td valign="top">string</td><td valign="top">Thông điệp lỗi chi tiết</td></tr></tbody></table>

&#x20;

Các mã lỗi phổ biến:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Mã lỗi</td><td valign="top">Mô tả</td></tr><tr><td valign="top">1001</td><td valign="top">Invalid request format</td></tr><tr><td valign="top">1002</td><td valign="top">Authorization failed</td></tr><tr><td valign="top">2001</td><td valign="top">Knowledge not found</td></tr><tr><td valign="top">2002</td><td valign="top">Retrieval service unavailable</td></tr></tbody></table>

&#x20;

### Example Error Response

{\
&#x20; "error\_code": 1002,\
&#x20; "error\_msg": "Authorization failed. Please check your API key."\
}

&#x20;

*📖 Trước: \[Kết nối External Knowledge]\(./06-external-knowledge.md) · Tiếp: \[Quản lý Documents & Chunks]\(./08-manage-documents-chunks.md)*
