กำลังคิด

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

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

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

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

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

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

ใช้โมเดลการคิด

ใช้โมเดลการคิดเช่นเดียวกับที่คุณใช้Geminiโมเดลอื่นๆ (เริ่มต้นGemini APIผู้ให้บริการที่คุณเลือก สร้างอินสแตนซ์ GenerativeModel ฯลฯ) โมเดลเหล่านี้สามารถใช้สำหรับงานสร้างข้อความหรือโค้ด เช่น การสร้างเอาต์พุตที่มีโครงสร้าง หรือการวิเคราะห์อินพุตแบบมัลติโมดัล (เช่น รูปภาพ วิดีโอ เสียง หรือ PDF) คุณยังใช้โมเดลการคิดได้เมื่อสตรีมเอาต์พุต

รุ่นที่รองรับความสามารถนี้

เฉพาะรุ่น Gemini 2.5 เท่านั้นที่รองรับความสามารถนี้

  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite
ค่าเริ่มต้น

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

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

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

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

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

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

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

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

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

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

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

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

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

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

กำหนดงบประมาณการคิดใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel การกำหนดค่าจะยังคงอยู่ตลอดอายุการใช้งานของอินสแตนซ์ หากต้องการใช้งบประมาณการคิดที่แตกต่างกันสำหรับคำขอที่แตกต่างกัน ให้สร้างอินสแตนซ์ 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_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_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_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_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(thinkingBudget: 1024);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_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_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

ปิดใช้การคิด

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

ในสถานการณ์เหล่านี้ คุณสามารถปิดใช้ (หรือปิด) การคิดได้โดยทำดังนี้

  • Gemini 2.5 Pro: ปิดใช้การคิดไม่ได้
  • Gemini 2.5 Flash: ตั้งค่า thinkingBudget เป็น 0 โทเค็น
  • Gemini 2.5 Flash‑Lite: ปิดใช้การคิดโดยค่าเริ่มต้น

ส่งเสริมการคิดแบบไดนามิก

คุณสามารถให้โมเดลตัดสินใจได้ว่าจะคิดเมื่อใดและคิดมากน้อยเพียงใด (เรียกว่าการคิดแบบไดนามิก) โดยตั้งค่า thinkingBudget เป็น -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}");
}

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