Skip to content

Commit 4066185

Browse files
committed
Refactor token usage and enhance logging capabilities
- Updated `TokenUsage` class properties to `PromptTokens` and `CompletionTokens`. - Modified `Ask.razor` to display new token counts. - Added logger to `ChatService` constructor for improved logging. - Implemented logging for token usage in `ChatService` methods. - Changed logging level to `Debug` for paragraph storage in `VectorSearchService`. - Updated logging configuration in `appsettings.Development.json` for better visibility.
1 parent a0d1126 commit 4066185

5 files changed

Lines changed: 13 additions & 8 deletions

File tree

SqlDatabaseVectorSearch/Components/Pages/Ask.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@
235235
return string.Empty;
236236
}
237237

238-
return $"Input Token Count: {tokenUsage.InputTokenCount}<br />" +
239-
$"Output Token Count: {tokenUsage.OutputTokenCount}<br />" +
240-
$"Total Token Count: {tokenUsage.TotalTokenCount}";
238+
return $"Prompt tokens: {tokenUsage.PromptTokens}<br />" +
239+
$"Completion tokens: {tokenUsage.CompletionTokens}<br />" +
240+
$"Total tokens: {tokenUsage.TotalTokens}";
241241
}
242242
}
243243

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace SqlDatabaseVectorSearch.Models;
22

3-
public record class TokenUsage(int InputTokenCount, int OutputTokenCount)
3+
public record class TokenUsage(int PromptTokens, int CompletionTokens)
44
{
5-
public int TotalTokenCount => InputTokenCount + OutputTokenCount;
5+
public int TotalTokens => PromptTokens + CompletionTokens;
66
}

SqlDatabaseVectorSearch/Services/ChatService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace SqlDatabaseVectorSearch.Services;
1212

13-
public class ChatService(IChatCompletionService chatCompletionService, TokenizerService tokenizerService, HybridCache cache, IOptions<AppSettings> appSettingsOptions)
13+
public class ChatService(IChatCompletionService chatCompletionService, TokenizerService tokenizerService, HybridCache cache, IOptions<AppSettings> appSettingsOptions, ILogger<ChatService> logger)
1414
{
1515
private readonly AppSettings appSettings = appSettingsOptions.Value;
1616

@@ -36,6 +36,7 @@ The reformulation must always explicitly contain the subject of the question.
3636
await UpdateCacheAsync(conversationId, chat, cancellationToken);
3737

3838
var tokenUsage = GetTokenUsage(reformulatedQuestion);
39+
logger.LogDebug("Reformulation: {TokenUsage}", tokenUsage);
3940

4041
return new(reformulatedQuestion.Content!, tokenUsage);
4142
}
@@ -53,6 +54,7 @@ public async Task<ChatResponse> AskQuestionAsync(Guid conversationId, IEnumerabl
5354
await SetChatHistoryAsync(conversationId, question, answer.Content!, cancellationToken);
5455

5556
var tokenUsage = GetTokenUsage(answer);
57+
logger.LogDebug("Ask question: {TokenUsage}", tokenUsage);
5658

5759
return new(answer.Content!, tokenUsage);
5860
}
@@ -78,6 +80,7 @@ public async IAsyncEnumerable<ChatResponse> AskStreamingAsync(Guid conversationI
7880
var tokenUsage = GetTokenUsage(token);
7981
if (tokenUsage is not null)
8082
{
83+
logger.LogDebug("Ask streaming: {TokenUsage}", tokenUsage);
8184
yield return new(null, tokenUsage);
8285
}
8386
}

SqlDatabaseVectorSearch/Services/VectorSearchService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task<ImportDocumentResponse> ImportAsync(Stream stream, string name
4747
// Save the document chunks and the corresponding embedding in the database.
4848
foreach (var (index, paragraph) in paragraphs.Index())
4949
{
50-
logger.LogInformation("Storing a paragraph of {TokenCount} tokens.", tokenizerService.CountChatCompletionTokens(paragraph));
50+
logger.LogDebug("Storing a paragraph of {TokenCount} tokens.", tokenizerService.CountChatCompletionTokens(paragraph));
5151

5252
var documentChunk = new Entities.DocumentChunk { Document = document, Index = index, Content = paragraph!, Embedding = embeddings[index].ToArray() };
5353
dbContext.DocumentChunks.Add(documentChunk);
@@ -103,7 +103,9 @@ public async IAsyncEnumerable<QuestionResponse> AskStreamingAsync(Question quest
103103
{
104104
// Reformulate the question taking into account the context of the chat to perform keyword search and embeddings.
105105
var reformulatedQuestion = reformulate ? await chatService.CreateQuestionAsync(question.ConversationId, question.Text, cancellationToken) : new(question.Text);
106+
106107
var embeddingTokenCount = tokenizerService.CountEmbeddingTokens(reformulatedQuestion.Text!);
108+
logger.LogDebug("Embedding Token Count: {EmbeddingTokenCount}", embeddingTokenCount);
107109

108110
// Perform Vector Search on SQL Database.
109111
var questionEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(reformulatedQuestion.Text!, cancellationToken: cancellationToken);

SqlDatabaseVectorSearch/appsettings.Development.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"LogLevel": {
44
"Default": "Information",
55
"Microsoft.AspNetCore": "Warning",
6-
"Microsoft.KernelMemory": "Debug"
6+
"SqlDatabaseVectorSearch": "Debug"
77
}
88
}
99
}

0 commit comments

Comments
 (0)