กำลังคิด

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

โมเดลการคิดมีการกำหนดค่าและตัวเลือกต่อไปนี้

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

    ควบคุมการกำหนดค่านี้ด้วยระดับความคิด (Gemini 3.x และรุ่นที่ใหม่กว่า) หรือด้วยงบประมาณความคิด (รุ่น Gemini 2.5)

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

  • จัดการลายเซ็นความคิด
    SDK ของ Firebase AI Logic จะจัดการลายเซ็นความคิดให้คุณโดยอัตโนมัติ ซึ่งจะช่วยให้โมเดลมีสิทธิ์เข้าถึงบริบทความคิดจากรอบก่อนๆ โดยเฉพาะเมื่อใช้การเรียกใช้ฟังก์ชัน

โปรดอ่านแนวทางปฏิบัติแนะนำและคำแนะนำในการแจ้ง เพื่อใช้โมเดลการคิด



แนวทางปฏิบัติแนะนำและคำแนะนำในการใช้โมเดลการคิด

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

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

  • แจ้งวิธีการทีละขั้นตอน
  • ระบุตัวอย่างคู่อินพุต-เอาต์พุตหลายรายการ
  • ให้คำแนะนำเกี่ยวกับวิธีเรียบเรียงและจัดรูปแบบเอาต์พุตและคำตอบ
  • ระบุขั้นตอนการยืนยันที่เฉพาะเจาะจง

นอกเหนือจากการแจ้งพรอมต์แล้ว ให้พิจารณาใช้คำแนะนำต่อไปนี้

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

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

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



ควบคุมปริมาณการคิด

คุณสามารถกำหนดค่าระดับ "การคิด" และการให้เหตุผลที่โมเดลสามารถทำได้ก่อน ที่จะส่งคืนคำตอบ การกำหนดค่านี้มีความสำคัญอย่างยิ่งโดยเฉพาะหากการลดเวลาในการตอบสนองหรือต้นทุนเป็นสิ่งสำคัญ

อย่าลืมดูการเปรียบเทียบความยากของงานเพื่อพิจารณาว่าโมเดล อาจต้องใช้ความสามารถในการคิดมากน้อยเพียงใด คำแนะนำระดับสูงมีดังนี้

  • ตั้งค่าการคิดที่ต่ำลงสำหรับงานที่มีความซับซ้อนน้อยกว่า หรือหากการลดเวลาในการตอบสนองหรือต้นทุนเป็นสิ่งสำคัญสำหรับคุณ
  • กำหนดค่าการคิดให้สูงขึ้นสำหรับงานที่ซับซ้อนมากขึ้น

ควบคุมการกำหนดค่านี้ด้วยระดับความคิด (Gemini 3.x และรุ่นที่ใหม่กว่า) หรือด้วยงบประมาณความคิด (รุ่น Gemini 2.5)

ระดับการคิด (Gemini 3.x และรุ่นที่ใหม่กว่า)

หากต้องการควบคุมระดับการคิดที่โมเดล Gemini 3.x และรุ่นที่ใหม่กว่าใช้เพื่อ สร้างคำตอบ คุณสามารถระบุระดับการคิดสำหรับจำนวน โทเค็นการคิดที่โมเดลได้รับอนุญาตให้ใช้ได้

กำหนดระดับการคิด

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดของผู้ให้บริการรายนั้นๆ ในหน้านี้

กำหนดระดับการคิดใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel ระบบจะรักษาการกำหนดค่าไว้ตลอดอายุการใช้งานของ อินสแตนซ์ หากต้องการใช้ระดับการคิดที่แตกต่างกันสำหรับคำขอต่างๆ ให้สร้างอินสแตนซ์ GenerativeModel ที่กำหนดค่าด้วยแต่ละระดับ

ดูข้อมูลเกี่ยวกับ ค่าที่รองรับสำหรับระดับการคิด ได้ที่ส่วนท้ายของส่วนนี้

Swift

กำหนดระดับการคิดใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(thinkingLevel: .low)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_3.x_MODEL_NAME",
  generationConfig: generationConfig
)

// ...

Kotlin

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      thinkingLevel = ThinkingLevel.LOW
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_3.x_MODEL_NAME",
  generationConfig,
)

// ...

Java

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setThinkingLevel(ThinkingLevel.LOW)
    .build();

