অ্যাপল অ্যাপে হাইব্রিড অভিজ্ঞতার জন্য কনফিগারেশন বিকল্প


এই পৃষ্ঠায় হাইব্রিড এবং অন-ডিভাইস অভিজ্ঞতার জন্য নিম্নলিখিত কনফিগারেশন বিকল্পগুলি বর্ণনা করা হয়েছে:

হাইব্রিড অভিজ্ঞতা তৈরির জন্য প্রারম্ভিক নির্দেশিকাটি আপনি সম্পূর্ণ করেছেন কিনা, তা নিশ্চিত করুন।

একটি "ইনফারেন্স মোড" সেট করুন

The examples in the getting started guide show how to implement attempting on-device inference first, and then falling back to the cloud-hosted model. This is only one of the available "inference modes" that you can implement.

হাইব্রিড ইনফারেন্স

  • ডিভাইস-ভিত্তিক ইনফারেন্সকে অগ্রাধিকার দিন : primary হিসেবে একটি 'সিস্টেম' মডেল এবং secondary হিসেবে একটি ক্লাউড মডেল সেট করুন।

    যদি অন-ডিভাইস মডেলটি উপলব্ধ থাকে এবং অনুরোধের ধরনটি সমর্থন করে, তবে সেটি ব্যবহার করার চেষ্টা করুন। অন্যথায়, ডিভাইসে একটি ত্রুটি লগ করুন এবং তারপরে স্বয়ংক্রিয়ভাবে ক্লাউড-হোস্টেড মডেলে ফিরে যান

    // Imports + initialization of Gemini API backend service
    // ...
    
    // Initialize a cloud model that supports your use case
    let cloudModel = ai.geminiModel(name: "GEMINI_MODEL_NAME")
    // Initialize an on-device model that supports your use case
    let systemModel = FirebaseAI.SystemLanguageModel.default
    
    // Create a GenerativeModelSession with a hybrid model.
    // Provide your preferred model as `primary` and your fallback model as `secondary`
    // Attempt to use the on-device model; otherwise, fall back to the cloud-hosted model.
    let session = ai.generativeModelSession(
      model: .hybridModel(primary: systemModel, secondary: cloudModel)
    )
    
  • ইন-ক্লাউড ইনফারেন্সকে অগ্রাধিকার দিন : primary হিসেবে একটি ক্লাউড মডেল এবং secondary একটি 'সিস্টেম' মডেল সেট করুন।

    Attempt to use the cloud-hosted model if the device is online and if the model is available. If the device is offline, fall back to the on-device model . In all other failure cases, throw an exception .

    // Imports + initialization of Gemini API backend service
    // ...
    
    // Initialize a cloud model that supports your use case
    let cloudModel = ai.geminiModel(name: "GEMINI_MODEL_NAME")
    // Initialize an on-device model that supports your use case
    let systemModel = FirebaseAI.SystemLanguageModel.default
    
    // Create a GenerativeModelSession with a hybrid model.
    // Provide your preferred model as `primary` and your fallback model as `secondary`
    // Attempt to use the cloud-hosted model; otherwise, fall back to the on-device model.
    let session = ai.generativeModelSession(
      model: .hybridModel(primary: cloudModel, secondary: systemModel)
    )
    

শুধুমাত্র ডিভাইসে অথবা শুধুমাত্র ক্লাউডে অনুমান

The SDK supports setting only a single model which means the SDK will only attempt either on-device or in-cloud inference. Also, you don't create a HybridModel for this use case. However, for a hybrid experience, you do need to create a HybridModel and set both primary and secondary models (as described above).

  • শুধুমাত্র ডিভাইসে ইনফারেন্সের জন্য : model একটি "সিস্টেম" মডেলে সেট করুন। এই ব্যবহারের ক্ষেত্রে কোনো HybridModel তৈরি করা হয় না।

    যদি অন-ডিভাইস মডেলটি উপলব্ধ থাকে এবং অনুরোধের ধরনটি সমর্থন করে, তবে সেটি ব্যবহার করার চেষ্টা করুন। অন্যথায়, একটি এক্সেপশন থ্রো করুন

    // Imports + initialization of Gemini API backend service
    // ...
    
    // Initialize an on-device model that supports your use case
    let systemModel = FirebaseAI.SystemLanguageModel.default
    
    // Create a GenerativeModelSession with the on-device model.
    let session = ai.generativeModelSession(
      model: systemModel
    )
    
  • শুধুমাত্র ইন-ক্লাউড ইনফারেন্সের জন্য : model একটি ক্লাউড মডেলে সেট করুন। এই ব্যবহারের ক্ষেত্রে কোনো HybridModel তৈরি করার প্রয়োজন নেই।

    ডিভাইসটি অনলাইন থাকলে এবং মডেলটি উপলব্ধ থাকলে ক্লাউড-হোস্টেড মডেলটি ব্যবহার করার চেষ্টা করুন। অন্যথায়, একটি এক্সেপশন থ্রো করুন

    // Imports + initialization of Gemini API backend service
    // ...
    
    // Initialize a cloud model that supports your use case
    let cloudModel = ai.geminiModel(name: "GEMINI_MODEL_NAME")
    
    // Create a GenerativeModelSession with a cloud model.
    let session = ai.generativeModelSession(
      model: cloudModel
    )
    

