做思考状

思考模型经过训练后,会生成“思考过程”,并将其作为回答的一部分提供。因此,思考模型在回答中展现出的推理能力比同等的基础模型更强。

默认情况下,系统会启用“思考过程”。您可以使用 Vertex AI Studio 查看完整的思考过程以及模型生成的回答。

支持的模型

以下模型支持思考功能:

使用思考模型

如需在受支持的模型中使用思考功能,请执行以下操作:

控制台

  1. 依次打开 Vertex AI Studio > 创建提示
  2. 模型面板中,点击切换模型,然后从菜单中选择一个受支持的模型
    • (仅限 Gemini 2.5 Flash)模型加载时,思考预算默认设置为自动
  3. (可选)系统指令字段中,为模型提供一些详细的指令,指示模型应以何种格式生成回答。
  4. 撰写提示字段中输入提示。
  5. 点击 运行

Gemini 会在生成回答之后返回回答。生成回答可能需要几秒钟的时间,具体取决于回答的复杂程度。

(仅限 Gemini 2.5 Flash)如需关闭思考功能,请将思考预算设置为关闭

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

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents="solve x^2 + 4x + 4 = 0",
)
print(response.text)
# Example Response:
#     Okay, let's solve the quadratic equation x² + 4x + 4 = 0.
#
#     We can solve this equation by factoring, using the quadratic formula, or by recognizing it as a perfect square trinomial.
#
#     **Method 1: Factoring**
#
#     1.  We need two numbers that multiply to the constant term (4) and add up to the coefficient of the x term (4).
#     2.  The numbers 2 and 2 satisfy these conditions: 2 * 2 = 4 and 2 + 2 = 4.
#     3.  So, we can factor the quadratic as:
#         (x + 2)(x + 2) = 0
#         or
#         (x + 2)² = 0
#     4.  For the product to be zero, the factor must be zero:
#         x + 2 = 0
#     5.  Solve for x:
#         x = -2
#
#     **Method 2: Quadratic Formula**
#
#     The quadratic formula for an equation ax² + bx + c = 0 is:
#     x = [-b ± sqrt(b² - 4ac)] / (2a)
#
#     1.  In our equation x² + 4x + 4 = 0, we have a=1, b=4, and c=4.
#     2.  Substitute these values into the formula:
#         x = [-4 ± sqrt(4² - 4 * 1 * 4)] / (2 * 1)
#         x = [-4 ± sqrt(16 - 16)] / 2
#         x = [-4 ± sqrt(0)] / 2
#         x = [-4 ± 0] / 2
#         x = -4 / 2
#         x = -2
#
#     **Method 3: Perfect Square Trinomial**
#
#     1.  Notice that the expression x² + 4x + 4 fits the pattern of a perfect square trinomial: a² + 2ab + b², where a=x and b=2.
#     2.  We can rewrite the equation as:
#         (x + 2)² = 0
#     3.  Take the square root of both sides:
#         x + 2 = 0
#     4.  Solve for x:
#         x = -2
#
#     All methods lead to the same solution.
#
#     **Answer:**
#     The solution to the equation x² + 4x + 4 = 0 is x = -2. This is a repeated root (or a root with multiplicity 2).

查看思考总结

“思考总结”是模型在生成回答时所经历的思考过程的简略版输出。您可以在 Gemini 2.5 Flash 和 Gemini 2.5 Pro 中查看思考总结。如需查看思考总结,请执行以下操作:

控制台

Vertex AI Studio 中默认启用思考总结。您可以展开思考面板,查看模型的总结性思考过程。

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, ThinkingConfig

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents="solve x^2 + 4x + 4 = 0",
    config=GenerateContentConfig(
        thinking_config=ThinkingConfig(include_thoughts=True)
    ),
)

print(response.text)
# Example Response:
#     Okay, let's solve the quadratic equation x² + 4x + 4 = 0.
#     ...
#     **Answer:**
#     The solution to the equation x² + 4x + 4 = 0 is x = -2. This is a repeated root (or a root with multiplicity 2).

for part in response.candidates[0].content.parts:
    if part and part.thought:  # show thoughts
        print(part.text)
