|
1 | | -using System.ComponentModel; |
2 | 1 | using System.Net.Mime; |
3 | 2 | using System.Text.Json.Serialization; |
4 | | -using Microsoft.AspNetCore.Http.HttpResults; |
5 | 3 | using Microsoft.EntityFrameworkCore; |
6 | 4 | using Microsoft.SemanticKernel; |
7 | | -using MimeMapping; |
8 | 5 | using SqlDatabaseVectorSearch.Components; |
9 | 6 | using SqlDatabaseVectorSearch.ContentDecoders; |
10 | 7 | using SqlDatabaseVectorSearch.DataAccessLayer; |
11 | 8 | using SqlDatabaseVectorSearch.Extensions; |
12 | | -using SqlDatabaseVectorSearch.Models; |
13 | 9 | using SqlDatabaseVectorSearch.Services; |
14 | 10 | using SqlDatabaseVectorSearch.Settings; |
15 | 11 | using SqlDatabaseVectorSearch.TextChunkers; |
|
135 | 131 | app.MapRazorComponents<App>() |
136 | 132 | .AddInteractiveServerRenderMode(); |
137 | 133 |
|
138 | | -app.MapPost("/api/ask", async (Question question, VectorSearchService vectorSearchService, CancellationToken cancellationToken, |
139 | | - [Description("If true, the question will be reformulated taking into account the context of the chat identified by the given ConversationId.")] bool reformulate = true) => |
140 | | -{ |
141 | | - var response = await vectorSearchService.AskQuestionAsync(question, reformulate, cancellationToken); |
142 | | - return TypedResults.Ok(response); |
143 | | -}) |
144 | | -.WithSummary("Asks a question") |
145 | | -.WithDescription("The question will be reformulated taking into account the context of the chat identified by the given ConversationId.") |
146 | | -.WithTags("Ask"); |
147 | | - |
148 | | -app.MapPost("/api/ask-streaming", (Question question, VectorSearchService vectorSearchService, CancellationToken cancellationToken, |
149 | | - [Description("If true, the question will be reformulated taking into account the context of the chat identified by the given ConversationId.")] bool reformulate = true) => |
150 | | -{ |
151 | | - async IAsyncEnumerable<QuestionResponse> Stream() |
152 | | - { |
153 | | - // Requests a streaming response. |
154 | | - var responseStream = vectorSearchService.AskStreamingAsync(question, reformulate, cancellationToken); |
155 | | - |
156 | | - await foreach (var delta in responseStream) |
157 | | - { |
158 | | - yield return delta; |
159 | | - } |
160 | | - } |
161 | | - |
162 | | - return Stream(); |
163 | | -}) |
164 | | -.WithSummary("Asks a question and gets the response as streaming") |
165 | | -.WithDescription("The question will be reformulated taking into account the context of the chat identified by the given ConversationId.") |
166 | | -.WithTags("Ask"); |
167 | | - |
168 | | -var documentsApiGroup = app.MapGroup("/api/documents").WithTags("Documents"); |
169 | | - |
170 | | -documentsApiGroup.MapGet(string.Empty, async (DocumentService documentService, CancellationToken cancellationToken) => |
171 | | -{ |
172 | | - var documents = await documentService.GetAsync(cancellationToken); |
173 | | - return TypedResults.Ok(documents); |
174 | | -}) |
175 | | -.WithSummary("Gets the list of documents"); |
176 | | - |
177 | | -documentsApiGroup.MapPost(string.Empty, async (IFormFile file, VectorSearchService vectorSearchService, CancellationToken cancellationToken, |
178 | | - [Description("The unique identifier of the document. If not provided, a new one will be generated. If you specify an existing documentId, the corresponding document will be overwritten.")] Guid? documentId = null) => |
179 | | -{ |
180 | | - using var stream = file.OpenReadStream(); |
181 | | - |
182 | | - // Note: file.ContentType is not 100% reliable (for example, for markdown file). |
183 | | - var response = await vectorSearchService.ImportAsync(stream, file.FileName, MimeUtility.GetMimeMapping(file.FileName), documentId, cancellationToken); |
184 | | - |
185 | | - return TypedResults.Ok(response); |
186 | | -}) |
187 | | -.DisableAntiforgery() |
188 | | -.ProducesProblem(StatusCodes.Status400BadRequest) |
189 | | -.WithSummary("Uploads a document") |
190 | | -.WithDescription("Uploads a document to SQL Database and saves its embedding using the native VECTOR type. The document will be indexed and used to answer questions. Currently, PDF, DOCX, TXT and MD files are supported."); |
191 | | - |
192 | | -documentsApiGroup.MapGet("{documentId:guid}/chunks", async (Guid documentId, DocumentService documentService, CancellationToken cancellationToken) => |
193 | | -{ |
194 | | - var documents = await documentService.GetChunksAsync(documentId, cancellationToken); |
195 | | - return TypedResults.Ok(documents); |
196 | | -}) |
197 | | -.WithSummary("Gets the list of chunks of a given document") |
198 | | -.WithDescription("The list does not contain embedding. Use '/api/documents/{documentId}/chunks/{documentChunkId}' to get the embedding for a given chunk."); |
199 | | - |
200 | | -documentsApiGroup.MapGet("{documentId:guid}/chunks/{documentChunkId:guid}", async Task<Results<Ok<DocumentChunk>, NotFound>> (Guid documentId, Guid documentChunkId, DocumentService documentService, CancellationToken cancellationToken) => |
201 | | -{ |
202 | | - var chunk = await documentService.GetChunkEmbeddingAsync(documentId, documentChunkId, cancellationToken); |
203 | | - if (chunk is null) |
204 | | - { |
205 | | - return TypedResults.NotFound(); |
206 | | - } |
207 | | - |
208 | | - return TypedResults.Ok(chunk); |
209 | | -}) |
210 | | -.ProducesProblem(StatusCodes.Status404NotFound) |
211 | | -.WithSummary("Gets the details of a given chunk, includings its embedding"); |
212 | | - |
213 | | -documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, DocumentService documentService, CancellationToken cancellationToken) => |
214 | | -{ |
215 | | - await documentService.DeleteAsync(documentId, cancellationToken); |
216 | | - return TypedResults.NoContent(); |
217 | | -}) |
218 | | -.WithSummary("Deletes a document") |
219 | | -.WithDescription("This endpoint deletes the document and all its chunks."); |
| 134 | +app.MapEndpoints(); |
220 | 135 |
|
221 | 136 | app.Run(); |
222 | 137 |
|
|
0 commit comments