Update VectorSearchService and appsettings.json

- Clarified comment in `ChatService.cs`.
- Added `TokenizerService` and `ILogger` parameters to `VectorSearchService` constructor.
- Updated paragraph splitting to use `tokenizerService.CountTokens`.
- Added logging for token count of each paragraph in `VectorSearchService`.
- Updated `ModelId` comment in `appsettings.json` to include "gpt-4o-mini".
- Changed `MaxTokensPerParagraph` in `appsettings.json` from 1024 to 1000.
This commit is contained in:
Marco Minerva
2025-01-28 16:09:34 +01:00
parent d53330934e
commit f15f387510
3 changed files with 9 additions and 6 deletions
@@ -100,7 +100,7 @@ public class ChatService(IChatCompletionService chatCompletionService, Tokenizer
var tokenCount = tokenizerService.CountTokens(text); var tokenCount = tokenizerService.CountTokens(text);
if (tokenCount > tokensAvailable) if (tokenCount > tokensAvailable)
{ {
// There isn't enough space to add the chunks. // There isn't enough space to add the current chunk.
break; break;
} }
@@ -13,7 +13,7 @@ using Entities = SqlDatabaseVectorSearch.DataAccessLayer.Entities;
namespace SqlDatabaseVectorSearch.Services; namespace SqlDatabaseVectorSearch.Services;
public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingGenerationService textEmbeddingGenerationService, ChatService chatService, TimeProvider timeProvider, IOptions<AppSettings> appSettingsOptions) public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingGenerationService textEmbeddingGenerationService, ChatService chatService, TokenizerService tokenizerService, TimeProvider timeProvider, IOptions<AppSettings> appSettingsOptions, ILogger<VectorSearchService> logger)
{ {
private readonly AppSettings appSettings = appSettingsOptions.Value; private readonly AppSettings appSettings = appSettingsOptions.Value;
@@ -34,12 +34,15 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
dbContext.Documents.Add(document); dbContext.Documents.Add(document);
// Split the content into chunks and generate the embeddings for each one. // Split the content into chunks and generate the embeddings for each one.
var paragraphs = TextChunker.SplitPlainTextParagraphs(TextChunker.SplitPlainTextLines(content, appSettings.MaxTokensPerLine), appSettings.MaxTokensPerParagraph, appSettings.OverlapTokens); var lines = TextChunker.SplitPlainTextLines(content, appSettings.MaxTokensPerLine, tokenizerService.CountTokens);
var paragraphs = TextChunker.SplitPlainTextParagraphs(lines, appSettings.MaxTokensPerParagraph, appSettings.OverlapTokens, tokenCounter: tokenizerService.CountTokens);
var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingsAsync(paragraphs); var embeddings = await textEmbeddingGenerationService.GenerateEmbeddingsAsync(paragraphs);
// Save the document chunks and the corresponding embedding in the database. // Save the document chunks and the corresponding embedding in the database.
foreach (var (index, paragraph) in paragraphs.Index()) foreach (var (index, paragraph) in paragraphs.Index())
{ {
logger.LogInformation("Storing a paragraph of {TokenCount} tokens.", tokenizerService.CountTokens(paragraph));
var documentChunk = new Entities.DocumentChunk { Document = document, Index = index, Content = paragraph!, Embedding = embeddings[index].ToArray() }; var documentChunk = new Entities.DocumentChunk { Document = document, Index = index, Content = paragraph!, Embedding = embeddings[index].ToArray() };
dbContext.DocumentChunks.Add(documentChunk); dbContext.DocumentChunks.Add(documentChunk);
} }
+3 -3
View File
@@ -6,7 +6,7 @@
"ChatCompletion": { "ChatCompletion": {
"Endpoint": "", "Endpoint": "",
"Deployment": "", "Deployment": "",
"ModelId": "", // o1, gpt-4o, gpt-4, gpt-3.5 "ModelId": "", // o1, gpt-4o, gpt-4o-mini, gpt-4, gpt-3.5
"ApiKey": "" "ApiKey": ""
}, },
"Embedding": { "Embedding": {
@@ -20,8 +20,8 @@
} }
}, },
"AppSettings": { "AppSettings": {
"MaxTokenPerLine": 300, "MaxTokensPerLine": 300,
"MaxTokensPerParagraph": 1024, "MaxTokensPerParagraph": 1000,
"OverlapTokens": 100, "OverlapTokens": 100,
"MaxRelevantChunks": 10, "MaxRelevantChunks": 10,
"MaxInputTokens": 16385, "MaxInputTokens": 16385,