-
Notifications
You must be signed in to change notification settings - Fork 10.4k
I have an IAsyncenumerable network request. But only refresh the UI after the response is completed #63342
-
I don't know what I need to pay attention to.
I think my code is fine
@if (Deltas == null) { <MButton OnClick="() => MessageChanged.InvokeAsync(Message.NewUserMsg(null))">NewUserMsg</MButton> <MButton OnClick="() => MessageChanged.InvokeAsync(Message.NewSystemMsg(null))">NewSystemMsg</MButton> <MButton OnClick="() => MessageChanged.InvokeAsync(Message.NewAssistantMsg(null))">NewAssistantMsg</MButton> <MButton OnClick="() => MessageChanged.InvokeAsync(new MessageTool(null, null))">MessageTool</MButton> <MButton OnClick=" Response ">From API</MButton> } else { @foreach (var item in Delta) { <text>@item.ReasoningContent</text> } <MDivider /> @foreach (var item in Delta) { <text>@item.Content</text> } <MDivider /> @foreach (var item in Delta) { <text>@item.ToolCalls?[0]</text> } } @code { [Inject] DeepSeekApiClient ds { get; init; } ChatResponseDelta<ChoiceDelta>? Deltas = null; List<MessageAssistant> Delta = []; async Task Response() { Deltas = await ds.ChatStreamAsync(); var c = Observable.Interval(TimeSpan.FromMilliseconds(1)).ToAsyncEnumerable(); await foreach (var item in Deltas.Zip(c, (a, b) => a)) { await Task.Delay(1); Delta.Add(item.Delta); StateHasChanged(); } await MessageChanged.InvokeAsync((await Deltas).Choices[0].Message); } [Parameter] public EventCallback<Message?> MessageChanged { get; set; } }
DeepSeekApiClient code in github
Beta Was this translation helpful? Give feedback.
All reactions
If you are using blazor webassembly try to also set request.SetBrowserResponseStreamingEnabled(true);
See https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-9.0#httpclient-and-httprequestmessage-with-fetch-api-request-options
With .NET 10 it will be the default: dotnet/runtime#112442
Replies: 2 comments
-
In the console program. He will receive data immediately upon response.
But in the browser, I saw that there was response data in the network during browser debugging. So he should refresh at this time.
He is a Server Sent Events data. I don't know if it's related.
Beta Was this translation helpful? Give feedback.
All reactions
-
If you are using blazor webassembly try to also set request.SetBrowserResponseStreamingEnabled(true);
See https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-9.0#httpclient-and-httprequestmessage-with-fetch-api-request-options
With .NET 10 it will be the default: dotnet/runtime#112442
Beta Was this translation helpful? Give feedback.