Tư duy của Gemini

Các mô hình thuộc dòng Gemini 3 và 2.5 sử dụng"quá trình tư duy" giúp cải thiện đáng kể khả năng suy luận và lập kế hoạch nhiều bước, nhờ đó, các mô hình này rất hiệu quả đối với các tác vụ phức tạp như lập trình, toán học nâng cao và phân tích dữ liệu.

Khi bạn sử dụng mô hình tư duy, Gemini sẽ suy luận nội bộ trước khi phản hồi. API Tương tác hiển thị quá trình suy luận này thông qua các bước thought, các bước riêng biệt xuất hiện theo trình tự thời gian cùng với các lệnh gọi hàm, dữ liệu đầu vào của người dùng hoặc dữ liệu đầu ra của mô hình trong mảng steps.

Mỗi bước tư duy chứa hai trường:

Trường Bắt buộc Mô tả
signature ✅ Có Một bản trình bày được mã hoá về trạng thái suy luận nội bộ của mô hình. Luôn xuất hiện, ngay cả khi mô hình thực hiện quá trình suy luận tối thiểu.
summary ❌ Không Một mảng nội dung (văn bản và/hoặc hình ảnh) tóm tắt quá trình suy luận. Có thể trống tuỳ thuộc vào cấu hình thinking_summaries, liệu mô hình có thực hiện đủ quá trình suy luận hay không hoặc loại nội dung (ví dụ: hình ảnh tiềm ẩn có thể không có bản tóm tắt bằng văn bản).

Tương tác với quá trình tư duy

Việc bắt đầu tương tác với mô hình tư duy cũng tương tự như mọi yêu cầu tương tác khác. Hãy chỉ định một trong các mô hình có hỗ trợ tư duy trong trường model:

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.5-flash",
    input="Explain the concept of Occam's Razor and provide a simple, everyday example."
)
print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3.5-flash",
    input: "Explain the concept of Occam's Razor and provide a simple, everyday example."
});
console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "Explain the concept of Occam'\''s Razor and provide a simple example."
  }'

Bản tóm tắt ý tưởng

Bản tóm tắt ý tưởng cung cấp thông tin chi tiết về quá trình suy luận nội bộ của mô hình. Theo mặc định, chỉ có dữ liệu đầu ra cuối cùng được trả về. Bạn có thể bật bản tóm tắt ý tưởng bằng thinking_summaries:

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.5-flash",
    input="What is the sum of the first 50 prime numbers?",
    generation_config={
        "thinking_summaries": "auto"
    }
)

for step in interaction.steps:
    if step.type == "thought":
        print("Thought summary:")
        if step.summary:
            for content_block in step.summary:
                if content_block.type == "text":
                    print(content_block.text)
        print()
    elif step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print("Answer:")
                print(content_block.text)
                print()

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3.5-flash",
    input: "What is the sum of the first 50 prime numbers?",
    generation_config: {
        thinking_summaries: "auto"
    }
});

for (const step of interaction.steps) {
    if (step.type === "thought") {
        console.log("Thought summary:");
        if (step.summary) {
            for (const contentBlock of step.summary) {
                if (contentBlock.type === "text") console.log(contentBlock.text);
            }
        }
    } else if (step.type === "model_output") {
        for (const contentBlock of step.content) {
            if (contentBlock.type === "text") {
                console.log("Answer:");
                console.log(contentBlock.text);
            }
        }
    }
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "What is the sum of the first 50 prime numbers?",
    "generation_config": {
      "thinking_summaries": "auto"
    }
  }'

Khối tư duy chỉ có thể chứa chữ ký mà không có bản tóm tắt trong những trường hợp sau:

  • Các yêu cầu đơn giản, trong đó mô hình không suy luận đủ để tạo bản tóm tắt
  • thinking_summaries: "none", trong đó bản tóm tắt bị tắt rõ ràng
  • Một số loại nội dung tư duy nhất định, chẳng hạn như hình ảnh, có thể không có bản tóm tắt bằng văn bản

Mã của bạn phải luôn xử lý các khối tư duy trong đó summary trống hoặc không có.

Truyền trực tuyến với quá trình tư duy

Sử dụng tính năng truyền trực tuyến để nhận bản tóm tắt ý tưởng tăng dần trong quá trình tạo. Các khối tư duy được phân phối bằng cách sử dụng Sự kiện được gửi bởi máy chủ (SSE) với hai loại delta riêng biệt:

