| בדוגמאות שבדף הזה מניחים שסיימתם את השלב תחילת העבודה: גישה אל Gemini API באמצעות מסגרת Foundation Models של Apple. |
במדריך הזה מוסבר איך לשלוח סוגים שונים של בקשות אל Gemini API באמצעות מסגרת Foundation Models של Apple, באמצעות Firebase AI Logic SDK לפלטפורמות של Apple.
בדף הזה מוצגות דוגמאות לאופן השליחה של סוגי הבקשות הבאים:
- יצירת טקסט מקלט טקסט בלבד
- יצירת טקסט במהלך סשן רב-שלבי (צ'אט)
- יצירת טקסט מקלט מרובה-אופנים (כמו תמונות)
- יצירת תמונות לפי קלט טקסט בלבד
יצירת טקסט
מודלים של Gemini תומכים ביכולות הבאות ליצירת טקסט:
- יצירת טקסט מקלט טקסט בלבד
- יצירת טקסט במהלך סשן רב-שלבי (צ'אט)
- יצירת טקסט מקלט מרובה-אופנים (כמו תמונות)
מודלים שתומכים ביכולת הזו
gemini-3.1-pro-previewgemini-3.5-flashgemini-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)
הצגת התשובה באופן שוטף
כדי לקבל אינטראקציות מהירות יותר, לא צריך לחכות לתוצאה המלאה מהמודל, אלא אפשר להשתמש בסטרימינג כדי לטפל בתוצאות חלקיות. כדי להזרים את התגובה, צריך להשתמש ב-streamResponse(to:) במקום ב-respond(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 ליצור טקסט על ידי מתן הנחיה עם טקסט וקובץ, כמו תמונה או 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-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)
הצגת התשובה באופן שוטף
כדי לקבל אינטראקציות מהירות יותר, לא צריך לחכות לתוצאה המלאה מהמודל, אלא אפשר להשתמש בסטרימינג כדי לטפל בתוצאות חלקיות. כדי להזרים את התגובה, צריך להשתמש ב-streamResponse במקום ב-respond.
// 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)
אתם יכולים לבקש ממודל ליצירת תמונות (כמו מודל Nano Banana) ליצור תמונה באמצעות הנחיה עם קלט טקסט בלבד.Gemini
בדוגמה הבאה מוצג איך ליצור רק תמונה, אבל מודלים ליצירת תמונות יכולים ליצור גם תמונות וגם טקסט.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-previewgemini-3.5-flashgemini-3.1-flash-litegemini-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
שליחת משוב על הגישה אל Gemini API דרך מסגרת Foundation Models של Apple