กำลังคิด

โมเดลซีรีส์ Gemini 2.5 ใช้ "กระบวนการคิด" ภายในที่ปรับปรุงความสามารถในการให้เหตุผลและวางแผนแบบหลายขั้นตอนได้อย่างมาก ทำให้มีประสิทธิภาพสูงสำหรับงานที่ซับซ้อน เช่น การเขียนโค้ด คณิตศาสตร์ขั้นสูง และการวิเคราะห์ข้อมูล

คู่มือนี้จะแสดงวิธีใช้ความสามารถด้านการคิดของ Gemini โดยใช้ Gemini API

ก่อนเริ่มต้น

ตรวจสอบว่าคุณใช้รูปแบบชุด 2.5 ที่รองรับสำหรับการคิด คุณอาจพบว่าการสำรวจโมเดลเหล่านี้ใน AI Studio มีประโยชน์ก่อนที่จะเจาะลึก API

การสร้างเนื้อหาด้วยการคิด

การส่งคำขอด้วยรูปแบบการคิดจะคล้ายกับคำขอสร้างเนื้อหาอื่นๆ ความแตกต่างที่สําคัญอยู่ที่การระบุโมเดลที่รองรับการคิดรายการใดรายการหนึ่งในช่อง model ดังที่แสดงในตัวอย่างการสร้างข้อความต่อไปนี้

Python

from google import genai

client = genai.Client()
prompt = "Explain the concept of Occam's Razor and provide a simple, everyday example."
response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents=prompt
)

print(response.text)

JavaScript

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

const ai = new GoogleGenAI({});

async function main() {
  const prompt = "Explain the concept of Occam's Razor and provide a simple, everyday example.";

  const response = await ai.models.generateContent({
    model: "gemini-2.5-pro",
    contents: prompt,
  });

  console.log(response.text);
}

main();

Go

package main

