Bỏ qua, đến nội dung

Tham chiếu API — AI Knowledge Base ​

Tổng quan Resources ​

ResourceMô tảQuyền truy cập
aiKnowledgeBasesQuản lý knowledge basesAdmin
aiDocumentsQuản lý tài liệu trong knowledge baseAdmin
aiDocumentChunksChunks sau khi chia nhỏ tài liệuNội bộ
aiEmbeddingsVector embeddingsNội bộ

Knowledge Base ​

Tạo knowledge base ​

typescript
await agent.resource('aiKnowledgeBases').create({
  values: {
    name: 'Chính sách nhân sự',
    description: 'Tất cả quy định về nghỉ phép, lương thưởng, phúc lợi',
    embeddingModel: 'text-embedding-3-small',
  },
});

Lấy danh sách knowledge bases ​

typescript
const response = await agent.resource('aiKnowledgeBases').list({
  sort: ['-createdAt'],
});
// response.body.data = [{ id, name, description, embeddingModel, documentCount, ... }]

Cập nhật knowledge base ​

typescript
await agent.resource('aiKnowledgeBases').update({
  filterByTk: kbId,
  values: {
    name: 'Tên mới',
    description: 'Mô tả cập nhật',
  },
});

Xoá knowledge base ​

typescript
// Xoá knowledge base + tất cả tài liệu, chunks, embeddings liên quan
await agent.resource('aiKnowledgeBases').destroy({
  filterByTk: kbId,
});

Documents (Tài liệu) ​

Upload tài liệu ​

typescript
await agent.resource('aiDocuments').create({
  values: {
    knowledgeBaseId: kbId,
    file: fileBlob,          // File object hoặc Blob
    chunkSize: 1000,         // Tuỳ chọn: kích thước chunk
    chunkOverlap: 200,       // Tuỳ chọn: độ chồng lấp
  },
});

Lấy danh sách tài liệu ​

typescript
const docs = await agent.resource('aiDocuments').list({
  filter: { knowledgeBaseId: kbId },
  sort: ['-createdAt'],
});

Xoá tài liệu ​

typescript
// Xoá tài liệu + tất cả chunks và embeddings của tài liệu
await agent.resource('aiDocuments').destroy({
  filterByTk: docId,
});

Tìm kiếm (Search / Retrieval) ​

Tìm kiếm ngữ nghĩa ​

typescript
const results = await agent.resource('aiKnowledgeBases').search({
  filterByTk: kbId,
  values: {
    query: 'Chính sách nghỉ phép năm',
    topK: 5,   // Số kết quả tối đa trả về
  },
});
// results.body.data = [{ content, score, metadata: { documentId, fileName, ... } }]

Kết quả trả về được sắp xếp theo cosine similarity — score càng cao càng liên quan.

Database Collections ​

CollectionMô tảCác field chính
aiKnowledgeBasesDanh sách knowledge basesname, description, embeddingModel
aiDocumentsTài liệu đã uploadknowledgeBaseId, fileName, status, chunkCount
aiDocumentChunksChunks sau khi chia nhỏdocumentId, content, index
aiEmbeddingsVector embeddingschunkId, vector, metadata

Document Object ​

TrườngKiểuMô tả
idbigintID tài liệu
knowledgeBaseIdbigintKnowledge base chứa tài liệu
fileNamestringTên file gốc khi upload
fileTypestringLoại file (pdf, docx, txt, md, csv)
statusstringTrạng thái: processing, ready, failed
chunkCountintegerSố chunks đã tạo
fileSizebigintKích thước file (bytes)
createdAtdatetimeThời gian upload
updatedAtdatetimeThời gian cập nhật gần nhất

Giới hạn kỹ thuật ​

Tham sốGiá trịGhi chú
File tối đa50MBChia file lớn thành nhiều phần
Định dạng hỗ trợPDF, DOCX, TXT, MD, CSVThêm format qua DocumentLoaders
Số tài liệu / KB100 (mặc định)Cấu hình được
Embedding API keyBắt buộcDùng key của LLM provider tương ứng
TopK mặc định5Có thể tăng/giảm qua parameter