Refactor citation and token usage handling

Updated comments for clarity and streamlined logic for managing tokenUsageResponse. Removed explicit null checks in favor of a null-coalescing assignment. Ensured citations are always extracted and returned at the end of the streaming process.
This commit is contained in:
Marco Minerva
2025-06-18 14:51:26 +02:00
parent e0cf824dd6
commit 765daa8544
@@ -115,7 +115,7 @@ public partial class VectorSearchService(IServiceProvider serviceProvider, Appli
if (token?.Contains('【') == true) if (token?.Contains('【') == true)
{ {
// Citations are started when the first token contains a 【 character. // Citations are started when we encounter a token containing a 【 character.
// We need to track it because we don't want to return the citations in the actual response. // We need to track it because we don't want to return the citations in the actual response.
areCitationsStarted = true; areCitationsStarted = true;
} }
@@ -126,22 +126,12 @@ public partial class VectorSearchService(IServiceProvider serviceProvider, Appli
} }
// Token usage is expected in the last message. // Token usage is expected in the last message.
tokenUsageResponse = tokenUsage is not null ? new(tokenUsage) : null; tokenUsageResponse ??= tokenUsage is not null ? new(tokenUsage) : null;
if (tokenUsageResponse is not null)
{
// Response is complete, we can return the citations.
var (_, citations) = ExtractCitations(fullAnswer.ToString());
yield return new(null, StreamState.End, tokenUsageResponse, citations);
}
} }
// If the token usage has not been returned in the last message, we must explicitly tell that the stream is ended.
if (tokenUsageResponse is null)
{
// Extract citations at the end of streaming. // Extract citations at the end of streaming.
var (_, citations) = ExtractCitations(fullAnswer.ToString()); var (_, citations) = ExtractCitations(fullAnswer.ToString());
yield return new(null, StreamState.End, null, citations); yield return new(null, StreamState.End, tokenUsageResponse, citations);
}
} }
private async Task<(ChatResponse ReformulatedQuestion, int EmbeddingTokenCount, IEnumerable<Entities.DocumentChunk> Chunks)> CreateContextAsync(Question question, bool reformulate, CancellationToken cancellationToken) private async Task<(ChatResponse ReformulatedQuestion, int EmbeddingTokenCount, IEnumerable<Entities.DocumentChunk> Chunks)> CreateContextAsync(Question question, bool reformulate, CancellationToken cancellationToken)