chat.py


Overview

The chat.py file is a Flask-based REST API controller module responsible for managing chat assistant entities within the InfiniFlow platform. It provides endpoints to create, update, delete, and list chat assistants (chats) scoped per tenant (user or organization).

The chats represent conversational assistants configured with knowledge bases (datasets), LLM models, prompt templates, and various operational parameters. This module handles the validation, persistence, and retrieval of chat configurations by interacting with several backend services and enforcing tenant-based access control.


Detailed Component Documentation

Routes Summary

HTTP Method

Endpoint

Function

Description

POST

/chats

create

Create a new chat assistant

PUT

/chats/<chat_id>

update

Update an existing chat assistant

DELETE

/chats

delete

Delete one or more chat assistants

GET

/chats

list_chat

List chats with optional filters


Functions


create(tenant_id)

Create a new chat assistant for the specified tenant.

Parameters:

Request Body (JSON):

Returns:

Functionality:

  1. Validates dataset ownership and ensures datasets have parsed content.

  2. Checks that all datasets use the same embedding model.

  3. Validates and extracts the LLM model information.

  4. Retrieves tenant info and defaults missing prompt parameters.

  5. Validates prompt parameters usage.

  6. Generates a unique UUID for the chat.

  7. Prevents duplicate chat names within the tenant.

  8. Saves the chat configuration via DialogService.

  9. Formats the response by renaming certain keys to user-friendly aliases.

  10. Returns the chat configuration as JSON.

Usage Example:

POST /chats
Authorization: Bearer <token>
Content-Type: application/json

{
  "name": "SupportAssistant",
  "dataset_ids": ["dataset1", "dataset2"],
  "llm": {"model_name": "gpt-4"},
  "prompt": {
    "system": "You are a helpful assistant...",
    "parameters": [{"key": "knowledge", "optional": false}]
  }
}

update(tenant_id, chat_id)

Update an existing chat assistant for the specified tenant.

Parameters:

Request Body (JSON):

Returns:

Functionality:

  1. Validates tenant ownership of the chat.

  2. Validates dataset ownership and parsing if datasets are updated.

  3. Validates consistent embedding models across datasets.

  4. Validates LLM model existence.

  5. Updates prompt configuration with proper key mappings.

  6. Checks for duplicate chat names.

  7. Merges new prompt and LLM settings with existing ones.

  8. Updates the chat entry in the database.

  9. Returns success or error accordingly.


delete(tenant_id)

Delete one or multiple chat assistants of the tenant.

Parameters:

Request Body (JSON):

Returns:

Functionality:


list_chat(tenant_id)

Retrieve a paginated list of chat assistants for the tenant with optional filters.

Parameters (Query String):

Returns:

Functionality:


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

Below is a Mermaid class diagram illustrating the main functions and their relationships to external services and key data components within chat.py.

classDiagram
    class chat {
        +create(tenant_id)
        +update(tenant_id, chat_id)
        +delete(tenant_id)
        +list_chat(tenant_id)
    }

    class DialogService {
        +query(...)
        +save(...)
        +update_by_id(...)
        +get_by_id(...)
        +get_list(...)
    }

    class KnowledgebaseService {
        +accessible(...)
        +query(...)
        +get_by_ids(...)
    }

    class TenantLLMService {
        +split_model_name_and_factory(...)
        +query(...)
    }

    class TenantService {
        +get_by_id(...)
    }

    class Utils {
        +get_uuid()
        +check_duplicate_ids(...)
        +get_result(...)
        +get_error_data_result(...)
    }

    chat --> DialogService : uses
    chat --> KnowledgebaseService : uses
    chat --> TenantLLMService : uses
    chat --> TenantService : uses
    chat --> Utils : uses

Summary

The chat.py file is a critical API controller facilitating tenant-scoped management of chat assistants. It ensures data consistency, ownership validation, and proper configuration of LLM and knowledge base integrations through well-defined REST endpoints. The module integrates tightly with backend services managing chats, datasets, tenant LLM models, and tenant metadata, providing a secure and robust interface for client applications to manage chat assistants effectively.