diff --git a/README.md b/README.md index e116d95..87e2201 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ # SQL Database Vector Search Sample -A repository that showcases the Vector Support in Azure SQL Database to perform embeddings and RAG \ No newline at end of file +A repository that showcases the Vector Support in Azure SQL Database to perform embeddings and RAG with Azure OpenAI. + +> [!IMPORTANT] +> Usage of this application requires the Vector Support feature in Azure SQL Database, currently in EAP. [See this blog post](https://devblogs.microsoft.com/azure-sql/announcing-eap-native-vector-support-in-azure-sql-database/) for more details. + +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. Database access is done using the [EFCore.SqlServer.VectorSearch](https://github.com/efcore/EfCore.SqlServer.VectorSearch) library. + +### Setup + +- Create an Azure SQL Database 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. +- 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. \ No newline at end of file diff --git a/Script.sql b/Script.sql new file mode 100644 index 0000000..2b62ad4 --- /dev/null +++ b/Script.sql @@ -0,0 +1,30 @@ +CREATE TABLE [dbo].[DocumentChunks]( + [Id] [uniqueidentifier] NOT NULL, + [DocumentId] [uniqueidentifier] NOT NULL, + [Content] [nvarchar](max) NOT NULL, + [Embedding] [varbinary](8000) NOT NULL, + CONSTRAINT [PK_DocumentChunks] PRIMARY KEY CLUSTERED +( + [Id] ASC +)) +GO + +CREATE TABLE [dbo].[Documents]( + [Id] [uniqueidentifier] NOT NULL, + [Name] [nvarchar](255) NOT NULL, + [CreationDate] [datetimeoffset](7) NOT NULL, + CONSTRAINT [PK_Documents] PRIMARY KEY CLUSTERED +( + [Id] ASC +)) +GO + +ALTER TABLE [dbo].[DocumentChunks] ADD CONSTRAINT [DF_DocumentChunks_Id] DEFAULT (newid()) FOR [Id] +GO + +ALTER TABLE [dbo].[Documents] ADD CONSTRAINT [DF_Documents_Id] DEFAULT (newid()) FOR [Id] +GO + +ALTER TABLE [dbo].[DocumentChunks] WITH CHECK ADD CONSTRAINT [FK_DocumentChunks_Documents] FOREIGN KEY([DocumentId]) +REFERENCES [dbo].[Documents] ([Id]) +GO diff --git a/SqlDatabaseVectorSearch/Models/Question.cs b/SqlDatabaseVectorSearch/Models/Question.cs index e853813..4fb45db 100644 --- a/SqlDatabaseVectorSearch/Models/Question.cs +++ b/SqlDatabaseVectorSearch/Models/Question.cs @@ -1,3 +1,3 @@ namespace SqlDatabaseVectorSearch.Models; -public record class Question(Guid ConversationId, string Text) : Search(Text); +public record class Question(Guid ConversationId, string Text); diff --git a/SqlDatabaseVectorSearch/Models/Search.cs b/SqlDatabaseVectorSearch/Models/Search.cs deleted file mode 100644 index a3a3077..0000000 --- a/SqlDatabaseVectorSearch/Models/Search.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace SqlDatabaseVectorSearch.Models; - -public record class Search(string Text); - diff --git a/SqlDatabaseVectorSearch/Program.cs b/SqlDatabaseVectorSearch/Program.cs index ab39ecb..dc2a595 100644 --- a/SqlDatabaseVectorSearch/Program.cs +++ b/SqlDatabaseVectorSearch/Program.cs @@ -101,7 +101,7 @@ documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, VectorS .WithOpenApi(operation => { operation.Summary = "Deletes a document"; - operation.Description = "This endpoint deletes the document and all its chunks from SQL Server"; + operation.Description = "This endpoint deletes the document and all its chunks from SQL Server."; return operation; }); diff --git a/SqlDatabaseVectorSearch/appsettings.Development.json b/SqlDatabaseVectorSearch/appsettings.Development.json index 1fc08ee..63fe121 100644 --- a/SqlDatabaseVectorSearch/appsettings.Development.json +++ b/SqlDatabaseVectorSearch/appsettings.Development.json @@ -1,7 +1,4 @@ { - "ConnectionStrings": { - "SqlConnection": "" - }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/SqlServerVectorSearch.png b/SqlServerVectorSearch.png new file mode 100644 index 0000000..2d83803 Binary files /dev/null and b/SqlServerVectorSearch.png differ