-
Notifications
You must be signed in to change notification settings - Fork 191
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
TypeError: fetch failed #29
Comments
got same errorw
output:
|
same error, how to fix it? |
我知道了,有这个错误的应该都是在中国吧。谷歌代码能力不行fetch没用到代理。
|
Try replacing:
with:
|
I've copy-pasted the entire sample code as provided in the original post, inserted my API key, and run it using This seems odd because the According to nodejs/undici#1248 there should be a For anyone who wants to make small modifications to the node_modules code themselves that you think will fix it, there's always https://www.npmjs.com/package/patch-package. |
@objectIsNotDefined Please revoke your API key. I edited the comment, but since it's leaked you need to invalidate it. @iProgramme @lzghades @objectIsNotDefined from which region you are trying to access Gemini API? Not important, but if you are running async function from the main module, then you should handle Promise rejection e.g., like that:
|
试了一下只能改sdk里的indexjs了,先安装npm i undici,然后修改代码 async function makeRequest(url, body) { |
@xuzhiyang @lzghades |
got the same error. I use it in China,and I still got the same error when using VPN to proxy to Taiwan or Japan |
The VPN software I am using is Clash Verge, and I have selected a Japanese node. I have tried enabling the tun mode in the terminal once, and also attempted to directly activate the tun mode using Clash Verge (reference link: Clash Verge教程). However, I still encounter the same error |
what do you mean by |
So, I reported that error in China because the Chinese region does not support accessing Google. Is it possible to use VPN and enable tun mode to be effective? |
我用中文再重复一遍吧,对于墙一类的问题,在任何repo提都不合适,因为他们什么也做不了解决不了。而你要做的是使用合理的代理工具和代理模式。正如我上面所言,很多程序和应用并不走系统代理,所以一般的proxy对于他们并不生效,这里也是这样一种情况。tun模式和路由代理是常见的更底层的透明代理,可以用于解决这类情况,具体怎么配置,碰到哪些环境问题,怎么调试等等,远远超出我几个字能解释的范围,所以建议你去别处搜索和学习。 |
楼主问题还没解决?下面是解决方法 随便跟你说下openai的 PS: 记得在控制台设置https_proxy变量 |
我成功了,如下图。 完整代码如下 const { GoogleGenerativeAI } = require("@google/generative-ai");
const { setGlobalDispatcher, ProxyAgent } = require("undici");
const dispatcher = new ProxyAgent({ uri: new URL('http://127.0.0.1:7890').toString() });
setGlobalDispatcher(dispatcher);
// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI('xxxxxxxxxxxxx');
async function run() {
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const prompt = "Write a story about a magic backpack."
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
}
run(); |
In fact, the fetchOptions parameter is currently not initialized with parameters other than timeout |
I was facing the same issue. I switched to node version |
this repo support the proxy feature: https://github.com/wujohns/google-generative-ai |
My solution to not breaking the code structure is: const http = require('http')
const httpProxy = require('http-proxy')
const { HttpsProxyAgent } = require('https-proxy-agent')
const proxy = httpProxy.createProxyServer()
const agent = new HttpsProxyAgent('http://127.0.0.1:7890')
const server = http.createServer(function (req, res) {
// req.headers.host = 'api.openai.com'
req.headers.host = 'generativelanguage.googleapis.com'
proxy.web(req, res, {
target: 'https://generativelanguage.googleapis.com', // google
// target: 'https://api.openai.com/v1', // openai
agent,
})
proxy.on('error', function (err) {
console.log(err)
})
})
server.listen(8080, '0.0.0.0') Then use it in the code: const { GoogleGenerativeAI } = require("@google/generative-ai");
// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI('xxxxxxxxxxxxxxxx');
async function run() {
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: "gemini-pro" }, { baseUrl:'http://127.0.0.1:8080' }); // setup baseUrl
const prompt = "Write a story about a magic backpack."
const chat = await model.startChat('');
const result = await chat.sendMessageStream(prompt);
let text = ''
for await (const chunk of result.stream) {
text += chunk.text();
}
console.log(text);
}
run(); |
Actual Behavior
Steps to Reproduce the Problem
Specifications
The text was updated successfully, but these errors were encountered: