gpt4 book ai didi

c# - 我在 C# OpenAI 库中收到 HttpClient.Timeout 错误

转载 作者:行者123 更新时间:2023-12-02 22:47:16 24 4
gpt4 key购买 nike

我在我的 c# 项目中使用 OpenAI 库,但如果超过 100 秒没有收到响应,我会收到以下错误。我无法添加自定义 httpclient 元素。我怎么解决这个问题。提前致谢。

‘系统线程任务.TaskCanceledException: 请求是由于配置的 HttpClient.Timeout 为 100 秒而取消过去,“

我使用的库:https://github.com/OkGoDoIt/OpenAI-API-dotnet

我的代码:

   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)
}
});

最佳答案

针对其他问题的解决方案:

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)
}
});

关于c# - 我在 C# OpenAI 库中收到 HttpClient.Timeout 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76491056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com