内容生成参数

本页介绍了您可以在对模型的请求中设置的可选抽样参数。不同模型的可用参数可能有所不同。如需了解详情,请参阅参考文档

令牌采样参数

Top-P

Top-P 可更改模型选择输出词元的方式。系统会按照概率从最高到最低的顺序选择词元,直到所选词元的概率总和等于 top-P 的值。例如,如果词元 A、B 和 C 的概率分别为 0.3、0.2 和 0.1,并且 top-P 值为 0.5,则模型将选择 A 或 B 作为下一个词元(通过温度确定),并会排除 C,将其作为候选词元。

指定较低的值可获得随机程度较低的回答,指定较高的值可获得随机程度较高的回答。

如需了解详情,请参阅 topP

温度

温度 (temperature) 在生成回复期间用于采样,在应用 topPtopK 时会生成回复。温度可以控制词元选择的随机性。 较低的温度有利于需要更少开放性或创造性回复的提示,而较高的温度可以带来更具多样性或创造性的结果。温度为 0 表示始终选择概率最高的词元。在这种情况下,给定提示的回复大多是确定的,但可能仍然有少量变化。

如果模型返回的回答过于笼统、过于简短,或者模型给出后备回答,请尝试提高温度。

温度越低,结果越可预测(但并非完全确定)。如需了解详情,请参阅 temperature

停止参数

输出词元数上限

设置 maxOutputTokens 以限制响应中生成的令牌数量。一个词元约为 4 个字符,因此 100 个词元对应大约 60-80 个单词。设置较低的值可限制响应的长度。

停止序列

stopSequences 中定义字符串,以告知模型在响应中遇到其中一个字符串时,停止生成文本。如果某个字符串在响应中多次出现,则响应会在首次出现该字符串的位置截断。字符串区分大小写。

令牌处罚参数

频次惩罚

正值会惩罚生成的文本中反复出现的词元,从而降低重复内容概率。最小值为 -2.0。最大值为 2.0,但不包括该数值。如需了解详情,请参阅 frequencyPenalty

在家/外出状态惩罚

正值会惩罚已生成文本中已存在的词元,从而增加生成更多样化内容的概率。最小值为 -2.0。最大值为 2.0,但不包括该数值。如需了解详情,请参阅 presencePenalty

高级参数

您可以使用这些参数在响应中返回有关令牌的更多信息,或控制响应的可变性。

输出令牌的对数概率

返回每个生成步骤中排名靠前的候选 token 的对数概率。模型的所选 token 可能与每个步骤中排名靠前的候选 token 不同。使用介于 120 范围内的整数值指定要返回的候选项数量。如需了解详情,请参阅 logprobs。您还需要将 responseLogprobs 参数设为 true,才能使用此功能。

responseLogprobs 参数会返回模型在每个步骤中选择的 token 的对数概率。

如需了解详情,请参阅 Logprobs 简介笔记本。

种子

当种子固定为特定值时,模型会尽最大努力为重复请求提供相同的回答。无法保证确定性输出。此外,更改模型或参数设置(例如温度)可能会导致回答发生变化,即使您使用相同的种子值也是如此。默认情况下,系统会使用随机种子值。 如需了解详情,请参阅 seed

示例

以下示例展示了如何使用参数来调整模型的回答。

Gen AI SDK for Python

安装

pip install --upgrade google-genai

如需了解详情,请参阅 SDK 参考文档

设置环境变量以将 Gen AI SDK 与 Vertex AI 搭配使用:

# 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 参考文档

设置环境变量以将 Gen AI SDK 与 Vertex AI 搭配使用:

# 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
}

后续步骤