以 Google 搜尋建立基準

本頁說明如何使用 Google 搜尋 (採用公開網路資料) 建立模型回覆的基準。此外,還會說明搜尋建議,這些建議會納入您的回覆。

如要將模型連結至網路中的世界知識、各種主題或最新資訊,請使用「以 Google 搜尋建立基準」功能。

如要進一步瞭解 Vertex AI 中的模型基礎,請參閱基礎總覽

支援的模型

本節列出支援透過 Google 搜尋建立基準的模型。

支援的語言

如需支援的語言清單,請參閱「語言」一文。

請按照下列操作說明,以公開網路資料建立模型基準。

注意事項

  • 如要使用「以 Google 搜尋建立基準」功能,請務必啟用 Google 搜尋建議。詳情請參閱「使用 Google 搜尋建議」。

  • 如要獲得最佳結果,請將溫度設為 1.0。如要進一步瞭解如何設定這項設定,請參閱模型參考資料中的 Gemini API 請求主體

  • 使用 Google 搜尋做為基礎的查詢次數上限為每天一百萬次。如有其他疑問,請與Google Cloud 支援團隊聯絡。

控制台

如要在 Vertex AI Studio 中使用「以 Google 搜尋結果為依據」,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Vertex AI Studio」頁面。

    前往 Vertex AI Studio

  2. 按一下「任意形式」Freeform分頁標籤。
  3. 在側邊面板中,按一下「基礎模型回覆」切換鈕。
  4. 按一下「自訂」,然後將 Google 搜尋設為來源。
  5. 在文字方塊中輸入提示,然後按一下「提交」

現在,系統會根據 Google 搜尋結果生成提示回覆。

Python

安裝

pip install --upgrade google-genai

詳情請參閱 SDK 參考說明文件

設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True

from google import genai
from google.genai.types import (
    GenerateContentConfig,
    GoogleSearch,
    HttpOptions,
    Tool,
)

client = genai.Client(http_options=HttpOptions(api_version="v1"))

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="When is the next total solar eclipse in the United States?",
    config=GenerateContentConfig(
        tools=[
            # Use Google Search Tool
            Tool(google_search=GoogleSearch())
        ],
    ),
)

print(response.text)
# Example response:
# 'The next total solar eclipse in the United States will occur on ...'

Go

瞭解如何安裝或更新 Go

詳情請參閱 SDK 參考說明文件

設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True

import (
	"context"
	"fmt"
	"io"

	genai "google.golang.org/genai"
)

// generateWithGoogleSearch shows how to generate text using Google Search.
func generateWithGoogleSearch(w io.Writer) error {
	ctx := context.Background()

	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
	})
	if err != nil {
		return fmt.Errorf("failed to create genai client: %w", err)
	}

	modelName := "gemini-2.0-flash-001"
	contents := []*genai.Content{
		{Parts: []*genai.Part{
			{Text: "When is the next total solar eclipse in the United States?"},
		},
			Role: "user"},
	}
	config := &genai.GenerateContentConfig{
		Tools: []*genai.Tool{
			{GoogleSearch: &genai.GoogleSearch{}},
		},
	}

	resp, err := client.Models.GenerateContent(ctx, modelName, contents, config)
	if err != nil {
		return fmt.Errorf("failed to generate content: %w", err)
	}

	respText := resp.Text()

	fmt.Fprintln(w, respText)

	// Example response:
	// The next total solar eclipse in the United States will occur on March 30, 2033, but it will only ...

	return nil
}

REST

使用任何要求資料之前,請先替換以下項目:

  • LOCATION:處理要求的區域。
  • PROJECT_ID:您的專案 ID
  • MODEL_ID:多模態模型的模型 ID。
  • TEXT: 提示中要加入的文字指令。

HTTP 方法和網址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:generateContent

JSON 要求主體:

{
  "contents": [{
    "role": "user",
    "parts": [{
      "text": "TEXT"
    }]
  }],
  "tools": [{
    "googleSearch": {}
  }],
  "model": "projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID"
}

如要傳送要求,請展開以下其中一個選項:

您應該會收到如下的 JSON 回應:

{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "The weather in Chicago this weekend, will be partly cloudy. The temperature will be between 49°F (9°C) and 55°F (13°C) on Saturday and between 51°F (11°C) and 56°F (13°C) on Sunday. There is a slight chance of rain on both days.\n"
          }
        ]
      },
      "finishReason": "STOP",
      "groundingMetadata": {
        "webSearchQueries": [
          "weather in Chicago this weekend"
        ],
        "searchEntryPoint": {
          "renderedContent": "..."
        },
        "groundingChunks": [
          {
            "web": {
              "uri": "https://www.google.com/search?q=weather+in+Chicago,+IL",
              "title": "Weather information for locality: Chicago, administrative_area: IL",
              "domain": "google.com"
            }
          },
          {
            "web": {
              "uri": "...",
              "title": "weatherbug.com",
              "domain": "weatherbug.com"
            }
          }
        ],
        "groundingSupports": [
          {
            "segment": {
              "startIndex": 85,
              "endIndex": 214,
              "text": "The temperature will be between 49°F (9°C) and 55°F (13°C) on Saturday and between 51°F (11°C) and 56°F (13°C) on Sunday."
            },
            "groundingChunkIndices": [
              0
            ],
            "confidenceScores": [
              0.8662828
            ]
          },
          {
            "segment": {
              "startIndex": 215,
              "endIndex": 261,
              "text": "There is a slight chance of rain on both days."
            },
            "groundingChunkIndices": [
              1,
              0
            ],
            "confidenceScores": [
              0.62836814,
              0.6488607
            ]
          }
        ],
        "retrievalMetadata": {}
      }
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 10,
    "candidatesTokenCount": 98,
    "totalTokenCount": 108,
    "trafficType": "ON_DEMAND",
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 10
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 98
      }
    ]
  },
  "modelVersion": "gemini-2.0-flash",
  "createTime": "2025-05-19T14:42:55.000643Z",
  "responseId": "b0MraIMFoqnf-Q-D66G4BQ"
}

