semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: The operation was cancelled because it exceeded the configured timeout of 0:01:40

Open R-Liebert opened this issue 2 years ago • 8 comments

Describe the bug Using Azure OpenAI SK timesout during more complex requests to GPT-4. The suggestion is "Network timeout can be adjusted in ClientOptions.Retry.NetworkTimeout." Which is not an issue if you use OpenAI directly and can change the httpclient. But I can't find a way to change it in SK v1.5.

To Reproduce

  1. Initialize the kernel and connect to Azure OpenAI the following way: 2.
using System.Reflection;
using Microsoft.SemanticKernel;

namespace ComplexTask1;

class Program
{
    // The main entry point for the program
    static void Main(string[] args)
    {
        // Run the asynchronous method and wait for it to complete
        RunAsync().GetAwaiter().GetResult();
    }

    // Asynchronous method to run the program
    public static async Task RunAsync()
    {
        // Define the API key for the Azure OpenAI service
        string apiKey = "<AZURE-API-KEY>";
        // Define the name of the deployment for the Azure OpenAI service
        string chatDeploymentName = "gpt-4-1106-Preview";
        // Define the endpoint for the Azure OpenAI service
        string endpoint = "AZURE-ENDPOINT";

        // Create a new builder for the kernel
        var builder = Kernel.CreateBuilder();

        // Add the Azure OpenAI Chat Completion service to the builder
        builder.Services.AddAzureOpenAIChatCompletion(
            chatDeploymentName,   // The name of your deployment (e.g., "gpt-4-1106-Preview")
            endpoint,             // The endpoint of your Azure OpenAI service
            apiKey                // The API key of your Azure OpenAI service 
        );

        // Build the kernel
        var kernel = builder.Build();
  1. Ask the LLM to solve a complex task. I created a function from prompt. But this shouldn't make a difference.
  2. Wait for 100 seconds. If the task was complex and GPT-4 was slow enough
  3. See error

Expected behavior I was trying to extend the time the following way, but it's not the correct method anymore.

var clientOptions = new OpenAIClientOptions(); clientOptions.Retry.MaxRetries = 1; clientOptions.Retry.NetworkTimeout = TimeSpan.FromMinutes(4);

var openAIClient = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(apiKey), clientOptions);

var builder = new KernelBuilder(); builder.WithAzureChatCompletionService(chatDeploymentName, openAIClient); var kernel = builder.Build();

Now I need a place in the OpenAI Azure SDK to change the client options.

Platform

  • OS: Windows
  • IDE: VS Code
  • Language: C#
  • Source: NuGet package version 1.5

Additional context I've seen different solutions to this issue for OpenAI, previously versions of SK, but none seems to cover Azure OpenAI chat completion in SK v.1.5. It's not an issue that is repeatable at every call, as the llm is faster some times, but it's often enough to require an increased timout time for robust behaviour.

R-Liebert avatar Mar 06 '24 15:03 R-Liebert

Here is my way to configure it: var httpClient = new HttpClient { Timeout = TimeSpan.FromMinutes(3), }; kernelBuilder.AddAzureOpenAIChatCompletion(deploymentName, _options.Endpoint, _options.Key, httpClient: httpClient);

hanhsia avatar Mar 08 '24 08:03 hanhsia

Thanks @hanhsia! I wasn't aware of that being an option. It looks like using serivces and dependecies injection is redundant and should be depreaciated in favor of this solution. As it has more options it would be preferable if this was the best practice used in the samples.

R-Liebert avatar Mar 08 '24 08:03 R-Liebert

@R-Liebert you're wrong, I'm not going to explain now how DI works so take a look at this example https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example40_DIContainer.cs then just add to it: collection.AddSingleton<HttpClient>(() => new HttpClient { Timeout = TimeSpan.FromMinutes(3) }); You also should configure dns timeout but this is explained in httpClient best practices in microsoft docs.

Krzysztof318 avatar Mar 08 '24 08:03 Krzysztof318

Ok, thanks for the link.

R-Liebert avatar Mar 08 '24 09:03 R-Liebert

This didn't solve the issue unfortunatly. I was just lucky with my requests and got a false sense of hope. Even with Timeout at 10 min I get ---> System.Threading.Tasks.TaskCanceledException: The operation was cancelled because it exceeded the configured timeout of 0:01:40. Network timeout can be adjusted in ClientOptions.Retry.NetworkTimeout.

R-Liebert avatar Mar 08 '24 09:03 R-Liebert

then use this way: var clientOptions = new OpenAIClientOptions(); clientOptions.Retry.MaxRetries = 2; clientOptions.Retry.NetworkTimeout = TimeSpan.FromMinutes(3); var openAIClient = new OpenAIClient(new Uri(_options.Endpoint), _options.Token, clientOptions);

kernelBuilder.AddAzureOpenAIChatCompletion(modelName, openAIClient);

hanhsia avatar Mar 12 '24 05:03 hanhsia

Thanks @hanhsia! This solved the issue. I've now confirmed a request with >2 min processing time with an succesful response. You're a real mvp!

R-Liebert avatar Mar 12 '24 13:03 R-Liebert

Link now goes nowhere.

legistek avatar May 09 '26 11:05 legistek