# Example Response:
#     **My Thought Process for Solving the Quadratic Equation**
#
#     Alright, let's break down this quadratic, x² + 4x + 4 = 0. First things first:
#     it's a quadratic; the x² term gives it away, and we know the general form is
#     ax² + bx + c = 0.
#
#     So, let's identify the coefficients: a = 1, b = 4, and c = 4. Now, what's the
#     most efficient path to the solution? My gut tells me to try factoring; it's
#     often the fastest route if it works. If that fails, I'll default to the quadratic
#     formula, which is foolproof. Completing the square? It's good for deriving the
#     formula or when factoring is difficult, but not usually my first choice for
#     direct solving, but it can't hurt to keep it as an option.
#
#     Factoring, then. I need to find two numbers that multiply to 'c' (4) and add
#     up to 'b' (4). Let's see... 1 and 4 don't work (add up to 5). 2 and 2? Bingo!
#     They multiply to 4 and add up to 4. This means I can rewrite the equation as
#     (x + 2)(x + 2) = 0, or more concisely, (x + 2)² = 0. Solving for x is now
#     trivial: x + 2 = 0, thus x = -2.
#
#     Okay, just to be absolutely certain, I'll run the quadratic formula just to
#     double-check. x = [-b ± √(b² - 4ac)] / 2a. Plugging in the values, x = [-4 ±
#     √(4² - 4 * 1 * 4)] / (2 * 1). That simplifies to x = [-4 ± √0] / 2. So, x =
#     -2 again – a repeated root. Nice.
#
#     Now, let's check via completing the square. Starting from the same equation,
#     (x² + 4x) = -4. Take half of the b-value (4/2 = 2), square it (2² = 4), and
#     add it to both sides, so x² + 4x + 4 = -4 + 4. Which simplifies into (x + 2)²
#     = 0. The square root on both sides gives us x + 2 = 0, therefore x = -2, as
#      expected.
#
#     Always, *always* confirm! Let's substitute x = -2 back into the original
#     equation: (-2)² + 4(-2) + 4 = 0. That's 4 - 8 + 4 = 0. It checks out.
#
#     Conclusion: the solution is x = -2. Confirmed.

接收思考签名

思考签名是模型内部思考过程的加密表示形式。启用思考和函数调用后,模型会在响应对象中返回思考签名。为确保模型在对话的多个回合中保持上下文,您必须在后续请求中返回思路签名。

在以下情况下,您会收到想法签名:

  • 已启用思考功能,并生成了想法。
  • 请求包含函数声明

以下示例展示了如何将思考与函数声明调用搭配使用:

Python

# Create user friendly response with function result and call the model again
# ...Create a function response part (No change)

# Append thought signatures, function call and result of the function execution to contents
function_call_content = response.candidates[0].content
# Append the model's function call message, which includes thought signatures
contents.append(function_call_content)
contents.append(types.Content(role="user", parts=[function_response_part])) # Append the function response

final_response = client.models.generate_content(
    model="gemini-2.5-flash",
    config=config,
    contents=contents,
)

print(final_response.text)
      

如需了解详情,请参阅函数调用页面。

使用函数调用时,还需考虑以下用量限制:

  • 签名由模型在回答的其他部分(例如函数调用、文本、文本或想法总结部分)中返回。在后续对话轮次中,将包含所有部分的完整回答返回给模型。
  • 签名不能串联在一起。
  • 签名以一系列部分的形式发送。您必须按相同的顺序返回这些部分。

控制思考预算

您可以控制模型在生成回答的过程中进行思考的程度。此上限指标称为“思考预算”,适用于模型的整个思考过程。默认情况下,模型会自动控制其思考程度不超过 8,192 个 token。

如果您需要比默认思考预算更多或更少的 token,则可以手动设置 token 数量上限。您可以为不太复杂的任务设置较低的 token 限额,为较复杂的任务设置较高的限额。

下表显示了您可以为每个受支持的模型设置的最低和最高 token 预算:

型号 最低 token 数额 最高 token 数额
Gemini 2.5 Flash 1 24,576
Gemini 2.5 Pro 128 32,768
Gemini 2.5 Flash-Lite 512 24,576

如果在使用 Gemini 2.5 Flash 和 Gemini 2.5 Flash-Lite 时将思考预算设置为 0,则思考功能会关闭。对于 Gemini 2.5 Pro,思考功能无法关闭。

如果您希望模型在使用 API 时控制思考预算,可以将思考预算设置为 -1

控制台

  1. 依次打开 Vertex AI Studio > 创建提示
  2. 模型面板中,点击切换模型,然后从菜单中选择一个受支持的模型
  3. 思考预算下拉选择器中选择手动,然后使用滑块调整思考预算限额。

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, ThinkingConfig

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="solve x^2 + 4x + 4 = 0",
    config=GenerateContentConfig(
        thinking_config=ThinkingConfig(
            thinking_budget=1024,  # Use `0` to turn off thinking
        )
    ),
)

