1

I am using the OpenAI library in my c# project, but I get the following error if it does not receive a response for more than 100 seconds. I cannot add a custom httpclient element. how can I solve this problem. Thanks in advance.

‘system Threading Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing,‘

The library I use: https://github.com/OkGoDoIt/OpenAI-API-dotnet

my code:

   OpenAIAPI api = new OpenAIAPI(apiKey);
                var result = await api.Chat.CreateChatCompletionAsync(new ChatRequest()
                {
                    Model = Model.ChatGPTTurbo,
                    Temperature = 0.5,
                    Messages = new ChatMessage[]
                    {
            new ChatMessage(ChatMessageRole.System, ""),
            new ChatMessage(ChatMessageRole.User, prompt)
                    }
                });
Ahrika
  • 11
  • 3

1 Answers1

0

Solution for those who have other problems:

using System.Net.Http;

public class CustomHttpClientFactory : IHttpClientFactory
{
    public HttpClient CreateClient(string name)
    {
        var httpClient = new HttpClient();
        httpClient.Timeout = TimeSpan.FromSeconds(200);

        return httpClient;
    }
}

OpenAIAPI api = new OpenAIAPI(apiKey);  
api.HttpClientFactory = new CustomHttpClientFactory();
var result = await api.Chat.CreateChatCompletionAsync(new ChatRequest()
{
    Model = Model.ChatGPTTurbo,
    Temperature = 0.5,
    Messages = new ChatMessage[]
{
    new ChatMessage(ChatMessageRole.System, ""),
    new ChatMessage(ChatMessageRole.User, prompt)
}
});
Ahrika
  • 11
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 18 '23 at 06:22