Proxy Server Support? #148
-
In my region(mainland China), I cannot access the OPENAI API directly and must use a proxy server. Could we add support for proxy servers? Here's what the code looks like.
Of course, this is just the simplest scenario. What do you think about this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Additional support may be added in the future, but for now, I think you can achieve your goal with the current code. Check the constructor method where you can pass your HTTP client. Although it's a bit limited at the moment, it may solve your issue. Also, you can set the BaseDomain to use a custom domain. https://github.com/betalgo/openai/blob/master/OpenAI.SDK/Managers/OpenAIService.cs |
Beta Was this translation helpful? Give feedback.
-
Hello fellow, if you need to use proxy in dependency injection, you can refer to my code. var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOptions<OpenAiOptions>().Configure(settings => { settings.ApiKey = "YOUR_KEY"; });
var webProxy = new WebProxy(AppSettingsConstVars.ProxyUrl, false);
var proxyHttpClientHandler = new HttpClientHandler
{
Proxy = webProxy,
UseProxy = AppSettingsConstVars.ProxyUseProxy
};
builder.Services.AddHttpClient<IOpenAIService, OpenAIService>(option =>
{
option.Timeout = TimeSpan.FromSeconds(10);
}).ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler { Proxy = "YOUR_PROXY_URL", UseProxy = true };
}); |
Beta Was this translation helpful? Give feedback.
-
with the new version, you can use it like this serviceCollection.AddOpenAIService().ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler { Proxy = "YOUR_PROXY_URL", UseProxy = true };
}); |
Beta Was this translation helpful? Give feedback.
with the new version, you can use it like this