อัปเดตแอป Firebase AI Logic แบบไดนามิกด้วยการกำหนดค่าระยะไกลของ Firebase

เมื่อเรียกใช้ Gemini API จากแอปโดยใช้ SDK Firebase AI Logic คำขอจะมีพารามิเตอร์หลายรายการที่ควบคุมคำตอบของ Generative AI โดยปกติแล้วข้อมูลนี้จะประกอบด้วยชื่อโมเดล การกำหนดค่ารุ่นของโมเดล (โทเค็นสูงสุด อุณหภูมิ ฯลฯ) การตั้งค่า ความปลอดภัย คำสั่งของระบบ และข้อมูลพรอมต์

ในกรณีส่วนใหญ่ คุณอาจต้องการเปลี่ยนการตั้งค่าเหล่านี้แบบออนดีมานด์หรือตามความจำเป็นสำหรับสถานการณ์ต่างๆ ดังนี้

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

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

คู่มือโซลูชันนี้จะระบุ Use Case ที่แนะนําโดยเฉพาะและอธิบายวิธี เพิ่ม Remote Config ลงในแอป Generative AI

ข้ามไปที่การติดตั้งใช้งานโค้ด

เหตุใดจึงควรใช้ Firebase Remote Config กับแอป

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

กรณีการใช้งานที่สำคัญสำหรับ Remote Config ด้วยแอป Generative AI

เราขอแนะนำให้ใช้ Remote Config กับ Firebase AI Logic สำหรับกรณีการใช้งานที่สำคัญต่อไปนี้

  • อัปเกรดเป็นโมเดลเวอร์ชันล่าสุดโดยไม่ต้องอัปเดตแอป ใช้พารามิเตอร์ Remote Config เพื่อเปลี่ยนชื่อโมเดลตามต้องการ เพื่อให้คุณอัปเกรดเป็นโมเดล Gemini เวอร์ชันล่าสุดที่ต้องการได้ทันทีที่พร้อมใช้งาน

  • อัปเดตวิธีการของระบบและการตั้งค่าความปลอดภัยโดยไม่ต้องอัปเดตแอป จัดเก็บวิธีการของระบบและการตั้งค่าความปลอดภัยไว้ในRemote Config พารามิเตอร์เพื่อให้คุณเปลี่ยนได้ตามต้องการหากพบปัญหาหลังจากการติดตั้งใช้งาน

  • ลดความเสี่ยงและบังคับใช้ความปลอดภัยของ AI: ใช้Remote Configการเปิดตัวเพื่อเผยแพร่การเปลี่ยนแปลงของ Generative AI ให้แก่ผู้ใช้ iOS และ Android อย่างปลอดภัยและค่อยเป็นค่อยไป

กรณีการใช้งานขั้นสูงและที่แนะนำสำหรับ Remote Config ด้วยแอป Generative AI

หลังจากติดตั้งเครื่องมือในแอปด้วย Remote Config และ Google Analytics คุณจะสำรวจกรณีการใช้งานขั้นสูงได้ดังนี้

  • ตั้งค่าตำแหน่งตามตำแหน่งของไคลเอ็นต์ ใช้Remote Configเงื่อนไขเพื่อ ตั้งค่าตำแหน่งที่คุณเข้าถึงโมเดล ตามตำแหน่งที่ตรวจพบของไคลเอ็นต์

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

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

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

    ตัวอย่างเช่น หากคุณใช้ Generative AI เพื่อให้การสนับสนุนทางเทคนิคในแอป คุณอาจต้องการตั้งค่าคำสั่งของระบบที่เฉพาะเจาะจงกับแพลตฟอร์มแอป เพื่อให้มั่นใจว่าผู้ใช้แพลตฟอร์ม Android, iOS และเว็บจะได้รับคำสั่งที่ถูกต้อง

  • ปรับเปลี่ยนประสบการณ์การใช้งานให้เหมาะกับผู้ใช้แต่ละราย ใช้Remote Configการปรับเปลี่ยนในแบบของคุณกับแอปและเกมบนอุปกรณ์เคลื่อนที่เพื่อกำหนดการตั้งค่า Generative AI ที่เหมาะสมที่สุดสำหรับผู้ใช้แต่ละรายโดยอัตโนมัติ

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

  • เพิ่มประสิทธิภาพประสบการณ์และผลลัพธ์ของแอป ใช้ A/B Testing กับ Remote Config กับแอปและเกมบนอุปกรณ์เคลื่อนที่เพื่อทดสอบการเปลี่ยนแปลงพารามิเตอร์ Generative AI ในกลุ่มผู้ใช้ต่างๆ เพื่อดูว่าการเปลี่ยนแปลงดังกล่าวส่งผลต่อเมตริกสําคัญ เช่น การรักษาผู้ใช้ไว้และรายได้อย่างไร

