mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
Refactor caching and OpenAPI integration
Updated Program.cs to replace Swagger with OpenApi and MemoryCache with HybridCache. Refactored ChatService.cs to use HybridCache asynchronously. Removed MessageLimit from AppSettings.cs and appsettings.json. Updated SqlDatabaseVectorSearch.csproj to include HybridCache package and update dependencies.
This commit is contained in:
@@ -1,14 +1,13 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.OpenApi.Models;
|
|
||||||
using Microsoft.SemanticKernel;
|
using Microsoft.SemanticKernel;
|
||||||
using MinimalHelpers.OpenApi;
|
|
||||||
using SqlDatabaseVectorSearch.DataAccessLayer;
|
using SqlDatabaseVectorSearch.DataAccessLayer;
|
||||||
using SqlDatabaseVectorSearch.Models;
|
using SqlDatabaseVectorSearch.Models;
|
||||||
using SqlDatabaseVectorSearch.Services;
|
using SqlDatabaseVectorSearch.Services;
|
||||||
using SqlDatabaseVectorSearch.Settings;
|
using SqlDatabaseVectorSearch.Settings;
|
||||||
using TinyHelpers.AspNetCore.Extensions;
|
using TinyHelpers.AspNetCore.Extensions;
|
||||||
using TinyHelpers.AspNetCore.Swagger;
|
using TinyHelpers.AspNetCore.OpenApi;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Configuration.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true);
|
builder.Configuration.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true);
|
||||||
@@ -22,13 +21,18 @@ builder.Services.AddSingleton(TimeProvider.System);
|
|||||||
builder.Services.AddSqlServer<ApplicationDbContext>(builder.Configuration.GetConnectionString("SqlConnection"), options =>
|
builder.Services.AddSqlServer<ApplicationDbContext>(builder.Configuration.GetConnectionString("SqlConnection"), options =>
|
||||||
{
|
{
|
||||||
options.UseVectorSearch();
|
options.UseVectorSearch();
|
||||||
options.EnableRetryOnFailure();
|
|
||||||
}, options =>
|
}, options =>
|
||||||
{
|
{
|
||||||
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||||
});
|
});
|
||||||
|
|
||||||
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,
|
// 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.
|
// so that embeddings themselves can be generated more accurately.
|
||||||
@@ -40,11 +44,8 @@ builder.Services.AddSingleton<TokenizerService>();
|
|||||||
builder.Services.AddSingleton<ChatService>();
|
builder.Services.AddSingleton<ChatService>();
|
||||||
builder.Services.AddScoped<VectorSearchService>();
|
builder.Services.AddScoped<VectorSearchService>();
|
||||||
|
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddOpenApi(options =>
|
||||||
builder.Services.AddSwaggerGen(options =>
|
|
||||||
{
|
{
|
||||||
options.SwaggerDoc("v1", new OpenApiInfo { Title = "SQL Database Vector Search API", Version = "v1" });
|
|
||||||
|
|
||||||
options.AddDefaultResponse();
|
options.AddDefaultResponse();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -61,11 +62,11 @@ app.UseStatusCodePages();
|
|||||||
|
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseSwagger();
|
app.MapOpenApi();
|
||||||
app.UseSwaggerUI(options =>
|
app.UseSwaggerUI(options =>
|
||||||
{
|
{
|
||||||
options.RoutePrefix = string.Empty;
|
options.RoutePrefix = string.Empty;
|
||||||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "SQL Database Vector Search API v1");
|
options.SwaggerEndpoint("/openapi/v1.json", builder.Environment.ApplicationName);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,24 +77,15 @@ documentsApiGroup.MapGet(string.Empty, async (VectorSearchService vectorSearchSe
|
|||||||
var documents = await vectorSearchService.GetDocumentsAsync();
|
var documents = await vectorSearchService.GetDocumentsAsync();
|
||||||
return TypedResults.Ok(documents);
|
return TypedResults.Ok(documents);
|
||||||
})
|
})
|
||||||
.WithOpenApi(operation =>
|
.WithSummary("Gets the list of documents");
|
||||||
{
|
|
||||||
operation.Summary = "Gets the list of documents";
|
|
||||||
return operation;
|
|
||||||
});
|
|
||||||
|
|
||||||
documentsApiGroup.MapGet("{documentId:guid}/chunks", async (Guid documentId, VectorSearchService vectorSearchService) =>
|
documentsApiGroup.MapGet("{documentId:guid}/chunks", async (Guid documentId, VectorSearchService vectorSearchService) =>
|
||||||
{
|
{
|
||||||
var documents = await vectorSearchService.GetDocumentChunksAsync(documentId);
|
var documents = await vectorSearchService.GetDocumentChunksAsync(documentId);
|
||||||
return TypedResults.Ok(documents);
|
return TypedResults.Ok(documents);
|
||||||
})
|
})
|
||||||
.WithOpenApi(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.");
|
||||||
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;
|
|
||||||
});
|
|
||||||
|
|
||||||
documentsApiGroup.MapGet("{documentId:guid}/chunks/{documentChunkId:guid}", async Task<Results<Ok<DocumentChunk>, NotFound>> (Guid documentId, Guid documentChunkId, VectorSearchService vectorSearchService) =>
|
documentsApiGroup.MapGet("{documentId:guid}/chunks/{documentChunkId:guid}", async Task<Results<Ok<DocumentChunk>, NotFound>> (Guid documentId, Guid documentChunkId, VectorSearchService vectorSearchService) =>
|
||||||
{
|
{
|
||||||
@@ -105,13 +97,11 @@ documentsApiGroup.MapGet("{documentId:guid}/chunks/{documentChunkId:guid}", asyn
|
|||||||
|
|
||||||
return TypedResults.Ok(chunk);
|
return TypedResults.Ok(chunk);
|
||||||
})
|
})
|
||||||
.WithOpenApi(operation =>
|
.ProducesProblem(StatusCodes.Status404NotFound)
|
||||||
{
|
.WithSummary("Gets the details of a given chunk, includings its embedding");
|
||||||
operation.Summary = "Gets the details of a given chunk, includings its embedding";
|
|
||||||
return operation;
|
|
||||||
});
|
|
||||||
|
|
||||||
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();
|
using var stream = file.OpenReadStream();
|
||||||
documentId = await vectorSearchService.ImportAsync(stream, file.FileName, documentId);
|
documentId = await vectorSearchService.ImportAsync(stream, file.FileName, documentId);
|
||||||
@@ -119,43 +109,26 @@ documentsApiGroup.MapPost(string.Empty, async (IFormFile file, VectorSearchServi
|
|||||||
return TypedResults.Ok(new UploadDocumentResponse(documentId.Value));
|
return TypedResults.Ok(new UploadDocumentResponse(documentId.Value));
|
||||||
})
|
})
|
||||||
.DisableAntiforgery()
|
.DisableAntiforgery()
|
||||||
.WithOpenApi(operation =>
|
.ProducesProblem(StatusCodes.Status400BadRequest)
|
||||||
{
|
.WithSummary("Uploads a document")
|
||||||
operation.Summary = "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.");
|
||||||
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;
|
|
||||||
});
|
|
||||||
|
|
||||||
documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, VectorSearchService vectorSearchService) =>
|
documentsApiGroup.MapDelete("{documentId:guid}", async (Guid documentId, VectorSearchService vectorSearchService) =>
|
||||||
{
|
{
|
||||||
await vectorSearchService.DeleteDocumentAsync(documentId);
|
await vectorSearchService.DeleteDocumentAsync(documentId);
|
||||||
return TypedResults.NoContent();
|
return TypedResults.NoContent();
|
||||||
})
|
})
|
||||||
.WithOpenApi(operation =>
|
.WithSummary("Deletes a document")
|
||||||
{
|
.WithDescription("This endpoint deletes the document and all its chunks.");
|
||||||
operation.Summary = "Deletes a document";
|
|
||||||
operation.Description = "This endpoint deletes the document and all its chunks.";
|
|
||||||
|
|
||||||
return operation;
|
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) =>
|
||||||
|
|
||||||
app.MapPost("/api/ask", async (Question question, VectorSearchService vectorSearchService, bool reformulate = true) =>
|
|
||||||
{
|
{
|
||||||
var response = await vectorSearchService.AskQuestionAsync(question, reformulate);
|
var response = await vectorSearchService.AskQuestionAsync(question, reformulate);
|
||||||
return TypedResults.Ok(response);
|
return TypedResults.Ok(response);
|
||||||
})
|
})
|
||||||
.WithOpenApi(operation =>
|
.WithSummary("Asks a question")
|
||||||
{
|
.WithDescription("The question will be reformulated taking into account the context of the chat identified by the given ConversationId.")
|
||||||
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;
|
|
||||||
})
|
|
||||||
.WithTags("Ask");
|
.WithTags("Ask");
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Hybrid;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.SemanticKernel.ChatCompletion;
|
using Microsoft.SemanticKernel.ChatCompletion;
|
||||||
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
|
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
|
||||||
@@ -7,13 +7,13 @@ using SqlDatabaseVectorSearch.Settings;
|
|||||||
|
|
||||||
namespace SqlDatabaseVectorSearch.Services;
|
namespace SqlDatabaseVectorSearch.Services;
|
||||||
|
|
||||||
public class ChatService(IMemoryCache cache, IChatCompletionService chatCompletionService, TokenizerService tokenizerService, IOptions<AppSettings> appSettingsOptions)
|
public class ChatService(IChatCompletionService chatCompletionService, TokenizerService tokenizerService, HybridCache cache, IOptions<AppSettings> appSettingsOptions)
|
||||||
{
|
{
|
||||||
private readonly AppSettings appSettings = appSettingsOptions.Value;
|
private readonly AppSettings appSettings = appSettingsOptions.Value;
|
||||||
|
|
||||||
public async Task<string> CreateQuestionAsync(Guid conversationId, string question)
|
public async Task<string> CreateQuestionAsync(Guid conversationId, string question)
|
||||||
{
|
{
|
||||||
var chat = new ChatHistory(cache.Get<ChatHistory?>(conversationId) ?? []);
|
var chat = await GetChatHistoryAsync(conversationId);
|
||||||
|
|
||||||
var embeddingQuestion = $"""
|
var embeddingQuestion = $"""
|
||||||
Reformulate the following question taking into account the context of the chat to perform embeddings search:
|
Reformulate the following question taking into account the context of the chat to perform embeddings search:
|
||||||
@@ -85,23 +85,33 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Add question and answer to the chat history.
|
// Add question and answer to the chat history.
|
||||||
var history = new ChatHistory(cache.Get<ChatHistory?>(conversationId) ?? []);
|
await SetChatHistoryAsync(conversationId, question, answer.Content!);
|
||||||
history.AddUserMessage(question);
|
|
||||||
history.AddAssistantMessage(answer.Content!);
|
|
||||||
|
|
||||||
await UpdateCacheAsync(conversationId, history);
|
|
||||||
|
|
||||||
return 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);
|
||||||
if (chat.Count > appSettings.MessageLimit)
|
|
||||||
{
|
|
||||||
chat = new ChatHistory(chat.TakeLast(appSettings.MessageLimit));
|
|
||||||
}
|
|
||||||
|
|
||||||
cache.Set(conversationId, chat, appSettings.MessageExpiration);
|
private async Task<ChatHistory> GetChatHistoryAsync(Guid conversationId)
|
||||||
return Task.CompletedTask;
|
{
|
||||||
|
var historyCache = await cache.GetOrCreateAsync(conversationId.ToString(),
|
||||||
|
(cancellationToken) =>
|
||||||
|
{
|
||||||
|
return ValueTask.FromResult<ChatHistory>([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
var chat = new ChatHistory(historyCache);
|
||||||
|
return chat;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,5 @@ public class AppSettings
|
|||||||
|
|
||||||
public int MaxOutputTokens { get; init; } = 800;
|
public int MaxOutputTokens { get; init; } = 800;
|
||||||
|
|
||||||
public int MessageLimit { get; init; }
|
|
||||||
|
|
||||||
public TimeSpan MessageExpiration { get; init; }
|
public TimeSpan MessageExpiration { get; init; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<NoWarn>$(NoWarn);SKEXP0001;SKEXP0010;SKEXP0050;</NoWarn>
|
<NoWarn>$(NoWarn);SKEXP0001;SKEXP0010;SKEXP0050;EXTEXP0018</NoWarn>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -12,15 +12,15 @@
|
|||||||
<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.3" />
|
<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.3" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="[8.0.11,9.0.0)" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="[8.0.11,9.0.0)" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" Version="9.0.0-preview.9.24556.5" />
|
||||||
<PackageReference Include="Microsoft.ML.Tokenizers" Version="1.0.0" />
|
<PackageReference Include="Microsoft.ML.Tokenizers" Version="1.0.0" />
|
||||||
<PackageReference Include="Microsoft.ML.Tokenizers.Data.Cl100kBase" Version="1.0.0" />
|
<PackageReference Include="Microsoft.ML.Tokenizers.Data.Cl100kBase" Version="1.0.0" />
|
||||||
<PackageReference Include="Microsoft.ML.Tokenizers.Data.O200kBase" Version="1.0.0" />
|
<PackageReference Include="Microsoft.ML.Tokenizers.Data.O200kBase" Version="1.0.0" />
|
||||||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.31.0" />
|
<PackageReference Include="Microsoft.SemanticKernel" Version="1.32.0" />
|
||||||
<PackageReference Include="MinimalHelpers.OpenApi" Version="2.0.17" />
|
<PackageReference Include="MinimalHelpers.OpenApi" Version="2.0.17" />
|
||||||
<PackageReference Include="PdfPig" Version="0.1.9" />
|
<PackageReference Include="PdfPig" Version="0.1.9" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="7.1.0" />
|
||||||
<PackageReference Include="TinyHelpers.AspNetCore" Version="4.0.5" />
|
<PackageReference Include="TinyHelpers.AspNetCore" Version="4.0.5" />
|
||||||
<PackageReference Include="TinyHelpers.AspNetCore.Swashbuckle" Version="4.0.5" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -26,7 +26,6 @@
|
|||||||
"MaxRelevantChunks": 10,
|
"MaxRelevantChunks": 10,
|
||||||
"MaxInputTokens": 16385,
|
"MaxInputTokens": 16385,
|
||||||
"MaxOutputTokens": 800,
|
"MaxOutputTokens": 800,
|
||||||
"MessageLimit": 20,
|
|
||||||
"MessageExpiration": "00:05:00"
|
"MessageExpiration": "00:05:00"
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
|
|||||||
Reference in New Issue
Block a user