From a0a6df9cb3b323e386237132bfc3966201470e32 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Mon, 24 Feb 2025 12:01:22 +0100 Subject: [PATCH] Add loading state and database migration support - Updated `Documents.razor` to show a loading spinner when documents are being fetched, introducing an `isLoading` state variable. - Added `ConfigureDatabaseAsync` method in `Program.cs` for handling database migrations at startup. - Modified `SqlDatabaseVectorSearch.csproj` to include `Microsoft.EntityFrameworkCore.Tools` for migration management. - Enhanced documentation in `docs.md` regarding the `Dimensions` property and `VECTOR` column size requirements. - Created initial migration files to define the database schema for `Documents` and `DocumentChunks` tables. - Defined `Document` and `DocumentChunk` entities in migration files for data storage and retrieval. --- .../Components/Pages/Documents.razor | 28 ++++-- .../00000000000000_Initial.Designer.cs | 93 +++++++++++++++++++ .../Migrations/00000000000000_Initial.cs | 64 +++++++++++++ .../ApplicationDbContextModelSnapshot.cs | 90 ++++++++++++++++++ SqlDatabaseVectorSearch/Program.cs | 13 ++- .../SqlDatabaseVectorSearch.csproj | 4 + SqlDatabaseVectorSearch/wwwroot/docs.md | 10 +- 7 files changed, 285 insertions(+), 17 deletions(-) create mode 100644 SqlDatabaseVectorSearch/DataAccessLayer/Migrations/00000000000000_Initial.Designer.cs create mode 100644 SqlDatabaseVectorSearch/DataAccessLayer/Migrations/00000000000000_Initial.cs create mode 100644 SqlDatabaseVectorSearch/DataAccessLayer/Migrations/ApplicationDbContextModelSnapshot.cs diff --git a/SqlDatabaseVectorSearch/Components/Pages/Documents.razor b/SqlDatabaseVectorSearch/Components/Pages/Documents.razor index 3722858..8bea253 100644 --- a/SqlDatabaseVectorSearch/Components/Pages/Documents.razor +++ b/SqlDatabaseVectorSearch/Components/Pages/Documents.razor @@ -47,7 +47,7 @@ -@if (documents.Count == 0) +@if (isLoading && documents.Count == 0) {
@@ -104,6 +104,7 @@ else private Button uploadButton = default!; private Button deleteButton = default!; + private bool isLoading = true; private IList documents = []; private UploadDocumentRequest uploadDocumentRequest = new(); @@ -133,16 +134,25 @@ else private async Task LoadDocumentsAsync(IServiceProvider services) { - var documentService = services.GetRequiredService(); - var dbDocuments = await documentService.GetAsync(); + isLoading = true; - documents.Clear(); - foreach (var dbDocument in dbDocuments) + try { - documents.Add(new SelectableDocument(dbDocument.Id, dbDocument.Name, dbDocument.CreationDate, dbDocument.ChunkCount) - { - LocalCreationDateString = await GetLocalDateTimeStringAsync(dbDocument.CreationDate) - }); + var documentService = services.GetRequiredService(); + var dbDocuments = await documentService.GetAsync(); + + documents.Clear(); + foreach (var dbDocument in dbDocuments) + { + documents.Add(new SelectableDocument(dbDocument.Id, dbDocument.Name, dbDocument.CreationDate, dbDocument.ChunkCount) + { + LocalCreationDateString = await GetLocalDateTimeStringAsync(dbDocument.CreationDate) + }); + } + } + finally + { + isLoading = false; } } diff --git a/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/00000000000000_Initial.Designer.cs b/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/00000000000000_Initial.Designer.cs new file mode 100644 index 0000000..bb1f760 --- /dev/null +++ b/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/00000000000000_Initial.Designer.cs @@ -0,0 +1,93 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SqlDatabaseVectorSearch.DataAccessLayer; + +#nullable disable + +namespace SqlDatabaseVectorSearch.DataAccessLayer.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20250224102351_Initial")] + partial class Initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreationDate") + .HasColumnType("datetimeoffset"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("nvarchar(255)"); + + b.HasKey("Id"); + + b.ToTable("Documents", (string)null); + }); + + modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.DocumentChunk", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DocumentId") + .HasColumnType("uniqueidentifier"); + + b.PrimitiveCollection("Embedding") + .IsRequired() + .HasColumnType("vector(1536)"); + + b.Property("Index") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentChunks", (string)null); + }); + + modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.DocumentChunk", b => + { + b.HasOne("SqlDatabaseVectorSearch.DataAccessLayer.Entities.Document", "Document") + .WithMany("Chunks") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("FK_DocumentChunks_Documents"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.Document", b => + { + b.Navigation("Chunks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/00000000000000_Initial.cs b/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/00000000000000_Initial.cs new file mode 100644 index 0000000..a44fde0 --- /dev/null +++ b/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/00000000000000_Initial.cs @@ -0,0 +1,64 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SqlDatabaseVectorSearch.DataAccessLayer.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Documents", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + Name = table.Column(type: "nvarchar(255)", maxLength: 255, nullable: false), + CreationDate = table.Column(type: "datetimeoffset", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Documents", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DocumentChunks", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + DocumentId = table.Column(type: "uniqueidentifier", nullable: false), + Index = table.Column(type: "int", nullable: false), + Content = table.Column(type: "nvarchar(max)", nullable: false), + Embedding = table.Column(type: "vector(1536)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DocumentChunks", x => x.Id); + table.ForeignKey( + name: "FK_DocumentChunks_Documents", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_DocumentChunks_DocumentId", + table: "DocumentChunks", + column: "DocumentId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DocumentChunks"); + + migrationBuilder.DropTable( + name: "Documents"); + } + } +} diff --git a/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/ApplicationDbContextModelSnapshot.cs b/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..8be4784 --- /dev/null +++ b/SqlDatabaseVectorSearch/DataAccessLayer/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,90 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SqlDatabaseVectorSearch.DataAccessLayer; + +#nullable disable + +namespace SqlDatabaseVectorSearch.DataAccessLayer.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreationDate") + .HasColumnType("datetimeoffset"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("nvarchar(255)"); + + b.HasKey("Id"); + + b.ToTable("Documents", (string)null); + }); + + modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.DocumentChunk", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DocumentId") + .HasColumnType("uniqueidentifier"); + + b.PrimitiveCollection("Embedding") + .IsRequired() + .HasColumnType("vector(1536)"); + + b.Property("Index") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentChunks", (string)null); + }); + + modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.DocumentChunk", b => + { + b.HasOne("SqlDatabaseVectorSearch.DataAccessLayer.Entities.Document", "Document") + .WithMany("Chunks") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("FK_DocumentChunks_Documents"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.Document", b => + { + b.Navigation("Chunks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SqlDatabaseVectorSearch/Program.cs b/SqlDatabaseVectorSearch/Program.cs index f8d4bb7..f6695ca 100644 --- a/SqlDatabaseVectorSearch/Program.cs +++ b/SqlDatabaseVectorSearch/Program.cs @@ -86,6 +86,7 @@ builder.Services.AddDefaultProblemDetails(); builder.Services.AddDefaultExceptionHandler(); var app = builder.Build(); +await ConfigureDatabaseAsync(app.Services); // Configure the HTTP request pipeline. app.UseHttpsRedirection(); @@ -96,7 +97,7 @@ app.UseWhen(context => context.IsWebRequest(), builder => { builder.UseExceptionHandler("/error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + // The default HSTS value is 30 days. builder.UseHsts(); } @@ -217,4 +218,12 @@ documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, Documen .WithSummary("Deletes a document") .WithDescription("This endpoint deletes the document and all its chunks."); -app.Run(); \ No newline at end of file +app.Run(); + +static async Task ConfigureDatabaseAsync(IServiceProvider serviceProvider) +{ + await using var scope = serviceProvider.CreateAsyncScope(); + var dbContext = scope.ServiceProvider.GetRequiredService(); + + await dbContext.Database.MigrateAsync(); +} \ No newline at end of file diff --git a/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj b/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj index 3a3f57f..4658723 100644 --- a/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj +++ b/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj @@ -14,6 +14,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SqlDatabaseVectorSearch/wwwroot/docs.md b/SqlDatabaseVectorSearch/wwwroot/docs.md index fcf9f37..62df785 100644 --- a/SqlDatabaseVectorSearch/wwwroot/docs.md +++ b/SqlDatabaseVectorSearch/wwwroot/docs.md @@ -2,13 +2,11 @@ ## Setup -- [Create an Azure SQL Database](https://learn.microsoft.com/en-us/azure/azure-sql/database/single-database-create-quickstart) on a server that has the Vector Support feature enabled -- Execute the [Scripts.sql](https://github.com/marcominerva/SqlDatabaseVectorSearch/blob/master/Scripts.sql) file to create the tables needed by the application - - 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. +- [Create an Azure SQL Database](https://learn.microsoft.com/en-us/azure/azure-sql/database/single-database-create-quickstart) - 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 documents with `/api/documents` endpoint. -- Ask questions using `/api/ask` or `/api/ask-streaming` endpoints. + - 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 the corresponding value. 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). +- You may need to update the size of the [`VECTOR`](https://github.com/marcominerva/SqlDatabaseVectorSearch/blob/master/SqlDatabaseVectorSearch/DataAccessLayer/ApplicationDbContext.cs?plain=1#L42C1-L42C47) column to match the size of the embedding model. The default value is 1536. Currently, the maximum allowed value is 1998. If you change it, remember to update also the Migration. +- Run the application and start importing your documents ## Supported features