กำลังคิด

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

ระดับการคิด

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

ตั้งค่าระดับการคิด

คลิกผู้ให้บริการ 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 4 Argon
gemini-4-argon
1 (ค่าเริ่มต้น)
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-4-argon, gemini-3.8-flash และ gemini-3.7-flash ไม่รองรับ ระดับการคิด MINIMAL หากตั้งค่าระดับการคิดเป็น MINIMAL คำขอจะล้มเหลวพร้อมข้อผิดพลาด 400



โควต้าการคิด (โมเดล Gemini 2.5)

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



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

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

    • "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}");
}

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