mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
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:
@@ -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.
|
// Extract citations at the end of streaming.
|
||||||
if (tokenUsageResponse is null)
|
var (_, citations) = ExtractCitations(fullAnswer.ToString());
|
||||||
{
|
yield return new(null, StreamState.End, tokenUsageResponse, citations);
|
||||||
// Extract citations at the end of streaming.
|
|
||||||
var (_, citations) = ExtractCitations(fullAnswer.ToString());
|
|
||||||
yield return new(null, StreamState.End, null, 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user