kb_app.py

Overview

kb_app.py is a Flask-based REST API module responsible for managing knowledge bases (KBs) within the InfiniFlow platform. It provides endpoints to create, update, delete, retrieve details, and list knowledge bases, as well as manage associated tags and knowledge graphs. The module enforces user authentication and authorization, ensuring that operations on knowledge bases are performed only by authorized users.

This file acts as the controller layer interfacing between HTTP requests and backend services such as KnowledgebaseService, DocumentService, and external components like settings.docStoreConn (likely an Elasticsearch or similar document store) and settings.retrievaler (a retrieval/search engine). It validates inputs, handles errors, and formats responses consistently.


Classes and Functions

This module does not define any classes but defines multiple Flask route handlers (functions) decorated for routes under the manager blueprint. Each function corresponds to an API endpoint.


1. create()

Route: POST /create

Description: Creates a new knowledge base for the authenticated user.

Parameters:

Returns:

Usage example:

POST /create
Content-Type: application/json
{
  "name": "My Knowledge Base"
}

Implementation details:


2. update()

Route: POST /update

Description: Updates metadata of an existing knowledge base.

Parameters:

Returns:

Important:


3. detail()

Route: GET /detail

Description: Retrieves detailed metadata for a specific knowledge base.

Parameters:

Returns:


4. list_kbs()

Route: POST /list

Description: Lists knowledge bases accessible by the user, supports pagination, filtering, and ordering.

Parameters:

Returns:


5. rm()

Route: POST /rm

Description: Removes a knowledge base and all associated documents and files.

Parameters:

Returns:

Implementation details:


6. list_tags(kb_id)

Route: GET /<kb_id>/tags

Description: Lists all tags associated with a specific knowledge base.

Parameters:

Returns:

Authorization: User must have access to the KB.


7. list_tags_from_kbs()

Route: GET /tags

Description: Lists tags from multiple knowledge bases.

Parameters:

Returns:

Authorization: User must have access to all specified KBs.


8. rm_tags(kb_id)

Route: POST /<kb_id>/rm_tags

Description: Removes specified tags from a knowledge base.

Parameters:

Returns:


9. rename_tags(kb_id)

Route: POST /<kb_id>/rename_tag

Description: Renames a tag in a knowledge base.

Parameters:

Returns:


10. knowledge_graph(kb_id)

Route: GET /<kb_id>/knowledge_graph

Description: Retrieves knowledge graph and mind map data for a knowledge base.

Parameters:

Returns:


11. delete_knowledge_graph(kb_id)

Route: DELETE /<kb_id>/knowledge_graph

Description: Deletes knowledge graph data from the document store for a knowledge base.

Parameters:

Returns:


12. get_meta()

Route: GET /get_meta

Description: Retrieves metadata of documents for multiple knowledge bases.

Parameters:

Returns:


Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class kb_app {
        +create()
        +update()
        +detail()
        +list_kbs()
        +rm()
        +list_tags(kb_id)
        +list_tags_from_kbs()
        +rm_tags(kb_id)
        +rename_tags(kb_id)
        +knowledge_graph(kb_id)
        +delete_knowledge_graph(kb_id)
        +get_meta()
    }

    kb_app ..> KnowledgebaseService : uses
    kb_app ..> DocumentService : uses
    kb_app ..> FileService : uses
    kb_app ..> TenantService : uses
    kb_app ..> UserTenantService : uses
    kb_app ..> settings.docStoreConn : uses for indexing/search
    kb_app ..> settings.retrievaler : uses for search/tags
    kb_app ..> STORAGE_IMPL : uses for storage bucket management

Summary

kb_app.py is the primary API controller for knowledge base management in InfiniFlow. It provides comprehensive endpoints to create, update, delete, retrieve, and list knowledge bases and their associated metadata, tags, and knowledge graphs. The file enforces strict authorization, validates inputs, and coordinates with backend services and external document stores to maintain consistency and performance. It is a critical component bridging user requests with persistent storage and search infrastructure.