瞭解回覆內容

如果模型提示成功從 Vertex AI Studio 或 API 連結至 Google 搜尋,回覆內容就會包含中繼資料和來源連結 (網址)。不過,這類中繼資料可能因多種原因而未提供,導致提示回覆內容未根據中繼資料生成。這些原因包括來源關聯性偏低,或是模型回覆中的資訊不完整。

連結支援

建議顯示基礎支援,因為這有助於驗證發布者的回應,並提供進一步學習的管道。

Google 搜尋來源的回覆基準支援應以內嵌和彙整形式顯示。舉例來說,請參考下圖的建議做法。

建立基準支援範例

使用其他搜尋引擎選項

使用「以 Google 搜尋建立基準」功能時,客戶應用程式可以:

  • 提供其他搜尋引擎選項,
  • 將其他搜尋引擎設為預設選項,
  • 顯示自有或第三方搜尋建議或搜尋結果,但須符合下列條件:非 Google 結果必須與 Google 的基礎結果和搜尋建議分開顯示,且顯示方式不得誤導使用者,或暗示結果來自 Google。

優點

使用 Google 搜尋做為工具時,可以執行下列需要規劃、推理和思考的複雜提示和工作流程:

  • 你可以提供基礎資訊,確保回覆內容是根據最新且最準確的資訊生成。
  • 您可以從網路上擷取構件進行分析。
  • 你可以尋找相關圖片、影片或其他媒體,輔助多模態推理或生成工作。
  • 您可以執行編碼、技術疑難排解和其他專業工作。
  • 你可以尋找特定區域的資訊,或協助準確翻譯內容。
  • 你可以找到相關網站並瀏覽。

使用 Google 搜尋建議

使用「以 Google 搜尋建立基準」功能時,如果回覆中包含搜尋建議,您必須在正式版和應用程式中顯示搜尋建議。

如要進一步瞭解「以 Google 搜尋建立基準」功能,請參閱這篇文章

具體來說,您必須顯示基礎回應中繼資料中包含的搜尋查詢。回覆內容包括:

  • "content":LLM 生成的回覆。
  • "webSearchQueries":用於搜尋建議的查詢。

舉例來說,在下列程式碼片段中,Gemini 會回覆以搜尋結果為依據的提示,該提示詢問的是某種熱帶植物。

"predictions": [
  {
    "content": "Monstera is a type of vine that thrives in bright indirect light…",
    "groundingMetadata": {
      "webSearchQueries": ["What's a monstera?"],
    }
  }
]

您可以擷取這項輸出內容,並使用搜尋建議顯示。

搜尋建議的規定

建議的規定如下:

規定 說明
建議的宣傳訊息
  • 在符合顯示規定的前提下,系統會直接顯示搜尋建議,不會進行任何變更。
  • 與搜尋建議互動時,系統會直接將您帶往搜尋結果網頁 (SRP)。
錯誤做法
  • 包括使用者輕觸和顯示搜尋結果網頁之間的任何畫面或額外步驟。
  • 在搜尋建議或相關的 LLM 回覆旁,顯示任何其他搜尋結果或建議。

多媒體廣告規定

顯示規定如下:

  • 請務必按照提供的內容顯示搜尋建議,且不得修改顏色、字型或外觀。請確保搜尋建議的顯示方式符合下列模擬畫面,例如淺色和深色模式:

  • 顯示以搜尋結果為依據的回覆時,對應的搜尋建議應會持續顯示。
  • 如要使用品牌宣傳,請務必嚴格遵守「歡迎使用品牌資源中心」中,Google 品牌特徵第三方使用指南的規定。
  • 使用「以 Google 搜尋建立基準」功能時,系統會顯示搜尋建議方塊。包含建議方塊的欄位寬度必須與 LLM 的有依據回覆相同。

輕觸時的行為

使用者輕觸動態磚後,系統會直接將他們帶往動態磚中顯示的搜尋字詞搜尋結果網頁。搜尋結果頁面可以在應用程式內建瀏覽器或獨立瀏覽器應用程式中開啟。請勿以任何方式縮小、移除或遮蔽搜尋結果網頁的顯示畫面。以下動畫模擬圖說明輕觸後顯示搜尋結果網頁的互動。

應用程式/電腦版範例

導入搜尋建議的程式碼

使用 API 根據搜尋結果生成回覆時,模型回覆會在 renderedContent 欄位中提供符合規範的 HTML 和 CSS 樣式,您可實作這些樣式,在應用程式中顯示搜尋建議。如要查看 API 回應範例,請參閱「透過搜尋功能進行基礎訓練」中的回應部分。

後續步驟