如何使用google/genai库关闭Gemini网页搜索Grounding功能?
关闭Gemini 2.0 Flash网页搜索Grounding功能的正确方法
你之前通过tools: []或省略config参数的方式无法关闭网页搜索,是因为Gemini 2.0 Flash的网页搜索(Grounding)功能由专门的groundingConfig参数控制,而非tools配置项。
正确代码示例
使用google/genai v0.13.0时,只需在generateContent的参数中显式添加groundingConfig: { enabled: false }即可禁用网页搜索:
const { GoogleGenAI } = require('@google/genai'); const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); async function main() { const response = await ai.models.generateContent({ model: "gemini-2.0-flash", contents: "you can search the web? get the current moon phase", groundingConfig: { enabled: false } }); console.log(response.text); } main();
补充说明
- Gemini 2.0系列模型默认开启网页搜索功能,必须显式通过
groundingConfig.enabled = false来禁用。 tools参数用于配置函数调用等工具能力,与网页搜索的控制逻辑无关,因此设置tools: []不会影响搜索功能的开关。
内容的提问来源于stack exchange,提问作者Chris Stryczynski
相关产品推荐
相关产品推荐

