กำลังคิด

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

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

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

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

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

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

PythonJavaScriptGoREST
from google import genai

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

print(response.text)
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });

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-flash-preview-05-20",  
    contents: prompt,
  });

  console.log(response.text);
}

main();
// import packages here

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GOOGLE_API_KEY")))
  if err != nil {
    log.Fatal(err)
  }
  defer client.Close()

  model := client.GenerativeModel("gemini-2.5-flash-preview-05-20")  
  resp, err := model.GenerateContent(ctx, genai.Text("Explain the concept of Occam's Razor and provide a simple, everyday example."))
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(resp.Text())
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=$GOOGLE_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."
         }
       ]
     }
   ]
 }'
 ```

สรุปความคิด (ทดลอง)

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

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

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

PythonJavaScriptGo
from google import genai
from google.genai import types

client = genai.Client(api_key="GOOGLE_API_KEY")
prompt = "What is the sum of the first 50 prime numbers?"
response = client.models.generate_content(
  model="gemini-2.5-flash-preview-05-20",
  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()
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash-preview-05-20",
    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();
package main

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

func main() {
  ctx := context.Background()
  client, _ := genai.NewClient(ctx, &genai.ClientConfig{
    APIKey:  os.Getenv("GOOGLE_API_KEY"),
    Backend: genai.BackendGeminiAPI,
  })

  contents := genai.Text("What is the sum of the first 50 prime numbers?")
  model := "gemini-2.5-flash-preview-05-20"
  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)
      }
    }
  }
}

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

PythonJavaScript
from google import genai
from google.genai import types

client = genai.Client(api_key="GOOGLE_API_KEY")

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-flash-preview-05-20",
    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
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });

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-flash-preview-05-20",
    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();

งบประมาณ

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

  • thinkingBudget ต้องเป็นจํานวนเต็มในช่วง 0 ถึง 24576
  • การตั้งค่างบประมาณการคิดเป็น 0 จะปิดใช้การคิด
  • โมเดลอาจใช้งบประมาณโทเค็นเกินหรือต่ำกว่าที่ควรจะเป็น ทั้งนี้ขึ้นอยู่กับพรอมต์
PythonJavaScriptGoREST
from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.5-flash-preview-05-20",
    contents="Provide a list of 3 famous physicists and their key contributions",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=1024)
    ),
)

print(response.text)
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash-preview-05-20",
    contents: "Provide a list of 3 famous physicists and their key contributions",
    config: {
      thinkingConfig: {
        thinkingBudget: 1024,
      },
    },
  });

  console.log(response.text);
}

main();
package main

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

func main() {
  ctx := context.Background()
  client, _ := genai.NewClient(ctx, &genai.ClientConfig{
    APIKey:  os.Getenv("GOOGLE_API_KEY"),
    Backend: genai.BackendGeminiAPI,
  })

  thinkingBudgetVal := int32(1024)

  contents := genai.Text("Provide a list of 3 famous physicists and their key contributions")
  model := "gemini-2.5-flash-preview-05-20"
  resp, _ := client.Models.GenerateContent(ctx, model, contents, &genai.GenerateContentConfig{
    ThinkingConfig: &genai.ThinkingConfig{
      ThinkingBudget: &thinkingBudgetVal,
    },
  })

fmt.Println(resp.Text())
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=$GOOGLE_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
    }
  }
}'

ราคา

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

PythonJavaScriptGo
# ...
print("Thoughts tokens:",response.usage_metadata.thoughts_token_count)
print("Output tokens:",response.usage_metadata.candidates_token_count)
// ...
console.log(`Thoughts tokens: ${response.usageMetadata.thoughtsTokenCount}`);
console.log(`Output tokens: ${response.usageMetadata.candidatesTokenCount}`);
// ...
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 เท่านั้น

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

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

คุณดูความสามารถทั้งหมดของโมเดลได้ในหน้าภาพรวมโมเดล

รุ่น สรุปการคิด งบประมาณการคิด
Gemini 2.5 Flash ✔️ ✔️
Gemini 2.5 Pro ✔️ X

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  • ดูข้อมูลเพิ่มเติมเกี่ยวกับเวอร์ชันตัวอย่างของ Gemini 2.5 Pro และ Thinking ของ Gemini Flash 2.5 ได้ที่หน้าโมเดล