Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

I have an IAsyncenumerable network request. But only refresh the UI after the response is completed #63342

Answered by campersau
zms9110750 asked this question in Q&A
Discussion options

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

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
0 replies
Answer selected by zms9110750
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
area-blazor Includes: Blazor, Razor Components

AltStyle によって変換されたページ (->オリジナル) /