Firebase रिमोट कॉन्फ़िगरेशन की मदद से, अपने Firebase AI Logic ऐप्लिकेशन को डाइनैमिक तौर पर अपडेट करना

Firebase AI Logic SDK टूल का इस्तेमाल करके, अपने ऐप्लिकेशन से Gemini API को कॉल करते समय, आपके अनुरोध में कई पैरामीटर होते हैं. ये पैरामीटर, जनरेटिव एआई के जवाबों को कंट्रोल करते हैं. आम तौर पर, इनमें मॉडल का नाम, मॉडल जनरेशन कॉन्फ़िगरेशन (ज़्यादा से ज़्यादा टोकन, तापमान वगैरह), सुरक्षा सेटिंग, सिस्टम के निर्देश, और प्रॉम्प्ट डेटा शामिल होता है.

ज़्यादातर मामलों में, आपको इन सेटिंग को मांग पर या ज़रूरत के हिसाब से बदलना होगा. ऐसा कई मामलों में करना पड़ सकता है:

  • नया ऐप्लिकेशन रिलीज़ किए बिना, जनरेटिव एआई मॉडल को अपडेट करें. पुराने वर्शन बंद होने से पहले, नए और बेहतर मॉडल के वर्शन पर अपग्रेड किया जा सकता है. इसके अलावा, उपयोगकर्ताओं की ज़रूरतों और एट्रिब्यूट के आधार पर, कम कीमत वाले या बेहतर परफ़ॉर्मेंस वाले मॉडल पर स्विच किया जा सकता है. इसके अलावा, खास उपयोगकर्ता सेगमेंट (जैसे, बीटा टेस्टर) के लिए, नए और बेहतर मॉडल को शर्तों के साथ डिप्लॉय किया जा सकता है.
  • मॉडल को ऐक्सेस करने की जगह सेट करें, ताकि वह आपके उपयोगकर्ताओं के करीब हो.
  • सिस्टम के अलग-अलग निर्देशों और प्रॉम्प्ट की A/B टेस्टिंग करें. इसके बाद, अपने उपयोगकर्ताओं के लिए, एक्सपेरिमेंट की सबसे अच्छी वैल्यू को धीरे-धीरे रोल आउट करें.
  • अपने ऐप्लिकेशन में जनरेटिव एआई की सुविधाओं को तुरंत दिखाने या छिपाने के लिए, सुविधा फ़्लैग का इस्तेमाल करें.

Firebase Remote Config, ये सभी काम करता है और इससे आपको ज़रूरत के हिसाब से पैरामीटर वैल्यू अपडेट करने की सुविधा मिलती है. साथ ही, Firebase कंसोल में सेट की गई विशेषताओं से मैच करने वाले ऐप्लिकेशन इंस्टेंस के लिए, शर्त के मुताबिक पैरामीटर वैल्यू अपडेट की जा सकती हैं. इसके लिए, आपको अपने ऐप्लिकेशन का नया वर्शन रिलीज़ करने की ज़रूरत नहीं है.

इस समाधान गाइड में, इस्तेमाल के खास उदाहरणों के साथ-साथ, जनरेटिव एआई ऐप्लिकेशन में Remote Config जोड़ने का तरीका बताया गया है.

कोड लागू करने पर जाएं

अपने ऐप्लिकेशन के साथ Firebase Remote Config का इस्तेमाल क्यों करना चाहिए?

Firebase Remote Config की मदद से, ऐप्लिकेशन को अपडेट किए बिना, ऐप्लिकेशन के काम करने के तरीके में डाइनैमिक तरीके से बदलाव किया जा सकता है. यह सुविधा, जनरेटिव एआई का इस्तेमाल करने वाले ऐप्लिकेशन के लिए खास तौर पर कारगर है. इन ऐप्लिकेशन में, तेज़ी से बदलाव करना और उन्हें बेहतर बनाना ज़रूरी होता है.

जनरेटिव एआई ऐप्लिकेशन के साथ Remote Config के इस्तेमाल के ज़रूरी उदाहरण

