api_app.py
Overview
The api_app.py file implements a RESTful API backend for managing conversational AI interactions, document uploads, knowledge base retrieval, and API token management within the InfiniFlow system. It uses the Flask framework and integrates with various database services, user authentication via Flask-Login, and custom business logic for handling conversations, knowledge bases, files, and tasks.
This file primarily exposes API endpoints under a Flask blueprint (assumed named manager) that enable:
API token creation, listing, and deletion.
Starting new conversations and processing message completions with streaming support.
Uploading and parsing documents into knowledge bases.
Querying conversation histories, document chunks, and knowledge base documents.
Retrieving relevant chunks from documents based on user queries.
Supporting AI bot question answering and retrieval functionality.
The file acts as a controller layer connecting HTTP requests with backend service classes managing database models, file storage, and AI models.
Detailed Explanation of Endpoints and Functions
1. new_token()
Route: POST
/new_tokenAuthentication: Required (
@login_required)Purpose: Creates a new API token associated with the current user's tenant and a conversation or canvas.
Input:
JSON body may contain:
canvas_id(optional): if provided, token is linked to a canvas dialog.dialog_id(optional): otherwise, linked to a dialog.
Output: JSON object with token details or error message.
Usage Example:
curl -X POST "/new_token" -H "Content-Type: application/json" -d '{"canvas_id": "1234"}'Implementation details:
Retrieves tenant info via
UserTenantService.Generates a confirmation token string.
Saves token info using
APITokenService.save.
2. token_list()
Route: GET
/token_listAuthentication: Required
Purpose: Retrieves a list of API tokens for the current user's tenant filtered by dialog or canvas ID.
Input:
Query parameters:
dialog_idorcanvas_id.
Output: JSON list of token objects.
Notes: Returns error if tenant not found.
3. rm()
Route: POST
/rmAuthentication: Required
Purpose: Deletes API tokens based on provided token strings and tenant ID.
Input:
JSON body must include:
tokens(list of strings)tenant_id(string)
Output: JSON boolean success or error.
Validation: Request validated for required fields.
4. stats()
Route: GET
/statsAuthentication: Required
Purpose: Retrieves conversation statistics (page views, unique visitors, tokens used, response speed, etc.) for the tenant over a date range.
Input:
Query params:
from_date(default last 7 days)to_date(default now)canvas_id(optional)
Output: JSON object with arrays of stats keyed by date.
Implementation: Uses
API4ConversationService.stats.
5. set_conversation()
Route: GET
/new_conversationAuthentication: No explicit login required but uses API token in Authorization header.
Purpose: Starts a new conversation session linked to an API token's dialog or canvas.
Input:
HTTP header:
Authorization: Bearer <token>Optional query param:
user_id
Output: JSON conversation object with id, dialog_id, user_id, initial assistant message.
Implementation details:
Verifies token.
Loads dialog or canvas DSL (domain specific language) to create a
Canvasobject if source is "agent".Saves conversation record via
API4ConversationService.
6. completion()
Route: POST
/completionAuthentication: Uses API token in Authorization header.
Purpose: Processes a conversation message to generate an AI assistant completion response. Supports streaming output.
Input:
JSON body with:
conversation_id(str)messages(list of dicts with roles and content)Optional:
stream(bool)
Output:
If streaming: Server-Sent Events (SSE) stream of partial responses.
Otherwise: JSON with full answer and references.
Implementation details:
Filters system messages and handles message IDs.
If conversation source is "agent", uses
Canvasto generate answers.Otherwise, uses
chat()function from dialog service.Updates conversation messages and references in DB.
Usage: Supports interactive chat UI with streaming responses.
7. get_conversation(conversation_id)
Route: GET
/conversation/<conversation_id>Authentication: Uses API token.
Purpose: Retrieves conversation history and references.
Input: Authorization header token.
Output: JSON conversation object.
Note: Verifies token matches conversation dialog.
8. upload()
Route: POST
/document/uploadAuthentication: Uses API token.
Purpose: Uploads a file to a knowledge base and optionally triggers parsing and indexing.
Input:
multipart/form-dataincluding:kb_name(knowledgebase name)file(file upload)Optional
parser_id,runflags in form data.
Output: JSON document info or error.
Implementation details:
Validates file type and duplicates.
Stores file blob via
STORAGE_IMPL.Creates DB document record.
If run flag set, enqueues parsing tasks.
Limitations: Enforces max file number per user via env var.
9. upload_parse()
Route: POST
/document/upload_and_parseAuthentication: Uses API token.
Purpose: Uploads multiple files and directly parses them linked to a conversation.
Input:
multipart files
file(multiple allowed)form field
conversation_id
Output: List of document IDs uploaded and parsed.
10. list_chunks()
Route: POST
/list_chunksAuthentication: Uses API token.
Purpose: Lists chunks (text excerpts) from a document by doc name or doc id.
Input: JSON body with either:
doc_nameor
doc_id
Output: List of chunk summaries with content, document name, and image id.
Implementation: Uses
settings.retrievaler.chunk_list.
11. get_chunk(chunk_id)
Route: GET
/get_chunk/<chunk_id>Authentication: Uses API token.
Purpose: Retrieves detailed chunk information by chunk ID.
Output: JSON chunk data with some fields (vectors, tokens) stripped out.
Implementation: Uses
settings.docStoreConn.get.
12. list_kb_docs()
Route: POST
/list_kb_docsAuthentication: Uses API token.
Purpose: Lists documents in a knowledge base with pagination and filtering.
Input: JSON with:
kb_namepage,page_sizeorderby,desckeywordsstatus(filter by task status)types(filter by file types)
Output: JSON with total count and list of documents.
Validation: Checks for valid status and file types.
13. docinfos()
Route: POST
/document/infosAuthentication: Uses API token.
Purpose: Retrieves detailed document metadata for a list of doc IDs.
Input: JSON with
doc_idslist.Output: JSON list of document info dicts.
14. document_rm()
Route: DELETE
/documentAuthentication: Uses API token.
Purpose: Deletes documents by doc IDs or names for the tenant.
Input: JSON with:
doc_ids(optional)doc_names(optional)
Output: JSON boolean success or error with messages.
Implementation details:
Deletes DB document entries, file references, and storage blobs.
Handles errors aggregately.
15. completion_faq()
Route: POST
/completion_aibotkAuthentication: Uses API token passed in JSON Authorization field.
Purpose: Specialized completion endpoint for FAQ/AI bot style queries returning answer plus optional inline base64 encoded images.
Input: JSON with:
Authorization(token)conversation_idword(query)
Output: JSON response with answer text and embedded images (base64).
Implementation: Similar to
completion(), but processes image references in answers and encodes them inline.
16. retrieval()
Route: POST
/retrievalAuthentication: Uses API token.
Purpose: Retrieves relevant document chunks from knowledge bases based on a question.
Input: JSON with:
kb_id(list)doc_ids(list, optional)question(str)Pagination and filtering params:
page,page_size,similarity_threshold,vector_similarity_weight,top_k,highlightOptional
rerank_id,keywordflags
Output: JSON with ranked relevant chunks.
Implementation details:
Validates that all KBs use the same embedding model.
Uses
LLMBundleto get embedding and rerank models.Optionally enriches query with keyword extraction.
Calls
settings.retrievaler.retrievalwith rank features.
Important Implementation Details and Algorithms
API Token Management: Tokens are tied to tenants and dialogs or canvases. Tokens are generated with cryptographic confirmation tokens and tracked with timestamps.
Conversation Handling: Conversations can originate from "agent" canvases or dialogs. The system supports streaming AI assistant responses via Server-Sent Events (SSE) by yielding incremental answers.
Canvas DSL: For "agent" conversations, the canvas DSL (a JSON string representing conversation flow) is loaded and wrapped in a
Canvasclass which manages state, user inputs, and AI runs.Document Upload & Parsing: Uploaded files are stored in blob storage (
STORAGE_IMPL), and parsing tasks are queued for indexing and chunking. File types and parsers are determined dynamically.Chunk Retrieval & Filtering: Retrieval uses embedding similarity and optional reranking, leveraging LLM bundles and keyword extraction to improve search relevance. Results exclude vector data for privacy/performance.
Error Handling: Uniform error responses are returned with JSON error codes and messages. Server errors are caught and returned with HTTP 500-like JSON.
Security: Most endpoints require API token authentication. Tokens are verified to belong to the tenant and match the conversation or dialog context.
Interaction with Other System Components
Database Models and Services: Uses multiple services from
api.db.servicessuch asAPITokenService,API4ConversationService,DocumentService,DialogService,KnowledgebaseService,FileService,TaskService, andUserTenantServiceto query and manipulate persistent data.Storage Layer: Uses
STORAGE_IMPL(storage factory implementation) to save and retrieve files and blobs for documents and images.AI Models and Utilities:
LLMBundlefor managing LLM model instances (embedding, chat, rerank).Canvasclass for managing agent conversation DSL and execution.chat()for dialog-based chat completions.keyword_extractionandlabel_questionfor query enhancement.settings.retrievalerfor document retrieval logic.
Authentication: Uses Flask-Login's
current_userfor user context and protects some endpoints with@login_required. API tokens are used for most other endpoints.Flask Framework: Implements RESTful endpoints with Flask routes on the
managerblueprint.
Visual Diagram: Class and Function Structure (API Endpoints and Services Interaction)
classDiagram
class api_app {
+new_token()
+token_list()
+rm()
+stats()
+set_conversation()
+completion()
+get_conversation(conversation_id)
+upload()
+upload_parse()
+list_chunks()
+get_chunk(chunk_id)
+list_kb_docs()
+docinfos()
+document_rm()
+completion_faq()
+retrieval()
}
class APITokenService {
+save()
+query()
+filter_delete()
}
class API4ConversationService {
+save()
+get_by_id()
+append_message()
+stats()
}
class DocumentService {
+insert()
+get_by_ids()
+get_doc_count()
+update_by_id()
+remove_document()
+get_tenant_id()
+get_tenant_id_by_name()
+get_doc_id_by_doc_name()
+get_by_kb_id()
}
class KnowledgebaseService {
+get_by_name()
+get_kb_ids()
+get_by_ids()
}
class UserTenantService {
+query()
}
class FileService {
+get_root_folder()
+init_knowledgebase_docs()
+get_kb_folder()
+new_a_file_from_kb()
+add_file_from_kb()
+filter_delete()
}
class File2DocumentService {
+get_storage_address()
+get_by_document_id()
+delete_by_document_id()
}
class TaskService {
+filter_delete()
}
class DialogService {
+get_by_id()
}
class UserCanvasService {
+get_by_id()
}
class Canvas {
+__init__()
+add_user_input()
+run()
+get_prologue()
}
class chat {
+function()
}
api_app --> APITokenService : uses
api_app --> API4ConversationService : uses
api_app --> DocumentService : uses
api_app --> KnowledgebaseService : uses
api_app --> UserTenantService : uses
api_app --> FileService : uses
api_app --> File2DocumentService : uses
api_app --> TaskService : uses
api_app --> DialogService : uses
api_app --> UserCanvasService : uses
api_app --> Canvas : instantiates
api_app --> chat : calls
Summary
api_app.py provides a comprehensive API layer for managing conversational AI workflows, document ingestion, knowledge base retrieval, and token-based authentication. It tightly integrates with backend services and AI components, supporting both synchronous and streaming interactions. The file is central to the system's ability to handle user conversations, document management, and retrieval operations securely and efficiently.
This module is designed for use within a Flask web application and expects integration with supporting service modules and storage backends as outlined in the imports and service usage.