內容產生參數

本頁面會顯示您可以在模型要求中設定的選用取樣參數。不同模型可用的參數不盡相同。詳情請參閱參考說明文件

符記取樣參數

Top-P

「Top-P」會影響模型選取輸出符記的方式。模型會按照可能性最高到最低的順序選取符記,直到所選符記的可能性總和等於 Top-P 值。舉例來說,假設詞元 A、B 和 C 的可能性分別為 0.3、0.2 和 0.1,而 Top-P 值為 0.5,模型會依據溫度參數選擇 A 或 B 做為下一個詞元,並排除 C 做為候選詞元。

如要取得較不隨機的回覆,請指定較低的值;如要取得較隨機的回覆,請調高此值。

詳情請參閱 topP

溫度參數

溫度會在應用 topPtopK 時,用於回覆產生期間的取樣。溫度參數會決定選取詞元時的隨機程度。如果您想藉由提示生成較不具開放性和創意性的回覆,建議調低溫度參數。另一方面,如果溫度參數較高,則可能產生較多樣化或有創意的結果。如果溫度參數為 0,系統一律會選取可能性最高的詞元。在這種情況下,系統會根據特定提示提供的回覆,但仍可能出現少量變化。

如果模型的回覆太普通、太短或提供了備用回覆,再試試看調高溫度參數。

溫度越低,結果就越可預測 (但並非完全決定論)。詳情請參閱 temperature

停止參數

輸出詞元數量上限

設定 maxOutputTokens,即可限制回應中產生的符記數量。一個符記約為四個字元,因此 100 個符記大約對應於 60 到 80 個字詞。設定較低的值,限制回應長度。

停止序列

stopSequences 中定義字串,如果回應中遇到其中一個字串,就會告知模型停止產生文字。如果字串在回應中出現多次,系統會在首次遇到該字串時截斷回應。字串會區分大小寫。

權杖懲罰參數

展示頻率懲罰

正值會對重複出現在生成文字中的符記處以懲罰,降低重複內容的機率。最小值為 -2.0。最大值為 2.0 (不含)。詳情請參閱 frequencyPenalty

在家狀態罰分

正值會對已出現在生成文字中的符記處以懲罰,提高產生更多元內容的機率。最小值為 -2.0。最大值為 2.0 (不含)。詳情請參閱 presencePenalty

進階參數

您可以使用這些參數,在回應中傳回更多權杖相關資訊,或控制回應的變化。

記錄輸出符記的機率

傳回每個產生步驟中,最優先候選符記的對數機率。模型選擇的符記可能與每個步驟的候選符記不同。使用 120 範圍內的整數值,指定要傳回的候選項目數量。詳情請參閱 logprobs。您也必須將 responseLogprobs 參數設為 true,才能使用這項功能。

responseLogprobs 參數會傳回模型在每個步驟中選擇的符記對應的對數機率。

詳情請參閱「Logprobs 簡介」的 Notebook。

種子

當種子固定為特定值時,模型會盡力為重複要求提供相同的回應。無法保證會產生確定性輸出內容。此外,即使使用相同的種子值,變更模型或參數設定 (例如溫度) 也會導致回覆有所差異。根據預設,系統會使用隨機種子值。詳情請參閱 seed

範例

以下是使用參數調整模型回應的範例。

Gen AI SDK for 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, HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Why is the sky blue?",
    # See the SDK documentation at
    # https://googleapis.github.io/python-genai/genai.html#genai.types.GenerateContentConfig
    config=GenerateContentConfig(
        temperature=0,
        candidate_count=1,
        response_mime_type="application/json",
        top_p=0.95,
        top_k=20,
        seed=5,
        max_output_tokens=500,
        stop_sequences=["STOP!"],
        presence_penalty=0.0,
        frequency_penalty=0.0,
    ),
)
print(response.text)
# Example response:
# {
#   "explanation": "The sky appears blue due to a phenomenon called Rayleigh scattering. When ...
# }

Gen AI SDK for Go

瞭解如何安裝或更新 Gen AI SDK for 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"
)

// generateWithConfig shows how to generate text using a text prompt and custom configuration.
func generateWithConfig(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.Text("Why is the sky blue?")
	// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.GenerateContentConfig
	config := &genai.GenerateContentConfig{
		Temperature:      genai.Ptr(0.0),
		CandidateCount:   genai.Ptr(int64(1)),
		ResponseMIMEType: "application/json",
	}

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

	respText, err := resp.Text()
	if err != nil {
		return fmt.Errorf("failed to convert model response to text: %w", err)
	}
	fmt.Fprintln(w, respText)
	// Example response:
	// {
	//   "explanation": "The sky is blue due to a phenomenon called Rayleigh scattering ...
	// }

	return nil
}

後續步驟