Loại delta Chứa Thời điểm gửi
thought_summary Nội dung tóm tắt bằng văn bản hoặc hình ảnh Một hoặc nhiều delta có bản tóm tắt tăng dần
thought_signature Chữ ký mật mã delta cuối cùng trước step.stop

Python

from google import genai

client = genai.Client()

prompt = """
Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue.
Alice does not live in the red house.
Bob does not live in the green house.
Carol does not live in the red or green house.
Which house does each person live in?
"""

thoughts = ""
answer = ""

stream = client.interactions.create(
    model="gemini-3.5-flash",
    input=prompt,
    generation_config={
        "thinking_summaries": "auto"
    },
    stream=True
)

for event in stream:
    if event.event_type == "step.delta":
        if event.delta.type == "thought_summary":
            if not thoughts:
                print("Thinking...")
            summary_text = event.delta.content.text
            print(f"[Thought] {summary_text}", end="")
            thoughts += summary_text
        elif event.delta.type == "text" and event.delta.text:
            if not answer:
                print("\nAnswer:")
            print(event.delta.text, end="")
            answer += event.delta.text

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const prompt = `Alice, Bob, and Carol each live in a different house on the same
street: red, green, and blue. Alice does not live in the red house.
Bob does not live in the green house.
Carol does not live in the red or green house.
Which house does each person live in?`;

let thoughts = "";
let answer = "";

const stream = await client.interactions.create({
    model: "gemini-3.5-flash",
    input: prompt,
    generation_config: {
        thinking_summaries: "auto"
    },
    stream: true
});

for await (const event of stream) {
    if (event.event_type === "step.delta") {
        if (event.delta.type === "thought_summary") {
            if (!thoughts) console.log("Thinking...");
            const text = event.delta.content?.text || "";
            process.stdout.write(`[Thought] ${text}`);
            thoughts += text;
        } else if (event.delta.type === "text" && event.delta.text) {
            if (!answer) console.log("\nAnswer:");
            process.stdout.write(event.delta.text);
            answer += event.delta.text;
        }
    }
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  --no-buffer \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue. Alice does not live in the red house. Bob does not live in the green house. Carol does not live in the red or green house. Which house does each person live in?",
    "generation_config": {
      "thinking_summaries": "auto"
    },
    "stream": true
  }'

Phản hồi truyền trực tuyến sử dụng Sự kiện được gửi bởi máy chủ (SSE) và bao gồm các bước và sự kiện, ví dụ:

event: interaction.created
data: {"interaction":{"id":"v1_xxx","status":"in_progress","object":"interaction","model":"gemini-3.5-flash"},"event_type":"interaction.created"}

event: step.start
data: {"index":0,"step":{"signature":"","summary":[{"text":"**Evaluating the clues**\n\nI'm considering...","type":"text"}],"type":"thought"},"event_type":"step.start"}

event: step.delta
data: {"index":0,"delta":{"signature":"EpoGCpcGAXLI2nx/...","type":"thought_signature"},"event_type":"step.delta"}

event: step.stop
data: {"index":0,"event_type":"step.stop"}

event: step.start
data: {"index":1,"step":{"content":[{"text":"Based on the clues provided, here","type":"text"}],"type":"model_output"},"event_type":"step.start"}

event: step.delta
data: {"index":1,"delta":{"text":" is the answer to your question...","type":"text"},"event_type":"step.delta"}

event: step.stop
data: {"index":1,"event_type":"step.stop"}

event: interaction.completed
data: {"interaction":{"id":"v1_xxx","status":"completed","usage":{"total_tokens":530,"total_input_tokens":62,"total_output_tokens":171,"total_thought_tokens":297}},"event_type":"interaction.completed"}

event: done
data: [DONE]

Kiểm soát quá trình tư duy

Theo mặc định, các mô hình Gemini tham gia vào quá trình tư duy động, tự động điều chỉnh mức độ nỗ lực suy luận dựa trên độ phức tạp của yêu cầu. Bạn có thể kiểm soát hành vi này bằng cách sử dụng tham số thinking_level.