การติดตั้งเครื่องมือในแอป Generative AI ด้วย Firebase Remote Config จะช่วยให้คุณ สร้างแอปพลิเคชันที่ทำงานด้วยระบบ AI ที่ยืดหยุ่น ปลอดภัย และคุ้มค่า พร้อมทั้งสร้าง ประสบการณ์ที่น่าพึงพอใจสำหรับผู้ใช้

เพิ่ม Firebase Remote Config ลงในแอป

ในคู่มือโซลูชันนี้ คุณจะใช้ Firebase Remote Config เพื่อ อัปเดตพารามิเตอร์แบบไดนามิกในแอป Android ที่ใช้ Firebase AI Logic SDK คุณจะได้เรียนรู้วิธีต่อไปนี้

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

ข้อกำหนดเบื้องต้น

คู่มือนี้ถือว่าคุณคุ้นเคยกับการพัฒนาแอปสำหรับแพลตฟอร์มของคุณ

โปรดตรวจสอบว่าคุณได้ทำสิ่งต่อไปนี้ก่อนที่จะเริ่มต้น

  • ทําตามFirebase AI Logicคู่มือเริ่มต้นใช้งาน ซึ่งอธิบายวิธีตั้งค่าโปรเจ็กต์ Firebase เชื่อมต่อแอปกับ Firebase เพิ่ม SDK เริ่มต้นบริการแบ็กเอนด์ สําหรับผู้ให้บริการ "Gemini API" ที่เลือก และสร้างอินสแตนซ์โมเดล

  • เปิดใช้ Google Analytics ในโปรเจ็กต์ Firebase และเพิ่ม SDK ลงในแอป (ต้องใช้สําหรับการกําหนดเป้าหมายตามเงื่อนไข เช่น การตั้งค่าตําแหน่งที่คุณเข้าถึงโมเดลตามตําแหน่งของอุปกรณ์ไคลเอ็นต์)

ขั้นตอนที่ 1: ตั้งค่าพารามิเตอร์ในคอนโซล Firebase

