Apple के Foundation Models फ़्रेमवर्क के ज़रिए Gemini API को ऐक्सेस करने पर उपलब्ध सुविधाएँ


इस पेज पर दिए गए उदाहरणों में यह माना गया है कि आपने शुरू करें: Apple के Foundation Models फ़्रेमवर्क के ज़रिए Gemini API को ऐक्सेस करना लेख में दी गई ज़रूरी शर्तें पूरी कर ली हैं.


इस गाइड में, Apple के प्लैटफ़ॉर्म के लिए Firebase AI Logic SDK का इस्तेमाल करके, Apple के Foundation Models फ़्रेमवर्क के ज़रिए Gemini API को अलग-अलग तरह के अनुरोध भेजने का तरीका बताया गया है.

इस पेज पर, इन तरह के अनुरोध भेजने के उदाहरण दिए गए हैं:



टेक्स्ट जनरेट करना

Gemini मॉडल, टेक्स्ट जनरेट करने के लिए इन सुविधाओं का इस्तेमाल करते हैं:

इस सुविधा का इस्तेमाल करने वाले मॉडल

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite

सिर्फ़ टेक्स्ट वाले इनपुट से टेक्स्ट जनरेट करना

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

सिर्फ़ टेक्स्ट वाले इनपुट का इस्तेमाल करके, Gemini मॉडल से टेक्स्ट जनरेट करने के लिए कहा जा सकता है.

import FoundationModels
import FirebaseCore
import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Initialize a `geminiLanguageModel` with a Gemini model that supports your use case.
let model = ai.geminiLanguageModel(name: "gemini-3.5-flash")

// Provide a prompt that contains text.
let prompt = "Write a story about a magic backpack."

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// For a single-turn interaction, create a new session each time you call the model.
let session = LanguageModelSession(model: model)

// Generate a text response to the prompt.
let response = try await session.respond(to: prompt)
print(response.content)

जवाब को स्ट्रीम करना

मॉडल से जनरेट होने वाले पूरे नतीजे का इंतज़ार करने के बजाय, आंशिक नतीजों को मैनेज करने के लिए स्ट्रीमिंग का इस्तेमाल करके, तेज़ी से इंटरैक्शन किया जा सकता है. जवाब को स्ट्रीम करने के लिए, respond(to:) के बजाय streamResponse(to:) का इस्तेमाल करें.

// imports
// initialization of Gemini API backend service and a `geminiLanguageModel`

// Provide a prompt that contains text.
let prompt = "Write a story about a magic backpack."

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// For a single-turn interaction, create a new session each time you call the model.
let session = LanguageModelSession(model: model)

// Generate a text response to the prompt.
// To stream the response, use `streamResponse(to:)` instead of `respond(to:)`
let stream = session.streamResponse(to: "Write a story about a magic backpack.")
var response = ""
for try await snapshot in stream {
  // The snapshot contains *all* content generated so far.
  response = snapshot.content
}

सिलसिलेवार बातचीत सेशन (चैट) के दौरान टेक्स्ट जनरेट करना

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

import FoundationModels
import FirebaseCore
import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Initialize a `geminiLanguageModel` with a Gemini model that supports your use case.
let model = ai.geminiLanguageModel(name: "gemini-3.5-flash")

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// The session maintains state between each request.
let session = LanguageModelSession(model: model)

// Generate a text response to an initial prompt.
let response = try await session.respond(to: "Hello! I'd like to learn more about Albert Einstein.")
print(response.content)  // Example response from model: "What would you like to know?"

// Continue using the existing session. Each prompt and response is added to the transcript.
let response2 = try await session.respond(to: "When was he born?")
print(response2.content)  // Example response from model: "March 14, 1879"

मल्टीमोडल इनपुट (जैसे, इमेज) से टेक्स्ट जनरेट करना

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

टेक्स्ट और किसी फ़ाइल (जैसे, इमेज या पीडीएफ़) का इस्तेमाल करके, Gemini मॉडल से टेक्स्ट जनरेट करने के लिए कहा जा सकता है.

