กำลังคิด

โมเดลชุด 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 ใน การกำหนดค่าคำขอ จากนั้นคุณจะเข้าถึงข้อมูลสรุปได้โดยการวนซ้ำผ่าน response ของพารามิเตอร์ parts และตรวจสอบบูลีน 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("Answer:")
      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)
      }
    }
  }
}

ลายเซ็นความคิด

เนื่องจากการเรียกข้อความและการสร้างเนื้อหาของ Gemini API มาตรฐานเป็นแบบไม่มีสถานะ เมื่อใช้การคิดในการโต้ตอบแบบหลายรอบ (เช่น แชท) โมเดลจะไม่มี สิทธิ์เข้าถึงบริบทความคิดจากรอบก่อนหน้า

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

คุณจะได้รับลายเซ็นความคิดในกรณีต่อไปนี้

คุณดูตัวอย่างการคิดด้วยการเรียกใช้ฟังก์ชันได้ในหน้าการเรียกใช้ฟังก์ชัน

ข้อจำกัดในการใช้งานอื่นๆ ที่ควรพิจารณาเมื่อใช้การเรียกใช้ฟังก์ชันมีดังนี้

  • ระบบจะแสดงลายเซ็นจากโมเดลภายในส่วนอื่นๆ ในการตอบกลับ เช่น การเรียกใช้ฟังก์ชันหรือข้อความ ส่งคืน คำตอบทั้งหมดพร้อมทุกส่วนกลับไปยังโมเดลในรอบถัดไป
  • อย่าต่อชิ้นส่วนที่มีลายเซ็นเข้าด้วยกัน
  • อย่านำส่วนที่มีลายเซ็นไปรวมกับส่วนที่ไม่มีลายเซ็น

ราคา

เมื่อเปิดใช้การคิด ราคาการตอบกลับคือผลรวมของโทเค็นเอาต์พุตและโทเค็นการคิด คุณดูจำนวนโทเค็นการคิดทั้งหมดที่สร้างขึ้นได้จากฟิลด์ 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 ซึ่งจะช่วยให้โมเดลโต้ตอบกับระบบภายนอก รันโค้ด หรือเข้าถึงข้อมูลแบบเรียลไทม์ได้ โดยจะรวมผลลัพธ์ไว้ในการให้เหตุผล และคำตอบสุดท้าย

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

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

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

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

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

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

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

  • หากต้องการดูตัวอย่างที่เจาะลึกมากขึ้น เช่น

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

    และอื่นๆ ลองใช้ตำราอาหารแห่งความคิดของเรา

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

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