สร้างRemote Configเทมเพลตไคลเอ็นต์และกำหนดค่าพารามิเตอร์และ ค่าเพื่อดึงและใช้ในแอป

  1. เปิดโปรเจ็กต์ Firebase ใน Firebase คอนโซล จากนั้นจากเมนูการนำทาง ให้ขยายเรียกใช้ แล้วเลือก Remote Config
  2. ตรวจสอบว่าได้เลือกไคลเอ็นต์จากตัวเลือกไคลเอ็นต์/เซิร์ฟเวอร์ ที่ด้านบนของหน้าแล้ว
  3. เริ่มเทมเพลตไคลเอ็นต์โดยคลิกสร้างการกำหนดค่า (หรือเพิ่มพารามิเตอร์หากเคยใช้เทมเพลตไคลเอ็นต์มาก่อน)
  4. กำหนดพารามิเตอร์ที่ต้องการควบคุมด้วย Remote Config เช่น

    ชื่อพารามิเตอร์ คำอธิบาย ประเภท ค่าเริ่มต้น
    model_name ชื่อโมเดล ดูชื่อโมเดลที่พร้อมใช้งาน สตริง gemini-2.5-flash
    system_instructions คำสั่งของระบบเปรียบเสมือน "คำนำ" ที่คุณเพิ่มก่อนที่โมเดลจะได้รับคำสั่งเพิ่มเติมจากผู้ใช้ปลายทางเพื่อมีอิทธิพลต่อลักษณะการทำงานของโมเดล สตริง You are a helpful assistant who knows everything there is to know about Firebase!
    prompt พรอมต์เริ่มต้นที่จะใช้กับฟีเจอร์ Generative AI สตริง I am a developer who wants to know more about Firebase!
    vertex_location ใช้ได้เฉพาะเมื่อใช้ Vertex AI Gemini API เท่านั้น
    ควบคุมตำแหน่งเพื่อเข้าถึงโมเดล คุณสามารถตั้งค่าเงื่อนไขเพื่อกำหนดค่าตัวเลือกนี้ตามสถานที่ตั้งของลูกค้าที่ Google Analytics ตรวจพบ
    สตริง global
  5. เมื่อเพิ่มพารามิเตอร์เสร็จแล้ว ให้คลิกเผยแพร่การเปลี่ยนแปลง หากนี่ไม่ใช่Remote Configเทมเพลตใหม่ ให้ตรวจสอบการเปลี่ยนแปลงแล้วคลิกเผยแพร่การเปลี่ยนแปลงอีกครั้ง

ขั้นตอนที่ 2: เพิ่มและเริ่มต้น Remote Config ในแอป

เพิ่มRemote Configไลบรารีและตั้งค่า Remote Config ภายในแอป

Swift

ในขั้นตอนFirebase AI Logicการตั้งค่า คุณได้เพิ่ม Firebase SDK ลงในแอปแล้ว แต่จะต้องเพิ่ม Remote Config ด้วย

  1. เปิดโปรเจ็กต์ใน Xcode แล้วไปที่File > Add Package Dependencies

  2. เลือก firebase-ios-sdk แล้วคลิกเพิ่มแพ็กเกจ

  3. จากแถบนำทางโปรเจ็กต์ ให้เลือกแอป > เป้าหมาย > แอป

  4. จากแท็บทั่วไป ให้เลื่อนไปที่เฟรมเวิร์ก ไลบรารี และ เนื้อหาที่ฝัง

  5. คลิก + แล้วเลือก FirebaseRemoteConfig จากนั้นคลิกเพิ่ม

  6. เพิ่ม FirebaseRemoteConfig import ลงในโค้ด

    import FirebaseRemoteConfig
    
  7. ภายในคลาสที่เหมาะสมสำหรับแอป ให้เริ่มต้น Firebase และเพิ่ม Remote Configลงในตรรกะของแอปพลิเคชันหลัก

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

    let remoteConfig = RemoteConfig.remoteConfig()
    let settings = RemoteConfigSettings()
    settings.minimumFetchInterval = 3600
    remoteConfig.configSettings = settings
    

    ในแอปเริ่มต้นอย่างรวดเร็ว โค้ดนี้จะอยู่ภายใน VertexAISampleApp ในคลาส AppDelegate

Kotlin

  1. เพิ่มทรัพยากร Dependency Remote Config ลงในไฟล์ Gradle ระดับโมดูล (ระดับแอป) (โดยปกติคือ app/build.gradle.kts หรือ app/build.gradle)

    dependencies {
        implementation(platform("com.google.firebase:firebase-bom:33.15.0"))
        implementation("com.google.firebase:firebase-ai")
        implementation("com.google.firebase:firebase-config")
        // ... other dependencies
    }
    
  2. เพิ่ม Remote Config ลงในตรรกะของแอปพลิเคชันหลัก โดยคุณจะ เริ่มต้น Remote Config และเพิ่มช่วงเวลาการดึงข้อมูลขั้นต่ำได้ดังนี้

    val remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig
    val configSettings = remoteConfigSettings {
    minimumFetchIntervalInSeconds = 3600
    }
    remoteConfig.setConfigSettingsAsync(configSettings)
    