Mô hình Quá trình tư duy mặc định Các cấp độ được hỗ trợ
gemini-3.1-pro-preview Bật (cao) thấp, trung bình, cao
gemini-3.1-flash-lite-image Bật (tối thiểu) tối thiểu, cao
gemini-3-flash-preview Bật (cao) thấp, trung bình, cao
gemini-3-pro-preview Bật (cao) thấp, cao
gemini-3.5-flash Bật (trung bình) thấp, trung bình, cao
gemini-2.5-pro Bật thấp, trung bình, cao
gemini-2.5-flash Bật thấp, trung bình, cao
gemini-2.5-flash-lite Tắt thấp, trung bình, cao

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.5-flash",
    input="Provide a list of 3 famous physicists and their key contributions",
    generation_config={
        "thinking_level": "low"
    }
)
print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3.5-flash",
    input: "Provide a list of 3 famous physicists and their key contributions",
    generation_config: {
        thinking_level: "low"
    }
});
console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "Provide a list of 3 famous physicists and their key contributions",
    "generation_config": {
      "thinking_level": "low"
    }
  }'

Chữ ký tư duy

Chữ ký tư duy là bản trình bày được mã hoá về quá trình suy luận nội bộ của mô hình. Bạn phải có chữ ký tư duy để duy trì tính liên tục của quá trình suy luận trong các lượt tương tác nhiều lượt.

API Tương tác giúp việc xử lý chữ ký tư duy trở nên đơn giản hơn nhiều so với API generateContent.

Theo mặc định, khi bạn sử dụng Interactions API ở chế độ có trạng thái (bằng cách đặt store: true và truyền previous_interaction_id trong các lượt tiếp theo), máy chủ sẽ tự động quản lý trạng thái cuộc trò chuyện, bao gồm tất cả các khối tư duy và chữ ký. Ở chế độ này, bạn không cần làm gì liên quan đến chữ ký. Chữ ký được xử lý hoàn toàn ở phía máy chủ.

Chế độ không trạng thái

Nếu bạn tự quản lý trạng thái cuộc trò chuyện (chế độ không trạng thái) và truyền toàn bộ nhật ký dữ liệu đầu vào và đầu ra trong mỗi yêu cầu:

  • Bạn PHẢI luôn gửi lại tất cả các khối thought chính xác như khi nhận được từ mô hình.
  • Bạn KHÔNG NÊN xoá hoặc sửa đổi các khối tư duy khỏi nhật ký, vì các khối này chứa chữ ký cần thiết để mô hình tiếp tục quá trình suy luận.
  • Khi chuyển đổi mô hình trong một phiên, bạn vẫn nên gửi lại các khối tư duy của mô hình trước đó. Phần phụ trợ quản lý khả năng tương thích.

Giá

Khi quá trình tư duy được bật, giá phản hồi là tổng số mã thông báo đầu ra và mã thông báo tư duy. Bạn có thể lấy tổng số mã thông báo tư duy đã tạo từ trường total_thought_tokens.

Python

print("Thoughts tokens:", interaction.usage.total_thought_tokens)
print("Output tokens:", interaction.usage.total_output_tokens)

JavaScript

console.log(`Thoughts tokens: ${interaction.usage.total_thought_tokens}`);
console.log(`Output tokens: ${interaction.usage.total_output_tokens}`);

Các mô hình tư duy tạo ra các ý tưởng đầy đủ để cải thiện chất lượng của phản hồi cuối cùng, sau đó xuất bản tóm tắt để cung cấp thông tin chi tiết về quá trình tư duy. Giá dựa trên mã thông báo tư duy đầy đủ mà mô hình cần tạo, mặc dù chỉ có bản tóm tắt được xuất từ API.

Bạn có thể tìm hiểu thêm về mã thông báo trong hướng dẫn Đếm mã thông báo.

Các phương pháp hay nhất

Sử dụng các mô hình tư duy một cách hiệu quả bằng cách làm theo các nguyên tắc sau.

  • Xem xét quá trình suy luận: Phân tích bản tóm tắt ý tưởng để hiểu các lỗi và cải thiện câu lệnh.
  • Kiểm soát ngân sách tư duy: Nhắc mô hình tư duy ít hơn đối với các dữ liệu đầu ra dài để tiết kiệm mã thông báo.
  • Các tác vụ đơn giản: Sử dụng quá trình tư duy thấp để truy xuất hoặc phân loại thông tin thực tế (ví dụ: "DeepMind được thành lập ở đâu?").
  • Các tác vụ vừa phải: Sử dụng quá trình tư duy mặc định để so sánh các khái niệm hoặc suy luận sáng tạo (ví dụ: So sánh xe điện và xe hybrid).
  • Các tác vụ phức tạp: Sử dụng quá trình tư duy tối đa để lập trình nâng cao, toán học hoặc lập kế hoạch nhiều bước (ví dụ: Giải các bài toán AIME).

Bước tiếp theo