GenerationConfig generationConfig = GenerationConfig.builder()
    .setThinkingConfig(thinkingConfig)
    .build();

// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                  /* modelName */ "GEMINI_3.x_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// ...

Web

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
const generationConfig = {
  thinkingConfig: {
    thinkingLevel: ThinkingLevel.LOW
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_3.x_MODEL_NAME", generationConfig });

// ...

Dart

ตั้งค่าพารามิเตอร์ใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
final thinkingConfig = ThinkingConfig.withThinkingLevel(ThinkingLevel.low);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_3.x_MODEL_NAME',
  config: generationConfig,
);

// ...

Unity

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
var thinkingConfig = new ThinkingConfig(thinkingLevel: ThinkingLevel.Low);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_3.x_MODEL_NAME",
  generationConfig: generationConfig
);

// ...

ค่าระดับการคิดที่รองรับ

ตารางต่อไปนี้แสดงค่าระดับการคิดที่คุณตั้งค่าได้สำหรับแต่ละโมเดลโดยการกำหนดค่า thinkingLevel ของโมเดล

MINIMAL LOW MEDIUM HIGH

โมเดลใช้โทเค็นให้น้อยที่สุดเท่าที่จะทำได้ แทบไม่ต้องคิด

งานที่มีความซับซ้อนต่ำ

โมเดลใช้โทเค็นน้อยลง ลดเวลาในการตอบสนองและต้นทุน

งานที่เรียบง่ายและงานที่มีปริมาณงานสูง

โมเดลใช้แนวทางที่สมดุล

งานที่มีความซับซ้อนปานกลาง

โมเดลใช้โทเค็นจนถึงระดับสูงสุด

พรอมต์ที่ซับซ้อนซึ่งต้องใช้การให้เหตุผลอย่างลึกซึ้ง

Gemini 3.x Pro
gemini-3.1-pro-preview
(ค่าเริ่มต้น)
Gemini 3.x Flash
gemini-3.8-flash
gemini-3.7-flash
gemini-3.6-flash
gemini-3.5-flash

ไม่รองรับโดย gemini-3.8-flash หรือ gemini-3.7-flash 1
(ค่าเริ่มต้น)
Gemini 3.x Flash‑Lite
gemini-3.5-flash-lite
gemini-3.1-flash-lite
(ค่าเริ่มต้น)
Gemini 3.x Pro Image
gemini-3-pro-image
("Nano Banana Pro")
(ค่าเริ่มต้น)
Gemini 3.x Flash Image
gemini-3.1-flash-image
("Nano Banana 2")
(ค่าเริ่มต้น)
Gemini 3.x Flash‑Lite Image
gemini-3.1-flash-lite-image
("Nano Banana 2 Lite")
(ค่าเริ่มต้น)

1 gemini-3.8-flash และ gemini-3.7-flash ไม่รองรับระดับการคิด MINIMAL หากคุณ ตั้งระดับความคิดเป็น MINIMAL คำขอจะล้มเหลวพร้อมข้อผิดพลาด 400



การพิจารณางบประมาณ (Gemini 2.5 โมเดล)

หากต้องการควบคุมปริมาณการคิดที่โมเดล Gemini 2.5 สามารถใช้เพื่อสร้างคำตอบ คุณสามารถระบุงบประมาณการคิดสำหรับจำนวนโทเค็นการคิดที่โมเดลได้รับอนุญาตให้ใช้

กำหนดงบประมาณการคิด

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดของผู้ให้บริการรายนั้นๆ ในหน้านี้

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

ดูข้อมูลเกี่ยวกับ ค่าที่รองรับสำหรับงบประมาณการพิจารณา ได้ในส่วนนี้

Swift

กำหนดงบประมาณการคิดใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking budget value appropriate for your model (example value shown here).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(thinkingBudget: 1024)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_2.5_MODEL_NAME",
  generationConfig: generationConfig
)

// ...

Kotlin

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking budget value appropriate for your model (example value shown here).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      thinkingBudget = 1024
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_2.5_MODEL_NAME",
  generationConfig,
)

// ...

Java

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking budget value appropriate for your model (example value shown here).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setThinkingBudget(1024)
    .build();

GenerationConfig generationConfig = GenerationConfig.builder()
    .setThinkingConfig(thinkingConfig)
    .build();

// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                  /* modelName */ "GEMINI_2.5_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// ...