import FoundationModels
import FirebaseCore
import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Initialize a `geminiLanguageModel` with a Gemini model that supports your use case.
let model = ai.geminiLanguageModel(name: "gemini-3.5-flash")

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// For a single-turn interaction, create a new session each time you call the model.
let session = LanguageModelSession(model: model)

let cgImage: CGImage = // ... fetch CGImage from your datasource.
let response = try await session.respond {
  "What are the dominant colors of this image, in order?"
  Attachment(cgImage)
}
print(response.content)

जवाब को स्ट्रीम करना

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

// imports
// initialization of Gemini API backend service and a `geminiLanguageModel`

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// For a single-turn interaction, create a new session each time you call the model.
let session = LanguageModelSession(model: model)

let cgImage: CGImage = // ... fetch CGImage from your datasource.
let stream = session.streamResponse {
  "What are the dominant colors of this image, in order?"
  Attachment(cgImage)
}

var response = ""
for try await snapshot in stream {
  // The snapshot contains *all* content generated so far.
  response = snapshot.content
}
print(response)



"Nano Banana" मॉडल का इस्तेमाल करके इमेज जनरेट करना

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

इस सुविधा का इस्तेमाल करने वाले मॉडल

  • gemini-3-pro-image (इसे "Nano Banana Pro" भी कहा जाता है)
  • gemini-3.1-flash-image (इसे "Nano Banana 2" भी कहा जाता है)

सिर्फ़ टेक्स्ट वाले इनपुट का इस्तेमाल करके, Gemini के इमेज जनरेट करने वाले मॉडल (जैसे, "Nano Banana" मॉडल) से इमेज जनरेट करने के लिए कहा जा सकता है.

यहां दिए गए उदाहरण में, सिर्फ़ इमेज जनरेट करने का तरीका दिखाया गया है. हालांकि, Gemini इमेज जनरेट करने वाले मॉडल, इमेज और टेक्स्ट, दोनों जनरेट कर सकते हैं.

import FoundationModels
import FirebaseCore
import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Initialize a `geminiLanguageModel` with a Gemini image-generating model that supports your use case.
let model = ai.geminiLanguageModel(name: "gemini-3.1-flash-image"
    options:
      GeminiGenerationOptions(responseModalities: .image)
)

let session = LanguageModelSession(model: model)
let response = try await session.respond(
          to: "Generate an image of the Eiffel tower with fireworks in the background."
        )

var generatedImage: CIImage?
// Find the image in the transcriptEntries.
for entry in response.transcriptEntries {
  if case let .response(response) = entry {
    for segment in response.segments {
      if case let .attachment(attachment) = segment,
          case let .image(image) = attachment.content {
        generatedImage = image.ciImage
      }
    }
  }
}



स्ट्रक्चर्ड JSON आउटपुट जनरेट करना

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

इस सुविधा का इस्तेमाल करने वाले मॉडल

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-3-pro-image

Gemini मॉडल, जवाबों को अनस्ट्रक्चर्ड टेक्स्ट के तौर पर दिखाते हैं. हालांकि, कुछ इस्तेमाल के मामलों में स्ट्रक्चर्ड टेक्स्ट की ज़रूरत होती है. जैसे, JSON. उदाहरण के लिए, हो सकता है कि आप जवाब का इस्तेमाल, डाउनस्ट्रीम के अन्य ऐसे टास्क के लिए कर रहे हों जिनके लिए, डेटा स्कीमा सेट अप करना ज़रूरी है.

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

import FoundationModels
import FirebaseCore
import FirebaseAILogic

@Generable(description: "Basic profile information about a cat")
struct CatProfile {
  var name: String
  @Guide(description: "The age of the cat", .range(0 ... 20))
  var age: Int
  @Guide(description: "A one sentence profile about the cat's personality")
  var profile: String
}

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Initialize a `geminiLanguageModel` with a Gemini model that supports your use case.
let model = ai.geminiLanguageModel(name: "gemini-3.5-flash")
let session = LanguageModelSession(model: model)

let response = try await session.respond(
  to: "Generate a cute rescue cat profile with an Elvish theme",
  generating: CatProfile.self
)
let cat = response.content


Apple के Foundation Models फ़्रेमवर्क के ज़रिए Gemini API को ऐक्सेस करने के बारे मेंसुझाव, शिकायत या राय दें