เมื่อเรียกใช้ Gemini API จากแอปโดยใช้ Firebase AI Logic SDK คำขอของคุณจะมีพารามิเตอร์หลายรายการที่ควบคุมคำตอบของ Generative AI ซึ่งมักจะประกอบด้วยชื่อโมเดล การกำหนดค่าการสร้างโมเดล (โทเค็นสูงสุด อุณหภูมิ ฯลฯ) การตั้งค่าความปลอดภัย คำสั่งของระบบ และข้อมูลพรอมต์
ในกรณีส่วนใหญ่ คุณจะต้องเปลี่ยนข้อมูลเหล่านี้ตามต้องการหรือตามความจำเป็นในสถานการณ์ต่างๆ ต่อไปนี้
- อัปเดตโมเดล Generative AI โดยไม่ต้องเผยแพร่แอปใหม่ คุณสามารถอัปเกรดเป็นโมเดลเวอร์ชันใหม่ที่มีความเสถียรก่อนที่จะเลิกใช้งานเวอร์ชันเก่า เปลี่ยนไปใช้โมเดลที่ประหยัดค่าใช้จ่ายหรือมีประสิทธิภาพสูงขึ้นตามความต้องการและคุณลักษณะของผู้ใช้ หรือทำให้โมเดลล่าสุดและดีที่สุดใช้งานได้ตามเงื่อนไขกับกลุ่มผู้ใช้บางกลุ่ม (เช่น ผู้ทดสอบเบต้า)
- ตั้งค่าตำแหน่งที่คุณเข้าถึงโมเดลเพื่อให้อยู่ใกล้ผู้ใช้มากขึ้น
- ทดสอบวิธีการและข้อความแจ้งของระบบแบบ A/B จากนั้นค่อยๆ เปิดตัวค่าการทดสอบที่ได้ผลลัพธ์ดีที่สุดแก่ผู้ใช้
- ใช้ Flag ฟีเจอร์เพื่อแสดงหรือซ่อนฟีเจอร์ 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 ในกลุ่มผู้ใช้ต่างๆ เพื่อดูว่าการเปลี่ยนแปลงดังกล่าวส่งผลต่อเมตริกหลัก เช่น การคงผู้ใช้ไว้และรายได้อย่างไร
การใช้ Firebase Remote Config ในการตรวจสอบแอป Generative AI จะช่วยให้คุณสร้างแอปพลิเคชันที่ทำงานด้วยระบบ 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เทมเพลตไคลเอ็นต์และกำหนดค่าพารามิเตอร์และค่าเพื่อดึงข้อมูลและใช้ในแอป
- เปิดโปรเจ็กต์ Firebase ในคอนโซล Firebase จากนั้นจากเมนูการนำทาง ให้ขยายเรียกใช้ แล้วเลือกRemote Config
- ตรวจสอบว่าได้เลือกไคลเอ็นต์จากตัวเลือกไคลเอ็นต์/เซิร์ฟเวอร์ที่ด้านบนของหน้า
- เริ่มเทมเพลตไคลเอ็นต์โดยคลิกสร้างการกําหนดค่า (หรือเพิ่มพารามิเตอร์หากคุณเคยใช้เทมเพลตไคลเอ็นต์มาก่อน)
กำหนดพารามิเตอร์ที่ต้องการควบคุมด้วย Remote Config เช่น
ชื่อพารามิเตอร์ คำอธิบาย ประเภท ค่าเริ่มต้น model_name
ชื่อรุ่น ดูชื่อรุ่นที่ใช้ได้ สตริง gemini-2.0-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 ตรวจพบสตริง us-central1
เมื่อเพิ่มพารามิเตอร์เสร็จแล้ว ให้คลิกเผยแพร่การเปลี่ยนแปลง หากเทมเพลตนี้ไม่ใช่เทมเพลต Remote Config ใหม่ ให้ตรวจสอบการเปลี่ยนแปลงแล้วคลิกเผยแพร่การเปลี่ยนแปลงอีกครั้ง
ขั้นตอนที่ 2: เพิ่มและเริ่มต้น Remote Config ในแอป
เพิ่มคลัง Remote Config และตั้งค่า Remote Config ภายในแอป
Swift
คุณได้เพิ่ม Firebase SDK ลงในแอปแล้วเป็นส่วนหนึ่งของFirebase AI Logicการตั้งค่า แต่จะต้องเพิ่มRemote Configด้วย
เปิดโปรเจ็กต์ใน Xcode แล้วไปที่File > Add Package Dependency
เลือก firebase-ios-sdk แล้วคลิกเพิ่มแพ็กเกจ
จากเครื่องมือนำทางโปรเจ็กต์ ให้เลือกแอป > เป้าหมาย > แอป
จากแท็บทั่วไป ให้เลื่อนไปที่เฟรมเวิร์ก ไลบรารี และเนื้อหาที่ฝัง
คลิก + แล้วเลือก FirebaseRemoteConfig จากนั้นคลิก Add
เพิ่มการนําเข้า
FirebaseRemoteConfig
ลงในโค้ดimport FirebaseRemoteConfig
ในคลาสที่เหมาะสมสําหรับแอป ให้เริ่มต้น Firebase และเพิ่ม Remote Config ลงในตรรกะแอปพลิเคชันหลัก
ในส่วนนี้ คุณจะรวม Remote Config และRemote Config ตัวรับฟังแบบเรียลไทม์ เป็นการนําเข้าเพื่อให้แอปดึงข้อมูลค่าใหม่แบบเรียลไทม์ และเพิ่มช่วงเวลาการดึงข้อมูลขั้นต่ำ ดังนี้
let remoteConfig = RemoteConfig.remoteConfig() let settings = RemoteConfigSettings() settings.minimumFetchInterval = 3600 remoteConfig.configSettings = settings
ในแอปเริ่มต้นใช้งาน รายการนี้จะอยู่ใน
VertexAISampleApp
ภายในคลาสAppDelegate
Kotlin
เพิ่มทรัพยากร Dependency Remote Config ลงในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติจะเป็น
app/build.gradle.kts
หรือapp/build.gradle
)dependencies { implementation(platform("com.google.firebase:firebase-bom:33.14.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:33.14.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 แล้ว ให้เริ่มต้น Remote Config ดังนี้สำหรับ Firebase AI Logic SDK
// 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 Logicimport 'package:firebase_vertexai/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
เปิด Remote Config ในคอนโซล Firebase
ในแท็บพารามิเตอร์ ให้เปิดเมนู แล้วเลือกดาวน์โหลดค่าเริ่มต้น
เมื่อได้รับข้อความแจ้ง ให้เปิดใช้ .plist สำหรับ iOS แล้วคลิกดาวน์โหลดไฟล์
บันทึกไฟล์ในไดเรกทอรีแอปพลิเคชัน
หากใช้แอปตัวอย่าง ให้บันทึกภายใน
FirebaseVertexAI/Sample/VertexAISample
ใน Xcode ให้คลิกขวาที่แอปแล้วเลือกเพิ่มไฟล์
หากใช้ข้อมูลตัวอย่าง ให้คลิกขวาที่ VertexAISample แล้วเลือกเพิ่มไฟล์ไปยัง "VertexAISample"
เลือก remote_config_defaults.plist แล้วคลิกเพิ่ม
อัปเดตโค้ดแอปเพื่ออ้างอิงไฟล์เริ่มต้น
// Set default values remoteConfig.setDefaults(fromPlist: "remote_config_defaults")
Kotlin
เปิด Remote Config จากคอนโซล Firebase
ในแท็บพารามิเตอร์ ให้เปิดเมนู แล้วเลือกดาวน์โหลดค่าเริ่มต้น
เมื่อได้รับข้อความแจ้ง ให้เปิดใช้ .xml สำหรับ Android แล้วคลิกดาวน์โหลดไฟล์
บันทึกไฟล์ในไดเรกทอรีทรัพยากร XML ของแอป
อัปเดตไฟล์กิจกรรมหลักเพื่อเพิ่มค่าเริ่มต้นหลังจาก
configSettings
ที่คุณเพิ่มไว้ก่อนหน้านี้// Set default values. remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
Java
เปิด Remote Config ในคอนโซล Firebase
ในแท็บพารามิเตอร์ ให้เปิดเมนู แล้วเลือกดาวน์โหลดค่าเริ่มต้น
เมื่อได้รับข้อความแจ้ง ให้เปิดใช้ .xml สำหรับ Android แล้วคลิกดาวน์โหลดไฟล์
บันทึกไฟล์ในไดเรกทอรีทรัพยากร XML ของแอป
อัปเดตไฟล์กิจกรรมหลักเพื่อเพิ่มค่าเริ่มต้นหลังจาก
configSettings
ที่คุณเพิ่มไว้ก่อนหน้านี้// Set default values. mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);
Web
คุณสามารถตั้งค่าเริ่มต้นในโค้ดได้โดยตรง ดังนี้
// Set default Remote Config parameter values
remoteConfig.defaultConfig = {
model_name: 'gemini-2.0-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: 'us-central1',
};
Dart
คุณสามารถตั้งค่าเริ่มต้นในโค้ดได้โดยตรง ดังนี้
remoteConfig.setDefaults(const {
"model_name": "gemini-2.0-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": "us-central1"
});
Unity
คุณสามารถตั้งค่าเริ่มต้นในโค้ดได้โดยตรง ดังนี้
await remoteConfig.SetDefaultsAsync(
new System.Collections.Generic.Dictionary<string, object>() {
{ "model_name", "gemini-2.0-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", "us-central1" }
}
);
ขั้นตอนที่ 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
และ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 Listeners แบบเรียลไทม์
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 ลงในโค้ดไคลเอ็นต์เพื่อเปิดใช้การกำหนดเป้าหมาย
สำหรับแอปและเกมบนอุปกรณ์เคลื่อนที่
ทดสอบการตั้งค่ารูปแบบต่างๆ ด้วย Remote Config และ A/B Testing
เปิดตัวการเปลี่ยนแปลงพารามิเตอร์รูปแบบทีละน้อยโดยใช้Remote Configการเปิดตัว (iOS ขึ้นไปและ Android เท่านั้น)
ใช้Remote Configการปรับให้เหมาะกับผู้ใช้เพื่อใช้แมชชีนเลิร์นนิงในการกำหนดการตั้งค่าที่ดีที่สุดสำหรับผู้ใช้แต่ละราย (iOS+, Android และ Unity เท่านั้น)