Web

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Use a thinking budget value appropriate for your model (example value shown here).
const generationConfig = {
  thinkingConfig: {
    thinkingBudget: 1024
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_2.5_MODEL_NAME", generationConfig });

// ...

Dart

ตั้งค่าพารามิเตอร์ใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking budget value appropriate for your model (example value shown here).
final thinkingConfig = ThinkingConfig.withThinkingBudget(1024);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_2.5_MODEL_NAME',
  config: generationConfig,
);

// ...

Unity

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Use a thinking budget value appropriate for your model (example value shown here).
var thinkingConfig = new ThinkingConfig(thinkingBudget: 1024);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_2.5_MODEL_NAME",
  generationConfig: generationConfig
);

// ...

ค่าที่รองรับสำหรับงบประมาณการคิด

ตารางต่อไปนี้แสดงค่าของงบประมาณการคิดที่คุณตั้งค่าได้สำหรับแต่ละโมเดลโดยการกำหนดค่า thinkingBudget ของโมเดล

รุ่น ค่าเริ่มต้น ช่วงงบประมาณที่ใช้ได้สำหรับการคิด ค่าที่ใช้
ปิดใช้การคิด
คุณค่าในการ
กระตุ้นการคิดแบบไดนามิก
มูลค่าขั้นต่ำ ค่าสูงสุด
Gemini 2.5 Pro 8,192 128 32,768 ปิดใช้ไม่ได้ -1
Gemini 2.5 Flash 8,192 1 24,576 0 -1
Gemini 2.5 Flash‑Lite 0
(ปิดใช้การคิดโดยค่าเริ่มต้น)
512 24,576 0
(หรือจะไม่กำหนดค่าการคิดงบประมาณเลยก็ได้)
-1



ความซับซ้อนของงานสำหรับโมเดลการคิดทั้งหมด

  • งานง่ายๆ — ไม่จำเป็นต้องใช้ความคิดมากนัก
    คำขอตรงไปตรงมาที่ไม่จำเป็นต้องใช้การให้เหตุผลที่ซับซ้อน เช่น การดึงข้อมูลข้อเท็จจริงหรือการจัดประเภท ตัวอย่าง

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

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

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



สรุปความคิด

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

เหตุผลที่ควรใส่สรุปความคิดในคำตอบมีดังนี้

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

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

ข้อควรทราบที่สำคัญเกี่ยวกับสรุปความคิดมีดังนี้

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

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

เปิดใช้สรุปความคิด

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดของผู้ให้บริการรายนั้นๆ ในหน้านี้

คุณเปิดใช้สรุปความคิดได้โดยตั้งค่า includeThoughts เป็น true ในการกำหนดค่าโมเดล จากนั้นคุณจะเข้าถึงข้อมูลสรุปได้โดยตรวจสอบฟิลด์ thoughtSummary จากการตอบกลับ

ตัวอย่างต่อไปนี้แสดงวิธีเปิดใช้และดึงข้อมูลสรุปความคิด พร้อมคำตอบ

Swift

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(includeThoughts: true)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
)

let response = try await model.generateContent("solve x^2 + 4x + 4 = 0")

// Handle the response that includes thought summaries.
if let thoughtSummary = response.thoughtSummary {
  print("Thought Summary: \(thoughtSummary)")
}
guard let text = response.text else {
  fatalError("No text in response.")
}
print("Answer: \(text)")

Kotlin

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      includeThoughts = true
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_MODEL_NAME",
  generationConfig,
)

val response = model.generateContent("solve x^2 + 4x + 4 = 0")

// Handle the response that includes thought summaries.
response.thoughtSummary?.let {
    println("Thought Summary: $it")
}
response.text?.let {
    println("Answer: $it")
}

Java

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setIncludeThoughts(true)
    .build();

GenerationConfig generationConfig = GenerationConfig.builder()
    .setThinkingConfig(thinkingConfig)
    .build();

// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                  /* modelName */ "GEMINI_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// Handle the response that includes thought summaries.
ListenableFuture responseFuture = model.generateContent("solve x^2 + 4x + 4 = 0");
Futures.addCallback(responseFuture, new FutureCallback() {
    @Override
    public void onSuccess(GenerateContentResponse response) {
        if (response.getThoughtSummary() != null) {
            System.out.println("Thought Summary: " + response.getThoughtSummary());
        }
        if (response.getText() != null) {
            System.out.println("Answer: " + response.getText());
        }
    }

    @Override
    public void onFailure(Throwable t) {
        // Handle error
    }
}, MoreExecutors.directExecutor());

