终于蹲到了!早就写好了这个脚本一直用不上。
原理贴:🌋 Gemini 2.0 Flash 已免费支持 Google 联网搜索!API 也支持!!
和 GCP Vertex AI 一样,但 GCP 要 $35/1000次! 无法普及,且走 Vertex AI 渠道的用户群远不如 Google AI Studio 的多。现在 Gemini 2.0 Flash 免费量大管饱 Google 联网搜索集成终于可以飞入寻常百姓家了!
直接上代码:
// 保存原始的 fetch 函数
const originalFetch = window.fetch;
// 重写 fetch 函数
window.fetch = async function(...args) {
const [url, options] = args;
// 检查 URL 是否以 'gemini-2.0-flash-exp' 结尾
if (url.endsWith('gemini-2.0-flash-exp:streamGenerateContent')) {
if (options?.method === 'POST') {
try {
// 获取请求体
const body = JSON.parse(options.body);
// 添加 tools 参数
body.tools = [{
"googleSearch": {}
}];
// 更新请求参数
options.body = JSON.stringify(body);
} catch (e) {
console.log('解析请求体失败:', e);
}
}
}
// 调用原始的 fetch 函数
return originalFetch.apply(this, args);
};
// 监听发送按钮点击
const sendButton = document.querySelector('button.button_icon-button__VwAMf.chat_chat-input-send__GFQZo');
if (sendButton) {
console.log('已设置请求拦截器');
}
使用方法:
- 先打开 NextChat 聊天界面
- 摁一下 F12,在「控制台」粘贴上述函数,回车
- 看到显示 已设置请求拦截器 即可
效果:
注意事项:
- 需要使用
gemini-2.0-flash-exp
模型,和 AI Stuido 名称一致。- 刷新页面失效,要用就重复上述步骤。
- 如果你用搜索很频繁,可以丢到油猴里保持生效。
此外,还有两种更便捷但 有门槛 的方法:
- 把脚本写到中转的 Cloudflare Worker 里。 用别人的中转也可以。
- 直接魔改 NextChat 客户端。
但 F12 的方式有手就行,所以,就这样。
最后,硅!基!流!动!点击图片登陆你我各得 14 元! AFF
在 中转 塞参数的方式已有两个佬写好:
Uni-API:[开源] (支持o1,道德审查)开发了一个适合宝宝体质的 API 转发器 uni-api,目前已经 200+ star - #597,来自 fangyuan99
huggingface部署gemini代理:https://linux.do/t/topic/292029
↑ 上面是 Gemini 原始格式的,用 New API 的 OpenAI 格式的话可以用这位佬的
// 保存原始的 fetch 函数
const originalFetch = window.fetch;
// 重写 fetch 函数
window.fetch = async function(...args) {
// 如果是 POST 请求
if (args[1]?.method === 'POST') {
try {
// 获取请求体
const body = JSON.parse(args[1].body);
// 如果包含 messages 数组,说明是目标请求
if (Array.isArray(body.messages) && body.model.toLowerCase().includes("gemini-2.0-flash-exp")) {
// 添加 tools 参数
body.tools = [
{
"type": "function",
"function": {
"name": "googleSearch"
}
}]
// 更新请求参数
args[1].body = JSON.stringify(body);
}
} catch (e) {
console.log('解析请求体失败:', e);
}
}
// 调用原始的 fetch 函数
return originalFetch.apply(this, args);
};
// 监听发送按钮点击
const sendButton = document.querySelector('button.button_icon-button__VwAMf.chat_chat-input-send__GFQZo');
if (sendButton) {
console.log('已设置请求拦截器');