เมื่อเรียกใช้ Gemini API จากแอปโดยใช้ Firebase AI Logic SDK คำขอของคุณจะมีพารามิเตอร์จำนวนหนึ่งที่ควบคุมการตอบกลับของ Generative AI โดยปกติแล้วข้อมูลเหล่านี้จะรวมถึงชื่อโมเดล การกำหนดค่าการสร้างโมเดล (เช่น โทเค็นเอาต์พุตสูงสุด) การตั้งค่าความปลอดภัย คำสั่งของระบบ และข้อมูลพรอมต์
ในกรณีส่วนใหญ่ คุณจะต้องเปลี่ยนการตั้งค่าเหล่านี้ตามต้องการหรือตามความจำเป็นในสถานการณ์ต่างๆ ดังนี้
- อัปเดตโมเดล Generative AI โดยไม่ต้องเผยแพร่แอปใหม่ คุณสามารถ อัปเกรดเป็นโมเดลเวอร์ชันใหม่กว่าและเสถียรกว่าก่อนที่เวอร์ชันก่อนหน้าจะ ปิดตัวลง ลดระดับไปใช้โมเดลที่มีต้นทุนต่ำกว่าหรือมีประสิทธิภาพสูงกว่าตาม ความต้องการและคุณลักษณะของผู้ใช้ หรือติดตั้งใช้งานโมเดลล่าสุด และดีที่สุดแบบมีเงื่อนไขกับกลุ่มผู้ใช้ที่เฉพาะเจาะจง (เช่น ผู้ทดสอบเวอร์ชันเบต้า)
- ตั้งค่าตำแหน่งที่คุณเข้าถึงโมเดลเพื่อให้ใกล้กับผู้ใช้มากขึ้น
- ทำการทดสอบ A/B กับคำสั่งและพรอมต์ของระบบที่แตกต่างกัน จากนั้นค่อยๆ เปิดตัวค่าการทดสอบที่ได้ผลดีที่สุดต่อผู้ใช้
- ใช้ฟีเจอร์แฟล็กเพื่อแสดงหรือซ่อนฟีเจอร์ 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 แล้ว คุณจะสำรวจ Use Case ขั้นสูงได้ดังนี้
ตั้งค่าตำแหน่งตามตำแหน่งของไคลเอ็นต์: ใช้เงื่อนไข Remote Config เพื่อตั้งค่าตำแหน่งที่คุณเข้าถึงโมเดลตามตำแหน่งที่ตรวจพบของไคลเอ็นต์
ทดลองใช้โมเดลต่างๆ: ทดสอบและสลับไปมาระหว่างโมเดล Generative AI ต่างๆ ได้อย่างรวดเร็ว หรือแม้แต่เข้าถึงโมเดลต่างๆ สำหรับกลุ่มผู้ใช้ที่แตกต่างกัน เพื่อค้นหาโมเดลที่เหมาะสมที่สุดสำหรับ Use Case ที่เฉพาะเจาะจงของคุณ
เพิ่มประสิทธิภาพโมเดล: ปรับแต่งพารามิเตอร์ของโมเดล เช่น พรอมต์ของระบบ, โทเค็นเอาต์พุตสูงสุด, อุณหภูมิ และการตั้งค่าอื่นๆ
ใช้คำสั่งของระบบ พรอมต์ และการกำหนดค่าโมเดลที่แตกต่างกันตามแอตทริบิวต์ของไคลเอ็นต์: เมื่อใช้ 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 เพื่ออัปเดตพารามิเตอร์ในแอปที่ใช้ SDK ของ Firebase AI Logic แบบไดนามิก คุณจะได้เรียนรู้วิธีต่อไปนี้
- ดึงข้อมูลและเปิดใช้งานพารามิเตอร์ เช่น ชื่อโมเดลและคำสั่งของระบบ จาก Firebase Remote Config
- อัปเดตการเรียก Gemini API ให้ใช้พารามิเตอร์ที่ดึงข้อมูลแบบไดนามิก ซึ่งช่วยให้คุณสลับระหว่างโมเดลต่างๆ หรือแก้ไขคำสั่งของระบบได้โดยไม่ต้องอัปเดตแอป
- ควบคุมพารามิเตอร์จากระยะไกล โดยปรับลักษณะการทำงานและความสามารถของโมเดลตามต้องการ
ข้อกำหนดเบื้องต้น
คู่มือนี้ถือว่าคุณคุ้นเคยกับการพัฒนาแอปสำหรับแพลตฟอร์มของคุณแล้ว
ก่อนเริ่มต้น โปรดตรวจสอบว่าคุณได้ดำเนินการต่อไปนี้
ทําตาม Firebase AI Logicคู่มือการเริ่มต้นใช้งาน ซึ่งอธิบายวิธีตั้งค่าโปรเจ็กต์ Firebase เชื่อมต่อแอปกับ Firebase, เพิ่ม SDK, เริ่มต้นบริการแบ็กเอนด์ สําหรับผู้ให้บริการ "Gemini API" ที่เลือก และสร้างอินสแตนซ์โมเดล
เปิดใช้ Google Analytics ในโปรเจ็กต์ Firebase และเพิ่ม SDK ลงในแอป (ต้องใช้สําหรับการกําหนดเป้าหมายแบบมีเงื่อนไข เช่น การตั้งค่าตําแหน่งที่คุณเข้าถึงโมเดลตามตําแหน่งของอุปกรณ์ไคลเอ็นต์)
ขั้นตอนที่ 1: กำหนดค่าพารามิเตอร์ในคอนโซล Firebase
สร้างเทมเพลตไคลเอ็นต์ Remote Config และกำหนดค่าพารามิเตอร์และค่าต่างๆ เพื่อดึงข้อมูลและใช้ในแอป
ในคอนโซล Firebase ให้ไปที่ DevOps และการมีส่วนร่วม > Remote Config
จากตัวเลือกไคลเอ็นต์/เซิร์ฟเวอร์ที่ด้านบนของหน้า ให้เลือกไคลเอ็นต์
เริ่มเทมเพลตไคลเอ็นต์โดยคลิกสร้างการกำหนดค่า (หรือเพิ่มพารามิเตอร์หากคุณเคยใช้เทมเพลตไคลเอ็นต์มาก่อน)
กำหนดพารามิเตอร์ที่ต้องการควบคุมด้วย Remote Config เช่น
ชื่อพารามิเตอร์ คำอธิบาย ประเภท ค่าเริ่มต้น model_nameชื่อโมเดล ดูชื่อโมเดลที่พร้อมใช้งาน สตริง gemini-3.8-flashsystem_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!agent_platform_locationใช้ได้เฉพาะเมื่อใช้ Agent Platform Gemini API (formerly Vertex AI) เท่านั้น
ควบคุมตำแหน่ง เพื่อเข้าถึงโมเดล คุณสามารถตั้งค่าเงื่อนไขเพื่อกำหนดค่าตัวเลือกนี้ ตามตำแหน่งของลูกค้าที่ Google Analytics ตรวจพบสตริง globalเมื่อเพิ่มพารามิเตอร์เสร็จแล้ว ให้คลิกเผยแพร่การเปลี่ยนแปลง หากนี่ไม่ใช่Remote Configเทมเพลตใหม่ ให้ตรวจสอบการเปลี่ยนแปลงแล้วคลิกเผยแพร่การเปลี่ยนแปลงอีกครั้ง
ขั้นตอนที่ 2: เพิ่มและเริ่มต้นใช้งาน Remote Config ในแอป
เพิ่มRemote Configไลบรารีและตั้งค่า Remote Config ภายในแอป
Swift
คุณได้เพิ่ม Firebase SDK ลงในแอปแล้วในขั้นตอนการตั้งค่า Firebase AI Logic แต่จะต้องเพิ่ม Remote Config ด้วย
ใน Xcode เมื่อเปิดโปรเจ็กต์แล้ว ให้ไปที่ไฟล์ > เพิ่มการอ้างอิง แพ็กเกจ
เลือก firebase-ios-sdk แล้วคลิกเพิ่มแพ็กเกจ
จากแถบนำทางของโปรเจ็กต์ ให้เลือกแอป > เป้าหมาย > แอปของคุณ
จากแท็บทั่วไป ให้เลื่อนไปที่เฟรมเวิร์ก ไลบรารี และ เนื้อหาที่ฝัง
คลิก + แล้วเลือก FirebaseRemoteConfig จากนั้นคลิกเพิ่ม
เพิ่ม
FirebaseRemoteConfigimport ลงในโค้ดimport FirebaseRemoteConfigภายในคลาสที่เหมาะสมสำหรับแอป ให้เริ่มต้น Firebase และเพิ่ม Remote Config ลงในตรรกะแอปพลิเคชันหลัก
ในส่วนนี้ คุณจะรวม Remote Config และRemote Config Listener แบบเรียลไทม์เป็นการนําเข้าเพื่อให้แอปดึงค่าใหม่แบบเรียลไทม์ได้ และเพิ่มช่วงเวลาการดึงข้อมูลขั้นต่ำดังนี้
let remoteConfig = RemoteConfig.remoteConfig() let settings = RemoteConfigSettings() settings.minimumFetchInterval = 3600 remoteConfig.configSettings = settings
Kotlin
เพิ่มทรัพยากร Dependency Remote Config ลงในไฟล์ Gradle ระดับโมดูล (ระดับแอป) (โดยปกติคือ
app/build.gradle.ktsหรือapp/build.gradle)dependencies { implementation(platform("com.google.firebase:firebase-bom:35.0.0")) implementation("com.google.firebase:firebase-ai") implementation("com.google.firebase:firebase-config") // ... other dependencies }เพิ่ม Remote Config ลงในตรรกะแอปพลิเคชันหลัก ในส่วนนี้ คุณจะเริ่มต้น Remote Config และเพิ่มช่วงเวลาการดึงข้อมูลขั้นต่ำ
val remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig val configSettings = remoteConfigSettings { minimumFetchIntervalInSeconds = 3600 } remoteConfig.setConfigSettingsAsync(configSettings)
Java
เพิ่มทรัพยากร Dependency Remote Config ลงในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ
app/build.gradle.ktsหรือapp/build.gradle)dependencies { implementation(platform("com.google.firebase:firebase-bom:35.0.0")) implementation("com.google.firebase:firebase-ai") implementation("com.google.firebase:firebase-config") // ... other dependencies }เพิ่ม Remote Config ลงในตรรกะของแอปพลิเคชันหลัก ในส่วนนี้ คุณจะเริ่มต้น Remote Config และเพิ่มช่วงเวลาการดึงข้อมูลขั้นต่ำ
FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() .setMinimumFetchIntervalInSeconds(3600) .build(); mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
Web
เปิดโค้ดในเครื่องมือแก้ไขข้อความ แล้วนำเข้า Remote Config ดังนี้
import { getRemoteConfig } from 'firebase/remote-config';ภายในฟังก์ชันหลักและหลังจากที่เริ่มต้นแอป Firebase สําหรับ Firebase AI Logic SDK ให้เริ่มต้น Remote Config ดังนี้
// Initialize Remote Config and get a reference to the service const remoteConfig = getRemoteConfig(app);กำหนดช่วงเวลาการดึงข้อมูลขั้นต่ำดังนี้
remoteConfig.settings.minimumFetchIntervalMillis = 3600000;
Dart
จากไดเรกทอรีโปรเจ็กต์ Flutter ให้ติดตั้งและเพิ่ม Remote Config โดยใช้คำสั่งต่อไปนี้
flutter pub add firebase_remote_configเปิด
./lib/main.dartและเพิ่มการนำเข้าหลังจากการนำเข้าอื่นๆ ที่คุณเพิ่มเพื่อรองรับ Firebase AI Logic ดังนี้import 'package:firebase_ai/firebase_ai.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_remote_config/firebase_remote_config.dart';เพิ่มตัวแปร
_modelName,_systemInstructionsและ_promptลงในแอปเพื่อให้คุณใช้ในภายหลังได้late final String _modelName; late final String _systemInstructions; late final String _prompt;สร้างอินสแตนซ์ออบเจ็กต์ Remote Config แล้วตั้งช่วงการดึงข้อมูล ขั้นต่ำเพื่อให้มีการรีเฟรชบ่อยๆ โปรดเพิ่มโค้ดนี้หลังจาก เริ่มต้น Firebase แล้ว
final remoteConfig = FirebaseRemoteConfig.instance; await remoteConfig.setConfigSettings(RemoteConfigSettings( fetchTimeout: const Duration(seconds: 3600), minimumFetchInterval: const Duration(seconds: 3600), ));
Unity
เพิ่ม Remote Config ลงในโปรเจ็กต์ Unity โดยทำตามวิธีการเหล่านี้
สร้างอินสแตนซ์ออบเจ็กต์ 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
ในคอนโซล Firebase ให้ไปที่หน้า DevOps และการมีส่วนร่วม > การกำหนดค่าระยะไกล > พารามิเตอร์
เปิด
เมนู แล้ว เลือกดาวน์โหลดค่าเริ่มต้นเมื่อได้รับข้อความแจ้ง ให้เปิดใช้ .plist สำหรับ iOS แล้วคลิกดาวน์โหลดไฟล์
บันทึกไฟล์ในไดเรกทอรีแอปพลิเคชัน
ใน Xcode ให้คลิกขวาที่แอป แล้วเลือกเพิ่มไฟล์
เลือก remote_config_defaults.plist แล้วคลิกเพิ่ม
อัปเดตโค้ดแอปเพื่ออ้างอิงไฟล์ค่าเริ่มต้น โดยทำดังนี้
// Set default values for Remote Config parameters. remoteConfig.setDefaults(fromPlist: "remote_config_defaults")
Kotlin
ในคอนโซล Firebase ให้ไปที่หน้า DevOps และการมีส่วนร่วม > การกำหนดค่าระยะไกล > พารามิเตอร์
เปิด
เมนู แล้วเลือกดาวน์โหลดค่าเริ่มต้นเมื่อได้รับข้อความแจ้ง ให้เปิดใช้ .xml สำหรับ Android แล้วคลิกดาวน์โหลดไฟล์
บันทึกไฟล์ในไดเรกทอรีทรัพยากร XML ของแอป
อัปเดตไฟล์กิจกรรมหลักเพื่อเพิ่มค่าเริ่มต้นหลังจาก
configSettingsที่คุณเพิ่มไว้ก่อนหน้านี้// Set default values for Remote Config parameters. remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
Java
ในคอนโซล Firebase ให้ไปที่หน้า DevOps และการมีส่วนร่วม > การกำหนดค่าระยะไกล > พารามิเตอร์
เปิด
เมนู แล้วเลือกดาวน์โหลดค่าเริ่มต้นเมื่อได้รับข้อความแจ้ง ให้เปิดใช้ .xml สำหรับ Android แล้วคลิกดาวน์โหลดไฟล์
บันทึกไฟล์ในไดเรกทอรีทรัพยากร XML ของแอป
อัปเดตไฟล์กิจกรรมหลักเพื่อเพิ่มค่าเริ่มต้นหลังจาก
configSettingsที่คุณเพิ่มไว้ก่อนหน้านี้// Set default values for Remote Config parameters. mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);
Web
คุณตั้งค่าเริ่มต้นในโค้ดได้โดยตรง ดังนี้
// Set default values for Remote Config parameters.
remoteConfig.defaultConfig = {
model_name: 'gemini-3.8-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!',
agent_platform_location: 'global',
};
Dart
คุณสามารถตั้งค่าเริ่มต้นได้โดยตรงในโค้ด
// Set default values for Remote Config parameters.
remoteConfig.setDefaults(const {
"model_name": "gemini-3.8-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!",
"agent_platform_location": "global"
});
Unity
คุณสามารถตั้งค่าเริ่มต้นได้โดยตรงในโค้ด
// Set default values for Remote Config parameters.
await remoteConfig.SetDefaultsAsync(
new System.Collections.Generic.Dictionary<string, object>() {
{ "model_name", "gemini-3.8-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!" },
{ "agent_platform_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
เพิ่ม
getValueและfetchAndActivateลงในการนำเข้าimport { getValue, fetchAndActivate } from 'firebase/remote-config';ค้นหาโค้ดที่คุณระบุค่า Remote Config เริ่มต้น ต่อจากโค้ดบล็อกนั้น ให้เพิ่มโค้ดต่อไปนี้เพื่อดึงข้อมูลและเปิดใช้งานการกำหนดค่า รวมถึงกำหนดค่าให้กับค่าคงที่
modelName,systemInstructions,promptและagentPlatformLocation// 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 agentPlatformLocation = getValue(remoteConfig, 'agent_platform_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? _agentPlatformLocation = remoteConfig.getString("agent_platform_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
// Add a real-time Remote Config listener
onConfigUpdate(remoteConfig, {
next: (configUpdate) => {
console.log("Updated keys:", configUpdate.getUpdatedKeys());
if (configUpdate.getUpdatedKeys().has("welcome_message")) {
activate(remoteConfig).then(() => {
showWelcomeMessage();
});
}
},
error: (error) => {
console.log("Config update error:", error);
},
complete: () => {
console.log("Listening stopped.");
}
});
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 Agent Platform Gemini API backend service (formerly Vertex AI).
// The location for where to access the model will be sourced from Remote Config
let agentPlatformLocation = remoteConfig.configValue(forKey: "agent_platform_location").stringValue
let ai = FirebaseAI.firebaseAI(backend: .agentPlatform(location: agentPlatformLocation))
// 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 Agent Platform Gemini API backend service (formerly Vertex AI).
// The location for where to access the model will be sourced from Remote Config
val ai = Firebase.ai(backend = GenerativeBackend.agentPlatform(location = remoteConfig.getString("agent_platform_location")))
// 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 Agent Platform Gemini API backend service (formerly Vertex AI).
// The location for where to access the model will be sourced from Remote Config
FirebaseAI ai = FirebaseAI.getInstance(
GenerativeBackend.agentPlatform(remoteConfig.getString("agent_platform_location")));
// 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 Agent Platform Gemini API backend service (formerly Vertex AI).
// The location for where to access the model will be sourced from Remote Config
const ai = getAI(firebaseApp, { backend: new AgentPlatformBackend(agentPlatformLocation) });
// 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 Agent Platform Gemini API backend service (formerly Vertex AI).
// The location for where to access the model will be sourced from Remote Config
final ai = await FirebaseAI.agentPlatform(location: _agentPlatformLocation);
// 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 Agent Platform Gemini API backend service (formerly Vertex AI).
// The location for where to access the model will be sourced from Remote Config
var agentPlatformLocation = remoteConfig.GetValue("agent_platform_location").StringValue;
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.AgentPlatform(agentPlatformLocation));
// 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 ลงในโค้ดไคลเอ็นต์เพื่อเปิดใช้การกำหนดเป้าหมาย
สำหรับแอปและเกมบนอุปกรณ์เคลื่อนที่
ทดสอบการตั้งค่าโมเดลต่างๆ ด้วย Remote Config และ A/B Testing
ค่อยๆ เผยแพร่การเปลี่ยนแปลงพารามิเตอร์โมเดลโดยใช้การเปิดตัว Remote Config (iOS+ และ Android เท่านั้น)
ใช้Remote Configการปรับเปลี่ยนในแบบของคุณ เพื่อใช้แมชชีนเลิร์นนิงในการพิจารณาการตั้งค่าที่ดีที่สุดสำหรับผู้ใช้แต่ละราย (iOS+, Android และ Unity เท่านั้น)