Java

  1. เพิ่มทรัพยากร Dependency Remote Config ลงในไฟล์ Gradle ระดับโมดูล (ระดับแอป) (โดยปกติคือ app/build.gradle.kts หรือ app/build.gradle)

    dependencies {
        implementation(platform("com.google.firebase:firebase-bom:33.15.0"))
        implementation("com.google.firebase:firebase-ai")
        implementation("com.google.firebase:firebase-config")
        // ... other dependencies
    }
    
  2. เพิ่ม Remote Config ลงในตรรกะของแอปพลิเคชันหลัก โดยคุณจะ เริ่มต้น Remote Config และเพิ่มช่วงเวลาการดึงข้อมูลขั้นต่ำได้ดังนี้

    FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
        .setMinimumFetchIntervalInSeconds(3600)
        .build();
    mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
    

Web

  1. เปิดโค้ดในเครื่องมือแก้ไขข้อความและนำเข้า Remote Config ดังนี้

    import { getRemoteConfig } from 'firebase/remote-config';
    
  2. ภายในฟังก์ชันหลักและหลังจากที่เริ่มต้นแอป Firebase สําหรับ Firebase AI Logic SDK ให้เริ่มต้น Remote Config ดังนี้

      // Initialize Remote Config and get a reference to the service
      const remoteConfig = getRemoteConfig(app);
    
  3. กำหนดช่วงเวลาการดึงข้อมูลขั้นต่ำ

    remoteConfig.settings.minimumFetchIntervalMillis = 3600000;
    

Dart

  1. จากไดเรกทอรีโปรเจ็กต์ Flutter ให้ติดตั้งและเพิ่ม Remote Config โดยใช้คำสั่งต่อไปนี้

    flutter pub add firebase_remote_config
    
  2. เปิด ./lib/main.dart แล้วเพิ่มการนำเข้าหลังจากการนำเข้าอื่นๆ ที่คุณ เพิ่มเพื่อรองรับ Firebase AI Logic

    import 'package:firebase_vertexai/firebase_ai.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:firebase_remote_config/firebase_remote_config.dart';
    
  3. เพิ่มตัวแปร _modelName, _systemInstructions และ _prompt ลงใน แอปเพื่อให้เราใช้ตัวแปรเหล่านี้ได้ในภายหลัง

    late final String _modelName;
    late final String _systemInstructions;
    late final String _prompt;
    
  4. รับอินสแตนซ์ออบเจ็กต์ Remote Config แล้วตั้งช่วงการดึงข้อมูลขั้นต่ำ เพื่อให้มีการรีเฟรชบ่อยๆ อย่าลืมเพิ่มโค้ดนี้หลังจาก เริ่มต้น Firebase

      final remoteConfig = FirebaseRemoteConfig.instance;
      await remoteConfig.setConfigSettings(RemoteConfigSettings(
        fetchTimeout: const Duration(seconds: 3600),
        minimumFetchInterval: const Duration(seconds: 3600),
      ));
    

Unity

  1. เพิ่ม Remote Config ลงในโปรเจ็กต์ Unity โดยทำตามวิธีการเหล่านี้

  2. รับอินสแตนซ์ออบเจ็กต์ Remote Config แล้วตั้งช่วงการดึงข้อมูลขั้นต่ำ เพื่อให้มีการรีเฟรชบ่อยๆ อย่าลืมเพิ่มโค้ดนี้หลังจาก เริ่มต้น Firebase

    var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
    const int MillisecondsPerSecond = 1000;
    await remoteConfig.SetConfigSettingsAsync(new ConfigSettings() {
      FetchTimeoutInMilliseconds = 3600 * MillisecondsPerSecond,
      MinimumFetchIntervalInMilliseconds = 3600 * MillisecondsPerSecond
    });
    

ขั้นตอนที่ 3: ตั้งค่าพารามิเตอร์ในแอป

คุณควรกำหนดค่าพารามิเตอร์เริ่มต้นในแอปในออบเจ็กต์ Remote Config ซึ่งจะช่วยให้มั่นใจได้ว่าแอปจะทำงานตามที่คาดไว้แม้ว่าจะดึงค่าจากบริการ Remote Config ไม่ได้ก็ตาม

Swift

  1. ในคอนโซล Firebase ให้เปิด Remote Config

  2. ในแท็บพารามิเตอร์ ให้เปิด เมนู แล้วเลือกดาวน์โหลดค่าเริ่มต้น

  3. เมื่อได้รับข้อความแจ้ง ให้เปิดใช้ .plist สำหรับ iOS แล้วคลิกดาวน์โหลดไฟล์

  4. บันทึกไฟล์ในไดเรกทอรีแอปพลิเคชัน

    หากใช้แอปตัวอย่าง ให้บันทึกไว้ใน FirebaseVertexAI/Sample/VertexAISample

  5. ใน Xcode ให้คลิกขวาที่แอป แล้วเลือกเพิ่มไฟล์

    หากใช้ตัวอย่าง ให้คลิกขวาที่ VertexAISample แล้วเลือก เพิ่มไฟล์ไปยัง "VertexAISample"

  6. เลือก remote_config_defaults.plist แล้วคลิกเพิ่ม

  7. อัปเดตโค้ดของแอปเพื่ออ้างอิงไฟล์ค่าเริ่มต้น

    // Set default values
    remoteConfig.setDefaults(fromPlist: "remote_config_defaults")
    

Kotlin

  1. เปิด Remote Config จากคอนโซล Firebase

  2. ในแท็บพารามิเตอร์ ให้เปิด เมนู แล้วเลือกดาวน์โหลดค่าเริ่มต้น

  3. เมื่อระบบแจ้ง ให้เปิดใช้ .xml สำหรับ Android แล้วคลิกดาวน์โหลดไฟล์

  4. บันทึกไฟล์ในไดเรกทอรีทรัพยากร XML ของแอป

  5. อัปเดตไฟล์กิจกรรมหลักเพื่อเพิ่มค่าเริ่มต้นหลังจาก configSettings ที่คุณเพิ่มไว้ก่อนหน้านี้

    // Set default values.
    remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
    

Java

  1. ในคอนโซล Firebase ให้เปิด Remote Config

  2. ในแท็บพารามิเตอร์ ให้เปิด เมนู แล้วเลือกดาวน์โหลดค่าเริ่มต้น

  3. เมื่อระบบแจ้ง ให้เปิดใช้ .xml สำหรับ Android แล้วคลิกดาวน์โหลดไฟล์

  4. บันทึกไฟล์ในไดเรกทอรีทรัพยากร XML ของแอป

  5. อัปเดตไฟล์กิจกรรมหลักเพื่อเพิ่มค่าเริ่มต้นหลังจาก configSettings ที่คุณเพิ่มไว้ก่อนหน้านี้

    // Set default values.
    mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);
    

Web

คุณตั้งค่าเริ่มต้นได้โดยตรงในโค้ด

// Set default Remote Config parameter values
remoteConfig.defaultConfig = {
  model_name: 'gemini-2.5-flash',
  system_instructions:
    'You are a helpful assistant who knows everything there is to know about Firebase!',
  prompt: 'I am a developer who wants to know more about Firebase!',
  vertex_location: 'global',
};

Dart

คุณตั้งค่าเริ่มต้นได้โดยตรงในโค้ด

remoteConfig.setDefaults(const {
  "model_name": "gemini-2.5-flash",
  "system_instructions": "You are a helpful assistant who knows everything there is to know about Firebase!",
  "prompt": "I am a developer who wants to know more about Firebase!",
  "vertex_location": "global"
});

Unity

คุณตั้งค่าเริ่มต้นได้โดยตรงในโค้ด

await remoteConfig.SetDefaultsAsync(
  new System.Collections.Generic.Dictionary<string, object>() {
    { "model_name", "gemini-2.5-flash" },
    { "system_instructions", "You are a helpful assistant who knows everything there is to know about Firebase!" },
    { "prompt", "I am a developer who wants to know more about Firebase!" },
    { "vertex_location", "global" }
  }
);

ขั้นตอนที่ 4: ดึงข้อมูลและเปิดใช้งานค่า