ডিভাইসে থাকা মডেলটি উপলব্ধ আছে কিনা তা পরীক্ষা করুন।

Manual checks for on-device availability are only necessary if you want to surface that information to the user or request that end-users take action to download the on-device model. If the on-device model is unavailable – and you've set primary to an on-device model and secondary to a cloud model – then the SDK will automatically fallback to using the cloud-hosted model.

ডিভাইসে থাকা মডেলটি আসলেই ব্যবহারযোগ্য কিনা তা ম্যানুয়ালি পরীক্ষা করতে, isAvailable প্রপার্টিটি যাচাই করুন:

if FirebaseAI.SystemLanguageModel.default.isAvailable {
  // The on-device model is ready to use.
} else {
  // The on-device model is unavailable.
}

ডিভাইসের নির্দিষ্ট মডেলের প্রাপ্যতার কারণ যাচাই করতে, availability প্রপার্টিটি পরীক্ষা করুন:

switch FirebaseAI.SystemLanguageModel.default.availability {
case .available:
  // The on-device model is ready to use.
  break
case .unavailable(.deviceNotEligible):
  // This device does not support Apple Intelligence.
  break
case .unavailable(.appleIntelligenceNotEnabled):
  // The user has not enabled Apple Intelligence in Settings.
  break
case .unavailable(.modelNotReady):
  // The model is still being downloaded.
  break
case let .unavailable(reason):
  // The model is unavailable due to the specified `reason`.
  break
}

অন-ডিভাইস নাকি ইন-ক্লাউড ইনফারেন্স ব্যবহার করা হয়েছিল তা নির্ধারণ করুন।

If you use a HybridModel (and set both primary and secondary models), then it might be helpful to know which model was used for a given request. This information is provided by the modelVersion property of rawResponse in each response.

আপনি যখন এই প্রপার্টিটি অ্যাক্সেস করবেন, তখন ফেরত আসা মানটি নিম্নলিখিতগুলির মধ্যে একটি হবে:

  • ব্যবহৃত ক্লাউড-হোস্টেড মডেল: মডেলের নাম, যেমন gemini-3.1-flash-lite
  • ডিভাইসে ব্যবহৃত মডেল: apple-foundation-models-system-language-model
// let response = try await session.respond(to: ...

print("You used: \(response.rawResponse.modelVersion)")

print(response.content)

প্রতিক্রিয়া নিয়ন্ত্রণ করতে মডেল কনফিগারেশন ব্যবহার করুন

মডেলে প্রতিটি অনুরোধের সাথে, মডেলটি কীভাবে প্রতিক্রিয়া তৈরি করবে তা নিয়ন্ত্রণ করতে আপনি একটি মডেল কনফিগারেশন পাঠাতে পারেন। ক্লাউড-হোস্টেড মডেল এবং অন-ডিভাইস মডেল বিভিন্ন কনফিগারেশন বিকল্প প্রদান করে ( ক্লাউড বনাম অন-ডিভাইস প্যারামিটার)।

  • ক্লাউড-হোস্টেড মডেলগুলোর কনফিগারেশন একটি GenerationConfig এ সেট করুন।
  • অন-ডিভাইস মডেলগুলির কনফিগারেশন FirebaseAI.GenerationOptions এর মধ্যে সেট করুন।

মডেলে করা প্রতিটি অনুরোধের জন্য এই বিকল্পগুলি কনফিগার করা হয়।

এখানে একটি উদাহরণ দেওয়া হলো যা হাইব্রিড ইনফারেন্সের জন্য ক্লাউড-হোস্টেড এবং অন-ডিভাইস মডেলের কনফিগারেশন নির্ধারণ করে:

// ...

let response = try await session.respond(
  to: "Why is the sky blue?",
  options: .hybrid(
    // Config for cloud-hosted model
    gemini: GenerationConfig(
      temperature: 0.8,
      topP: 0.9,
      thinkingConfig: ThinkingConfig(thinkingLevel: .high)
    ),
    // Config for on-device model
    foundationModels: FirebaseAI.GenerationOptions(
      sampling: .random(probabilityThreshold: 0.9),
      temperature: 0.8
    )
  )
)

// ...


Firebase AI Logic ব্যবহারের অভিজ্ঞতা সম্পর্কে মতামত দিন।