Actual Behavior
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async makeRequest (/Users/yubowen/Desktop/MyPrivate/node_modules/@google/generative-ai/dist/index.js:195:20)
at async generateContentStream (/Users/yubowen/Desktop/MyPrivate/node_modules/@google/generative-ai/dist/index.js:519:22)
Node.js v18.18.0
Steps to Reproduce the Problem
- npm install @google/generative-ai
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" });
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();
- node index.js
Specifications
楼主问题还没解决?下面是解决方法 google api:
const { setGlobalDispatcher, ProxyAgent } = require("undici");
const dispatcher = new ProxyAgent({ uri: new URL(process.env.https_proxy).toString() });
//全局fetch调用启用代理
setGlobalDispatcher(dispatcher);
随便跟你说下openai的
const { HttpsProxyAgent } = require('https-proxy-agent');
const openai = new OpenAI({
apiKey: XXXXXXXXX,
httpAgent: new HttpsProxyAgent(process.env.https_proxy) // 设置代理
});
PS: 记得在控制台设置https_proxy变量
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890
@xuzhiyang
试了一下只能改sdk里的indexjs了,先安装npm i undici,然后修改代码 const { ProxyAgent } = require(‘undici’);
async function makeRequest(url, body) { let response; try { const proxyHost = ‘xxx’; const proxyPort = xxx; const proxyUrl =
http://${proxyHost}:${proxyPort}
; const client = new ProxyAgent(proxyUrl); response = await fetch(url.toString(), { method: “POST”, headers: { “Content-Type”: “application/json”, “x-goog-api-client”: getClientHeaders(), “x-goog-api-key”: url.apiKey, }, body, dispatcher: client });我用中文再重复一遍吧,对于墙一类的问题,在任何repo提都不合适,因为他们什么也做不了解决不了。而你要做的是使用合理的代理工具和代理模式。正如我上面所言,很多程序和应用并不走系统代理,所以一般的proxy对于他们并不生效,这里也是这样一种情况。tun模式和路由代理是常见的更底层的透明代理,可以用于解决这类情况,具体怎么配置,碰到哪些环境问题,怎么调试等等,远远超出我几个字能解释的范围,所以建议你去别处搜索和学习。
@xuzhiyang @lzghades There are a lot of programs that do not use the system proxy (such as node fetch). In such cases, you need to use tun mode or deploy a proxy on your router. And it is also common to patch node_modules with tools like patch-package, yarn patch, or pnpm patch
我知道了,有这个错误的应该都是在中国吧。谷歌代码能力不行fetch没用到代理。 也就是说虽然电脑科学上网了,但fetch科学不了。 虽然知道这个问题,但不知道怎么解决,总不能去修改“node_modules/@google/generative-ai/dist/index.js:195”这文件吧,npm install就被覆盖了。 就是要解决GoogleGenerativeAI使用HttpsProxyAgent方法问题。谁解决了告诉我一声xuzhiyang@gmail.com谢了。
Hello, GoogleGenerativeAI module how to use HttpsProxyAgent? thx.