Skip to content
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

Closed
fanheshe opened this issue Jun 13, 2023 · 5 comments
Closed

Example41_HttpClientUsage it is invalid #1445

fanheshe opened this issue Jun 13, 2023 · 5 comments
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code

Comments

@fanheshe
Copy link

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

image

No matter how I modify it continues to work

@JadynWong
Copy link
Contributor

JadynWong commented Jun 13, 2023

I think the BaseAddress may not affect the request. The completed URI path is already included in the constructed HttpRequestMessage.

https://github.com/Azure/azure-sdk-for-net/blob/c6253cc7c7d1d9d9e69a5983b10491b3960ef29a/sdk/openai/Azure.AI.OpenAI/src/Custom/OpenAIClient.cs#L576-L609

@alexchaomander alexchaomander added bug Something isn't working .NET Issue or Pull requests regarding .NET code labels Jun 13, 2023
@fanheshe
Copy link
Author

How can I implement a custom httpclient

@JadynWong
Copy link
Contributor

JadynWong commented Jun 14, 2023

I guess you want to use some proxy openai services? If so, here are some of my suggestions.

  1. Implement your own ITextCompletion or IChatCompletion. Example16_CustomLLM.cs
  2. Go to the https://github.com/Azure/azure-sdk-for-net repository to raise this issue and inquire if the Azure team will support these proxy services from OpenAI. If they do support this, I think it should be easy. (It's better)
  3. A quick temporary solution with HttpClientHandler
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);
    }
}

@fanheshe
Copy link
Author

Thank you very much. It was a success

@UlyssesWu
Copy link

UlyssesWu commented Jun 24, 2023

  1. Go to the https://github.com/Azure/azure-sdk-for-net repository to raise this issue and inquire if the Azure team will support these proxy services from OpenAI. If they do support this, I think it should be easy. (It's better)

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
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code
Projects
None yet
Development

No branches or pull requests

5 participants