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.
This commit is contained in:
Marco Minerva
2025-02-24 12:01:22 +01:00
parent 08ebc517c8
commit a0a6df9cb3
7 changed files with 285 additions and 17 deletions
@@ -47,7 +47,7 @@
</div>
</EditForm>
@if (documents.Count == 0)
@if (isLoading && documents.Count == 0)
{
<div class="text-center">
<Spinner Type="SpinnerType.Dots" Class="me-3 mt-4" Color="SpinnerColor.Primary" />
@@ -104,6 +104,7 @@ else
private Button uploadButton = default!;
private Button deleteButton = default!;
private bool isLoading = true;
private IList<SelectableDocument> documents = [];
private UploadDocumentRequest uploadDocumentRequest = new();
@@ -133,16 +134,25 @@ else
private async Task LoadDocumentsAsync(IServiceProvider services)
{
var documentService = services.GetRequiredService<DocumentService>();
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<DocumentService>();
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;
}
}
@@ -0,0 +1,93 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTimeOffset>("CreationDate")
.HasColumnType("datetimeoffset");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.HasKey("Id");
b.ToTable("Documents", (string)null);
});
modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.DocumentChunk", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Content")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<Guid>("DocumentId")
.HasColumnType("uniqueidentifier");
b.PrimitiveCollection<string>("Embedding")
.IsRequired()
.HasColumnType("vector(1536)");
b.Property<int>("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
}
}
}
@@ -0,0 +1,64 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SqlDatabaseVectorSearch.DataAccessLayer.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Documents",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Name = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
CreationDate = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Documents", x => x.Id);
});
migrationBuilder.CreateTable(
name: "DocumentChunks",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
DocumentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Index = table.Column<int>(type: "int", nullable: false),
Content = table.Column<string>(type: "nvarchar(max)", nullable: false),
Embedding = table.Column<string>(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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "DocumentChunks");
migrationBuilder.DropTable(
name: "Documents");
}
}
}
@@ -0,0 +1,90 @@
// <auto-generated />
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<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTimeOffset>("CreationDate")
.HasColumnType("datetimeoffset");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.HasKey("Id");
b.ToTable("Documents", (string)null);
});
modelBuilder.Entity("SqlDatabaseVectorSearch.DataAccessLayer.Entities.DocumentChunk", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Content")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<Guid>("DocumentId")
.HasColumnType("uniqueidentifier");
b.PrimitiveCollection<string>("Embedding")
.IsRequired()
.HasColumnType("vector(1536)");
b.Property<int>("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
}
}
}
+11 -2
View File
@@ -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();
app.Run();
static async Task ConfigureDatabaseAsync(IServiceProvider serviceProvider)
{
await using var scope = serviceProvider.CreateAsyncScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync();
}
@@ -14,6 +14,10 @@
<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" Version="9.2.0-preview.1.25105.6" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.2.0" />
<PackageReference Include="Microsoft.ML.Tokenizers" Version="1.0.1" />
+4 -6
View File
@@ -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