mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
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.
This commit is contained in:
@@ -32,7 +32,8 @@ public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options
|
|||||||
entity.Property(e => e.Content).IsRequired();
|
entity.Property(e => e.Content).IsRequired();
|
||||||
entity.Property(e => e.Embedding)
|
entity.Property(e => e.Embedding)
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasMaxLength(8000).IsVector();
|
.HasMaxLength(8000)
|
||||||
|
.IsVector();
|
||||||
|
|
||||||
entity.HasOne(d => d.Document).WithMany(p => p.DocumentChunks)
|
entity.HasOne(d => d.Document).WithMany(p => p.DocumentChunks)
|
||||||
.HasForeignKey(d => d.DocumentId)
|
.HasForeignKey(d => d.DocumentId)
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
namespace SqlDatabaseVectorSearch.Models;
|
|
||||||
|
|
||||||
public record class MemoryResponse(string Question, string Answer);
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace SqlDatabaseVectorSearch.Models;
|
||||||
|
|
||||||
|
public record class Response(string Question, string Answer);
|
||||||
@@ -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.
|
// 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()
|
builder.Services.AddKernel()
|
||||||
|
.AddAzureOpenAITextEmbeddingGeneration(aiSettings.Embedding.Deployment, aiSettings.Embedding.Endpoint, aiSettings.Embedding.ApiKey)
|
||||||
.AddAzureOpenAIChatCompletion(aiSettings.ChatCompletion.Deployment, aiSettings.ChatCompletion.Endpoint, aiSettings.ChatCompletion.ApiKey);
|
.AddAzureOpenAIChatCompletion(aiSettings.ChatCompletion.Deployment, aiSettings.ChatCompletion.Endpoint, aiSettings.ChatCompletion.ApiKey);
|
||||||
|
|
||||||
builder.Services.AddScoped<ChatService>();
|
builder.Services.AddScoped<ChatService>();
|
||||||
@@ -73,7 +74,7 @@ documentsApiGroup.MapPost(string.Empty, async (IFormFile file, VectorSearchServi
|
|||||||
.WithOpenApi(operation =>
|
.WithOpenApi(operation =>
|
||||||
{
|
{
|
||||||
operation.Summary = "Uploads a document. Currently, only PDF files are supported";
|
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.";
|
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 =>
|
.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;
|
return operation;
|
||||||
});
|
});
|
||||||
@@ -109,26 +111,19 @@ documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, VectorS
|
|||||||
// return operation;
|
// return operation;
|
||||||
//});
|
//});
|
||||||
|
|
||||||
//app.MapPost("/api/ask", async Task<Results<Ok<MemoryResponse>, NotFound>> (Question question, ApplicationMemoryService memory, bool reformulate = true, double minimumRelevance = 0, string? index = null) =>
|
app.MapPost("/api/ask", async (Question question, VectorSearchService vectorSearchService, bool reformulate = true) =>
|
||||||
//{
|
{
|
||||||
// var response = await memory.AskQuestionAsync(question, reformulate, minimumRelevance, index);
|
var response = await vectorSearchService.AskQuestionAsync(question, reformulate);
|
||||||
// if (response is null)
|
return TypedResults.Ok(response);
|
||||||
// {
|
})
|
||||||
// return TypedResults.NotFound();
|
.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);
|
operation.Parameter("reformulate").Description = "If true, the question will be reformulated taking into account the context of the chat identified by the given ConversationId.";
|
||||||
//})
|
|
||||||
//.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.";
|
return operation;
|
||||||
// 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;
|
|
||||||
//});
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using Microsoft.Extensions.Caching.Memory;
|
using System.Text;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.SemanticKernel.ChatCompletion;
|
using Microsoft.SemanticKernel.ChatCompletion;
|
||||||
|
using SqlDatabaseVectorSearch.DataAccessLayer.Entities;
|
||||||
using SqlDatabaseVectorSearch.Settings;
|
using SqlDatabaseVectorSearch.Settings;
|
||||||
|
|
||||||
namespace SqlDatabaseVectorSearch.Services;
|
namespace SqlDatabaseVectorSearch.Services;
|
||||||
@@ -29,14 +31,40 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti
|
|||||||
return reformulatedQuestion.Content!;
|
return reformulatedQuestion.Content!;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AddInteractionAsync(Guid conversationId, string question, string answer)
|
public async Task<string> AskQuestionAsync(Guid conversationId, IEnumerable<DocumentChunk> chunks, string question)
|
||||||
{
|
{
|
||||||
var chat = new ChatHistory(cache.Get<ChatHistory?>(conversationId) ?? []);
|
var chat = new ChatHistory(cache.Get<ChatHistory?>(conversationId) ?? []);
|
||||||
|
|
||||||
chat.AddUserMessage(question);
|
var prompt = new StringBuilder("""
|
||||||
chat.AddAssistantMessage(answer);
|
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);
|
await UpdateCacheAsync(conversationId, chat);
|
||||||
|
|
||||||
|
return answer.Content!;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task UpdateCacheAsync(Guid conversationId, ChatHistory chat)
|
private Task UpdateCacheAsync(Guid conversationId, ChatHistory chat)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using Microsoft.SemanticKernel.Embeddings;
|
|||||||
using Microsoft.SemanticKernel.Text;
|
using Microsoft.SemanticKernel.Text;
|
||||||
using SqlDatabaseVectorSearch.DataAccessLayer;
|
using SqlDatabaseVectorSearch.DataAccessLayer;
|
||||||
using SqlDatabaseVectorSearch.DataAccessLayer.Entities;
|
using SqlDatabaseVectorSearch.DataAccessLayer.Entities;
|
||||||
|
using SqlDatabaseVectorSearch.Models;
|
||||||
using UglyToad.PdfPig;
|
using UglyToad.PdfPig;
|
||||||
using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor;
|
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 };
|
var document = new Document { Id = documentId.Value, Name = name, CreationDate = DateTimeOffset.UtcNow };
|
||||||
dbContext.Documents.Add(document);
|
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 paragraphs = TextChunker.SplitPlainTextParagraphs(TextChunker.SplitPlainTextLines(content, 300), 1024, 100);
|
||||||
var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingsAsync(paragraphs);
|
var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingsAsync(paragraphs);
|
||||||
|
|
||||||
@@ -58,29 +59,29 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
//public async Task<MemoryResponse?> AskQuestionAsync(Question question, bool reformulate = true, double minimumRelevance = 0, string? index = null)
|
public async Task<Response?> AskQuestionAsync(Question question, bool reformulate = true)
|
||||||
//{
|
{
|
||||||
// // Reformulate the following question taking into account the context of the chat to perform keyword search and embeddings:
|
// 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;
|
var reformulatedQuestion = reformulate ? await chatService.CreateQuestionAsync(question.ConversationId, question.Text) : question.Text;
|
||||||
|
|
||||||
// // Ask using the embedding search via Kernel Memory and the reformulated question.
|
// Perform Vector Search on SQL Server.
|
||||||
// // If tags are provided, use them as filters with OR logic.
|
var questionEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(reformulatedQuestion);
|
||||||
// var answer = await memory.AskAsync(reformulatedQuestion.TrimEnd([' ', '?']), index, filters: question.Tags.ToMemoryFilters(), minRelevance: minimumRelevance);
|
|
||||||
|
|
||||||
// // If you want to use an AND logic, set the "filter" parameter (instead of "filters").
|
var chunks = await dbContext.DocumentChunks
|
||||||
// //var answer = await memory.AskAsync(reformulatedQuestion.TrimEnd([' ', '?'], index, filter: question.Tags.ToMemoryFilter(), minRelevance: minimumRelevance);
|
.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)
|
var answer = await chatService.AskQuestionAsync(question.ConversationId, chunks, reformulatedQuestion);
|
||||||
// {
|
return new Response(reformulatedQuestion, answer);
|
||||||
// // 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;
|
|
||||||
//}
|
|
||||||
|
|
||||||
//public async Task<SearchResult?> SearchAsync(Search search, double minimumRelevance = 0, string? index = null)
|
//public async Task<SearchResult?> SearchAsync(Search search, double minimumRelevance = 0, string? index = null)
|
||||||
//{
|
//{
|
||||||
@@ -94,7 +95,7 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
// return searchResult;
|
// return searchResult;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
private Task<string> GetContentAsync(Stream stream)
|
private static Task<string> GetContentAsync(Stream stream)
|
||||||
{
|
{
|
||||||
var content = new StringBuilder();
|
var content = new StringBuilder();
|
||||||
|
|
||||||
|
|||||||
@@ -4,15 +4,15 @@
|
|||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<NoWarn>$(NoWarn);SKEXP0001;SKEXP0050;</NoWarn>
|
<NoWarn>$(NoWarn);SKEXP0001;SKEXP0010;SKEXP0050;</NoWarn>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="EFCore.SqlServer.VectorSearch" Version="0.1.1" />
|
<PackageReference Include="EFCore.SqlServer.VectorSearch" Version="0.1.1" />
|
||||||
<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.2" />
|
<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
|
||||||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.14.1" />
|
<PackageReference Include="Microsoft.SemanticKernel" Version="1.14.1" />
|
||||||
<PackageReference Include="MinimalHelpers.OpenApi" Version="2.0.8" />
|
<PackageReference Include="MinimalHelpers.OpenApi" Version="2.0.8" />
|
||||||
<PackageReference Include="PdfPig" Version="0.1.8" />
|
<PackageReference Include="PdfPig" Version="0.1.8" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||||
|
|||||||
@@ -18,10 +18,7 @@
|
|||||||
},
|
},
|
||||||
"AppSettings": {
|
"AppSettings": {
|
||||||
"MessageLimit": 20,
|
"MessageLimit": 20,
|
||||||
"MessageExpiration": "00:05:00",
|
"MessageExpiration": "00:05:00"
|
||||||
"StoragePath": "",
|
|
||||||
"VectorDbPath": "",
|
|
||||||
"QueuePath": ""
|
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|||||||
Reference in New Issue
Block a user