diff --git a/README.md b/README.md index 044cf77..ee762b6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # SQL Database Vector Search Sample A repository that showcases the native VECTOR type in Azure SQL Database to perform embeddings and RAG with Azure OpenAI. -The application is a Minimal API that exposes endpoints to load documents, generate embeddings and save them into the database as Vectors, and perform searches using Vector Search and RAG. Currently, only PDF files are supported. Vectors are saved and retrieved with Entity Framework Core using the [EFCore.SqlServer.VectorSearch](https://github.com/efcore/EfCore.SqlServer.VectorSearch) library. Embedding and Chat Completion are integrated with [Semantic Kernel](https://github.com/microsoft/semantic-kernel). +The application is a Minimal API that exposes endpoints to load documents, generate embeddings and save them into the database as Vectors, and perform searches using Vector Search and RAG. Currently, PDF, DOCX and TXT files are supported. Vectors are saved and retrieved with Entity Framework Core using the [EFCore.SqlServer.VectorSearch](https://github.com/efcore/EfCore.SqlServer.VectorSearch) library. Embedding and Chat Completion are integrated with [Semantic Kernel](https://github.com/microsoft/semantic-kernel). > [!NOTE] > If you prefer to use straight SQL, check out the [sql branch](https://github.com/marcominerva/SqlDatabaseVectorSearch/tree/sql). @@ -15,4 +15,4 @@ The application is a Minimal API that exposes endpoints to load documents, gener - You may need to update the size of the [`VECTOR`](https://github.com/marcominerva/SqlDatabaseVectorSearch/blob/master/Scripts.sql#L17) column to match the size of the embedding model. Currently, the maximum allowed value is 1998. - Open the [appsettings.json](https://github.com/marcominerva/SqlDatabaseVectorSearch/blob/master/SqlDatabaseVectorSearch/appsettings.json) file and set the connection string to the database and the other settings required by Azure OpenAI - If your embedding model supports shortening, like **text-embedding-3-small** and **text-embedding-3-large**, and you want to use this feature, you need to set the [`Dimensions`](https://github.com/marcominerva/SqlDatabaseVectorSearch/blob/master/SqlDatabaseVectorSearch/appsettings.json#L17) property to match the value you have used in the SQL script. If your model doesn't provide this feature, or do you want to use the default size, just leave the [`Dimensions`](https://github.com/marcominerva/SqlDatabaseVectorSearch/blob/master/SqlDatabaseVectorSearch/appsettings.json#L17) property to NULL. Keep in mind that **text-embedding-3-small** has a dimension of 1536, while **text-embedding-3-large** uses vectors with 3072 elements, so with this latter model it is mandatory to specify a value (that, as said, must be less or equal to 1998). -- Run the application and start importing your PDF documents. +- Run the application and start importing your documents. diff --git a/SqlDatabaseVectorSearch/ContentDecoders/DocxContentDecoder.cs b/SqlDatabaseVectorSearch/ContentDecoders/DocxContentDecoder.cs index 35dfd51..36b6d06 100644 --- a/SqlDatabaseVectorSearch/ContentDecoders/DocxContentDecoder.cs +++ b/SqlDatabaseVectorSearch/ContentDecoders/DocxContentDecoder.cs @@ -21,41 +21,5 @@ public class DocxContentDecoder : IContentDecoder } return Task.FromResult(content.ToString()); - - //foreach (var paragraph in body!.Elements()) - //{ - // foreach (var element in paragraph.Elements()) - // { - // if (element is Run run) - // { - // DecodeTextFromRun(run); - // } - // else if (element is Hyperlink hyperlink) - // { - // foreach (var hyperlinkRun in hyperlink.Elements()) - // { - // DecodeTextFromRun(hyperlinkRun); - // } - - // //var hyperlinkUri = doc.MainDocumentPart.HyperlinkRelationships.FirstOrDefault(r => r.Id == hyperlink.Id)?.Uri; - // //if (hyperlinkUri is not null) - // //{ - // // content.Append($" ({hyperlinkUri})"); - // //} - // } - // } - - // content.AppendLine(); // Preserve whitespace and blank lines. - //} - - //return Task.FromResult(content.ToString()); - - //void DecodeTextFromRun(Run run) - //{ - // foreach (var text in run.Elements()) - // { - // content.Append(text.Text); - // } - //} } } diff --git a/SqlDatabaseVectorSearch/ContentDecoders/TextContentDecoder.cs b/SqlDatabaseVectorSearch/ContentDecoders/TextContentDecoder.cs new file mode 100644 index 0000000..d9d6406 --- /dev/null +++ b/SqlDatabaseVectorSearch/ContentDecoders/TextContentDecoder.cs @@ -0,0 +1,12 @@ +namespace SqlDatabaseVectorSearch.ContentDecoders; + +public class TextContentDecoder : IContentDecoder +{ + public async Task DecodeAsync(Stream stream, string contentType) + { + using var readStream = new StreamReader(stream); + var content = await readStream.ReadToEndAsync(); + + return content; + } +} diff --git a/SqlDatabaseVectorSearch/Program.cs b/SqlDatabaseVectorSearch/Program.cs index 71301b6..4176fca 100644 --- a/SqlDatabaseVectorSearch/Program.cs +++ b/SqlDatabaseVectorSearch/Program.cs @@ -54,6 +54,7 @@ builder.Services.AddScoped(); builder.Services.AddKeyedSingleton(MediaTypeNames.Application.Pdf); builder.Services.AddKeyedSingleton("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); +builder.Services.AddKeyedSingleton(MediaTypeNames.Text.Plain); builder.Services.ConfigureHttpJsonOptions(options => { @@ -74,7 +75,15 @@ var app = builder.Build(); // Configure the HTTP request pipeline. app.UseHttpsRedirection(); -app.UseExceptionHandler(); +app.UseExceptionHandler(new ExceptionHandlerOptions +{ + StatusCodeSelector = exception => exception switch + { + NotSupportedException => StatusCodes.Status501NotImplemented, + _ => StatusCodes.Status500InternalServerError + } +}); + app.UseStatusCodePages(); app.MapOpenApi(); @@ -125,7 +134,7 @@ documentsApiGroup.MapPost(string.Empty, async (IFormFile file, VectorSearchServi .DisableAntiforgery() .ProducesProblem(StatusCodes.Status400BadRequest) .WithSummary("Uploads a document") -.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, only PDF files are supported."); +.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 and TXT files are supported."); documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, VectorSearchService vectorSearchService) => { diff --git a/SqlDatabaseVectorSearch/Services/VectorSearchService.cs b/SqlDatabaseVectorSearch/Services/VectorSearchService.cs index 87b7151..3e0eca1 100644 --- a/SqlDatabaseVectorSearch/Services/VectorSearchService.cs +++ b/SqlDatabaseVectorSearch/Services/VectorSearchService.cs @@ -18,7 +18,7 @@ public class VectorSearchService(IServiceProvider serviceProvider, ApplicationDb public async Task ImportAsync(Stream stream, string name, string contentType, Guid? documentId) { // Extract the contents of the file. - var decoder = serviceProvider.GetRequiredKeyedService(contentType); + var decoder = serviceProvider.GetKeyedService(contentType) ?? throw new NotSupportedException($"Content type '{contentType}' is not supported."); var content = await decoder.DecodeAsync(stream, contentType); await dbContext.Database.BeginTransactionAsync();