Search code examples
c#openapiopenai-apichatgpt-api

C# ASP.NET MVC web app - update view after completion of asynch calls w/ OpenAI


I have an ASP.NET MVC app in C# that I'm trying to asynchronously call a bunch of OpenAI requests in parallel and then wait on all results to return.

The following is a snippet from this code:

        foreach (ChatMessage msg in messages)
        {
            List<ChatMessage> tmpMessage = new List<ChatMessage>();
            tmpMessage.Add(msg);

            Task<ClientResult<ChatCompletion>> chatResult = client.CompleteChatAsync(tmpMessage, chatOptions);

            await chatResult;

            if (chatResult.IsCompleted && chatResult.Result.Value != null)
            {
                output.Add(chatResult.Result.Value.Content[0].Text);
            }
        }

Each string in the output list is a json structured output from OpenAI. I then go through each json output and manipulate it as needed.

My questions are: Is this truly asynchronous? I call the CompleteChatAsync but then I use await chatResult and I am uncertain if this is the way to do that asynchronously?

Second - the view portion of the webpage does not update once all results return and are processed. It just sits there. How do I refresh the view in the ASP.NET MVC web app in .NET?

Thanks!


Solution

  • To run several tasks "in parallel" (more correctly: concurrently), you can use Task.WhenAll(listOfTasks). The steps will be as follow:

    1. Create the list of tasks (without awaiting them)
    2. Fire them and wait for all of them to finish.
    3. Handle the results.

    example:

    // Step 1
    var allChatResults = new List<Task<ClientResult<ChatCompletion>>>();
    foreach (ChatMessage msg in messages)
    {
        var chatResult = client.CompleteChatAsync(tmpMessage, chatOptions);
        allChatResults.Add(chatResult);
    }
    
    // Step 2
    await Task.WhenAll(allChatResults);
    
    // Step 3
    foreach (Task chatResult in allChatResults)
    {
        if (chatResult.IsCompleted && chatResult.Result.Value != null)
        {
            output.Add(chatResult.Result.Value.Content[0].Text);
        }
    }