langchainjs
langchainjs copied to clipboard
How to set proxy for ChatGoogleGenerativeAI in LangChain JavaScript
OpenAI can set proxy through configuration.httpAgent, but I can't find where to set proxy for ChatGoogleGenerativeAI
🤖
Hey there, @huotu! I'm Dosu, a friendly bot here to lend a hand while we're waiting for a human maintainer to get back to you. I'm all set to help you squash bugs, answer your questions, and navigate the path to becoming a contributor. So don't hesitate to ask if you need anything. Let's make the most of our time together!
Based on the similar issues I found in the langchainjs repository, it seems that setting a proxy for ChatGoogleGenerativeAI
might not be directly supported in the same way as it is for ChatOpenAI
. However, there are a few workarounds you could try.
One solution suggested in the issue lanchain requested chatgpt twice, but the second time didn’t use the proxy. is to create a new ChatOpenAI
instance for each request. This ensures that the proxy configuration is used for all requests. Here's an example:
const chat = new ChatOpenAI(
{ temperature: 0, openAIApiKey: env.OPENAI_KEY },
{ httpAgent: new ProxyAgent() }
);
try {
const response = await chat.call([
new HumanChatMessage('Translate this sentence from English to French. I love programming.')
]);
console.log(response);
} catch (e) {
console.log(e);
}
Another solution suggested in the issue How to configure a forward proxy is to modify the basePath
in the ConfigurationParameters
for openai
when creating a new ChatOpenAI
instance. This allows you to route all requests to the OpenAI APIs through your proxy server. Here's an example:
const chat = new ChatOpenAI(
{ temperature: 0, openAIApiKey: env.OPENAI_KEY },
{ basePath: 'https://proxy.your-server.com/v1' }
);
try {
const response = await chat.call([
new HumanChatMessage('Translate this sentence from English to French. I love programming.')
]);
console.log(response);
} catch (e) {
console.log(e);
}
In this example, https://proxy.your-server.com/v1
is the URL of your forward proxy server. You should replace it with the URL of your own server.
Please note that these solutions assume that your proxy server is already set up to forward requests to the OpenAI APIs. If you need help setting up a forward proxy server, I recommend referring to the documentation of the proxy server software you are using.
If these solutions don't work for you, it could be a bug in the LangChain framework. In that case, I recommend creating a new issue in the LangChain repository detailing the problem and providing the logs and code you've shared here. The maintainers of the repository should be able to help diagnose and fix the issue.
Also, consider updating LangChain to the latest version (Release 0.0.209) as the issue might have been fixed in a more recent release.
I hope this helps! If you have any other questions, feel free to ask.
Best, Dosu
Sources
- lanchain requested chatgpt twice, but the second time didn’t use the proxy.
- How to configure a forward proxy
- Streaming does not work if run behind a proxy server
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
Same problem, waiting for solution
For ChatGoogleGenerativeAI, try this way:
import { setGlobalDispatcher, ProxyAgent } from 'undici';
const proxyAgent = new ProxyAgent({
uri: 'http://172.16.5.147:30010'
});
setGlobalDispatcher(proxyAgent);