From db4646330f35d78d7ad2a8024c42372b3a92494a Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Fri, 14 Jun 2024 12:59:09 +0200 Subject: [PATCH] Enhanced app with Azure AI and vector search - Modified `ApplicationDbContext.cs` to correct the `.IsVector()` method placement for `DocumentChunk`. - Removed `MemoryResponse.cs` class, indicating a move away from this model. - Enhanced `Program.cs` with Azure AI services integration for text embeddings and chat completions. Updated OpenAPI descriptions and reintroduced `/api/ask` with vector search. - Adjusted `ChatService.cs` to improve question-asking functionality using document chunks. - Updated `VectorSearchService.cs` with a new `AskQuestionAsync` method for advanced search and response capabilities. Made `GetContentAsync` static. - Formatted `SqlDatabaseVectorSearch.csproj` and managed NuGet package inclusions. - Simplified `appsettings.json` by removing unused keys. - Added a new `Response` record class for standardized service responses. --- .../DataAccessLayer/ApplicationDbContext.cs | 3 +- .../Models/MemoryResponse.cs | 3 -- SqlDatabaseVectorSearch/Models/Response.cs | 3 ++ SqlDatabaseVectorSearch/Program.cs | 37 +++++++-------- .../Services/ChatService.cs | 36 +++++++++++++-- .../Services/VectorSearchService.cs | 45 ++++++++++--------- .../SqlDatabaseVectorSearch.csproj | 10 ++--- SqlDatabaseVectorSearch/appsettings.json | 5 +-- 8 files changed, 82 insertions(+), 60 deletions(-) delete mode 100644 SqlDatabaseVectorSearch/Models/MemoryResponse.cs create mode 100644 SqlDatabaseVectorSearch/Models/Response.cs diff --git a/SqlDatabaseVectorSearch/DataAccessLayer/ApplicationDbContext.cs b/SqlDatabaseVectorSearch/DataAccessLayer/ApplicationDbContext.cs index 039296d..5e00897 100644 --- a/SqlDatabaseVectorSearch/DataAccessLayer/ApplicationDbContext.cs +++ b/SqlDatabaseVectorSearch/DataAccessLayer/ApplicationDbContext.cs @@ -32,7 +32,8 @@ public class ApplicationDbContext(DbContextOptions options entity.Property(e => e.Content).IsRequired(); entity.Property(e => e.Embedding) .IsRequired() - .HasMaxLength(8000).IsVector(); + .HasMaxLength(8000) + .IsVector(); entity.HasOne(d => d.Document).WithMany(p => p.DocumentChunks) .HasForeignKey(d => d.DocumentId) diff --git a/SqlDatabaseVectorSearch/Models/MemoryResponse.cs b/SqlDatabaseVectorSearch/Models/MemoryResponse.cs deleted file mode 100644 index 6d6010f..0000000 --- a/SqlDatabaseVectorSearch/Models/MemoryResponse.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace SqlDatabaseVectorSearch.Models; - -public record class MemoryResponse(string Question, string Answer); diff --git a/SqlDatabaseVectorSearch/Models/Response.cs b/SqlDatabaseVectorSearch/Models/Response.cs new file mode 100644 index 0000000..427d94e --- /dev/null +++ b/SqlDatabaseVectorSearch/Models/Response.cs @@ -0,0 +1,3 @@ +namespace SqlDatabaseVectorSearch.Models; + +public record class Response(string Question, string Answer); diff --git a/SqlDatabaseVectorSearch/Program.cs b/SqlDatabaseVectorSearch/Program.cs index ad8ca4c..8cb7925 100644 --- a/SqlDatabaseVectorSearch/Program.cs +++ b/SqlDatabaseVectorSearch/Program.cs @@ -26,6 +26,7 @@ builder.Services.AddMemoryCache(); // Semantical Kernel is used to reformulate questions taking into account all the previous interactions, so that embeddings can be generate more accurately. builder.Services.AddKernel() + .AddAzureOpenAITextEmbeddingGeneration(aiSettings.Embedding.Deployment, aiSettings.Embedding.Endpoint, aiSettings.Embedding.ApiKey) .AddAzureOpenAIChatCompletion(aiSettings.ChatCompletion.Deployment, aiSettings.ChatCompletion.Endpoint, aiSettings.ChatCompletion.ApiKey); builder.Services.AddScoped(); @@ -73,7 +74,7 @@ documentsApiGroup.MapPost(string.Empty, async (IFormFile file, VectorSearchServi .WithOpenApi(operation => { operation.Summary = "Uploads a document. Currently, only PDF files are supported"; - operation.Description = "Uploads a document to SQL Server. The document will be indexed and used to answer questions. The documentId is optional, if not provided a new one will be generated. If you specify an existing documentId, the document will be overridden."; + operation.Description = "Uploads a document to SQL Server and saves its embeddings using Vector Support. The document will be indexed and used to answer questions."; operation.Parameter("documentId").Description = "The unique identifier of the document. If not provided, a new one will be generated. If you specify an existing documentId, the document will be overridden."; @@ -88,7 +89,8 @@ documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, VectorS }) .WithOpenApi(operation => { - operation.Summary = "Delete a document from SQL Server"; + operation.Summary = "Deletes a document"; + operation.Description = "This endpoint deletes the documents and all its chunks from SQL Server"; return operation; }); @@ -109,26 +111,19 @@ documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, VectorS // return operation; //}); -//app.MapPost("/api/ask", async Task, NotFound>> (Question question, ApplicationMemoryService memory, bool reformulate = true, double minimumRelevance = 0, string? index = null) => -//{ -// var response = await memory.AskQuestionAsync(question, reformulate, minimumRelevance, index); -// if (response is null) -// { -// return TypedResults.NotFound(); -// } +app.MapPost("/api/ask", async (Question question, VectorSearchService vectorSearchService, bool reformulate = true) => +{ + var response = await vectorSearchService.AskQuestionAsync(question, reformulate); + return TypedResults.Ok(response); +}) +.WithOpenApi(operation => +{ + operation.Summary = "Asks a question"; + operation.Description = "The question will be reformulated taking into account the context of the chat identified by the given ConversationId."; -// return TypedResults.Ok(response); -//}) -//.WithOpenApi(operation => -//{ -// operation.Summary = "Ask a question to the Kernel Memory Service"; -// operation.Description = "Ask a question to the Kernel Memory Service using the provided question and optional tags. The question will be reformulated taking into account the context of the chat identified by the given ConversationId. If tags are provided, they will be used as filters with OR logic."; + operation.Parameter("reformulate").Description = "If true, the question will be reformulated taking into account the context of the chat identified by the given ConversationId."; -// operation.Parameter("reformulate").Description = "If true, the question will be reformulated taking into account the context of the chat identified by the given ConversationId."; -// operation.Parameter("minimumRelevance").Description = "The minimum Cosine Similarity required."; -// operation.Parameter("index").Description = "The index in which to search for documents. If not provided, the default index will be used ('default')."; - -// return operation; -//}); + return operation; +}); app.Run(); \ No newline at end of file diff --git a/SqlDatabaseVectorSearch/Services/ChatService.cs b/SqlDatabaseVectorSearch/Services/ChatService.cs index 21fe560..faeeba9 100644 --- a/SqlDatabaseVectorSearch/Services/ChatService.cs +++ b/SqlDatabaseVectorSearch/Services/ChatService.cs @@ -1,6 +1,8 @@ -using Microsoft.Extensions.Caching.Memory; +using System.Text; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; using Microsoft.SemanticKernel.ChatCompletion; +using SqlDatabaseVectorSearch.DataAccessLayer.Entities; using SqlDatabaseVectorSearch.Settings; namespace SqlDatabaseVectorSearch.Services; @@ -29,14 +31,40 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti return reformulatedQuestion.Content!; } - public async Task AddInteractionAsync(Guid conversationId, string question, string answer) + public async Task AskQuestionAsync(Guid conversationId, IEnumerable chunks, string question) { var chat = new ChatHistory(cache.Get(conversationId) ?? []); - chat.AddUserMessage(question); - chat.AddAssistantMessage(answer); + var prompt = new StringBuilder(""" + You can use only the information provided in this chat to answer questions. + If you don't know the answer, reply suggesting to refine the question. + Never answer to questions that are not related to this chat. + You must answer in the same language of the user's question. + Using the following information: + --- + + """); + + foreach (var result in chunks.Select(c => c.Content)) + { + prompt.AppendLine(result); + prompt.AppendLine("---"); + } + + prompt.AppendLine($""" + Answer the following question: + --- + {question} + """); + + chat.AddUserMessage(prompt.ToString()); + + var answer = await chatCompletionService.GetChatMessageContentAsync(chat)!; + chat.AddAssistantMessage(answer.Content!); await UpdateCacheAsync(conversationId, chat); + + return answer.Content!; } private Task UpdateCacheAsync(Guid conversationId, ChatHistory chat) diff --git a/SqlDatabaseVectorSearch/Services/VectorSearchService.cs b/SqlDatabaseVectorSearch/Services/VectorSearchService.cs index 4394bfc..e7ddc4d 100644 --- a/SqlDatabaseVectorSearch/Services/VectorSearchService.cs +++ b/SqlDatabaseVectorSearch/Services/VectorSearchService.cs @@ -4,6 +4,7 @@ using Microsoft.SemanticKernel.Embeddings; using Microsoft.SemanticKernel.Text; using SqlDatabaseVectorSearch.DataAccessLayer; using SqlDatabaseVectorSearch.DataAccessLayer.Entities; +using SqlDatabaseVectorSearch.Models; using UglyToad.PdfPig; using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor; @@ -30,7 +31,7 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG var document = new Document { Id = documentId.Value, Name = name, CreationDate = DateTimeOffset.UtcNow }; dbContext.Documents.Add(document); - // Splits the content into chunks of at most 1024 tokens and generate the embeddings for each one. + // Split the content into chunks of at most 1024 tokens and generate the embeddings for each one. var paragraphs = TextChunker.SplitPlainTextParagraphs(TextChunker.SplitPlainTextLines(content, 300), 1024, 100); var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingsAsync(paragraphs); @@ -58,29 +59,29 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG await dbContext.SaveChangesAsync(); } - //public async Task AskQuestionAsync(Question question, bool reformulate = true, double minimumRelevance = 0, string? index = null) - //{ - // // Reformulate the following question taking into account the context of the chat to perform keyword search and embeddings: - // var reformulatedQuestion = reformulate ? await chatService.CreateQuestionAsync(question.ConversationId, question.Text) : question.Text; + public async Task AskQuestionAsync(Question question, bool reformulate = true) + { + // Reformulate the following question taking into account the context of the chat to perform keyword search and embeddings: + var reformulatedQuestion = reformulate ? await chatService.CreateQuestionAsync(question.ConversationId, question.Text) : question.Text; - // // Ask using the embedding search via Kernel Memory and the reformulated question. - // // If tags are provided, use them as filters with OR logic. - // var answer = await memory.AskAsync(reformulatedQuestion.TrimEnd([' ', '?']), index, filters: question.Tags.ToMemoryFilters(), minRelevance: minimumRelevance); + // Perform Vector Search on SQL Server. + var questionEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(reformulatedQuestion); - // // If you want to use an AND logic, set the "filter" parameter (instead of "filters"). - // //var answer = await memory.AskAsync(reformulatedQuestion.TrimEnd([' ', '?'], index, filter: question.Tags.ToMemoryFilter(), minRelevance: minimumRelevance); + var chunks = await dbContext.DocumentChunks + .OrderBy(c => EF.Functions.VectorDistance("cosine", c.Embedding, questionEmbedding.ToArray())) + //.Select(c => new + //{ + // c.Id, + // c.DocumentId, + // c.Content, + // Distance = EF.Functions.VectorDistance("cosine", c.Embedding, questionEmbedding.ToArray()) + //}) + .Take(5) + .ToListAsync(); - // if (answer.NoResult == false) - // { - // // If the answer has been found, add the interaction to the chat, so that it will be used for the next reformulation. - // await chatService.AddInteractionAsync(question.ConversationId, reformulatedQuestion, answer.Result); - - // var response = new MemoryResponse(answer.Question, answer.Result, answer.RelevantSources); - // return response; - // } - - // return null; - //} + var answer = await chatService.AskQuestionAsync(question.ConversationId, chunks, reformulatedQuestion); + return new Response(reformulatedQuestion, answer); + } //public async Task SearchAsync(Search search, double minimumRelevance = 0, string? index = null) //{ @@ -94,7 +95,7 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG // return searchResult; //} - private Task GetContentAsync(Stream stream) + private static Task GetContentAsync(Stream stream) { var content = new StringBuilder(); diff --git a/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj b/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj index c63c9cb..3cdb7ee 100644 --- a/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj +++ b/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj @@ -4,15 +4,15 @@ net8.0 enable enable - $(NoWarn);SKEXP0001;SKEXP0050; + $(NoWarn);SKEXP0001;SKEXP0010;SKEXP0050; - - + + - - + + diff --git a/SqlDatabaseVectorSearch/appsettings.json b/SqlDatabaseVectorSearch/appsettings.json index 7ed4651..d9c99d7 100644 --- a/SqlDatabaseVectorSearch/appsettings.json +++ b/SqlDatabaseVectorSearch/appsettings.json @@ -18,10 +18,7 @@ }, "AppSettings": { "MessageLimit": 20, - "MessageExpiration": "00:05:00", - "StoragePath": "", - "VectorDbPath": "", - "QueuePath": "" + "MessageExpiration": "00:05:00" }, "Logging": { "LogLevel": {