Web

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
const generationConfig = {
  thinkingConfig: {
    includeThoughts: true
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });

const result = await model.generateContent("solve x^2 + 4x + 4 = 0");
const response = result.response;

// Handle the response that includes thought summaries.
if (response.thoughtSummary()) {
    console.log(`Thought Summary: ${response.thoughtSummary()}`);
}
const text = response.text();
console.log(`Answer: ${text}`);

Dart

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
final thinkingConfig = ThinkingConfig(includeThoughts: true);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_MODEL_NAME',
  generationConfig: generationConfig,
);

final response = await model.generateContent('solve x^2 + 4x + 4 = 0');

// Handle the response that includes thought summaries.
if (response.thoughtSummary != null) {
  print('Thought Summary: ${response.thoughtSummary}');
}
if (response.text != null) {
  print('Answer: ${response.text}');
}

Unity

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
var thinkingConfig = new ThinkingConfig(includeThoughts: true);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
);

var response = await model.GenerateContentAsync("solve x^2 + 4x + 4 = 0");

// Handle the response that includes thought summaries.
if (response.ThoughtSummary != null) {
    Debug.Log($"Thought Summary: {response.ThoughtSummary}");
}
if (response.Text != null) {
    Debug.Log($"Answer: {response.Text}");
}

สรุปความคิดในสตรีม

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

Swift

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(includeThoughts: true)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
)

let stream = try model.generateContentStream("solve x^2 + 4x + 4 = 0")

// Handle the streamed response that includes thought summaries.
var thoughts = ""
var answer = ""
for try await response in stream {
  if let thought = response.thoughtSummary {
    if thoughts.isEmpty {
      print("--- Thoughts Summary ---")
    }
    print(thought)
    thoughts += thought
  }

  if let text = response.text {
    if answer.isEmpty {
      print("--- Answer ---")
    }
    print(text)
    answer += text
  }
}

Kotlin

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      includeThoughts = true
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_MODEL_NAME",
  generationConfig,
)

// Handle the streamed response that includes thought summaries.
var thoughts = ""
var answer = ""
model.generateContentStream("solve x^2 + 4x + 4 = 0").collect { response ->
    response.thoughtSummary?.let {
        if (thoughts.isEmpty()) {
            println("--- Thoughts Summary ---")
        }
        print(it)
        thoughts += it
    }
    response.text?.let {
        if (answer.isEmpty()) {
            println("--- Answer ---")
        }
        print(it)
        answer += it
    }
}

Java

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setIncludeThoughts(true)
    .build();

GenerationConfig generationConfig = GenerationConfig.builder()
    .setThinkingConfig(thinkingConfig)
    .build();

// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                  /* modelName */ "GEMINI_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// Streaming with Java is complex and depends on the async library used.
// This is a conceptual example using a reactive stream.
Flowable responseStream = model.generateContentStream("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries
StringBuilder thoughts = new StringBuilder();
StringBuilder answer = new StringBuilder();

responseStream.subscribe(response -> {
    if (response.getThoughtSummary() != null) {
        if (thoughts.length() == 0) {
            System.out.println("--- Thoughts Summary ---");
        }
        System.out.print(response.getThoughtSummary());
        thoughts.append(response.getThoughtSummary());
    }
    if (response.getText() != null) {
        if (answer.length() == 0) {
            System.out.println("--- Answer ---");
        }
        System.out.print(response.getText());
        answer.append(response.getText());
    }
}, throwable -> {
    // Handle error
});

Web

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
const generationConfig = {
  thinkingConfig: {
    includeThoughts: true
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });

const result = await model.generateContentStream("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries.
let thoughts = "";
let answer = "";
for await (const chunk of result.stream) {
  if (chunk.thoughtSummary()) {
    if (thoughts === "") {
      console.log("--- Thoughts Summary ---");
    }
    // In Node.js, process.stdout.write(chunk.thoughtSummary()) could be used
    // to avoid extra newlines.
    console.log(chunk.thoughtSummary());
    thoughts += chunk.thoughtSummary();
  }

  const text = chunk.text();
  if (text) {
    if (answer === "") {
      console.log("--- Answer ---");
    }
    // In Node.js, process.stdout.write(text) could be used.
    console.log(text);
    answer += text;
  }
}

Dart

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
final thinkingConfig = ThinkingConfig(includeThoughts: true);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_MODEL_NAME',
  generationConfig: generationConfig,
);

final responses = model.generateContentStream('solve x^2 + 4x + 4 = 0');

// Handle the streamed response that includes thought summaries.
var thoughts = '';
var answer = '';
await for (final response in responses) {
  if (response.thoughtSummary != null) {
    if (thoughts.isEmpty) {
      print('--- Thoughts Summary ---');
    }
    thoughts += response.thoughtSummary!;
  }
  if (response.text != null) {
    if (answer.isEmpty) {
      print('--- Answer ---');
    }
    answer += response.text!;
  }
}

Unity

เปิดใช้สรุปความคิดใน GenerationConfig ซึ่งเป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
var thinkingConfig = new ThinkingConfig(includeThoughts: true);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
);

