-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example41_HttpClientUsage it is invalid #1445
Comments
I think the |
How can I implement a custom httpclient |
I guess you want to use some proxy openai services? If so, here are some of my suggestions.
serviceCollection.AddTransient<MyHttpMessageHandler>();
//Registration of a named HttpClient.
serviceCollection.AddHttpClient("test-client")
.ConfigurePrimaryHttpMessageHandler<MyHttpMessageHandler>();
serviceCollection.AddTransient<IKernel>((sp) =>
{
var factory = sp.GetRequiredService<IHttpClientFactory>();
var kernel = Kernel.Builder
.WithOpenAIChatCompletionService("<model-id>", "<api-key>", httpClient: factory.CreateClient("test-client"))
.Build();
return kernel;
});
private class MyHttpMessageHandler : HttpClientHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.RequestUri != null && request.RequestUri.Host.Equals("api.openai.com", StringComparison.OrdinalIgnoreCase))
{
request.RequestUri = new Uri($"https://openai.yourproxy.com{request.RequestUri.PathAndQuery}");
}
return base.SendAsync(request, cancellationToken);
}
} |
Thank you very much. It was a success |
This can aslo be done with some Reflection magic😉 like this (pseudocode) dynamic c = new OpenAIChatCompletion(...); //find a way to get or create this
c.Client._endpoint = new Uri(yourEndpoint);
c.Client._tokenCredential = DelegatedTokenCredential.Create(...); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
A clear and concise description of what the bug is.
To Reproduce
Steps to reproduce the behavior:
`
Example41_HttpClientUsage.Run();
Console.Read();
OpenAIClientBase
this.Client = new OpenAIClient(apiKey, options);
`
This will explain that the previously modified httpclient has been overwritten again
No matter how I modify it continues to work
The text was updated successfully, but these errors were encountered: