From 8c6cc3c969e4f8e0603ec13a83c38110580af25d Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Tue, 1 Oct 2024 17:35:59 +0200 Subject: [PATCH] Improve README, add comments, and clean up VectorSearchService Updated README.md for clarity and additional setup instructions: - Refined repository description to highlight native Vector type. - Rephrased note on Vector Support feature for readability. - Removed mention of EFCore.SqlServer.VectorSearch library. - Added instructions for updating VECTOR column size and setting Dimension property. Added comment in Scripts.sql to guide vector size setting in Embedding column. Cleaned up VectorSearchService.cs by removing unused and commented-out SQL command execution code. --- README.md | 16 +++++++------- Scripts.sql | 1 + .../Services/VectorSearchService.cs | 21 ------------------- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 3f8a0a3..1a522e1 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,18 @@ # SQL Database Vector Search Sample -A repository that showcases the Native Vector Support in Azure SQL Database to perform embeddings and RAG with Azure OpenAI. +A repository that showcases the native Vector type 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. +> 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. Currently, only PDF files are supported. Vectors are saved 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, only PDF files are supported. Embedding and Chat Completion are integrated with [Semantic Kernel](https://github.com/microsoft/semantic-kernel). ![SQL Database Vector Search](https://github.com/marcominerva/SqlDatabaseVectorSearch/blob/master/SqlDatabaseVectorSearch.png) ### 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. -- 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. -- Run the application and start importing your PDF documents. +- [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. +- 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 [`Dimension`](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 [`Dimension`](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. \ No newline at end of file diff --git a/Scripts.sql b/Scripts.sql index f0332cf..34bdcb5 100644 --- a/Scripts.sql +++ b/Scripts.sql @@ -13,6 +13,7 @@ CREATE TABLE [dbo].[DocumentChunks]( [DocumentId] [uniqueidentifier] NOT NULL, [Index] [int] NOT NULL, [Content] [nvarchar](max) NOT NULL, + -- Set the size of the vector to the same size of the your embedding model. [Embedding] [vector](1536) NOT NULL, CONSTRAINT [PK_DocumentChunks] PRIMARY KEY CLUSTERED ( diff --git a/SqlDatabaseVectorSearch/Services/VectorSearchService.cs b/SqlDatabaseVectorSearch/Services/VectorSearchService.cs index f1b57e0..bea3ece 100644 --- a/SqlDatabaseVectorSearch/Services/VectorSearchService.cs +++ b/SqlDatabaseVectorSearch/Services/VectorSearchService.cs @@ -139,27 +139,6 @@ public class VectorSearchService(SqlConnection sqlConnection, ITextEmbeddingGene ORDER BY VECTOR_DISTANCE('cosine', Embedding, CAST(@QuestionEmbedding AS VECTOR({questionEmbedding.Length}))); """, new { appSettings.MaxRelevantChunks, QuestionEmbedding = JsonSerializer.Serialize(questionEmbedding) }); - //await sqlConnection.OpenAsync(); - //await using var command = sqlConnection.CreateCommand(); - - //command.CommandText = $""" - // SELECT TOP (@MaxRelevantChunks) Content - // FROM DocumentChunks - // ORDER BY VECTOR_DISTANCE('cosine', Embedding, CAST(@QuestionEmbedding AS VECTOR({questionEmbedding.Length}))); - // """; - - //command.Parameters.AddWithValue("@MaxRelevantChunks", appSettings.MaxRelevantChunks); - //command.Parameters.AddWithValue("@QuestionEmbedding", JsonSerializer.Serialize(questionEmbedding)); - - //var chunks = new List(); - - //await using var reader = await command.ExecuteReaderAsync(); - //while (await reader.ReadAsync()) - //{ - // var content = reader.GetString(0); - // chunks.Add(content); - //} - var answer = await chatService.AskQuestionAsync(question.ConversationId, chunks, reformulatedQuestion); return new Response(reformulatedQuestion, answer); }