var stream = model.GenerateContentStreamAsync("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries.
var thoughts = "";
var answer = "";
await foreach (var response in stream)
{
    if (response.ThoughtSummary != null)
    {
        if (string.IsNullOrEmpty(thoughts))
        {
            Debug.Log("--- Thoughts Summary ---");
        }
        Debug.Log(response.ThoughtSummary);
        thoughts += response.ThoughtSummary;
    }
    if (response.Text != null)
    {
        if (string.IsNullOrEmpty(answer))
        {
            Debug.Log("--- Answer ---");
        }
        Debug.Log(response.Text);
        answer += response.Text;
    }
}



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

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

  • เปิดใช้การคิดและสร้างความคิด
  • คำขอมีประกาศฟังก์ชัน

หากต้องการใช้ประโยชน์จากลายเซ็นความคิด ให้ใช้การเรียกฟังก์ชันตามปกติ Firebase AI Logic SDK ช่วยลดความซับซ้อนของกระบวนการโดยการจัดการสถานะ และจัดการลายเซ็นความคิดให้คุณโดยอัตโนมัติ SDK จะส่งลายเซ็นความคิดที่สร้างขึ้นระหว่างการเรียก sendMessage หรือ sendMessageStream ครั้งถัดไปโดยอัตโนมัติ ในเซสชัน Chat



การกำหนดราคาและการนับโทเค็นความคิด

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

คุณสามารถเปิดใช้การตรวจสอบ AI ในFirebaseคอนโซล เพื่อตรวจสอบจำนวนโทเค็นการคิดสำหรับคำขอที่เปิดใช้การคิด

คุณดูจำนวนโทเค็นการคิดทั้งหมดได้จากthoughtsTokenCount ฟิลด์ในแอตทริบิวต์ usageMetadata ของการตอบกลับ

Swift

// ...

let response = try await model.generateContent("Why is the sky blue?")

if let usageMetadata = response.usageMetadata {
  print("Thoughts Token Count: \(usageMetadata.thoughtsTokenCount)")
}

Kotlin

// ...

val response = model.generateContent("Why is the sky blue?")

response.usageMetadata?.let { usageMetadata ->
    println("Thoughts Token Count: ${usageMetadata.thoughtsTokenCount}")
}

Java

// ...

ListenableFuture<GenerateContentResponse> response =
    model.generateContent("Why is the sky blue?");

Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String usageMetadata = result.getUsageMetadata();
        if (usageMetadata != null) {
            System.out.println("Thoughts Token Count: " +
                usageMetadata.getThoughtsTokenCount());
        }
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web

// ...

const response = await model.generateContent("Why is the sky blue?");

if (response?.usageMetadata?.thoughtsTokenCount != null) {
    console.log(`Thoughts Token Count: ${response.usageMetadata.thoughtsTokenCount}`);
}

Dart

// ...

final response = await model.generateContent(
  Content.text("Why is the sky blue?"),
]);

if (response?.usageMetadata case final usageMetadata?) {
  print("Thoughts Token Count: ${usageMetadata.thoughtsTokenCount}");
}

Unity

// ...

var response = await model.GenerateContentAsync("Why is the sky blue?");

if (response.UsageMetadata != null)
{
    UnityEngine.Debug.Log($"Thoughts Token Count: {response.UsageMetadata?.ThoughtsTokenCount}");
}

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