Gemini Google 搜索
本页说明如何为 Gemini 开启 Google 搜索并读取来源。模型 ID 与接口地址见 Gemini 系列概览。
开启 Google 搜索后,模型会联网检索并基于搜索结果回答,适合查询时效性信息。
Python(google-genai)
gemini_grounding.py
import os
from google import genai
from google.genai import types
client = genai.Client(
api_key=os.environ["OFOX_API_KEY"],
http_options={"api_version": "v1beta", "base_url": "https://api.ofox.ai/gemini"},
)
resp = client.models.generate_content(
model="google/gemini-3.5-flash",
contents="Python 最新的稳定版本是多少?",
config=types.GenerateContentConfig(tools=[types.Tool(google_search=types.GoogleSearch())]),
)
print(resp.text)
meta = resp.candidates[0].grounding_metadata
print("搜索词:", meta.web_search_queries)
for chunk in meta.grounding_chunks or []:
print("来源:", chunk.web.title, chunk.web.uri)- 原生接口的响应中,
groundingMetadata包含搜索词(webSearchQueries)与来源网页(groundingChunks)。 - OpenAI 兼容接口只返回回答,不返回来源。
- 搜索按次计费,单价见模型页 。
其他报错见常见报错。
最后更新于