print(response.text)
# Example response:
#     To solve the equation $x^2 + 4x + 4 = 0$, you can use several methods:
#     **Method 1: Factoring**
#     1.  Look for two numbers that multiply to the constant term (4) and add up to the coefficient of the $x$ term (4).
#     2.  The numbers are 2 and 2 ($2 \times 2 = 4$ and $2 + 2 = 4$).
#     ...
#     ...
#     All three methods yield the same solution. This quadratic equation has exactly one distinct solution (a repeated root).
#     The solution is **x = -2**.

# Token count for `Thinking`
print(response.usage_metadata.thoughts_token_count)
# Example response:
#     886

# Total token count
print(response.usage_metadata.total_token_count)
# Example response:
#     1525

提示技巧

有效的提示对于最大限度地发挥 Gemini 思考模型的优势而言至关重要。本部分简要介绍了一些适用于思考模型的提示实践。

首先提供一些一般性的指令,描述期望获得的结果。先从一个简单的提示开始,观察模型对应答的初步思考。

Describe the most unusual or striking architectural feature you've ever seen in a building or structure.
  
<...>
The most unusual architectural feature that has truly stayed with me is the "floating roof" of the
  Heydar Aliyev Center in Baku, Azerbaijan, designed by Zaha Hadid Architects.

Visually, it's breathtaking. Imagine a vast, undulating landscape of pure white, seemingly draped
over the building. It's not just a roof in the traditional sense; it's an extension of the ground,
flowing seamlessly upwards and over the building's various spaces before curving back down to meet
the earth again. There are no sharp angles, no rigid lines. Instead, everything is fluid, organic,
and continuous. It resembles a giant, pristine white wave frozen in motion, or perhaps a sand dune
sculpted by an unseen wind.
<...>
  

提供分步指令

如果初步尝试未获得理想结果,可以提供更明确的分步指令来优化提示,引导 Gemini 更有针对性地去解决问题。这样,您就可以通过明确的建议和指导来引导模型初步理解任务。

Now imagine that architectural feature is not just for looks, but has a specific function or
purpose related to the building's environment or the people who use it. Explain what that
function could be and how it enhances the building's overall design and usability.
  
<...>
Let's imagine that the "floating roof" of the Heydar Aliyev Center is not purely aesthetic but
serves a crucial environmental and user-centric function: it's designed as a sophisticated climate
moderator and natural ventilation system for the Baku environment.

Here's how this function would work and enhance the building:

Function: Passive Climate Control and Natural Ventilation System

Shading and Solar Heat Mitigation: The undulating curves of the white GFRC roof are strategically
shaped and oriented to provide optimal shading throughout the day, year-round. Baku experiences
intense sun, especially during the summer. The overhanging curves, acting like giant brise-soleils,
would block direct sunlight from hitting the building's glass facade, significantly reducing solar
heat gain. The varying heights and depths of the curves would create dynamic shade patterns, ensuring
that different parts of the building are shaded at different times of the day, optimizing comfort
and reducing the need for excessive air conditioning. The white color of the GFRC further enhances
this by reflecting a large portion of the solar radiation.
<...>
  

将多样本提示与思考功能结合使用

将多样本提示与思考功能结合使用可以进一步增强 Gemini 的理解能力,提高其回答的准确性。在提示中提供多个输入-输出对样本,为模型演示期望的行为及所需的输出格式。

Example 1:
User: What is the tallest mountain in the world?
Assistant: Mount Everest

Example 2:
User: What is the largest ocean?
Assistant: Pacific Ocean

User: What is the longest river in the world?
Assistant:
  
Amazon River
  

定义输出和行为

在构建用户将直接与模型互动的应用时,您可能需要提供相关风格指南来指示 Gemini 在进行输出和回答时应采用何种语气和格式。

系统指令

系统指令是指模型在处理提示之前所处理的一组指令。每次向模型输入提示时,模型都会先回想这些系统指令,因为通过这些指令可以获悉您期望的行为及回答。例如,您可以添加人设或角色、背景信息和格式设置指令等内容。

System Instruction: You are a helpful and concise coding tutor. Always provide code examples and explain them briefly.

User Prompt: Explain what is a closure in Javascript and give an example.
  
    function outerFunction(outerVar) {
      function innerFunction(innerVar) {
        console.log(outerVar); // Accessing outerVar from outerFunction's scope
        console.log(innerVar);
      }
      return innerFunction;
    }

    const closureExample = outerFunction('I am from outer');
    closureExample('I am from inner');

