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 相容介面只回傳回答,不回傳來源。
- 搜尋按次計費,單價請見模型頁 。
其他錯誤請見常見錯誤。
最後更新於