| इस पेज पर दिए गए उदाहरणों से यह माना जाता है कि आपने शुरू करें: Apple के Foundation Models फ़्रेमवर्क के ज़रिए Gemini API को ऐक्सेस करें को पूरा कर लिया है. |
इस गाइड में, Apple के Foundation Models फ़्रेमवर्क के ज़रिए Gemini API को अलग-अलग तरह के अनुरोध भेजने का तरीका बताया गया है. इसके लिए, Apple प्लैटफ़ॉर्म के लिए उपलब्ध Firebase AI Logic SDK का इस्तेमाल किया जाता है.
इस पेज पर, इन अनुरोधों को भेजने के उदाहरण दिए गए हैं:
- सिर्फ़ टेक्स्ट वाले इनपुट से टेक्स्ट जनरेट करना
- मल्टी-टर्न सेशन (चैट) के दौरान टेक्स्ट जनरेट करना
- मल्टीमॉडल इनपुट (जैसे, इमेज) से जनरेट किया गया टेक्स्ट
- सिर्फ़ टेक्स्ट वाले इनपुट से इमेज जनरेट करना
टेक्स्ट जनरेट करना
Gemini मॉडल, टेक्स्ट जनरेट करने के लिए इन सुविधाओं के साथ काम करते हैं:
- सिर्फ़ टेक्स्ट वाले इनपुट से टेक्स्ट जनरेट करना
- मल्टी-टर्न सेशन (चैट) के दौरान टेक्स्ट जनरेट करना
- मल्टीमॉडल इनपुट (जैसे, इमेज) से जनरेट किया गया टेक्स्ट
इस सुविधा के साथ काम करने वाले मॉडल
gemini-4-hydrogengemini-4-argongemini-4-carbongemini-3.8-flashgemini-3.5-flash-lite(और इससे पुरानाgemini-3.1-flash-lite)
सामान्य इस्तेमाल के लिए उपलब्ध पुराने Gemini 3.x Flash मॉडल में यह सुविधा काम करती है. हालांकि, अब इनका इस्तेमाल करने का सुझाव नहीं दिया जाता. इसकी वजह यह है कि इन्हें बंद करने की तारीखें आने वाली हैं और/या नए मॉडल पर अपने-आप रूट होने की सुविधा उपलब्ध है.
सिर्फ़ टेक्स्ट वाले इनपुट से टेक्स्ट जनरेट करना
|
इस पेज पर, सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट और कोड देखने के लिए, 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-4-hydrogen")
// 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-4-hydrogen")
// 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 मॉडल से टेक्स्ट जनरेट करने के लिए कहा जा सकता है. इसके लिए, टेक्स्ट और इमेज या PDF जैसी किसी फ़ाइल के साथ प्रॉम्प्ट दें.
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-4-hydrogen")
// 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-nano-banana-2.1(Nano Banana 2.1)gemini-3.1-flash-image(Nano Banana 2)gemini-3.1-flash-lite-image(Nano Banana 2 Lite)gemini-3-pro-image(Nano Banana Pro)
Gemini Nano Banana मॉडल को सिर्फ़ टेक्स्ट प्रॉम्प्ट देकर, इमेज जनरेट करने के लिए कहा जा सकता है.
यहां दिए गए उदाहरण में, सिर्फ़ इमेज जनरेट करने का तरीका बताया गया है. हालांकि, Nano Banana मॉडल, इमेज और टेक्स्ट, दोनों जनरेट कर सकते हैं.
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 image output.
let model = ai.geminiLanguageModel(name: "gemini-nano-banana-2.1"
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-4-hydrogengemini-4-argongemini-4-carbongemini-3.8-flashgemini-3.5-flash-lite(और इससे पुरानाgemini-3.1-flash-lite)gemini-3-pro-image(Nano Banana Pro)
सामान्य इस्तेमाल के लिए उपलब्ध पुराने Gemini 3.x Flash मॉडल में यह सुविधा काम करती है. हालांकि, अब इनका इस्तेमाल करने का सुझाव नहीं दिया जाता. इसकी वजह यह है कि इन्हें बंद करने की तारीखें आने वाली हैं और/या नए मॉडल पर अपने-आप रूट होने की सुविधा उपलब्ध है.
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_MODEL_NAME")
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