หลังจากตั้งค่าเริ่มต้นแล้ว ให้เพิ่มข้อมูลต่อไปนี้เพื่อดึงข้อมูลและเปิดใช้งานค่า

Swift

// Fetch and activate Remote Config values
remoteConfig.fetchAndActivate { status, error in
  if let error = error {
    print("Error fetching Remote Config: \(error.localizedDescription)")
  }
}

ซึ่งควรจะอัปเดตออบเจ็กต์ Remote Config ทุกครั้งที่มีการเผยแพร่เทมเพลต Remote Config ใหม่

Kotlin

// Fetch and activate Remote Config values
remoteConfig.fetchAndActivate()
      .addOnCompleteListener(this) { task ->
          if (task.isSuccessful) {
              val updated = task.result
              Log.d(TAG, "Remote Config values fetched and activated: $updated")
          } else {
              Log.e(TAG, "Error fetching Remote Config", task.exception)
          }
      }

Java

  // Fetch and activate Remote Config values
  mFirebaseRemoteConfig.fetchAndActivate()
    .addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
        @Override
        public void onComplete(@NonNull Task<Boolean> task) {
            if (task.isSuccessful()) {
                boolean updated = task.getResult();
                Log.d(TAG, "Config params updated: " + updated);
            } else {
                Log.e(TAG, "Error fetching Remote Config", task.exception)
            }
          }
    });

Web

  1. เพิ่ม getValue และ fetchAndActivate ลงในการนำเข้า

    import { getValue, fetchAndActivate } from 'firebase/remote-config';
    
  2. หลังจากโค้ดที่คุณเพิ่มเพื่อกำหนดค่าเริ่มต้นของค่า Remote Config ให้ดึงข้อมูลและเปิดใช้งานการกำหนดค่า จากนั้นกำหนดค่าให้กับค่าคงที่ modelName systemInstructions, prompt และ vertexLocation

    // Fetch and activate Remote Config.
    try {
      await fetchAndActivate(remoteConfig);
    } catch(err) {
      console.error('Remote Config fetch failed', err);
    }
    
    console.log('Remote Config fetched.');
    
    // Assign Remote Config values.
    const modelName = getValue(remoteConfig, 'model_name').asString();
    const systemInstructions = getValue(remoteConfig, 'system_instructions').asString();
    const prompt = getValue(remoteConfig, 'prompt').asString();
    const vertexLocation = getValue(remoteConfig, 'vertex_location').asString();
    

Dart

// Fetch and activate Remote Config.
remoteConfig.fetchAndActivate();

// Assign Remote Config values.
String? _modelName = remoteConfig.getString("model_name");
String? _systemInstructions = remoteConfig.getString("system_instructions");
String? _prompt = remoteConfig.getString("prompt");
String? _vertexLocation = remoteConfig.getString("vertex_location");

Unity

// Fetch and activate Remote Config values.
await remoteConfig.FetchAndActivateAsync();

ขั้นตอนที่ 5: เพิ่มRemote Configผู้ฟังแบบเรียลไทม์

เพิ่มเครื่องมือฟังRemote Configแบบเรียลไทม์ ลงในแอปเพื่อให้แน่ใจว่าการเปลี่ยนแปลงที่คุณทำกับRemote Configเทมเพลต จะเผยแพร่ไปยังไคลเอ็นต์ทันทีที่มีการอัปเดต

โค้ดต่อไปนี้จะอัปเดตออบเจ็กต์ Remote Config ทุกครั้งที่ค่าพารามิเตอร์ มีการเปลี่ยนแปลง

Swift

// Add real-time Remote Config
remoteConfig.addOnConfigUpdateListener { configUpdate, error in
  guard let configUpdate = configUpdate, error == nil else {
    print("Error listening for config updates: \(error?.localizedDescription ?? "No error available")")
    return
  }

  print("Updated keys: \(configUpdate.updatedKeys)")
  remoteConfig.activate { changed, error in
    guard error == nil else {
      print("Error activating config: \(error?.localizedDescription ?? "No error available")")
      return
    }
    print("Activated config successfully")
  }
}