Explanation:

A closure is created when a function (innerFunction in this example) is defined inside another function
(outerFunction) and the inner function remembers and has access to the variables from the outer
function's scope, even after the outer function has finished executing.

In the example:

outerFunction is called with 'I am from outer' and returns innerFunction.

closureExample now holds a reference to innerFunction.

When closureExample('I am from inner') is called, innerFunction executes.

Even though outerFunction has already finished, innerFunction can still access and use outerVar
(which is 'I am from outer') because of the closure.

In essence, the innerFunction "closes over" the variables of its outer function's scope, creating
a closure.
  

验证和反思

在系统指令或提示中直接添加具体的验证步骤。例如,让 Gemini 验证其来源、审查其推理过程、找出潜在错误并检查其最终回答。

    Verify the following statement and provide your sources: The capital of Brazil is Rio de Janeiro.
  
The statement "The capital of Brazil is Rio de Janeiro" is incorrect.

The current capital of Brazil is Brasília.

Rio de Janeiro was the capital of Brazil for a significant period, from 1763 to 1960. However, the
capital was moved to Brasília in 1960 as part of a plan to develop the country's interior and create
a more centrally located capital.

Verification and Sources:

You can easily verify this information from numerous reliable sources:

Encyclopedias:

Britannica: Searching for "Brazil" on Britannica will clearly state Brasília as the capital.

https://www.britannica.com/place/Brazil (Look for the "Capital" information in the introductory summary or "Geography" section)

Wikipedia: The Wikipedia article for "Brazil" also states Brasília as the capital.

https://en.wikipedia.org/wiki/Brazil (Look in the infobox or introductory section)

Government and Official Sources:

CIA World Factbook: This is a highly reliable source for country information. Searching for "Brazil"
will list Brasília as the capital.

https://www.cia.gov/the-world-factbook/countries/brazil/ (Look under "Government" > "Capital")

Official Website of the Brazilian Government (if accessible and in English): While navigating official
government websites can sometimes be complex, they would definitively list Brasília as the capital.

Geographic Resources:

National Geographic: Their website and publications will confirm Brasília as the capital of Brazil.

Atlases and Geography Textbooks: Any modern atlas or geography textbook will list Brasília as the capital.

Why the confusion?

The confusion might arise because:

Rio de Janeiro is a very famous Brazilian city: It's a major tourist destination, known for its
beaches, Carnival, and iconic landmarks like Christ the Redeemer. Its past status as capital adds
to its historical significance and public recognition.

Historical context: Rio de Janeiro was the capital for a long time. People who learned about Brazil
in the past might remember it as the capital.

In conclusion, the statement is false. The capital of Brazil is Brasília, not Rio de Janeiro. Rio
de Janeiro was the former capital but is no longer the current capital.

You can confidently use the sources listed above to verify this information.
  

调试和引导

Vertex AI Studio 可让开发者在界面中查看模型的思考过程。当开发者在调试过程中检查提示时,这些信息会非常有用:

分析思考过程

仔细检查 Gemini 的思考过程,找出可能出错的地方。此项分析可为优化提示提供有价值的线索,并帮助您在应用中始终获得预期的回答。

示例场景:如果 Gemini 的思考过程表明它误解了复杂指令中的某个步骤,则您可以优化提示,明确说明该特定步骤。

Step 1: Summarize the following article about quantum physics.
Step 2: Translate the summary into Spanish.
Step 3: Extract the key entities from the Spanish summary.

Article: ... [Article Text] ...
The user wants me to process an article about Google's new quantum chip called Willow in three steps:
Step 1: Summarize the article.
Step 2: Translate the summary into Spanish.
Step 3: Extract the key entities from the Spanish summary.

Let's break down each step.

Step 1: Summarize the article.
I need to read the article and identify the main points. The article is about Google's new quantum
chip, Willow, and its achievements. The key achievements are:

Exponential error reduction as qubits scale up.

Outperforming supercomputers in a benchmark computation (RCS).

Willow is a step towards commercially relevant quantum computing.

It's the first system below threshold for quantum error correction.

It achieved a benchmark computation in under 5 minutes that would take a supercomputer 10 septillion years.

It was fabricated in a new facility.

It has state-of-the-art performance and high quality qubits.

Google is looking towards real-world applications with Willow.

Quantum computing will benefit AI.

I will synthesize these points into a concise summary.

Step 2: Translate the summary into Spanish.
Once I have the summary in English, I will translate it into Spanish. I need to ensure the translation
is accurate and natural-sounding in Spanish.

