Replies: 2 comments 2 replies
-
AFAIK this opeanAI SDK is a wrapper of HttpClient using IHttpClientBuilder interface. In other words it is a typed HttpClient. The HttpClient is thread safe itself, so I can say that this SDK is thread safe. @kayhantolga, @BroMarduk Anyway I do not really understand why injected HttpClient is instantiated and disposed in this code? https://github.com/betalgo/openai/blob/master/OpenAI.SDK/Managers/OpenAIService.cs |
Beta Was this translation helpful? Give feedback.
-
I am using this package with Hangfire + custom-made "jobs". In a nutshell, it seems like it is thread-safe (no problems for the last year while we are prototyping). But yes, for other third-party calls, we are using But I don't use existing // Taken from OpenAI.GPT3.Extensions.OpenAIServiceCollectionExtensions.cs (to pass Timeout to HttpClient)
builder.Services.AddOptions<OpenAiOptions>()
.Configure(options =>
{
options.ApiKey = openaiKeySection.Value;
options.Organization = openaiOrganizationSection.Value;
options.DefaultModelId = defaultModelProvider.ModelId;
options.Validate();
});
var httpClientBuilder = builder.Services.AddHttpClient<IOpenAIService, OpenAIService>(options =>
{
options.Timeout = TimeSpan.FromMinutes(graceAISection.GetValue("RequestTimeoutMinutes", 5));
options.DefaultRequestHeaders.ConnectionClose = false;
});
httpClientBuilder.SetHandlerLifetime(TimeSpan.FromMinutes(5)); // default: 2 minutes
var rateLimitSection = graceAISection.GetSection("RateLimit");
var rpm = rateLimitSection.GetValue<double>("Chat:RPM");
var medianFirstRetryDelay = TimeSpan.FromSeconds(60 / rpm);
var retryCount = rateLimitSection.GetValue<int>("RetryCount");
if (retryCount > 0)
{
var retryPolicy = Policy.HandleResult<HttpResponseMessage>(response =>
{
if (response.StatusCode == HttpStatusCode.TooManyRequests)
{
// Log.Logger.InformationHere("{Message} {@Response}", "Too many requests", response);
return true;
}
return false;
})
.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay, retryCount));
httpClientBuilder.AddPolicyHandler(retryPolicy);
} |
Beta Was this translation helpful? Give feedback.
-
I was reading about multiple instances here, and I'm not sure if this is the answer to my question or not. I'd like to essentially for a
Parallel.ForEach
and have 4 threads running using a Scoped class I have (1 copy for each thread), and in said Scoped class I'm injectingIOpenAiService
using the regular olservices.AddOpenAIService();
. Is this OK to do? Does theIOpenAiService
work like a Singleton registration, or is it Scoped as well?Beta Was this translation helpful? Give feedback.
All reactions