हमारा सुझाव है कि इस्तेमाल के इन ज़रूरी उदाहरणों के लिए, Remote Config के साथ Firebase AI Logic का इस्तेमाल करें:

  • ऐप्लिकेशन अपडेट किए बिना, मॉडल के नए वर्शन पर अपग्रेड करना: ज़रूरत के हिसाब से मॉडल का नाम बदलने के लिए, Remote Config पैरामीटर का इस्तेमाल करें. इससे, आपके पसंदीदा Gemini मॉडल का नया वर्शन उपलब्ध होने पर, उसे तुरंत अपग्रेड किया जा सकता है.

  • ऐप्लिकेशन अपडेट किए बिना, सिस्टम के निर्देशों और सुरक्षा सेटिंग को अपडेट करना: Remote Config पैरामीटर में सिस्टम के निर्देशों और सुरक्षा सेटिंग को सेव करें, ताकि डिप्लॉयमेंट के बाद समस्याएं मिलने पर, आप उन्हें ज़रूरत के हिसाब से बदल सकें.

  • जोखिम कम करना और एआई की सुरक्षा को लागू करना: अपने iOS और Android उपयोगकर्ताओं के लिए, जनरेटिव एआई से जुड़े बदलावों को सुरक्षित और धीरे-धीरे रिलीज़ करने के लिए, Remote Config रोल आउट का इस्तेमाल करें.

जनरेटिव एआई ऐप्लिकेशन की मदद से, Remote Config के लिए बेहतर और सुझाए गए इस्तेमाल के उदाहरण

Remote Config और Google Analytics की मदद से अपने ऐप्लिकेशन को इंस्ट्रूमेंट करने के बाद, ऐप्लिकेशन के इस्तेमाल के बेहतर उदाहरणों को एक्सप्लोर किया जा सकता है:

Firebase Remote Config की मदद से, अपने जनरेटिव एआई ऐप्लिकेशन को इंस्ट्रूमेंट करके, एआई की मदद से काम करने वाले ऐसे ऐप्लिकेशन बनाए जा सकते हैं जो उपयोगकर्ताओं के लिए बेहतर अनुभव देते हों. साथ ही, ये ऐप्लिकेशन आसानी से इस्तेमाल किए जा सकते हों, सुरक्षित हों, और कम खर्च में काम कर सकते हों.

अपने ऐप्लिकेशन में Firebase Remote Config जोड़ना

इस समाधान गाइड में, आपको Firebase Remote Config का इस्तेमाल करके, अपने Android ऐप्लिकेशन में पैरामीटर को डाइनैमिक तौर पर अपडेट करना होगा. यह ऐप्लिकेशन, Firebase AI Logic SDK टूल का इस्तेमाल करता है. आपको इनके बारे में जानकारी मिलेगी:

  • Firebase Remote Config से मॉडल के नाम और सिस्टम के निर्देश जैसे पैरामीटर फ़ेच और चालू करें.
  • डाइनैमिक तौर पर वापस पाए गए पैरामीटर का इस्तेमाल करने के लिए, अपने Gemini API कॉल अपडेट करें. इससे, आपको ऐप्लिकेशन अपडेट किए बिना, अलग-अलग मॉडल के बीच स्विच करने या सिस्टम के निर्देशों में बदलाव करने की सुविधा मिलती है.
  • ज़रूरत के मुताबिक मॉडल के व्यवहार और क्षमताओं में बदलाव करके, पैरामीटर को रिमोट से कंट्रोल करें.

ज़रूरी शर्तें

इस गाइड में यह माना गया है कि आपको अपने प्लैटफ़ॉर्म के लिए ऐप्लिकेशन बनाने की जानकारी है.

शुरू करने से पहले, पक्का करें कि आपने ये काम कर लिए हों:

  • Firebase AI Logic शुरू करने के लिए गाइड को पूरा करें. इसमें, Firebase प्रोजेक्ट सेट अप करने, अपने ऐप्लिकेशन को Firebase से कनेक्ट करने, SDK टूल जोड़ने, चुने गए "Gemini API" प्रोवाइडर के लिए बैकएंड सेवा को शुरू करने, और मॉडल इंस्टेंस बनाने का तरीका बताया गया है.

  • अपने Firebase प्रोजेक्ट में Google Analytics को चालू करें और उसके SDK टूल को अपने ऐप्लिकेशन में जोड़ें. यह शर्तों के हिसाब से टारगेटिंग के लिए ज़रूरी है. जैसे, क्लाइंट डिवाइस की जगह की जानकारी के आधार पर, मॉडल को ऐक्सेस करने की जगह सेट करना.

पहला चरण: Firebase कंसोल में पैरामीटर वैल्यू सेट करना

क्लाइंट Remote Config टेंप्लेट बनाएं और ऐप्लिकेशन में फ़ेच और इस्तेमाल करने के लिए, पैरामीटर और वैल्यू कॉन्फ़िगर करें.

  1. Firebase कंसोल में अपना Firebase प्रोजेक्ट खोलें. इसके बाद, नेविगेशन मेन्यू में जाकर, रन करें को बड़ा करें और Remote Config चुनें.
  2. पक्का करें कि पेज पर सबसे ऊपर मौजूद क्लाइंट/सर्वर सिलेक्टर में, क्लाइंट चुना गया हो.
  3. कॉन्फ़िगरेशन बनाएं पर क्लिक करके क्लाइंट टेंप्लेट बनाएं. अगर आपने पहले क्लाइंट टेंप्लेट का इस्तेमाल किया है, तो पैरामीटर जोड़ें पर क्लिक करें.
  4. वे पैरामीटर तय करें जिन्हें आपको 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 जनरेटिव एआई की सुविधा के साथ इस्तेमाल करने के लिए डिफ़ॉल्ट प्रॉम्प्ट. स्ट्रिंग I am a developer who wants to know more about Firebase!
    vertex_location यह सिर्फ़ तब लागू होता है, जब Vertex AI Gemini API का इस्तेमाल किया जा रहा हो.
    मॉडल को ऐक्सेस करने के लिए, जगह की जानकारी को कंट्रोल करें. Google Analytics की मदद से पता लगाई गई क्लाइंट की जगह के आधार पर, इस विकल्प को कॉन्फ़िगर करने के लिए शर्तें सेट की जा सकती हैं.
    स्ट्रिंग us-central1
  5. पैरामीटर जोड़ने के बाद, बदलाव पब्लिश करें पर क्लिक करें. अगर यह नया Remote Config टेंप्लेट नहीं है, तो बदलावों की समीक्षा करें और फिर से बदलाव पब्लिश करें पर क्लिक करें.

दूसरा चरण: अपने ऐप्लिकेशन में Remote Config जोड़ना और उसे शुरू करना

अपने ऐप्लिकेशन में Remote Config लाइब्रेरी जोड़ें और Remote Config सेट अप करें.

Swift

Firebase AI Logic सेटअप के हिस्से के तौर पर, आपने अपने ऐप्लिकेशन में Firebase SDK टूल पहले ही जोड़ दिया है. हालांकि, आपको Remote Config भी जोड़ना होगा.

  1. Xcode में, प्रोजेक्ट खोलकर फ़ाइल > पैकेज की डिपेंडेंसी जोड़ें पर जाएं.

  2. firebase-ios-sdk चुनें. इसके बाद, पैकेज जोड़ें पर क्लिक करें.

  3. प्रोजेक्ट नेविगेटर से, अपना ऐप्लिकेशन > टारगेट > अपना ऐप्लिकेशन चुनें.

  4. सामान्य टैब से, स्क्रोल करके फ़्रेमवर्क, लाइब्रेरी, और एम्बेड किया गया कॉन्टेंट पर जाएं.

  5. + पर क्लिक करें और FirebaseRemoteConfig चुनें. इसके बाद, जोड़ें पर क्लिक करें.

  6. अपने कोड में FirebaseRemoteConfig इंपोर्ट जोड़ें:

    import FirebaseRemoteConfig
    
  7. अपने ऐप्लिकेशन के लिए सही क्लास में, Firebase को शुरू करें और अपने मुख्य ऐप्लिकेशन लॉजिक में Remote Config जोड़ें.

    यहां आपको Remote Config और Remote Config रीयल-टाइम लिसनर को इंपोर्ट के तौर पर शामिल करना होगा, ताकि ऐप्लिकेशन रीयल-टाइम में नई वैल्यू फ़ेच कर सके और फ़ेच करने के लिए कम से कम इंटरवल जोड़ सके:

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

    क्विकस्टार्ट ऐप्लिकेशन में, यह AppDelegate क्लास में VertexAISampleApp में होगा.