Step 3: Extract the key entities from the Spanish summary.
After translating the summary, I will identify and extract the key entities. Key entities are typically
nouns or noun phrases that represent important objects, concepts, or actors. In this context, they
will likely include:

Names of chips (Willow, Sycamore)

Companies/Organizations (Google Quantum AI, Google Research)

Technologies (quantum chip, quantum computer, qubits, quantum error correction, superconducting
quantum system, classical computers, supercomputers, AI)

Benchmarks (random circuit sampling, RCS)

People (Julian Kelly, Peter Shor, David Deutsch, Sergio Boixo, Hartmut Neven, John Preskill)

Concepts (exponential error reduction, below threshold, beyond breakeven, state-of-the-art performance,
commercially relevant applications, quantum mechanics, multiverse, T1 times, system benchmarks,
algorithmic benchmarks)

I will need to carefully review the Spanish summary to identify these entities and list them out.

Provide corrective feedback

If you observe errors or inconsistencies in Gemini's reasoning, provide corrective feedback in your prompt and the system instructions to anticipate where the model's response may deviate from your desired outcome.

You are an expert in botany. When asked to identify a plant, focus on leaf shape, flower structure, and stem characteristics.

Identify this plant based on these characteristics: ... [Plant Characteristics Description] ...

Previous incorrect response focused on habitat only. Please disregard habitat and focus on morphology.
Previous incorrect response focused on habitat only. Please disregard habitat and focus on morphology.
Tabebuia vellosoi

充分利用长输出和长篇思考

思考过程会生成“想法”,作为 token 输出的一部分,展示 Gemini 的详细推理过程。如要充分利用这些输出,您可以:

检查推理过程

重点关注 Gemini 的推理过程(可在 Vertex AI Studio 界面中查看),了解它如何分解任务以及如何为输出做出各种选择。这有助于您了解该模型得出结论的方式。

提供推理方面的指导

如果您希望输出内容特别长,不妨在提示中提供更多指导性指令,限定完成提示中呈现的任务所需的思考量。这样,您就可以为回答预留更多 token 输出。

*示例:如果您想要获得一份详细报告,则可以在提示中指定报告的各个部分和子部分,以引导 Gemini 进行思考并高效地构建输出内容。

Generate a detailed report on the impact of climate change on coastal ecosystems.

Report Structure:

Introduction

Rising Sea Levels
2.1. Impact on Mangroves
2.2. Impact on Coral Reefs

Ocean Acidification
3.1. Impact on Shellfish
3.2. Impact on Plankton

Extreme Weather Events
4.1. Increased Storm Intensity
4.2. Coastal Erosion

Conclusion

Provide detailed analysis within each section.
<...>
Climate Change Impact on Coastal Ecosystems: A Detailed Report

1.  Introduction

Coastal ecosystems, the dynamic interface between land and sea, are among the most productive and
biodiverse environments on Earth. They provide crucial ecosystem services, including coastal protection,
fisheries, carbon sequestration, and support for a vast array of species. However, these vital
ecosystems are increasingly threatened by the multifaceted impacts of climate change. Driven by
anthropogenic greenhouse gas emissions, climate change is manifesting in several interconnected ways
that are profoundly altering the physical and chemical conditions of coastal environments. This report
will delve into the key impacts of climate change on coastal ecosystems, focusing on rising sea levels,
ocean acidification, and extreme weather events, and their specific consequences for critical habitats
like mangroves and coral reefs, and key organisms like shellfish and plankton. Understanding these
impacts is crucial for developing effective conservation and management strategies to mitigate the
damage and enhance the resilience of these invaluable ecosystems.

2.  Rising Sea Levels

Rising sea levels are a direct and significant consequence of global warming, driven by thermal
expansion of seawater and the melting of land ice (glaciers and ice sheets). This phenomenon poses a
direct threat to low-lying coastal ecosystems by inundating habitats, altering salinity regimes, and
increasing erosion rates. The rate of sea level rise is accelerating, and projections for the 21st
century indicate a continued upward trend, with potentially devastating consequences for coastal
environments.

2.1. Impact on Mangroves

Mangrove forests are unique coastal ecosystems found in tropical and subtropical intertidal zones.
They provide numerous benefits, including coastal protection against storms, nursery grounds for
fish and invertebrates, and significant carbon sequestration...
<...>

后续步骤

您可以试用我们的 Colab 笔记本,亲自体验思考模型;也可以打开 Vertex AI 控制台,尝试向思考模型发出提示。