Kotlin

นอกจากนี้ คุณยังกําหนดค่าการกระทําภายใน addOnCompleteListenerการเปิดใช้งานได้ด้วย (ไม่บังคับ)

      // Add a real-time Remote Config listener
      remoteConfig.addOnConfigUpdateListener(object : ConfigUpdateListener {
          override fun onUpdate(configUpdate : ConfigUpdate) {
              Log.d(ContentValues.TAG, "Updated keys: " + configUpdate.updatedKeys);
              remoteConfig.activate().addOnCompleteListener {
                  // Optionally, add an action to perform on update here.
              }
          }

          override fun onError(error : FirebaseRemoteConfigException) {
              Log.w(ContentValues.TAG, "Config update error with code: " + error.code, error)
          }
      }

Java

นอกจากนี้ คุณยังกําหนดค่าการกระทําภายใน addOnCompleteListenerการเปิดใช้งานได้ด้วย (ไม่บังคับ)

  // Add a real-time Remote Config listener
  remoteConfig.addOnConfigUpdateListener(new ConfigUpdateListener() {
      @Override
      public void onUpdate(ConfigUpdate configUpdate) {
          Log.d(ContentValues.TAG, "Updated keys: " + configUpdate.getUpdatedKeys());
                remoteConfig.activate().addOnCompleteListener(new OnCompleteListener<Boolean>() {
                  @Override
                  public void onComplete(@NonNull Task<Boolean> task) {
                      // Optionally, add an action to perform on update here.
                  }
              });
          }

      @Override
      public void onError(FirebaseRemoteConfigException error) {
          Log.w(ContentValues.TAG, "Config update error with code: " + error.getCode(), error);
      }
  });

Web

ระบบไม่รองรับRemote Configผู้ฟังแบบเรียลไทม์สำหรับเว็บแอป

Dart

// Add a real-time Remote Config listener
remoteConfig.onConfigUpdated.listen((event) async {
  await remoteConfig.activate();
});

Unity

// Add a real-time Remote Config listener to automatically update whenever
// a new template is published.
// Note: the parameters can be anonymous as they are unused.

remoteConfig.OnConfigUpdateListener += (_, _) => {
  remoteConfig.ActivateAsync();
};

ขั้นตอนที่ 6: อัปเดตคำขอ Gemini API เพื่อใช้ค่า Remote Config

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

ตอนนี้ Remote Config ได้รับการกำหนดค่าอย่างสมบูรณ์แล้ว ให้อัปเดตโค้ดเพื่อแทนที่ค่าที่ฮาร์ดโค้ดด้วยค่าที่มาจาก Remote Config

Swift

// Initialize the Gemini Developer API backend service
// The Gemini Developer API doesn't support setting the location of a model
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` and add system instructions into its config
// Both the model name and the system instructions will be sourced from Remote Config
let modelName = remoteConfig.configValue(forKey: "model_name").stringValue
let systemInstructions = remoteConfig.configValue(forKey: "system_instructions").stringValue

let model = ai.generativeModel(
  modelName: modelName,
  systemInstruction: ModelContent(role: "system", parts: systemInstructions)
)

// Provide a prompt that contains text
// The text in the prompt will be sourced from Remote Config
let userPrompt = remoteConfig.configValue(forKey: "prompt").stringValue

// To generate text output, call `generateContent` with the text input
let response = try await model.generateContent(userPrompt)
if let text = response.text {
  print(text)
}

Kotlin

// Initialize the Gemini Developer API backend service
// The Gemini Developer API doesn't support setting the location of a model
val ai = Firebase.ai(backend = GenerativeBackend.googleAI())

// Create a `GenerativeModel` and add system instructions into its config
// Both the model name and the system instructions will be sourced from Remote Config
val model = ai.generativeModel(
  modelName = remoteConfig.getString("model_name"),
  systemInstruction = content { text(remoteConfig.getString("system_instructions")) }
)

// To generate text output, call `generateContent` with the text input
// The text in the prompt will be sourced from Remote Config
val response = model.generateContent(remoteConfig.getString("prompt"))
print(response.text)

Java

// Initialize the Gemini Developer API backend service
// The Gemini Developer API doesn't support setting the location of a model
FirebaseAI ai = FirebaseAI.getInstance(GenerativeBackend.googleAI());

// Create a `GenerativeModel` and add system instructions into its config
// Both the model name and the system instructions will be sourced from Remote Config
GenerativeModel gm = ai.generativeModel(
        /* modelName */ remoteConfig.getString("model_name"),
        /* generationConfig (optional) */ null,
        /* safetySettings (optional) */ null,
        /* tools (optional) */ null,
        /* toolsConfig (optional) */ null,
        /* systemInstruction (optional) */ new Content.Builder().addText(
                remoteConfig.getString("system_instructions")).build(),
        /* requestOptions (optional) */ new RequestOptions()
);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// Provide a prompt that contains text
// The text in the prompt will be sourced from Remote Config
Content userPrompt = new Content.Builder()
        .addText(remoteConfig.getString("prompt"))
        .build();

// To generate text output, call `generateContent` with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(userPrompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
    }

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

Web

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service
// The Gemini Developer API doesn't support setting the location of a model
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Create a `GenerativeModel` and add system instructions into its config
// Both the model name and the system instructions will be sourced from Remote Config
const model = getGenerativeModel(ai, {
  model: modelName,
  systemInstruction: systemInstruction
});

// Wrap in an async function so you can use await
async function run() {
  // Provide a prompt that contains text
  // The text in the prompt will be sourced from Remote Config
  const userPrompt = prompt;

  // To generate text output, call `generateContent` with the text input
  const result = await model.generateContent(userPrompt);

  const response = result.response;
  const text = response.text();
  console.log(text);
}

Dart

// Initialize the Gemini Developer API backend service
// The Gemini Developer API doesn't support setting the location of a model
final ai = await FirebaseAI.googleAI();

// Create a `GenerativeModel` and add system instructions into its config
// Both the model name and the system instructions will be sourced from Remote Config
final model =
      ai.generativeModel(
        model: _modelName,
        systemInstruction: Content.system(_systemInstructions),
      );

// Provide a prompt that contains text
// The text in the prompt will be sourced from Remote Config
final _userPrompt = [Content.text(_prompt)];

// To generate text output, call `generateContent` with the text input
final response = await model.generateContent(_userPrompt);
print(response.text);

Unity

// Initialize the Gemini Developer API backend service
// The Gemini Developer API doesn't support setting the location of a model
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());

// Create a `GenerativeModel` and add system instructions into its config
// Both the model name and the system instructions will be sourced from Remote Config
var modelName = remoteConfig.GetValue("model_name").StringValue;
var systemInstructions = remoteConfig.GetValue("system_instructions").StringValue;

var model = ai.GetGenerativeModel(
  modelName: modelName,
  systemInstruction: ModelContent.Text(systemInstructions)
);

// Provide a prompt that contains text
// The text in the prompt will be sourced from Remote Config
var userPrompt = remoteConfig.GetValue("prompt").StringValue;

// To generate text output, call `GenerateContentAsync` with the text input
var response = await model.GenerateContentAsync(userPrompt);
UnityEngine.Debug.Log(response.Text ?? "No text in response.");

ขั้นตอนที่ 7: เรียกใช้แอป

สร้างและเรียกใช้แอป แล้วตรวจสอบว่าแอปทำงานได้ ทำการเปลี่ยนแปลงการกำหนดค่าจากหน้า Remote Config ในคอนโซล Firebase เผยแพร่การเปลี่ยนแปลง และยืนยันผลลัพธ์

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

  • เรียนรู้เพิ่มเติมเกี่ยวกับ Remote Config

  • เพิ่ม Google Analytics ลงในโค้ดไคลเอ็นต์เพื่อเปิดใช้การกำหนดเป้าหมาย

  • สำหรับแอปและเกมบนอุปกรณ์เคลื่อนที่