mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-08-04 09:48:57 +00:00
Refactor embedding workflow: add ExtractChunksExecutor
Refactored the embedding workflow to introduce ExtractChunksExecutor, responsible for content decoding and chunk extraction before embedding generation. Registered ExtractChunksExecutor and GenerateEmbeddingExecutor as singletons, and StoreEmbeddingExecutor as scoped in DI. Updated workflow to sequence ExtractChunksExecutor → GenerateEmbeddingExecutor → StoreEmbeddingExecutor. Modified GenerateEmbeddingExecutor to accept ExtractChunksResponse. Updated ChatClientAgentOptions usage to use object initializers for clarity. Added ExtractChunksExecutor.cs.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using Microsoft.Agents.AI.Workflows;
|
||||
using SqlDatabaseVectorSearch.ContentDecoders;
|
||||
|
||||
namespace SqlDatabaseVectorSearch.Workflows;
|
||||
|
||||
public partial class ExtractChunksExecutor(IServiceProvider serviceProvider, ILogger<ExtractChunksExecutor> logger) : Executor(nameof(ExtractChunksExecutor))
|
||||
{
|
||||
[MessageHandler]
|
||||
private async ValueTask<ExtractChunksResponse> HandleAsync(EmbeddingRequest request, IWorkflowContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
// Extract the contents of the file.
|
||||
var decoder = serviceProvider.GetKeyedService<IContentDecoder>(request.ContentType) ?? throw new NotSupportedException($"Content type '{request.ContentType}' is not supported.");
|
||||
var chunks = await decoder.DecodeAsync(request.Content, request.ContentType, cancellationToken);
|
||||
|
||||
logger.LogDebug("Extracted {Count} chunks from '{FileName}'.", chunks.Count(), request.FileName);
|
||||
|
||||
return new(request, chunks);
|
||||
}
|
||||
}
|
||||
|
||||
public record class ExtractChunksResponse(EmbeddingRequest Request, IEnumerable<Chunk> Chunks);
|
||||
@@ -7,17 +7,14 @@ using SqlDatabaseVectorSearch.Settings;
|
||||
|
||||
namespace SqlDatabaseVectorSearch.Workflows;
|
||||
|
||||
public partial class GenerateEmbeddingExecutor(IServiceProvider serviceProvider, IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator, TokenizerService tokenizerService, IOptions<AppSettings> appSettingsOptions, ILogger<GenerateEmbeddingExecutor> logger) : Executor(nameof(GenerateEmbeddingExecutor))
|
||||
public partial class GenerateEmbeddingExecutor(IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator, TokenizerService tokenizerService, IOptions<AppSettings> appSettingsOptions, ILogger<GenerateEmbeddingExecutor> logger) : Executor(nameof(GenerateEmbeddingExecutor))
|
||||
{
|
||||
private readonly AppSettings appSettings = appSettingsOptions.Value;
|
||||
|
||||
[MessageHandler]
|
||||
private async ValueTask<EmbeddingResponse> HandleAsync(EmbeddingRequest request, IWorkflowContext context, CancellationToken cancellationToken)
|
||||
private async ValueTask<EmbeddingResponse> HandleAsync(ExtractChunksResponse chunks, IWorkflowContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
// Extract the contents of the file.
|
||||
var decoder = serviceProvider.GetKeyedService<IContentDecoder>(request.ContentType) ?? throw new NotSupportedException($"Content type '{request.ContentType}' is not supported.");
|
||||
var chunks = await decoder.DecodeAsync(request.Content, request.ContentType, cancellationToken);
|
||||
var chunkContents = chunks.Select(p => p.Content).ToList();
|
||||
var chunkContents = chunks.Chunks.Select(p => p.Content).ToList();
|
||||
|
||||
// We get the token count of the whole document because it is the total number of tokens used by the embedding (it may be necessary, for example, for cost analysis).
|
||||
var tokenCount = tokenizerService.CountEmbeddingTokens(string.Join(" ", chunkContents));
|
||||
@@ -33,7 +30,7 @@ public partial class GenerateEmbeddingExecutor(IServiceProvider serviceProvider,
|
||||
embeddings.AddRange(batchEmbeddings);
|
||||
}
|
||||
|
||||
return new EmbeddingResponse(request, chunks, embeddings, tokenCount);
|
||||
return new EmbeddingResponse(chunks.Request, chunks.Chunks, embeddings, tokenCount);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user