How AntSK Handles File Uploads and Document Processing: A Complete Pipeline Guide
AntSK processes file uploads through a multi-stage pipeline that saves files to disk, validates them via KMService, queues background import tasks through BackgroundTaskBroker, and processes documents using KernelMemory with configurable LLM embeddings and vector database storage.
The AntSK file upload and document processing pipeline is implemented in the aidotnet/antsk repository as a sophisticated workflow that bridges frontend uploads with backend AI document processing. This system handles everything from initial file validation to vector embedding generation, enabling semantic search capabilities across uploaded documents.
File Upload and Storage
The pipeline begins at the API layer with a dedicated endpoint for receiving multipart form data.
In src/AntSK/Controllers/FileController.cs, the UploadFile method handles POST /api/File/UploadFile requests:
// FileController.cs implementation
public async Task<IActionResult> UploadFile(IFormFile file)
{
// Validation and storage logic
}
The controller performs the following operations:
- Validates the incoming
IFormFilefor null checks and size constraints - Constructs a storage path using
FileDirOption.DirectoryPath/filesas the base directory - Generates a unique filename using GUID-based naming to prevent collisions
- Writes the file stream to disk asynchronously
- Returns the physical path (relative to
uploads) to the caller for downstream processing
Client-Side Validation and Tracking
Before files reach the background processing queue, the frontend validates and tracks them using KMService.
Located in src/AntSK/Domain/Domain/Service/KMService.cs, this service provides three critical methods for the upload workflow:
Validation via BeforeUpload:
public bool BeforeUpload(UploadFileItem file)
{
// Checks MIME type against allowed extensions
// Validates file size < 100MB
return true; // Only returns true for valid files
}
Completion Tracking via OnSingleCompleted:
When the AntDesign Upload component signals completion, this callback adds the file's name and URL to an internal _fileList collection.
Public Access via FileList Property:
public List<UploadFileItem> FileList { get; }
UI pages read this list to display uploaded files and enable the final import confirmation button.
Background Task Queueing
When the user confirms the upload on the KMS Detail page, the system transitions from synchronous file storage to asynchronous background processing.
In src/AntSK/Pages/KmsPage/KmsDetail.razor.cs, the FileHandleOk method iterates over the validated files:
foreach (var item in iKMService.FileList)
{
await _httpService.PostAsync(
$"{NavigationManager.BaseUri}api/KMS/ImportKMSTask",
new ImportKMSTaskDTO
{
ImportType = ImportType.File,
KmsId = KmsId,
FilePath = item.Url,
FileName = item.FileName,
IsQA = _isQa
});
}
The ImportKMSTaskDTO (defined in src/AntSK/Domain/Domain/Model/ImportKMSTaskReq.cs) encapsulates:
ImportType: File or URLKmsId: Target knowledge base identifierFilePath: Physical path from the upload stepIsQA: Boolean flag enabling QA-specific processing
Document Processing Pipeline
The core processing logic resides in ImportKMSService, orchestrated through the background task infrastructure.
Task Routing
- KMSController.ImportKMSTask receives the DTO and converts it to a request object
- BackgroundTaskBroker enqueues the work
- BackGroundTaskHandler.ExecuteAsync routes to
ImportKMSService.ImportKMSTask
KernelMemory Configuration
Before processing begins, KMService.GetMemoryByKMS constructs a KernelMemoryBuilder with provider-specific configurations:
// Pseudocode representing the configuration logic
var memory = new KernelMemoryBuilder()
.WithTextGenerationByAIType(chatModel) // OpenAI, Azure, etc.
.WithTextEmbeddingGenerationByAIType(embed) // BGE, DashScope, etc.
.WithMemoryDbByVectorDB(vectorDb) // Postgres, Redis, Qdrant, etc.
.Build<MemoryServerless>();
Document Import and Embedding Generation
For file imports, ImportKMSService.ImportKMSTask executes:
-
Memory Instance Retrieval: Calls
_kMService.GetMemoryByKMS(km.Id)to obtain the configuredMemoryServerlessinstance -
Optional QA Pipeline: When
req.IsQAis true, the pipeline injects additional handlers:TextExtractionHandlerfor content parsingQAHandlerfor question-answer pair generationGenerateEmbeddingsHandlerfor vector creationSaveRecordsHandlerfor persistence
-
Document Import: Executes
_memory.ImportDocumentAsyncwith a newDocumentinstance:await _memory.ImportDocumentAsync( new Document(fileid) .AddFile(req.FilePath) // Additional configuration based on import type ); -
Statistics Collection: Queries the memory store using
_kMService.GetDocumentByFileID(km.Id, fileid)to count indexed partitions -
Metadata Persistence: Updates the KMS detail record via
_kmsDetails_Repositories.Update(req.KmsDetail)with:- Original filename
- Generated file GUID
- Data count (partition count)
- Success status flag
Summary
- FileController.UploadFile in
src/AntSK/Controllers/FileController.cshandles initial file storage with GUID-based naming and returns physical paths - KMService provides client-side validation through
BeforeUploadand tracks completed uploads inFileListfor UI coordination - KmsDetail.razor.cs triggers background processing by posting
ImportKMSTaskDTOobjects to the KMS controller - BackgroundTaskBroker and BackGroundTaskHandler route tasks asynchronously to prevent UI blocking during document processing
- ImportKMSService.ImportKMSTask orchestrates the core pipeline: building KernelMemory instances with configurable LLM and embedding providers, executing document import with optional QA handlers, generating embeddings, and persisting metadata to the KMS repository
Frequently Asked Questions
How does AntSK validate file uploads before processing?
AntSK validates uploads at two levels. First, the KMService.BeforeUpload method in src/AntSK/Domain/Domain/Service/KMService.cs checks MIME types against allowed extensions and enforces a 100MB size limit on the client side. Second, FileController.UploadFile performs server-side validation of the IFormFile object before writing the stream to disk with a GUID-based filename.
What background task system does AntSK use for document processing?
AntSK uses a custom BackgroundTaskBroker queueing system implemented in src/AntSK/Domain/Domain/Other/BackGroundTaskHandler.cs. When the UI calls KMSController.ImportKMSTask, the controller converts the DTO to a request and enqueues it. The BackGroundTaskHandler.ExecuteAsync method dequeues tasks and routes them to ImportKMSService.ImportKMSTask, allowing document processing to run asynchronously without blocking the web interface.
How does AntSK configure AI models and vector databases for document embeddings?
AntSK dynamically builds KernelMemory configurations through KMService.GetMemoryByKMS in src/AntSK/Domain/Domain/Service/KMService.cs. The method constructs a KernelMemoryBuilder using helper methods like WithTextGenerationByAIType (for LLMs such as OpenAI or Azure OpenAI), WithTextEmbeddingGenerationByAIType (for embeddings like BGE or DashScope), and WithMemoryDbByVectorDB (for vector stores including Postgres, Redis, Qdrant, or Azure AI Search). This configuration is then used by ImportKMSService to generate embeddings and store document partitions.
What happens when the QA mode is enabled during file import?
When ImportKMSTaskDTO.IsQA is set to true, ImportKMSService.ImportKMSTask in src/AntSK/Domain/Domain/Service/ImportKMSService.cs injects additional processing handlers into the KernelMemory pipeline. These include TextExtractionHandler for parsing document content, QAHandler for generating question-answer pairs from the text, GenerateEmbeddingsHandler for creating vector embeddings, and SaveRecordsHandler for persisting the results. This QA-specific pipeline enables the system to build searchable knowledge bases optimized for question-answering scenarios.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →