mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
Optimize chunks retrieval
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
using Microsoft.Extensions.Caching.Memory;
|
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;
|
||||||
@@ -34,7 +33,7 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti
|
|||||||
return reformulatedQuestion.Content!;
|
return reformulatedQuestion.Content!;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> AskQuestionAsync(Guid conversationId, IEnumerable<DocumentChunk> chunks, string question)
|
public async Task<string> AskQuestionAsync(Guid conversationId, IEnumerable<string> chunks, string question)
|
||||||
{
|
{
|
||||||
var chat = new ChatHistory(""""
|
var chat = new ChatHistory(""""
|
||||||
"""
|
"""
|
||||||
@@ -51,7 +50,7 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti
|
|||||||
""");
|
""");
|
||||||
|
|
||||||
// TODO: Ensure that chunks are not too long, according to the model max token.
|
// TODO: Ensure that chunks are not too long, according to the model max token.
|
||||||
foreach (var result in chunks.Select(c => c.Content))
|
foreach (var result in chunks)
|
||||||
{
|
{
|
||||||
prompt.AppendLine(result);
|
prompt.AppendLine(result);
|
||||||
prompt.AppendLine("---");
|
prompt.AppendLine("---");
|
||||||
|
|||||||
@@ -104,6 +104,7 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
|
|
||||||
var chunks = await dbContext.DocumentChunks
|
var chunks = await dbContext.DocumentChunks
|
||||||
.OrderBy(c => EF.Functions.VectorDistance("cosine", c.Embedding, questionEmbedding.ToArray()))
|
.OrderBy(c => EF.Functions.VectorDistance("cosine", c.Embedding, questionEmbedding.ToArray()))
|
||||||
|
.Select(c => c.Content)
|
||||||
.Take(appSettings.MaxRelevantChunks)
|
.Take(appSettings.MaxRelevantChunks)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user