-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebAPIEndpoints.cs
More file actions
410 lines (359 loc) · 19.1 KB
/
WebAPIEndpoints.cs
File metadata and controls
410 lines (359 loc) · 19.1 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
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.Context;
using Microsoft.KernelMemory.DocumentStorage;
using Microsoft.KernelMemory.Service.AspNetCore.Models;
namespace Microsoft.KernelMemory.Service.AspNetCore;
public static class WebAPIEndpoints
{
public static IEndpointRouteBuilder AddKernelMemoryEndpoints(
this IEndpointRouteBuilder builder,
string apiPrefix = "/",
IEndpointFilter? authFilter = null)
{
builder.AddPostUploadEndpoint(apiPrefix, authFilter);
builder.AddGetIndexesEndpoint(apiPrefix, authFilter);
builder.AddDeleteIndexesEndpoint(apiPrefix, authFilter);
builder.AddDeleteDocumentsEndpoint(apiPrefix, authFilter);
builder.AddAskEndpoint(apiPrefix, authFilter);
builder.AddSearchEndpoint(apiPrefix, authFilter);
builder.AddUploadStatusEndpoint(apiPrefix, authFilter);
builder.AddGetDownloadEndpoint(apiPrefix, authFilter);
return builder;
}
public static void AddPostUploadEndpoint(
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
{
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
// File upload endpoint
var route = group.MapPost(Constants.HttpUploadEndpoint, async Task<IResult> (
HttpRequest request,
IKernelMemory service,
ILogger<KernelMemoryWebAPI> log,
IContextProvider contextProvider,
CancellationToken cancellationToken) =>
{
log.LogTrace("New upload HTTP request, content length {0}", request.ContentLength);
// Note: .NET doesn't yet support binding multipart forms including data and files
(HttpDocumentUploadRequest input, bool isValid, string errMsg)
= await HttpDocumentUploadRequest.BindHttpRequestAsync(request, cancellationToken)
.ConfigureAwait(false);
// Allow internal classes to access custom arguments via IContextProvider
contextProvider.InitContextArgs(input.ContextArguments);
log.LogTrace("Index '{0}'", input.Index);
if (!isValid)
{
log.LogError(errMsg);
return Results.Problem(detail: errMsg, statusCode: 400);
}
try
{
// UploadRequest => Document
var documentId = await service
.ImportDocumentAsync(input.ToDocumentUploadRequest(), contextProvider.GetContext(), cancellationToken)
.ConfigureAwait(false);
log.LogTrace("Doc Id '{1}'", documentId);
var url = Constants.HttpUploadStatusEndpointWithParams
.Replace(Constants.HttpIndexPlaceholder, input.Index, StringComparison.Ordinal)
.Replace(Constants.HttpDocumentIdPlaceholder, documentId, StringComparison.Ordinal);
return Results.Accepted(url, new UploadAccepted
{
DocumentId = documentId,
Index = input.Index,
Message = "Document upload completed, ingestion pipeline started"
});
}
catch (Exception e)
{
return Results.Problem(title: "Document upload failed", detail: e.Message, statusCode: 503);
}
})
.Produces<UploadAccepted>(StatusCodes.Status202Accepted)
.Produces<ProblemDetails>(StatusCodes.Status400BadRequest)
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden)
.Produces<ProblemDetails>(StatusCodes.Status503ServiceUnavailable);
if (authFilter != null) { route.AddEndpointFilter(authFilter); }
}
public static void AddGetIndexesEndpoint(
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
{
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
// List of indexes endpoint
var route = group.MapGet(Constants.HttpIndexesEndpoint,
async Task<IResult> (
IKernelMemory service,
ILogger<KernelMemoryWebAPI> log,
CancellationToken cancellationToken) =>
{
log.LogTrace("New index list HTTP request");
var result = new IndexCollection();
IEnumerable<IndexDetails> list = await service.ListIndexesAsync(cancellationToken)
.ConfigureAwait(false);
foreach (IndexDetails index in list)
{
result.Results.Add(index);
}
return Results.Ok(result);
})
.Produces<IndexCollection>(StatusCodes.Status200OK)
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden);
if (authFilter != null) { route.AddEndpointFilter(authFilter); }
}
public static void AddDeleteIndexesEndpoint(
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
{
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
// Delete index endpoint
var route = group.MapDelete(Constants.HttpIndexesEndpoint,
async Task<IResult> (
[FromQuery(Name = Constants.WebService.IndexField)]
string? index,
IKernelMemory service,
ILogger<KernelMemoryWebAPI> log,
CancellationToken cancellationToken) =>
{
log.LogTrace("New delete document HTTP request, index {Index}", index?.Replace("\r", string.Empty).Replace("\n", string.Empty));
await service.DeleteIndexAsync(index: index, cancellationToken)
.ConfigureAwait(false);
// There's no API to check the index deletion progress, so the URL is empty
var url = string.Empty;
return Results.Accepted(url, new DeleteAccepted
{
Index = index ?? string.Empty,
Message = "Index deletion request received, pipeline started"
});
})
.Produces<DeleteAccepted>(StatusCodes.Status202Accepted)
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden);
if (authFilter != null) { route.AddEndpointFilter(authFilter); }
}
public static void AddDeleteDocumentsEndpoint(
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
{
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
// Delete document endpoint
var route = group.MapDelete(Constants.HttpDocumentsEndpoint,
async Task<IResult> (
[FromQuery(Name = Constants.WebService.IndexField)]
string? index,
[FromQuery(Name = Constants.WebService.DocumentIdField)]
string documentId,
IKernelMemory service,
ILogger<KernelMemoryWebAPI> log,
CancellationToken cancellationToken) =>
{
log.LogTrace("New delete document HTTP request, index {Index}", index?.Replace("\r", string.Empty).Replace("\n", string.Empty));
await service.DeleteDocumentAsync(documentId: documentId, index: index, cancellationToken)
.ConfigureAwait(false);
var url = Constants.HttpUploadStatusEndpointWithParams
.Replace(Constants.HttpIndexPlaceholder, index, StringComparison.Ordinal)
.Replace(Constants.HttpDocumentIdPlaceholder, documentId, StringComparison.Ordinal);
return Results.Accepted(url, new DeleteAccepted
{
DocumentId = documentId,
Index = index ?? string.Empty,
Message = "Document deletion request received, pipeline started"
});
})
.Produces<DeleteAccepted>(StatusCodes.Status202Accepted)
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden);
if (authFilter != null) { route.AddEndpointFilter(authFilter); }
}
public static void AddAskEndpoint(
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
{
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
// Ask endpoint
var route = group.MapPost(Constants.HttpAskEndpoint,
async Task<IResult> (
MemoryQuery query,
IKernelMemory service,
ILogger<KernelMemoryWebAPI> log,
IContextProvider contextProvider,
CancellationToken cancellationToken) =>
{
// Allow internal classes to access custom arguments via IContextProvider
contextProvider.InitContextArgs(query.ContextArguments);
log.LogTrace("New search request, index {Index}, minRelevance {MinRelevance}", query.Index?.Replace("\r", string.Empty).Replace("\n", string.Empty), query.MinRelevance);
MemoryAnswer answer = await service.AskAsync(
question: query.Question,
index: query.Index,
filters: query.Filters,
minRelevance: query.MinRelevance,
context: contextProvider.GetContext(),
cancellationToken: cancellationToken)
.ConfigureAwait(false);
return Results.Ok(answer);
})
.Produces<MemoryAnswer>(StatusCodes.Status200OK)
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden);
if (authFilter != null) { route.AddEndpointFilter(authFilter); }
}
public static void AddSearchEndpoint(
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
{
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
// Search endpoint
var route = group.MapPost(Constants.HttpSearchEndpoint,
async Task<IResult> (
SearchQuery query,
IKernelMemory service,
ILogger<KernelMemoryWebAPI> log,
IContextProvider contextProvider,
CancellationToken cancellationToken) =>
{
// Allow internal classes to access custom arguments via IContextProvider
contextProvider.InitContextArgs(query.ContextArguments);
log.LogTrace("New search HTTP request, index {Index}, minRelevance {MinRelevance}", query.Index?.Replace("\r", string.Empty).Replace("\n", string.Empty), query.MinRelevance);
SearchResult answer = await service.SearchAsync(
query: query.Query,
index: query.Index,
filters: query.Filters,
minRelevance: query.MinRelevance,
limit: query.Limit,
context: contextProvider.GetContext(),
cancellationToken: cancellationToken)
.ConfigureAwait(false);
return Results.Ok(answer);
})
.Produces<SearchResult>(StatusCodes.Status200OK)
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden);
if (authFilter != null) { route.AddEndpointFilter(authFilter); }
}
public static void AddUploadStatusEndpoint(
this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
{
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
// Document status endpoint
var route = group.MapGet(Constants.HttpUploadStatusEndpoint,
async Task<IResult> (
[FromQuery(Name = Constants.WebService.IndexField)]
string? index,
[FromQuery(Name = Constants.WebService.DocumentIdField)]
string documentId,
IKernelMemory memoryClient,
ILogger<KernelMemoryWebAPI> log,
CancellationToken cancellationToken) =>
{
log.LogTrace("New document status HTTP request");
if (string.IsNullOrEmpty(documentId))
{
return Results.Problem(detail: $"'{Constants.WebService.DocumentIdField}' query parameter is missing or has no value", statusCode: 400);
}
DataPipelineStatus? pipeline = await memoryClient.GetDocumentStatusAsync(documentId: documentId, index: index, cancellationToken)
.ConfigureAwait(false);
if (pipeline == null)
{
return Results.Problem(detail: "Document not found", statusCode: 404);
}
if (pipeline.Empty)
{
return Results.Problem(detail: "Empty pipeline", statusCode: 404);
}
return Results.Ok(pipeline);
})
.Produces<DataPipelineStatus>(StatusCodes.Status200OK)
.Produces<ProblemDetails>(StatusCodes.Status400BadRequest)
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden)
.Produces<ProblemDetails>(StatusCodes.Status404NotFound);
if (authFilter != null) { route.AddEndpointFilter(authFilter); }
}
public static void AddGetDownloadEndpoint(this IEndpointRouteBuilder builder, string apiPrefix = "/", IEndpointFilter? authFilter = null)
{
RouteGroupBuilder group = builder.MapGroup(apiPrefix);
// File download endpoint
var route = group.MapGet(Constants.HttpDownloadEndpoint, async Task<IResult> (
[FromQuery(Name = Constants.WebService.IndexField)]
string? index,
[FromQuery(Name = Constants.WebService.DocumentIdField)]
string documentId,
[FromQuery(Name = Constants.WebService.FilenameField)]
string filename,
HttpContext httpContext,
IKernelMemory service,
ILogger<KernelMemoryWebAPI> log,
CancellationToken cancellationToken) =>
{
var isValid = !(
string.IsNullOrWhiteSpace(documentId) ||
string.IsNullOrWhiteSpace(filename));
var errMsg = "Missing required parameter";
log.LogTrace("New download file HTTP request, index {Index}, documentId {DocumentId}, fileName {FileName}",
index?.Replace("\r", string.Empty).Replace("\n", string.Empty),
documentId?.Replace("\r", string.Empty).Replace("\n", string.Empty),
filename?.Replace("\r", string.Empty).Replace("\n", string.Empty));
if (!isValid)
{
log.LogError(errMsg);
return Results.Problem(detail: errMsg, statusCode: 400);
}
try
{
// DownloadRequest => Document
var file = await service.ExportFileAsync(
documentId: documentId,
fileName: filename,
index: index,
cancellationToken: cancellationToken)
.ConfigureAwait(false);
if (file == null)
{
log.LogWarning("Returned file is NULL, file not found");
return Results.Problem(title: "File not found", statusCode: 404);
}
log.LogTrace("Downloading file {FileName}, size {FileSize}, type {FileType}",
filename?.Replace("\r", string.Empty).Replace("\n", string.Empty),
file.FileSize,
file.FileType?.Replace("\r", string.Empty).Replace("\n", string.Empty));
Stream resultingFileStream = await file.GetStreamAsync().WaitAsync(cancellationToken).ConfigureAwait(false);
var response = Results.Stream(
resultingFileStream,
contentType: file.FileType,
fileDownloadName: filename,
lastModified: file.LastWrite,
enableRangeProcessing: true);
// Add content length header if missing
if (response is FileStreamHttpResult { FileLength: null or 0 })
{
httpContext.Response.Headers.ContentLength = file.FileSize;
}
return response;
}
catch (DocumentStorageFileNotFoundException e)
{
return Results.Problem(title: "File not found", detail: e.Message, statusCode: 404);
}
catch (Exception e)
{
return Results.Problem(title: "File download failed", detail: e.Message, statusCode: 503);
}
})
.Produces<StreamableFileContent>(StatusCodes.Status200OK)
.Produces<ProblemDetails>(StatusCodes.Status404NotFound)
.Produces<ProblemDetails>(StatusCodes.Status401Unauthorized)
.Produces<ProblemDetails>(StatusCodes.Status403Forbidden)
.Produces<ProblemDetails>(StatusCodes.Status503ServiceUnavailable);
if (authFilter != null) { route.AddEndpointFilter(authFilter); }
}
#pragma warning disable CA1812 // used by logger, can't be static
// Class used to tag log entries and allow log filtering
private sealed class KernelMemoryWebAPI;
#pragma warning restore CA1812
}