-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
445 lines (392 loc) · 16.8 KB
/
Copy pathProgram.cs
File metadata and controls
445 lines (392 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using Azure;
using Azure.Search.Documents.Indexes;
using Azure.Storage.Blobs;
using DriftMind.DTOs;
using DriftMind.Services;
using DriftMind.Models;
using OpenAI;
using OpenAI.Chat;
using OpenAI.Embeddings;
using System.ClientModel;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Add Memory Cache for performance optimizations
builder.Services.AddMemoryCache(options =>
{
options.SizeLimit = 1000; // Maximum 1000 cache entries
options.CompactionPercentage = 0.25; // Remove 25% when limit reached
options.ExpirationScanFrequency = TimeSpan.FromMinutes(5); // Cleanup every 5 minutes
});
// Configure file upload options
builder.Services.Configure<FileUploadOptions>(
builder.Configuration.GetSection("FileUpload"));
// Azure OpenAI Configuration
var azureOpenAIEndpoint = builder.Configuration["AzureOpenAI:Endpoint"]!;
var azureOpenAIApiKey = builder.Configuration["AzureOpenAI:ApiKey"]!;
var azureOpenAIBaseUri = new Uri($"{azureOpenAIEndpoint.TrimEnd('/')}/openai/v1/");
var openAIApiCredential = new ApiKeyCredential(azureOpenAIApiKey);
var openAIClientOptions = new OpenAIClientOptions
{
Endpoint = azureOpenAIBaseUri
};
builder.Services.AddSingleton(sp => new ChatClient(
model: builder.Configuration["AzureOpenAI:ChatDeploymentName"] ?? "gpt-5.4",
credential: openAIApiCredential,
options: openAIClientOptions));
builder.Services.AddSingleton(sp => new EmbeddingClient(
model: builder.Configuration["AzureOpenAI:EmbeddingDeploymentName"] ?? "text-embedding-ada-002",
credential: openAIApiCredential,
options: openAIClientOptions));
// Azure Search Configuration
var azureSearchEndpoint = builder.Configuration["AzureSearch:Endpoint"]!;
var azureSearchApiKey = builder.Configuration["AzureSearch:ApiKey"]!;
builder.Services.AddSingleton(sp => new SearchIndexClient(new Uri(azureSearchEndpoint), new AzureKeyCredential(azureSearchApiKey)));
// Azure Blob Storage Configuration
var azureStorageConnectionString = builder.Configuration["AzureStorage:ConnectionString"]!;
builder.Services.AddSingleton(sp => new BlobServiceClient(azureStorageConnectionString));
// Register Services
builder.Services.AddScoped<ITextChunkingService, TextChunkingService>();
builder.Services.AddScoped<IEmbeddingService, EmbeddingService>();
builder.Services.AddScoped<ISearchService, SearchService>();
builder.Services.AddScoped<IFileProcessingService, FileProcessingService>();
builder.Services.AddScoped<IDocumentProcessingService, DocumentProcessingService>();
builder.Services.AddScoped<IChatService, ChatService>();
builder.Services.AddScoped<IQueryExpansionService, QueryExpansionService>();
builder.Services.AddScoped<ISearchHistoryService, SearchHistoryService>();
builder.Services.AddScoped<ISearchOrchestrationService, SearchOrchestrationService>();
builder.Services.AddScoped<IDocumentManagementService, DocumentManagementService>();
builder.Services.AddScoped<IBlobStorageService, BlobStorageService>();
builder.Services.AddScoped<IDownloadService, DownloadService>();
builder.Services.AddScoped<IDataMigrationService, DataMigrationService>();
// Configure URLs for production deployment
builder.WebHost.UseUrls("http://0.0.0.0:8081");
var app = builder.Build();
// Initialize Azure Search Index and Blob Storage
using (var scope = app.Services.CreateScope())
{
var searchService = scope.ServiceProvider.GetRequiredService<ISearchService>();
await searchService.InitializeIndexAsync();
var blobStorageService = scope.ServiceProvider.GetRequiredService<IBlobStorageService>();
await blobStorageService.InitializeAsync();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger(options =>
{
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_1;
});
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
// Upload Endpoint (File Upload Only)
app.MapPost("/upload", async (IFormFile file, string? documentId, string? metadata,
int? chunkSize, int? chunkOverlap, IDocumentProcessingService documentService) =>
{
if (file == null || file.Length == 0)
{
return Results.BadRequest(new UploadTextResponse
{
Success = false,
Message = "No file provided or file is empty."
});
}
var request = new UploadFileRequest
{
File = file,
DocumentId = documentId,
Metadata = metadata,
ChunkSize = chunkSize ?? 300,
ChunkOverlap = chunkOverlap ?? 20
};
var response = await documentService.ProcessFileAsync(request);
if (response.Success)
{
return Results.Ok(response);
}
if (string.Equals(response.ErrorCode, "Conflict", StringComparison.OrdinalIgnoreCase))
{
return Results.Conflict(new { error = response.Message, documentId = response.DocumentId });
}
return Results.Problem(
title: "Error processing file",
detail: response.Message,
statusCode: 500);
})
.WithName("UploadFile")
.WithSummary("Uploads a file, extracts text, splits it into chunks and creates embeddings")
.WithDescription("This endpoint accepts files (.txt, .md, .pdf, .docx), extracts text, splits it into chunks, creates embeddings and stores them in Azure AI Search. Maximum file size: 12MB.")
.DisableAntiforgery();
// Search Endpoint
app.MapPost("/search", async (SearchRequest request, ISearchOrchestrationService searchService, IConfiguration configuration) =>
{
if (string.IsNullOrWhiteSpace(request.Query))
{
return Results.BadRequest(new SearchResponse
{
Query = request.Query,
Success = false,
Message = "Search query cannot be empty."
});
}
if (request.MaxResults <= 0 || request.MaxResults > 50)
{
return Results.BadRequest(new SearchResponse
{
Query = request.Query,
Success = false,
Message = "MaxResults must be between 1 and 50."
});
}
// Apply default configuration values if needed
if (request.EnableQueryExpansion == false) // Only override if explicitly set to false
{
// Keep the user's setting
}
else
{
// Use configuration default if not explicitly set
request.EnableQueryExpansion = configuration.GetValue<bool>("QueryExpansion:EnabledByDefault", true);
}
var response = await searchService.SearchAsync(request);
return response.Success ? Results.Ok(response) : Results.Problem(
title: "Error during search",
detail: response.Message,
statusCode: 500);
})
.WithName("SearchDocuments")
.WithSummary("Searches documents and generates an answer with GPT-5 Chat")
.WithDescription("This endpoint performs a semantic search in the Azure AI Search database and generates an answer with GPT-5 Chat based on the found documents.");
// Documents List Endpoint
app.MapPost("/documents", async (DocumentListRequest request, IDocumentManagementService documentService) =>
{
if (request.MaxResults <= 0 || request.MaxResults > 100)
{
return Results.BadRequest(new DocumentListResponse
{
Success = false,
Message = "MaxResults must be between 1 and 100."
});
}
if (request.Skip < 0)
{
return Results.BadRequest(new DocumentListResponse
{
Success = false,
Message = "Skip must be 0 or greater."
});
}
var response = await documentService.GetAllDocumentsAsync(request);
return response.Success ? Results.Ok(response) : Results.Problem(
title: "Error retrieving documents",
detail: response.Message,
statusCode: 500);
})
.WithName("ListDocuments")
.WithSummary("Lists all documents in the database")
.WithDescription("This endpoint retrieves a list of all documents stored in Azure AI Search with their metadata, chunk counts, and sample content.");
// Alternative GET endpoint for simple document listing
app.MapGet("/documents", async (int maxResults, int skip, string? documentId, IDocumentManagementService documentService) =>
{
var request = new DocumentListRequest
{
MaxResults = maxResults > 0 ? Math.Min(maxResults, 100) : 50,
Skip = Math.Max(skip, 0),
DocumentIdFilter = documentId
};
var response = await documentService.GetAllDocumentsAsync(request);
return response.Success ? Results.Ok(response) : Results.Problem(
title: "Error retrieving documents",
detail: response.Message,
statusCode: 500);
})
.WithName("ListDocumentsGet")
.WithSummary("Lists all documents in the database (GET)")
.WithDescription("This endpoint retrieves a list of all documents stored in Azure AI Search. Use query parameters: maxResults (1-100, default 50), skip (default 0), documentId (optional filter).");
// Delete Document Endpoint
app.MapDelete("/documents/{documentId}", async (string documentId, IDocumentManagementService documentService) =>
{
if (string.IsNullOrWhiteSpace(documentId))
{
return Results.BadRequest(new DeleteDocumentResponse
{
DocumentId = documentId,
Success = false,
Message = "Document ID cannot be empty."
});
}
var request = new DeleteDocumentRequest { DocumentId = documentId };
var response = await documentService.DeleteDocumentAsync(request);
if (response.Success)
{
return Results.Ok(response);
}
else if (response.Message.Contains("not found"))
{
return Results.NotFound(response);
}
else
{
return Results.Problem(
title: "Error deleting document",
detail: response.Message,
statusCode: 500);
}
})
.WithName("DeleteDocument")
.WithSummary("Deletes a document and all its chunks")
.WithDescription("This endpoint deletes a document and all its associated chunks from Azure AI Search. The operation cannot be undone.");
// Alternative DELETE endpoint using request body
app.MapPost("/documents/delete", async (DeleteDocumentRequest request, IDocumentManagementService documentService) =>
{
if (string.IsNullOrWhiteSpace(request.DocumentId))
{
return Results.BadRequest(new DeleteDocumentResponse
{
DocumentId = request.DocumentId,
Success = false,
Message = "Document ID cannot be empty."
});
}
var response = await documentService.DeleteDocumentAsync(request);
if (response.Success)
{
return Results.Ok(response);
}
else if (response.Message.Contains("not found"))
{
return Results.NotFound(response);
}
else
{
return Results.Problem(
title: "Error deleting document",
detail: response.Message,
statusCode: 500);
}
})
.WithName("DeleteDocumentPost")
.WithSummary("Deletes a document and all its chunks (POST)")
.WithDescription("This endpoint deletes a document and all its associated chunks from Azure AI Search using a POST request with JSON body. The operation cannot be undone.");
// Secure Download Endpoints
app.MapPost("/download/token", async (GenerateDownloadTokenRequest request, IDownloadService downloadService) =>
{
if (string.IsNullOrWhiteSpace(request.DocumentId))
{
return Results.BadRequest(new { error = "DocumentId is required" });
}
if (request.ExpirationMinutes <= 0 || request.ExpirationMinutes > 60)
{
return Results.BadRequest(new { error = "ExpirationMinutes must be between 1 and 60" });
}
var response = await downloadService.GenerateDownloadTokenAsync(
request.DocumentId,
userId: null, // No user tracking needed
expiration: TimeSpan.FromMinutes(request.ExpirationMinutes));
if (!response.Success)
{
if (response.ErrorMessage?.Contains("not found") == true)
{
return Results.NotFound(new { error = response.ErrorMessage });
}
return Results.Problem(response.ErrorMessage ?? "Failed to generate download token");
}
return Results.Ok(response);
})
.WithName("GenerateDownloadToken")
.WithSummary("Generates a secure, time-limited download token for a document")
.WithDescription("Creates a secure token that allows downloading a specific document. The token expires after the specified time and can only be used for the requested document.")
.WithTags("Downloads");
// POST endpoint for secure file downloads using token in request body
app.MapPost("/download/file", async (TokenDownloadRequest request, IDownloadService downloadService) =>
{
if (string.IsNullOrWhiteSpace(request.Token))
{
return Results.BadRequest(new { error = "Download token is required" });
}
// 1. Validate token
var validation = await downloadService.ValidateDownloadTokenAsync(request.Token);
if (!validation.IsValid)
{
if (validation.ErrorMessage?.Contains("expired") == true)
{
return Results.Problem(
title: "Token Expired",
detail: "The download token has expired. Please generate a new one.",
statusCode: 410); // Gone
}
return Results.Problem(
title: "Invalid Token",
detail: validation.ErrorMessage ?? "Invalid download token",
statusCode: 401); // Unauthorized
}
// 2. Download file
try
{
var fileResult = await downloadService.GetFileForDownloadAsync(validation.DocumentId);
if (!fileResult.Success || fileResult.FileStream == null)
{
return Results.NotFound(new { error = fileResult.ErrorMessage ?? "File not found" });
}
// Use Results.Stream for better control over Content-Disposition header with umlauts
return Results.Stream(fileResult.FileStream, fileResult.ContentType, fileResult.FileName, enableRangeProcessing: true);
}
catch (Exception ex)
{
return Results.Problem($"Download failed: {ex.Message}");
}
})
.WithName("DownloadFileWithToken")
.WithSummary("Downloads a file using a secure token")
.WithDescription("Downloads the file associated with the provided download token. The token must be valid and not expired. Token is provided in the request body for better security.")
.WithTags("Downloads");
// Data Migration Endpoint (for administrators)
app.MapPost("/admin/migrate/optimize-metadata", async (IDataMigrationService migrationService) =>
{
var success = await migrationService.MigrateToOptimizedMetadataStorageAsync();
if (success)
{
return Results.Ok(new {
success = true,
message = "Migration completed successfully. Metadata is now stored only in chunk 0, reducing storage redundancy by ~98%."
});
}
else
{
return Results.Problem(
title: "Migration Failed",
detail: "Failed to complete metadata optimization migration. Check logs for details.",
statusCode: 500);
}
})
.WithName("MigrateOptimizeMetadata")
.WithSummary("Migrates existing documents to optimized metadata storage")
.WithDescription("Optimizes storage by moving all document metadata (filename, size, content type) to chunk 0 only, removing redundancy from other chunks. This reduces storage usage by ~98% for metadata.")
.WithTags("Administration", "Migration");
// Content Type Migration Endpoint
app.MapPost("/admin/migrate/fix-content-types", async (IDataMigrationService migrationService) =>
{
var success = await migrationService.FixContentTypesAsync();
if (success)
{
return Results.Ok(new {
success = true,
message = "Content type migration completed successfully. All documents now have correct MIME types based on file extensions."
});
}
else
{
return Results.Problem(
title: "Content Type Migration Failed",
detail: "Failed to complete content type migration. Check logs for details.",
statusCode: 500);
}
})
.WithName("MigrateFixContentTypes")
.WithSummary("Fixes incorrect content types for existing documents")
.WithDescription("Corrects content types for existing documents by mapping file extensions to proper MIME types (e.g., .pdf -> application/pdf instead of application/octet-stream). Updates both Azure Search index and blob storage metadata.")
.WithTags("Administration", "Migration");
app.Run();