dialog_app.py
Overview
dialog_app.py is a Flask-based REST API module that manages dialog entities within the InfiniFlow platform. It provides endpoints to create, update, retrieve, list, paginate, and delete dialogs associated with tenants (users or organizations). Dialogs represent configurable conversational setups, including prompt configurations, knowledge bases, and language model settings.
This module tightly integrates with underlying services responsible for dialogs, tenants, knowledge bases, and LLM configurations. It enforces user authentication and validates input data, ensuring dialogs are correctly maintained and queried within the multi-tenant environment.
Classes and Functions
This file does not define any classes but contains multiple Flask route handler functions, each providing a REST API endpoint. Below is a detailed explanation of each function.
1. set_dialog()
Route: /set
Methods: POST
Decorators:
@validate_request("prompt_config") (validates the presence of
"prompt_config"in JSON request)@login_required(requires authenticated user)
Purpose:
Creates a new dialog or updates an existing dialog with the specified configuration.
Parameters:
JSON request body with keys including (but not limited to):
dialog_id(optional): If provided, updates the existing dialog with this ID.name: Name of the dialog (string).description: Description of the dialog (string).icon: Icon associated with the dialog (string).top_n,top_k: Numeric parameters controlling dialog search behavior.rerank_id: Identifier for reranking model (string).similarity_threshold,vector_similarity_weight: Parameters for similarity scoring.llm_setting: Configuration dictionary for the language model.meta_data_filter: Metadata filters applied in dialog context (dict).prompt_config: Prompt configuration dictionary (mandatory).kb_ids: List of knowledge base IDs associated with the dialog.llm_id: Identifier of the LLM model used by the tenant.
Returns:
JSON response with the newly created or updated dialog data on success.
JSON error response with appropriate message on validation or processing failure.
Usage Example:
POST /set
{
"name": "Sales Support Dialog",
"description": "Handles sales queries",
"kb_ids": ["kb1", "kb2"],
"prompt_config": {
"system": "Your system prompt with {parameter}",
"parameters": [{"key": "parameter", "optional": False}]
},
"top_n": 5,
"llm_setting": {...}
}
Implementation Details:
Validates dialog name (type, emptiness, length).
Checks for duplicate dialog names within the current tenant and modifies the name accordingly.
Validates prompt configuration parameters ensuring all required parameters appear in the system prompt.
Ensures knowledge bases use consistent embedding models.
Uses
DialogServiceto save or update dialog entities in the database.Returns detailed error messages for invalid operations.
2. get()
Route: /get
Methods: GET
Decorators:
@login_required
Purpose:
Retrieves a dialog by its ID.
Parameters:
Query string parameter:
dialog_id(string) - The ID of the dialog to fetch.
Returns:
JSON response containing the dialog data including knowledge base names.
Error response if dialog not found or on server error.
Usage Example:
GET /get?dialog_id=1234-uuid
3. get_kb_names(kb_ids)
Purpose:
Helper function to retrieve valid knowledge base IDs and their corresponding names.
Parameters:
kb_ids: List of knowledge base IDs (strings).
Returns:
Tuple of two lists:
(valid_ids, names)where each name corresponds to the valid knowledge bases.
Details:
Skips knowledge bases that are not found or are marked invalid (status not
VALID).
4. list_dialogs()
Route: /list
Methods: GET
Decorators:
@login_required
Purpose:
Lists all valid dialogs belonging to the current tenant, ordered by creation time descending.
Returns:
JSON array of dialog objects enriched with knowledge base names.
5. list_dialogs_next()
Route: /next
Methods: POST
Decorators:
@login_required
Purpose:
Provides paginated and filtered dialogs, supporting keyword search, ordering, and multi-tenant ownership filtering.
Parameters:
Query string parameters:
keywords(string): Search keywords.page(int): Page number.page_size(int): Items per page.parser_id(string): Optional parser filter.orderby(string): Field to order by (default:create_time).desc(bool as string): Descending order flag (default: true).
JSON body:
owner_ids(list of tenant IDs): Filters dialogs by tenant ownership.
Returns:
JSON object with keys:
dialogs: List of dialog dicts for the page.total: Total number of dialogs matching the filters.
Details:
Supports filtering dialogs by tenants the user owns or specified owner IDs.
Uses
DialogService.get_by_tenant_idsfor querying dialogs.
6. rm()
Route: /rm
Methods: POST
Decorators:
@login_required@validate_request("dialog_ids")(validates presence ofdialog_idsin JSON)
Purpose:
Deletes (marks as invalid) dialogs by IDs, ensuring only owners can delete their dialogs.
Parameters:
JSON request body:
dialog_ids: List of dialog IDs to delete.
Returns:
JSON success status (true/false) with error message if unauthorized or failure occurs.
Details:
Checks that the current user is an owner of each dialog before deletion.
Updates dialog statuses to
INVALIDto mark as deleted.
Important Implementation Details and Algorithms
Duplicate Name Handling: When creating a new dialog, if the name already exists for the tenant, the system generates a unique duplicate name using the
duplicate_nameutility to avoid conflicts.Prompt Configuration Validation: Mandatory prompt parameters must be referenced in the system prompt string; otherwise, the request is rejected.
Multi-Tenant and Authorization Checks: All dialog operations are scoped by the current authenticated user’s tenant ID. Deletion operations verify ownership explicitly.
Embedding Model Consistency: When associating knowledge bases to dialogs, the embedding models used by each knowledge base must be consistent to prevent incompatible datasets within one dialog.
Soft Deletion: Dialogs are not physically deleted but marked as
INVALIDin status, allowing for possible recovery or auditing.Pagination and Filtering: Supports keyword search, ordering, and pagination in listing dialogs, accommodating scalable data retrieval.
Integration and Interactions
With
DialogService: Core CRUD operations on dialogs (query, save, update) are routed through this service, which abstracts database interactions.With
TenantService: Verifies tenant existence and retrieves tenant-level configurations such as default LLM IDs.With
KnowledgebaseService: Retrieves knowledge base metadata such as names and validates knowledge base states.With
TenantLLMService: Processes embedding model information attached to knowledge bases to ensure compatibility.With
UserTenantService: Checks tenant memberships for authorization, especially during deletion.With Flask-Login: Uses
current_userto identify the authenticated user, ensuring multi-tenant data isolation.With Utility Modules: For request validation, error handling, UUID generation, and standard JSON responses.
Visual Diagram: Class Diagram of dialog_app.py Functional Components
classDiagram
class DialogService {
+query(tenant_id, name, status, ...)
+save(**dialog_data)
+update_by_id(dialog_id, update_data)
+get_by_id(dialog_id)
+get_by_tenant_ids(tenants, user_id, page, size, orderby, desc, keywords, parser_id)
+update_many_by_id(dialog_list)
}
class TenantService {
+get_by_id(tenant_id)
}
class KnowledgebaseService {
+get_by_id(kb_id)
+get_by_ids(kb_ids)
}
class TenantLLMService {
+split_model_name_and_factory(embd_id)
}
class UserTenantService {
+query(user_id)
}
class DialogApp {
+set_dialog()
+get()
+list_dialogs()
+list_dialogs_next()
+rm()
+get_kb_names(kb_ids)
}
DialogApp --> DialogService : uses
DialogApp --> TenantService : uses
DialogApp --> KnowledgebaseService : uses
DialogApp --> TenantLLMService : uses
DialogApp --> UserTenantService : uses
Summary
dialog_app.py is a key API module for managing conversational dialogs in InfiniFlow. It provides secure, validated endpoints to create, update, fetch, list, and delete dialogs with integration to tenant configurations and knowledge bases. The module emphasizes multi-tenant data isolation, input validation, and consistent embedding models across knowledge bases. It leverages multiple service layers to abstract data management and maintain clean separation of concerns.
This documentation should assist developers and integrators in understanding and extending the dialog management capabilities of the InfiniFlow platform.