mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fad66a2fbf | |||
| 33c8fcb9dc | |||
| 09cd5cb9c7 | |||
| 2b669c191e | |||
| 2c5c164098 | |||
| aadab97133 | |||
| 3373fa42fe | |||
| 6c423fb306 | |||
| 29b8ebe283 | |||
| 091f76e0c6 | |||
| c8c989b42c | |||
| 017dda0785 | |||
| 6c5292d6c7 | |||
| 1fc6d3c945 |
@@ -131,6 +131,7 @@ csharp_prefer_braces = true:silent
|
||||
csharp_prefer_simple_using_statement = true:suggestion
|
||||
csharp_style_namespace_declarations = file_scoped:suggestion
|
||||
csharp_style_prefer_method_group_conversion = true:silent
|
||||
csharp_prefer_system_threading_lock = true:suggestion
|
||||
|
||||
# Expression-level preferences
|
||||
csharp_prefer_simple_default_expression = true:suggestion
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
# SQL Database Vector Search Sample
|
||||
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.
|
||||
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 and retrieved using direct SQL queries with [Dapper](https://github.com/DapperLib/Dapper). 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).
|
||||
> [!NOTE]
|
||||
> If you prefer to use Entity Framework Core, check out the [master branch](https://github.com/marcominerva/SqlDatabaseVectorSearch/tree/master).
|
||||
|
||||

|
||||

|
||||
|
||||
### 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.
|
||||
- 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).
|
||||
- Execute the [Scripts.sql](https://github.com/marcominerva/SqlDatabaseVectorSearch/blob/sql/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/sql/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/sql/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/sql/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/sql/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.
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
using System.ComponentModel;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Microsoft.SemanticKernel;
|
||||
using MinimalHelpers.OpenApi;
|
||||
using SqlDatabaseVectorSearch.Models;
|
||||
using SqlDatabaseVectorSearch.Services;
|
||||
using SqlDatabaseVectorSearch.Settings;
|
||||
using TinyHelpers.AspNetCore.Extensions;
|
||||
using TinyHelpers.AspNetCore.Swagger;
|
||||
using TinyHelpers.AspNetCore.OpenApi;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Configuration.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true);
|
||||
@@ -24,7 +23,13 @@ builder.Services.AddScoped(_ =>
|
||||
return sqlConnection;
|
||||
});
|
||||
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddHybridCache(options =>
|
||||
{
|
||||
options.DefaultEntryOptions = new()
|
||||
{
|
||||
LocalCacheExpiration = appSettings.MessageExpiration
|
||||
};
|
||||
});
|
||||
|
||||
// Semantic Kernel is used to generate embeddings and to reformulate questions taking into account all the previous interactions,
|
||||
// so that embeddings themselves can be generated more accurately.
|
||||
@@ -32,14 +37,11 @@ builder.Services.AddKernel()
|
||||
.AddAzureOpenAITextEmbeddingGeneration(aiSettings.Embedding.Deployment, aiSettings.Embedding.Endpoint, aiSettings.Embedding.ApiKey, dimensions: aiSettings.Embedding.Dimensions)
|
||||
.AddAzureOpenAIChatCompletion(aiSettings.ChatCompletion.Deployment, aiSettings.ChatCompletion.Endpoint, aiSettings.ChatCompletion.ApiKey);
|
||||
|
||||
builder.Services.AddScoped<ChatService>();
|
||||
builder.Services.AddSingleton<ChatService>();
|
||||
builder.Services.AddScoped<VectorSearchService>();
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
builder.Services.AddOpenApi(options =>
|
||||
{
|
||||
options.SwaggerDoc("v1", new OpenApiInfo { Title = "SQL Database Vector Search API", Version = "v1" });
|
||||
|
||||
options.AddDefaultResponse();
|
||||
});
|
||||
|
||||
@@ -56,11 +58,11 @@ app.UseStatusCodePages();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.MapOpenApi();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.RoutePrefix = string.Empty;
|
||||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "SQL Database Vector Search API v1");
|
||||
options.SwaggerEndpoint("/openapi/v1.json", builder.Environment.ApplicationName);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -71,24 +73,15 @@ documentsApiGroup.MapGet(string.Empty, async (VectorSearchService vectorSearchSe
|
||||
var documents = await vectorSearchService.GetDocumentsAsync();
|
||||
return TypedResults.Ok(documents);
|
||||
})
|
||||
.WithOpenApi(operation =>
|
||||
{
|
||||
operation.Summary = "Gets the list of documents";
|
||||
return operation;
|
||||
});
|
||||
.WithSummary("Gets the list of documents");
|
||||
|
||||
documentsApiGroup.MapGet("{documentId:guid}/chunks", async (Guid documentId, VectorSearchService vectorSearchService) =>
|
||||
{
|
||||
var documents = await vectorSearchService.GetDocumentChunksAsync(documentId);
|
||||
return TypedResults.Ok(documents);
|
||||
})
|
||||
.WithOpenApi(operation =>
|
||||
{
|
||||
operation.Summary = "Gets the list of chunks of a given document";
|
||||
operation.Description = "The list does not contain embedding. Use '/api/documents/{documentId}/chunks/{documentChunkId}' to get the embedding for a given chunk.";
|
||||
|
||||
return operation;
|
||||
});
|
||||
.WithSummary("Gets the list of chunks of a given document")
|
||||
.WithDescription("The list does not contain embedding. Use '/api/documents/{documentId}/chunks/{documentChunkId}' to get the embedding for a given chunk.");
|
||||
|
||||
documentsApiGroup.MapGet("{documentId:guid}/chunks/{documentChunkId:guid}", async Task<Results<Ok<DocumentChunk>, NotFound>> (Guid documentId, Guid documentChunkId, VectorSearchService vectorSearchService) =>
|
||||
{
|
||||
@@ -100,13 +93,11 @@ documentsApiGroup.MapGet("{documentId:guid}/chunks/{documentChunkId:guid}", asyn
|
||||
|
||||
return TypedResults.Ok(chunk);
|
||||
})
|
||||
.WithOpenApi(operation =>
|
||||
{
|
||||
operation.Summary = "Gets the details of a given chunk, includings its embedding";
|
||||
return operation;
|
||||
});
|
||||
.ProducesProblem(StatusCodes.Status404NotFound)
|
||||
.WithSummary("Gets the details of a given chunk, includings its embedding");
|
||||
|
||||
documentsApiGroup.MapPost(string.Empty, async (IFormFile file, VectorSearchService vectorSearchService, Guid? documentId = null) =>
|
||||
documentsApiGroup.MapPost(string.Empty, async (IFormFile file, VectorSearchService vectorSearchService,
|
||||
[Description("The unique identifier of the document. If not provided, a new one will be generated. If you specify an existing documentId, the corresponding document will be overwritten.")] Guid? documentId = null) =>
|
||||
{
|
||||
using var stream = file.OpenReadStream();
|
||||
documentId = await vectorSearchService.ImportAsync(stream, file.FileName, documentId);
|
||||
@@ -114,43 +105,26 @@ documentsApiGroup.MapPost(string.Empty, async (IFormFile file, VectorSearchServi
|
||||
return TypedResults.Ok(new UploadDocumentResponse(documentId.Value));
|
||||
})
|
||||
.DisableAntiforgery()
|
||||
.WithOpenApi(operation =>
|
||||
{
|
||||
operation.Summary = "Uploads a document";
|
||||
operation.Description = "Uploads a document to SQL Database and saves its embedding using the new native Vector type. The document will be indexed and used to answer questions. Currently, only PDF files are supported.";
|
||||
|
||||
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 corresponding document will be overwritten.";
|
||||
|
||||
return operation;
|
||||
});
|
||||
.ProducesProblem(StatusCodes.Status400BadRequest)
|
||||
.WithSummary("Uploads a document")
|
||||
.WithDescription("Uploads a document to SQL Database and saves its embedding using the new native Vector type. The document will be indexed and used to answer questions. Currently, only PDF files are supported.");
|
||||
|
||||
documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, VectorSearchService vectorSearchService) =>
|
||||
{
|
||||
await vectorSearchService.DeleteDocumentAsync(documentId);
|
||||
return TypedResults.NoContent();
|
||||
})
|
||||
.WithOpenApi(operation =>
|
||||
{
|
||||
operation.Summary = "Deletes a document";
|
||||
operation.Description = "This endpoint deletes the document and all its chunks.";
|
||||
.WithSummary("Deletes a document")
|
||||
.WithDescription("This endpoint deletes the document and all its chunks.");
|
||||
|
||||
return operation;
|
||||
});
|
||||
|
||||
app.MapPost("/api/ask", async (Question question, VectorSearchService vectorSearchService, bool reformulate = true) =>
|
||||
app.MapPost("/api/ask", async (Question question, VectorSearchService vectorSearchService,
|
||||
[Description("If true, the question will be reformulated taking into account the context of the chat identified by the given ConversationId.")] 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.";
|
||||
|
||||
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;
|
||||
})
|
||||
.WithSummary("Asks a question")
|
||||
.WithDescription("The question will be reformulated taking into account the context of the chat identified by the given ConversationId.")
|
||||
.WithTags("Ask");
|
||||
|
||||
app.Run();
|
||||
@@ -1,18 +1,14 @@
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
using Microsoft.SemanticKernel.ChatCompletion;
|
||||
using SqlDatabaseVectorSearch.Settings;
|
||||
|
||||
namespace SqlDatabaseVectorSearch.Services;
|
||||
|
||||
public class ChatService(IMemoryCache cache, IChatCompletionService chatCompletionService, IOptions<AppSettings> appSettingsOptions)
|
||||
public class ChatService(IChatCompletionService chatCompletionService, HybridCache cache)
|
||||
{
|
||||
private readonly AppSettings appSettings = appSettingsOptions.Value;
|
||||
|
||||
public async Task<string> CreateQuestionAsync(Guid conversationId, string question)
|
||||
{
|
||||
var chat = new ChatHistory(cache.Get<ChatHistory?>(conversationId) ?? []);
|
||||
var chat = await GetChatHistoryAsync(conversationId);
|
||||
|
||||
var embeddingQuestion = $"""
|
||||
Reformulate the following question taking into account the context of the chat to perform embeddings search:
|
||||
@@ -35,28 +31,28 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti
|
||||
|
||||
public async Task<string> AskQuestionAsync(Guid conversationId, IEnumerable<string> chunks, string question)
|
||||
{
|
||||
var chat = new ChatHistory(""""
|
||||
"""
|
||||
var chat = new ChatHistory("""
|
||||
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.
|
||||
For example, if the user asks "What is the capital of France?" and in this chat there isn't information about France, you should reply something like "This information isn't available in the given context".
|
||||
Never answer to questions that are not related to this chat.
|
||||
You must answer in the same language of the user's question.
|
||||
"""");
|
||||
""");
|
||||
|
||||
var prompt = new StringBuilder("""
|
||||
Using the following information:
|
||||
---
|
||||
|
||||
""");
|
||||
|
||||
// TODO: Ensure that chunks are not too long, according to the model max token.
|
||||
foreach (var result in chunks)
|
||||
foreach (var text in chunks)
|
||||
{
|
||||
prompt.AppendLine(result);
|
||||
prompt.AppendLine("---");
|
||||
prompt.Append(text);
|
||||
}
|
||||
|
||||
prompt.AppendLine($"""
|
||||
|
||||
=====
|
||||
Answer the following question:
|
||||
---
|
||||
{question}
|
||||
@@ -67,23 +63,33 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti
|
||||
var answer = await chatCompletionService.GetChatMessageContentAsync(chat)!;
|
||||
|
||||
// Add question and answer to the chat history.
|
||||
var history = new ChatHistory(cache.Get<ChatHistory?>(conversationId) ?? []);
|
||||
history.AddUserMessage(question);
|
||||
history.AddAssistantMessage(answer.Content!);
|
||||
|
||||
await UpdateCacheAsync(conversationId, history);
|
||||
await SetChatHistoryAsync(conversationId, question, answer.Content!);
|
||||
|
||||
return answer.Content!;
|
||||
}
|
||||
|
||||
private Task UpdateCacheAsync(Guid conversationId, ChatHistory chat)
|
||||
private async Task UpdateCacheAsync(Guid conversationId, ChatHistory chat)
|
||||
=> await cache.SetAsync(conversationId.ToString(), chat);
|
||||
|
||||
private async Task<ChatHistory> GetChatHistoryAsync(Guid conversationId)
|
||||
{
|
||||
if (chat.Count > appSettings.MessageLimit)
|
||||
var historyCache = await cache.GetOrCreateAsync(conversationId.ToString(),
|
||||
(cancellationToken) =>
|
||||
{
|
||||
chat = new ChatHistory(chat.TakeLast(appSettings.MessageLimit));
|
||||
return ValueTask.FromResult<ChatHistory>([]);
|
||||
});
|
||||
|
||||
var chat = new ChatHistory(historyCache);
|
||||
return chat;
|
||||
}
|
||||
|
||||
cache.Set(conversationId, chat, appSettings.MessageExpiration);
|
||||
return Task.CompletedTask;
|
||||
private async Task SetChatHistoryAsync(Guid conversationId, string question, string answer)
|
||||
{
|
||||
var history = await GetChatHistoryAsync(conversationId);
|
||||
|
||||
history.AddUserMessage(question);
|
||||
history.AddAssistantMessage(answer);
|
||||
|
||||
await UpdateCacheAsync(conversationId, history);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ using Microsoft.SemanticKernel.Embeddings;
|
||||
using Microsoft.SemanticKernel.Text;
|
||||
using SqlDatabaseVectorSearch.Models;
|
||||
using SqlDatabaseVectorSearch.Settings;
|
||||
using TinyHelpers.Extensions;
|
||||
using UglyToad.PdfPig;
|
||||
using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor;
|
||||
|
||||
@@ -45,7 +44,7 @@ public class VectorSearchService(SqlConnection sqlConnection, ITextEmbeddingGene
|
||||
var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingsAsync(paragraphs);
|
||||
|
||||
// Save the document chunks and the corresponding embedding in the database.
|
||||
foreach (var (paragraph, index) in paragraphs.WithIndex())
|
||||
foreach (var (index, paragraph) in paragraphs.Index())
|
||||
{
|
||||
await sqlConnection.ExecuteAsync($"""
|
||||
INSERT INTO DocumentChunks (DocumentId, [Index], Content, Embedding)
|
||||
|
||||
@@ -10,7 +10,5 @@ public class AppSettings
|
||||
|
||||
public int MaxRelevantChunks { get; init; } = 5;
|
||||
|
||||
public int MessageLimit { get; init; }
|
||||
|
||||
public TimeSpan MessageExpiration { get; init; }
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<NoWarn>$(NoWarn);SKEXP0001;SKEXP0010;SKEXP0050;</NoWarn>
|
||||
<NoWarn>$(NoWarn);SKEXP0001;SKEXP0010;SKEXP0050;EXTEXP0018</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
|
||||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.25.0" />
|
||||
<PackageReference Include="MinimalHelpers.OpenApi" Version="2.0.17" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" Version="9.0.0-preview.9.24556.5" />
|
||||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.32.0" />
|
||||
<PackageReference Include="MinimalHelpers.OpenApi" Version="2.1.2" />
|
||||
<PackageReference Include="PdfPig" Version="0.1.9" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
|
||||
<PackageReference Include="TinyHelpers" Version="3.1.18" />
|
||||
<PackageReference Include="TinyHelpers.AspNetCore" Version="3.1.19" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="7.2.0" />
|
||||
<PackageReference Include="TinyHelpers.AspNetCore" Version="4.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
"MaxTokensPerParagraph": 1024,
|
||||
"OverlapTokens": 100,
|
||||
"MaxRelevantChunks": 10,
|
||||
"MessageLimit": 20,
|
||||
"MessageExpiration": "00:05:00"
|
||||
},
|
||||
"Logging": {
|
||||
|
||||
Reference in New Issue
Block a user