import (
  "context"
  "fmt"
  "log"
  "os"
  "google.golang.org/genai"
)

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, nil)
  if err != nil {
      log.Fatal(err)
  }

  prompt := "Explain the concept of Occam's Razor and provide a simple, everyday example."
  model := "gemini-2.5-pro"

  resp, _ := client.Models.GenerateContent(ctx, model, genai.Text(prompt), nil)

  fmt.Println(resp.Text())
}

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent" \
 -H "x-goog-api-key: $GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -X POST \
 -d '{
   "contents": [
     {
       "parts": [
         {
           "text": "Explain the concept of Occam\'s Razor and provide a simple, everyday example."
         }
       ]
     }
   ]
 }'
 ```

งบประมาณ

พารามิเตอร์ thinkingBudget จะบอกให้โมเดลทราบจํานวนโทเค็นการคิดที่จะใช้เมื่อสร้างคําตอบ จำนวนโทเค็นที่สูงขึ้นมักจะช่วยให้สามารถให้เหตุผลได้ละเอียดยิ่งขึ้น ซึ่งจะเป็นประโยชน์ในการรับมือกับงานที่ซับซ้อนมากขึ้น หากเวลาในการตอบสนองสำคัญกว่า ให้ใช้งบประมาณที่ต่ำลงหรือปิดใช้การคิดโดยตั้งค่า thinkingBudget เป็น 0 การตั้งค่า thinkingBudget เป็น -1 จะเปิดการคิดแบบไดนามิก ซึ่งหมายความว่าโมเดลจะปรับงบประมาณตามความซับซ้อนของคําขอ

thinkingBudget รองรับใน Gemini 2.5 Flash, 2.5 Pro และ 2.5 Flash-Lite เท่านั้น ทั้งนี้ขึ้นอยู่กับพรอมต์ที่ระบุ โมเดลอาจใช้งบประมาณโทเค็นเกินหรือต่ำกว่างบประมาณ

ต่อไปนี้เป็นรายละเอียดการกําหนดค่า thinkingBudget สําหรับรูปแบบแต่ละประเภท

รุ่น การตั้งค่าเริ่มต้น
(ไม่ได้ตั้งค่างบประมาณการคิด)
ช่วง ปิดใช้การคิด เปิดใช้การคิดแบบไดนามิก
2.5 Pro การคํานวณแบบไดนามิก: โมเดลจะตัดสินใจว่าจะคิดเมื่อใดและคิดมากน้อยเพียงใด 128 ถึง 32768 ไม่มี: ปิดใช้การคิดไม่ได้ thinkingBudget = -1
2.5 Flash การคํานวณแบบไดนามิก: โมเดลจะตัดสินใจว่าจะคิดเมื่อใดและคิดมากน้อยเพียงใด 0 ถึง 24576 thinkingBudget = 0 thinkingBudget = -1
2.5 Flash Lite โมเดลไม่คิด 512 ถึง 24576 thinkingBudget = 0 thinkingBudget = -1

Python

from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents="Provide a list of 3 famous physicists and their key contributions",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=1024)
        # Turn off thinking:
        # thinking_config=types.ThinkingConfig(thinking_budget=0)
        # Turn on dynamic thinking:
        # thinking_config=types.ThinkingConfig(thinking_budget=-1)
    ),
)

print(response.text)

JavaScript

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

const ai = new GoogleGenAI({});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-pro",
    contents: "Provide a list of 3 famous physicists and their key contributions",
    config: {
      thinkingConfig: {
        thinkingBudget: 1024,
        // Turn off thinking:
        // thinkingBudget: 0
        // Turn on dynamic thinking:
        // thinkingBudget: -1
      },
    },
  });

  console.log(response.text);
}

main();

Go

package main

import (
  "context"
  "fmt"
  "google.golang.org/genai"
  "os"
)

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, nil)
  if err != nil {
      log.Fatal(err)
  }

  thinkingBudgetVal := int32(1024)

  contents := genai.Text("Provide a list of 3 famous physicists and their key contributions")
  model := "gemini-2.5-pro"
  resp, _ := client.Models.GenerateContent(ctx, model, contents, &genai.GenerateContentConfig{
    ThinkingConfig: &genai.ThinkingConfig{
      ThinkingBudget: &thinkingBudgetVal,
      // Turn off thinking:
      // ThinkingBudget: int32(0),
      // Turn on dynamic thinking:
      // ThinkingBudget: int32(-1),
    },
  })

fmt.Println(resp.Text())
}

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
  "contents": [
    {
      "parts": [
        {
          "text": "Provide a list of 3 famous physicists and their key contributions"
        }
      ]
    }
  ],
  "generationConfig": {
    "thinkingConfig": {
          "thinkingBudget": 1024
          # Thinking off:
          # "thinkingBudget": 0
          # Turn on dynamic thinking:
          # "thinkingBudget": -1
    }
  }
}'

สรุปความคิด

สรุปความคิดคือเวอร์ชันสังเคราะห์ของความคิดดิบของโมเดล และมอบข้อมูลเชิงลึกเกี่ยวกับกระบวนการใช้เหตุผลภายในของโมเดล โปรดทราบว่างบประมาณการคิดจะมีผลกับความคิดดิบของโมเดล ไม่ใช่กับสรุปความคิด

คุณเปิดใช้สรุปความคิดได้โดยตั้งค่า includeThoughts เป็น true ในการกําหนดค่าคําขอ จากนั้นคุณจะเข้าถึงสรุปได้โดยวนผ่าน parts ของพารามิเตอร์ response และตรวจสอบบูลีน thought

ต่อไปนี้คือตัวอย่างที่แสดงวิธีเปิดใช้และเรียกข้อมูลสรุปความคิดโดยไม่ต้องสตรีม ซึ่งจะแสดงผลสรุปความคิดสุดท้ายรายการเดียวพร้อมคําตอบ

Python

from google import genai
from google.genai import types

client = genai.Client()
prompt = "What is the sum of the first 50 prime numbers?"
response = client.models.generate_content(
  model="gemini-2.5-pro",
  contents=prompt,
  config=types.GenerateContentConfig(
    thinking_config=types.ThinkingConfig(
      include_thoughts=True
    )
  )
)

for part in response.candidates[0].content.parts:
  if not part.text:
    continue
  if part.thought:
    print("Thought summary:")
    print(part.text)
    print()
  else:
    print("Answer:")
    print(part.text)
    print()

JavaScript

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

const ai = new GoogleGenAI({});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-pro",
    contents: "What is the sum of the first 50 prime numbers?",
    config: {
      thinkingConfig: {
        includeThoughts: true,
      },
    },
  });

  for (const part of response.candidates[0].content.parts) {
    if (!part.text) {
      continue;
    }
    else if (part.thought) {
      console.log("Thoughts summary:");
      console.log(part.text);
    }
    else {
      console.log("Answer:");
      console.log(part.text);
    }
  }
}

main();

Go

package main

import (
  "context"
  "fmt"
  "google.golang.org/genai"
  "os"
)

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, nil)
  if err != nil {
      log.Fatal(err)
  }

  contents := genai.Text("What is the sum of the first 50 prime numbers?")
  model := "gemini-2.5-pro"
  resp, _ := client.Models.GenerateContent(ctx, model, contents, &genai.GenerateContentConfig{
    ThinkingConfig: &genai.ThinkingConfig{
      IncludeThoughts: true,
    },
  })

  for _, part := range resp.Candidates[0].Content.Parts {
    if part.Text != "" {
      if part.Thought {
        fmt.Println("Thoughts Summary:")
        fmt.Println(part.Text)
      } else {
        fmt.Println("Answer:")
        fmt.Println(part.Text)
      }
    }
  }
}

และนี่คือตัวอย่างการใช้การทํางานแบบสตรีมมิง ซึ่งจะแสดงสรุปแบบต่อเนื่องที่เพิ่มขึ้นเรื่อยๆ ในระหว่างการสร้าง

Python

from google import genai
from google.genai import types

client = genai.Client()

prompt = """
Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue.
The person who lives in the red house owns a cat.
Bob does not live in the green house.
Carol owns a dog.
The green house is to the left of the red house.
Alice does not own a cat.
Who lives in each house, and what pet do they own?
"""

thoughts = ""
answer = ""

for chunk in client.models.generate_content_stream(
    model="gemini-2.5-pro",
    contents=prompt,
    config=types.GenerateContentConfig(
      thinking_config=types.ThinkingConfig(
        include_thoughts=True
      )
    )
):
  for part in chunk.candidates[0].content.parts:
    if not part.text:
      continue
    elif part.thought:
      if not thoughts:
        print("Thoughts summary:")
      print(part.text)
      thoughts += part.text
    else:
      if not answer:
        print("Thoughts summary:")
      print(part.text)
      answer += part.text

JavaScript

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

const ai = new GoogleGenAI({});

const prompt = `Alice, Bob, and Carol each live in a different house on the same
street: red, green, and blue. The person who lives in the red house owns a cat.
Bob does not live in the green house. Carol owns a dog. The green house is to
the left of the red house. Alice does not own a cat. Who lives in each house,
and what pet do they own?`;

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

async function main() {
  const response = await ai.models.generateContentStream({
    model: "gemini-2.5-pro",
    contents: prompt,
    config: {
      thinkingConfig: {
        includeThoughts: true,
      },
    },
  });

  for await (const chunk of response) {
    for (const part of chunk.candidates[0].content.parts) {
      if (!part.text) {
        continue;
      } else if (part.thought) {
        if (!thoughts) {
          console.log("Thoughts summary:");
        }
        console.log(part.text);
        thoughts = thoughts + part.text;
      } else {
        if (!answer) {
          console.log("Answer:");
        }
        console.log(part.text);
        answer = answer + part.text;
      }
    }
  }
}

await main();

Go

package main

import (
  "context"
  "fmt"
  "log"
  "os"
  "google.golang.org/genai"
)

const prompt = `
Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue.
The person who lives in the red house owns a cat.
Bob does not live in the green house.
Carol owns a dog.
The green house is to the left of the red house.
Alice does not own a cat.
Who lives in each house, and what pet do they own?
`

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, nil)
  if err != nil {
      log.Fatal(err)
  }

  contents := genai.Text(prompt)
  model := "gemini-2.5-pro"

  resp := client.Models.GenerateContentStream(ctx, model, contents, &genai.GenerateContentConfig{
    ThinkingConfig: &genai.ThinkingConfig{
      IncludeThoughts: true,
    },
  })

  for chunk := range resp {
    for _, part := range chunk.Candidates[0].Content.Parts {
      if len(part.Text) == 0 {
        continue
      }

      if part.Thought {
        fmt.Printf("Thought: %s\n", part.Text)
      } else {
        fmt.Printf("Answer: %s\n", part.Text)
      }
    }
  }
}

ราคา

เมื่อเปิดใช้การคิด ราคาการตอบกลับจะเป็นผลรวมของโทเค็นเอาต์พุตและโทเค็นการคิด คุณดูจํานวนโทเค็นการคิดทั้งหมดที่สร้างขึ้นได้จากช่อง thoughtsTokenCount

Python

# ...
print("Thoughts tokens:",response.usage_metadata.thoughts_token_count)
print("Output tokens:",response.usage_metadata.candidates_token_count)

JavaScript

// ...
console.log(`Thoughts tokens: ${response.usageMetadata.thoughtsTokenCount}`);
console.log(`Output tokens: ${response.usageMetadata.candidatesTokenCount}`);

Go

// ...
usageMetadata, err := json.MarshalIndent(response.UsageMetadata, "", "  ")
if err != nil {
  log.Fatal(err)
}
fmt.Println("Thoughts tokens:", string(usageMetadata.thoughts_token_count))
fmt.Println("Output tokens:", string(usageMetadata.candidates_token_count))

โมเดลการคิดจะสร้างความคิดแบบสมบูรณ์เพื่อปรับปรุงคุณภาพของคำตอบสุดท้าย จากนั้นจะแสดงสรุปเพื่อให้ข้อมูลเชิงลึกเกี่ยวกับกระบวนการคิด ดังนั้น ราคาจะอิงตามโทเค็นความคิดแบบเต็มๆ ที่โมเดลต้องสร้างเพื่อสร้างสรุป แม้ว่าจะมีเพียงสรุปเท่านั้นที่แสดงผลจาก API

ดูข้อมูลเพิ่มเติมเกี่ยวกับโทเค็นได้ในคู่มือการนับโทเค็น

โมเดลที่รองรับ

ฟีเจอร์การคิดใช้ได้กับทุกรุ่นในซีรีส์ 2.5 คุณดูความสามารถทั้งหมดของโมเดลได้ในหน้าภาพรวมโมเดล

แนวทางปฏิบัติแนะนำ

ส่วนนี้มีคำแนะนำบางส่วนในการใช้รูปแบบการคิดอย่างมีประสิทธิภาพ ดังเช่นเคย การทำตามคำแนะนำและแนวทางปฏิบัติแนะนำเกี่ยวกับพรอมต์จะช่วยให้คุณได้รับผลลัพธ์ที่ดีที่สุด

การแก้ไขข้อบกพร่องและการควบคุม

  • ตรวจสอบเหตุผล: เมื่อคุณไม่ได้รับการตอบกลับตามที่คาดหวังจากโมเดลการคิด การวิเคราะห์สรุปความคิดของ Gemini อย่างละเอียดอาจช่วยได้ คุณสามารถดูวิธีที่ระบบแจกแจงงานและสรุปผลลัพธ์ รวมถึงใช้ข้อมูลดังกล่าวเพื่อแก้ไขให้ได้ผลลัพธ์ที่ถูกต้อง

  • ให้คําแนะนําในการให้เหตุผล: หากต้องการเอาต์พุตที่ยาวเป็นพิเศษ คุณอาจต้องให้คําแนะนําในพรอมต์เพื่อจำกัดปริมาณการคิดที่โมเดลใช้ ซึ่งจะช่วยให้คุณจองเอาต์พุตโทเค็นได้มากขึ้นสำหรับการตอบกลับ

ความซับซ้อนของงาน

  • งานง่ายๆ (อาจปิดใช้การคิด): สําหรับคําขอที่ตรงไปตรงมาซึ่งไม่จําเป็นต้องใช้การหาเหตุผลที่ซับซ้อน เช่น การดึงข้อมูลข้อเท็จจริงหรือการแยกประเภท ระบบจะไม่ใช้การคิด ตัวอย่างเช่น
    • "DeepMind ก่อตั้งขึ้นที่ไหน"
    • "อีเมลนี้เป็นการขอนัดหมายการประชุมหรือแค่ให้ข้อมูล"
  • งานระดับปานกลาง (ค่าเริ่มต้น/มีการคิดบ้าง): คำขอทั่วไปจำนวนมากได้รับประโยชน์จากการประมวลผลแบบทีละขั้นตอนหรือการทําความเข้าใจที่ละเอียดยิ่งขึ้น Gemini ใช้ความสามารถด้านการคิดในการทำงานต่างๆ ได้อย่างยืดหยุ่น เช่น
    • เปรียบเทียบการสังเคราะห์แสงกับการเจริญเติบโต
    • เปรียบเทียบรถยนต์ไฟฟ้ากับรถยนต์ไฮบริด
  • งานยาก (ความสามารถในการคิดสูงสุด): สำหรับงานที่ซับซ้อนมาก เช่น การแก้โจทย์คณิตศาสตร์ที่ซับซ้อนหรืองานเขียนโค้ด เราขอแนะนำให้ตั้งงบประมาณการคิดให้สูง งานประเภทนี้กำหนดให้โมเดลต้องใช้ความสามารถในการใช้เหตุผลและวางแผนอย่างเต็มรูปแบบ ซึ่งมักจะเกี่ยวข้องกับขั้นตอนภายในหลายขั้นตอนก่อนที่จะให้คำตอบ ตัวอย่างเช่น
    • แก้ปัญหาที่ 1 ใน AIME 2025: หาผลรวมของฐานจำนวนเต็มทั้งหมด b > 9 ซึ่ง 17b เป็นตัวหารของ 97b
    • เขียนโค้ด Python สําหรับเว็บแอปพลิเคชันที่แสดงข้อมูลตลาดหุ้นแบบเรียลไทม์ รวมถึงการตรวจสอบสิทธิ์ของผู้ใช้ ทำงานให้มีประสิทธิภาพมากที่สุด

การใช้เครื่องมือและความสามารถ

โมเดลการคิดใช้ได้กับเครื่องมือและความสามารถทั้งหมดของ Gemini ซึ่งช่วยให้แบบจําลองโต้ตอบกับระบบภายนอก เรียกใช้โค้ด หรือเข้าถึงข้อมูลแบบเรียลไทม์ได้ โดยนําผลลัพธ์มาใช้ในการหาเหตุผลและคำตอบสุดท้าย

  • เครื่องมือค้นหาช่วยให้โมเดลสามารถค้นหาข้อมูลล่าสุดหรือข้อมูลนอกเหนือจากข้อมูลที่ใช้ฝึกได้ ซึ่งมีประโยชน์สำหรับคำถามเกี่ยวกับเหตุการณ์ล่าสุดหรือหัวข้อที่เฉพาะเจาะจงมาก

  • เครื่องมือการเรียกใช้โค้ดช่วยให้โมเดลสร้างและเรียกใช้โค้ด Python เพื่อทําการคํานวณ จัดการข้อมูล หรือแก้ปัญหาด้วยอัลกอริทึมได้ดีที่สุด โมเดลจะรับเอาต์พุตของโค้ดและนำไปใช้ในคำตอบได้

  • เอาต์พุตที่มีโครงสร้างช่วยให้คุณจำกัดให้ Gemini ตอบกลับด้วย JSON ได้ ซึ่งจะเป็นประโยชน์อย่างยิ่งสำหรับการผสานรวมเอาต์พุตของโมเดลเข้ากับแอปพลิเคชัน

  • การเรียกใช้ฟังก์ชันจะเชื่อมต่อโมเดลการคิดกับเครื่องมือและ API ภายนอก เพื่อให้สามารถหาเหตุผลว่าควรเรียกใช้ฟังก์ชันใดและระบุพารามิเตอร์ใด

  • บริบท URL จะระบุ URL ให้กับโมเดลเพื่อเป็นบริบทเพิ่มเติมสำหรับพรอมต์ จากนั้นโมเดลจะดึงข้อมูลเนื้อหาจาก URL เหล่านั้น และใช้เนื้อหาดังกล่าวเพื่อแจ้งและกำหนดคำตอบ

คุณลองดูตัวอย่างการใช้เครื่องมือกับรูปแบบการคิดได้ในตำราการคิด

ขั้นตอนถัดไปคือ

  • หากต้องการดูตัวอย่างโดยละเอียดเพิ่มเติม เช่น

    • การใช้เครื่องมือกับการคิด
    • สตรีมมิงพร้อมวิธีคิด
    • การปรับงบประมาณการคิดผลลัพธ์ที่แตกต่างกัน

    และอื่นๆ อีกมากมาย โปรดดูตำราการคิด

  • การครอบคลุมการคิดพร้อมใช้งานในคู่มือความเข้ากันได้กับ OpenAI แล้ว

  • ดูข้อมูลเพิ่มเติมเกี่ยวกับ Gemini 2.5 Pro, Gemini Flash 2.5 และ Gemini 2.5 Flash-Lite ได้ที่หน้าโมเดล