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:
Marco Minerva
2025-01-28 11:00:45 +01:00
parent 44c6193674
commit 1ef2d384ec
4 changed files with 57 additions and 9 deletions
@@ -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: