रिमोट कॉन्फ़िगरेशन का इस्तेमाल करके वर्शन टेंप्लेट


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

इसलिए, अगर आपको बड़े बदलाव करने हैं या बदलावों को धीरे-धीरे लागू करना है, तो आपको प्रोडक्शन कोड में इस्तेमाल किए गए टेंप्लेट को नहीं बदलना चाहिए.

इसके बजाय, हमारा सुझाव है कि मॉडल को किए गए अनुरोध में इस्तेमाल किए गए टेंप्लेट आईडी की वैल्यू को कंट्रोल करने के लिए, Firebase Remote Config का इस्तेमाल करें.

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

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

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

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

  1. Firebase कंसोल में अपना Firebase प्रोजेक्ट खोलें. इसके बाद, नेविगेशन मेन्यू में जाकर, चलाएं को बड़ा करें और Remote Config को चुनें.

  2. पक्का करें कि पेज पर सबसे ऊपर मौजूद, क्लाइंट/सर्वर सिलेक्टर में से क्लाइंट चुना गया हो.

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

  4. template_id पैरामीटर तय करें:

    पैरामीटर का नाम ब्यौरा टाइप डिफ़ॉल्ट वैल्यू
    template_id टेंप्लेट आईडी. स्ट्रिंग my-first-template-v1-0-0
  5. इस पैरामीटर को जोड़ने के बाद, बदलाव पब्लिश करें पर क्लिक करें. अगर यह नया Remote Config टेंप्लेट नहीं है, तो बदलावों की समीक्षा करें और बदलाव पब्लिश करें पर फिर से क्लिक करें.

दूसरा चरण: अपने ऐप्लिकेशन में 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 FirebaseRemoteConfig
    
  7. अपने ऐप्लिकेशन की सही क्लास में, Firebase को शुरू करें और Remote Config को अपने मुख्य ऐप्लिकेशन लॉजिक में जोड़ें.

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

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

Kotlin

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

    dependencies {
        implementation(platform("com.google.firebase:firebase-bom:34.5.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:34.5.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;
    

तीसरा चरण: ऐप्लिकेशन में मौजूद पैरामीटर की वैल्यू सेट करना

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

Swift

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

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

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

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

  5. Xcode में, अपने ऐप्लिकेशन पर राइट क्लिक करें और फ़ाइलें जोड़ें चुनें

  6. remote_config_defaults.plist को चुनें. इसके बाद, जोड़ें पर क्लिक करें.

  7. डिफ़ॉल्ट फ़ाइल का रेफ़रंस देने के लिए, अपने ऐप्लिकेशन कोड को अपडेट करें:

    // Set default values for Remote Config parameters.
    remoteConfig.setDefaults(fromPlist: "remote_config_defaults")
    

Kotlin

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

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

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

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

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

    // Set default values for Remote Config parameters.
    remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
    

Java

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

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

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

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

  5. अपनी मुख्य ऐक्टिविटी फ़ाइल को अपडेट करें, ताकि आपने पहले जो 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 = {
  template_id: 'my-first-template-v1-0-0',
};

चौथा चरण: वैल्यू को फ़ेच और ऐक्टिवेट करना

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

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. उस कोड का पता लगाएं जहां मॉडल के नाम के लिए डिफ़ॉल्ट वैल्यू तय की गई है. उस कोड ब्लॉक के ठीक बाद, कॉन्फ़िगरेशन को फ़ेच और चालू करने के लिए यहां दिया गया कोड जोड़ें. साथ ही, फ़ेच की गई वैल्यू को templateID कॉन्स्टेंट असाइन करें.

    // 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 templateID = getValue(remoteConfig, 'template_id').asString();
    

पांचवां चरण: रीयल-टाइम 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 श्रोता उपलब्ध नहीं हैं.

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

इस पेज पर, सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट और कोड देखने के लिए, Gemini API सेवा देने वाली कंपनी पर क्लिक करें.

Remote Config को पूरी तरह से कॉन्फ़िगर करने के बाद, अपने कोड को अपडेट करें. ऐसा इसलिए, ताकि हार्ड-कोड की गई वैल्यू को Remote Config से सोर्स की गई वैल्यू से बदला जा सके.

Swift

import FirebaseAI

let templateID = remoteConfig.configValue(forKey: "template_id").stringValue
let model = FirebaseAI.firebaseAI(backend: .googleAI()).templateGenerativeModel()
let customerName = "Jane"

// When making the `generateContent` call, source the template ID value from Remote Config
let response = try await model.generateContent(
  templateID: templateID,
  // Provide the values for any input variables required by your template.
  inputs: [
    "customerName": customerName
  ]
)

// ...

Kotlin

// ...

val model = Firebase.ai().templateGenerativeModel()
val customerName = "Jane"

// When making the `generateContent` call, source the template ID value from Remote Config
val response = model.generateContent(
  remoteConfig.getString("template_id"),
  // Provide the values for any input variables required by your template.
  mapOf(
    "customerName" to customerName
  )
)

val text = response.text
println(text)

Java

// ...

TemplateGenerativeModel ai = FirebaseAI.getInstance()
    .templateGenerativeModel(null /* Request Options */);

TemplateGenerativeModelFutures model = TemplateGenerativeModelFutures.from(ai);
String customerName = "Jane";

// When making the `generateContent` call, source the template ID value from Remote Config
Future<GenerateContentResponse> response = model.generateContent(
    remoteConfig.getString("template_id"),
    // Provide the values for any input variables required by your template.
    mapOf("customerName", customerName)

);
addCallback(response,
      new FutureCallback<GenerateContentResponse>() {
          public void onSuccess(GenerateContentResponse result) {
            System.out.println(result.getText());
          }
          public void onFailure(Throwable t) {
            reportError(t);
          }
    }
executor);

// ...

Web

// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

const model = getTemplateGenerativeModel(ai);
const templateID = getValue(remoteConfig, 'template_id').asString();
const customerName = 'Jane';

// When making the `generateContent` call, source the template ID value from Remote Config
const result = await model.generateContent(
  templateID,
  // Provide the values for any input variables required by your template
  {
    customerName: customerName,
  }
);

// ...

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

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

अगले चरण

  • Remote Config और Firebase AI Logic के इस्तेमाल के अन्य उदाहरणों को लागू करने के बारे में ज़्यादा जानें.

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

    • Remote Config और A/B Testing की मदद से, टेंप्लेट में किए गए बदलावों को टेस्ट करें.

    • Remote Config रोलआउट का इस्तेमाल करके, बदलावों को धीरे-धीरे रिलीज़ करें. यह सुविधा सिर्फ़ iOS+ और Android के लिए उपलब्ध है.

    • Remote Config निजीकरण का इस्तेमाल करें. इससे मशीन लर्निंग का इस्तेमाल करके, अलग-अलग उपयोगकर्ताओं के लिए सबसे अच्छी सेटिंग तय की जा सकती हैं. यह सुविधा सिर्फ़ iOS+, Android, और Unity के लिए उपलब्ध है.