Kotlin

  1. अपने मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर app/build.gradle.kts या app/build.gradle) में Remote Config डिपेंडेंसी जोड़ें:

    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
    }
    
  2. अपने मुख्य ऐप्लिकेशन लॉजिक में Remote Config जोड़ें. यहां, आपको Remote Config को शुरू करना होगा और फ़ेच करने के लिए कम से कम इंटरवल जोड़ना होगा:

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

Java

  1. अपने मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर app/build.gradle.kts या app/build.gradle) में Remote Config डिपेंडेंसी जोड़ें:

    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
    }
    
  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 AI Logic SDK टूल के लिए Firebase ऐप्लिकेशन को शुरू करने के बाद, 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. अपने Unity प्रोजेक्ट में Remote Config जोड़ने के लिए, इन निर्देशों का पालन करें.

  2. Remote Config ऑब्जेक्ट इंस्टेंस पाएं और बार-बार रीफ़्रेश करने के लिए, फ़ेच करने का कम से कम इंटरवल सेट करें. Firebase को शुरू करने के बाद, इसे ज़रूर जोड़ें.

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

तीसरा चरण: इन-ऐप्लिकेशन पैरामीटर वैल्यू सेट करना

आपको Remote Config ऑब्जेक्ट में, इन-ऐप्लिकेशन डिफ़ॉल्ट पैरामीटर वैल्यू सेट करनी चाहिए. इससे यह पक्का होता है कि आपका ऐप्लिकेशन उम्मीद के मुताबिक काम करता रहे, भले ही वह Remote Config सेवा से वैल्यू फ़ेच न कर पाए.

Swift

  1. Firebase कंसोल में, Remote Config खोलें.

  2. पैरामीटर टैब में, मेन्यू खोलें और डिफ़ॉल्ट वैल्यू डाउनलोड करें को चुनें.

  3. जब कहा जाए, तब iOS के लिए .plist चालू करें. इसके बाद, फ़ाइल डाउनलोड करें पर क्लिक करें.

  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. Firebase कंसोल में जाकर, Remote Config खोलें.

  2. पैरामीटर टैब में, मेन्यू खोलें और डिफ़ॉल्ट वैल्यू डाउनलोड करें को चुनें.

  3. जब कहा जाए, तब Android के लिए .xml चालू करें. इसके बाद, फ़ाइल डाउनलोड करें पर क्लिक करें.

  4. फ़ाइल को अपने ऐप्लिकेशन की एक्सएमएल संसाधन डायरेक्ट्री में सेव करें.

  5. पहले जोड़े गए configSettings के बाद डिफ़ॉल्ट वैल्यू जोड़ने के लिए, अपनी मुख्य गतिविधि फ़ाइल अपडेट करें:

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

Java

  1. Firebase कंसोल में, Remote Config खोलें.

  2. पैरामीटर टैब में, मेन्यू खोलें और डिफ़ॉल्ट वैल्यू डाउनलोड करें को चुनें.

  3. जब कहा जाए, तब Android के लिए .xml चालू करें. इसके बाद, फ़ाइल डाउनलोड करें पर क्लिक करें.

  4. फ़ाइल को अपने ऐप्लिकेशन की एक्सएमएल संसाधन डायरेक्ट्री में सेव करें.

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

चौथा चरण: वैल्यू फ़ेच करना और उन्हें चालू करना

डिफ़ॉल्ट वैल्यू सेट करने के बाद, वैल्यू फ़ेच और चालू करने के लिए यह जोड़ें.

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();

पांचवां चरण: रीयल-टाइम 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();
};

छठा चरण: Remote Config वैल्यू का इस्तेमाल करने के लिए, Gemini API अनुरोधों को अपडेट करना

इस पेज पर, सेवा देने वाली कंपनी से जुड़ा कॉन्टेंट और कोड देखने के लिए, 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.");

सातवां चरण: ऐप्लिकेशन चलाना

ऐप्लिकेशन बनाएं और चलाएं. साथ ही, पुष्टि करें कि वह काम कर रहा है. Firebase कंसोल में Remote Config पेज पर जाकर, अपने कॉन्फ़िगरेशन में बदलाव करें. इसके बाद, बदलावों को पब्लिश करें और नतीजे की पुष्टि करें.

अगले चरण

  • Remote Config के बारे में और जानें.

  • टारगेटिंग चालू करने के लिए, अपने क्लाइंट कोड में Google Analytics जोड़ें.

  • मोबाइल ऐप्लिकेशन और गेम के लिए: