mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
Add streaming support and improve JSON serialization
- Updated `Response` record class to allow nullable `Question` and `Answer` properties; moved `StreamState` enum to a new file. - Added `JsonStringEnumConverter` in `Program.cs` for better enum serialization. - Corrected terminology in document upload endpoint description. - Introduced `/api/ask-streaming` endpoint for streaming question responses. - Added `AskStreamingAsync` method in `VectorSearchService` for handling streaming logic. - Created `StreamState.cs` to define `StreamState` enum with `Start`, `Append`, and `End` values.
This commit is contained in:
@@ -88,6 +88,25 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
||||
return new Response(reformulatedQuestion, answer);
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<Response> AskStreamingAsync(Question question, bool reformulate = true)
|
||||
{
|
||||
var (reformulatedQuestion, chunks) = await CreateContextAsync(question, reformulate);
|
||||
|
||||
var answerStream = chatService.AskStreamingAsync(question.ConversationId, chunks, reformulatedQuestion);
|
||||
|
||||
// The first message contains the original question.
|
||||
yield return new Response(reformulatedQuestion, null, StreamState.Start);
|
||||
|
||||
// Return each token as a partial response.
|
||||
await foreach (var token in answerStream)
|
||||
{
|
||||
yield return new Response(null, token, StreamState.Append);
|
||||
}
|
||||
|
||||
// The last message tells the client that the stream has ended.
|
||||
yield return new Response(null, null, StreamState.End);
|
||||
}
|
||||
|
||||
private async Task<(string Question, IEnumerable<string> Chunks)> CreateContextAsync(Question question, bool reformulate = true)
|
||||
{
|
||||
// Reformulate the following question taking into account the context of the chat to perform keyword search and embeddings:
|
||||
|
||||
Reference in New Issue
Block a user