Skip to main content

Overview

The Knowledge Base API provides comprehensive endpoints for listing, filtering, searching, and managing knowledge base entries, including category management and statistical insights.

List All Knowledge Bases

Endpoint: GET /api/knowledgebase/list Retrieve all knowledge base entries with optional filtering by vectorization status and custom sorting.

Query Parameters

sortBy
string
Sort order for the results.Values:
  • uploadedAt - Sort by upload date (newest first)
  • lastAccessedAt - Sort by last access time
  • accessCount - Sort by usage frequency
  • questionCount - Sort by number of questions asked
  • name - Sort alphabetically by name
Default: uploadedAt (descending)
vectorStatus
string
Filter by vectorization status.Values: PENDING, PROCESSING, COMPLETED, FAILEDExample: ?vectorStatus=COMPLETED returns only ready-to-query knowledge bases

Response

code
integer
Response status code. 200 indicates success.
message
string
Response message.
data
array
Array of knowledge base entries.

Examples

# Get all knowledge bases
curl 'http://localhost:8080/api/knowledgebase/list'

Response Example

{
  "code": 200,
  "message": "success",
  "data": [
    {
      "id": 1,
      "name": "Technical Documentation",
      "category": "Engineering",
      "originalFilename": "tech-docs-v2.pdf",
      "fileSize": 2457600,
      "contentType": "application/pdf",
      "uploadedAt": "2026-03-10T14:30:00",
      "lastAccessedAt": "2026-03-10T16:45:30",
      "accessCount": 12,
      "questionCount": 45,
      "vectorStatus": "COMPLETED",
      "vectorError": null,
      "chunkCount": 156
    },
    {
      "id": 2,
      "name": "API Reference Guide",
      "category": "Engineering",
      "originalFilename": "api-reference.docx",
      "fileSize": 1048576,
      "contentType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      "uploadedAt": "2026-03-09T09:15:00",
      "lastAccessedAt": "2026-03-10T14:20:10",
      "accessCount": 8,
      "questionCount": 23,
      "vectorStatus": "COMPLETED",
      "vectorError": null,
      "chunkCount": 89
    },
    {
      "id": 3,
      "name": "User Manual",
      "category": null,
      "originalFilename": "user-manual.pdf",
      "fileSize": 3145728,
      "contentType": "application/pdf",
      "uploadedAt": "2026-03-10T11:00:00",
      "lastAccessedAt": null,
      "accessCount": 0,
      "questionCount": 0,
      "vectorStatus": "PROCESSING",
      "vectorError": null,
      "chunkCount": null
    }
  ]
}

Get Knowledge Base by ID

Endpoint: GET /api/knowledgebase/{id} Retrieve detailed information about a specific knowledge base.

Path Parameters

id
integer
required
Knowledge base unique identifier.

Examples

curl 'http://localhost:8080/api/knowledgebase/1'

Response

{
  "code": 200,
  "message": "success",
  "data": {
    "id": 1,
    "name": "Technical Documentation",
    "category": "Engineering",
    "originalFilename": "tech-docs-v2.pdf",
    "fileSize": 2457600,
    "contentType": "application/pdf",
    "uploadedAt": "2026-03-10T14:30:00",
    "lastAccessedAt": "2026-03-10T16:45:30",
    "accessCount": 12,
    "questionCount": 45,
    "vectorStatus": "COMPLETED",
    "vectorError": null,
    "chunkCount": 156
  }
}

Error Response

{
  "code": 404,
  "message": "知识库不存在"
}

Search Knowledge Bases

Endpoint: GET /api/knowledgebase/search Search knowledge bases by name or filename using keyword matching.

Query Parameters

keyword
string
required
Search keyword to match against knowledge base names and filenames.Example: ?keyword=documentation

Examples

curl 'http://localhost:8080/api/knowledgebase/search?keyword=documentation'

Response

Returns the same structure as the list endpoint, filtered by keyword:
{
  "code": 200,
  "message": "success",
  "data": [
    {
      "id": 1,
      "name": "Technical Documentation",
      "originalFilename": "tech-docs-v2.pdf",
      // ... other fields
    }
  ]
}

Category Management

Organize knowledge bases using categories for better organization and filtering.

Get All Categories

Endpoint: GET /api/knowledgebase/categories Retrieve a list of all unique categories in use.
curl 'http://localhost:8080/api/knowledgebase/categories'
Response:
{
  "code": 200,
  "message": "success",
  "data": ["Engineering", "Sales", "HR", "Legal"]
}

Get Knowledge Bases by Category

Endpoint: GET /api/knowledgebase/category/{category} Retrieve all knowledge bases in a specific category.
category
string
required
Category name (case-sensitive).
Example:
curl 'http://localhost:8080/api/knowledgebase/category/Engineering'
Response:
{
  "code": 200,
  "message": "success",
  "data": [
    // Array of knowledge bases in the "Engineering" category
  ]
}

Get Uncategorized Knowledge Bases

Endpoint: GET /api/knowledgebase/uncategorized Retrieve all knowledge bases without a category assigned.
curl 'http://localhost:8080/api/knowledgebase/uncategorized'

Update Knowledge Base Category

Endpoint: PUT /api/knowledgebase/{id}/category Assign or update the category for a knowledge base.
id
integer
required
Knowledge base ID.
category
string
required
New category name. Pass null or empty string to remove category.
Example:
curl -X PUT 'http://localhost:8080/api/knowledgebase/1/category' \
  -H 'Content-Type: application/json' \
  -d '{"category": "Engineering"}'
Response:
{
  "code": 200,
  "message": "success",
  "data": null
}

Statistics

Endpoint: GET /api/knowledgebase/stats Get aggregate statistics about all knowledge bases.

Response

data
object

Example

curl 'http://localhost:8080/api/knowledgebase/stats'
Response:
{
  "code": 200,
  "message": "success",
  "data": {
    "totalCount": 15,
    "totalQuestionCount": 342,
    "totalAccessCount": 128,
    "completedCount": 12,
    "processingCount": 2
  }
}

Understanding Vector Status

The vectorStatus field indicates the readiness of a knowledge base for querying:
StatusDescriptionCan Query?Actions
PENDINGQueued for vectorizationNoWait for processing to start
PROCESSINGCurrently being vectorizedNoMonitor until completion
COMPLETEDReady for queriesYesQuery normally
FAILEDVectorization error occurredNoCheck vectorError and revectorize
Only knowledge bases with vectorStatus: COMPLETED can be queried. Use the list endpoint with ?vectorStatus=COMPLETED to get query-ready knowledge bases.

Common Use Cases

Building a Knowledge Base Selector

// Fetch only completed knowledge bases for a query UI
const response = await fetch(
  'http://localhost:8080/api/knowledgebase/list?vectorStatus=COMPLETED&sortBy=name'
);

const knowledgeBases = await response.json();

// Populate a dropdown/selector
knowledgeBases.data.forEach(kb => {
  const option = document.createElement('option');
  option.value = kb.id;
  option.text = `${kb.name} (${kb.chunkCount} chunks)`;
  selector.appendChild(option);
});

Monitoring Vectorization Progress

import requests
import time

def wait_for_vectorization(kb_id, timeout=300):
    """Poll until knowledge base vectorization completes."""
    start = time.time()
    
    while time.time() - start < timeout:
        response = requests.get(
            f'http://localhost:8080/api/knowledgebase/{kb_id}'
        )
        kb = response.json()['data']
        
        status = kb['vectorStatus']
        if status == 'COMPLETED':
            print(f"Vectorization complete: {kb['chunkCount']} chunks")
            return True
        elif status == 'FAILED':
            print(f"Vectorization failed: {kb['vectorError']}")
            return False
        
        print(f"Status: {status}, waiting...")
        time.sleep(5)
    
    print("Timeout waiting for vectorization")
    return False

Organizing by Category

// Group knowledge bases by category
const response = await fetch('http://localhost:8080/api/knowledgebase/list');
const knowledgeBases = await response.json();

const byCategory = knowledgeBases.data.reduce((acc, kb) => {
  const category = kb.category || 'Uncategorized';
  if (!acc[category]) acc[category] = [];
  acc[category].push(kb);
  return acc;
}, {});

console.log(byCategory);
// { "Engineering": [...], "Sales": [...], "Uncategorized": [...] }

See Also

Build docs developers (and LLMs) love