قابلیت‌های دردسترس هنگام دسترسی به Gemini API ازطریق چارچوب «مدل‌های پایه» Apple


نمونه‌های این صفحه فرض می‌کنند که شما شروع به کار: دسترسی به Gemini API ازطریق چارچوب مدل‌های بنیادی Apple را تکمیل کرده‌اید.


این راهنما به شما نشان می‌دهد که چگونه انواع مختلف درخواست‌ها را ازطریق چارچوب «مدل‌های بنیادی» Apple بااستفاده از کیت توسعه نرم‌افزار Firebase AI Logic برای پلاتفرم‌های Apple به Gemini API ارسال کنید.

این صفحه نمونه‌هایی از نحوه ارسال انواع درخواست‌های زیر را نشان می‌دهد:



تولید نوشتار

مدل‌های Gemini از قابلیت‌های زیر برای تولید نوشتار پشتیبانی می‌کنند:

مدل‌هایی که از این قابلیت پشتیبانی می‌کنند

  • gemini-4-hydrogen
  • gemini-4-argon
  • gemini-4-carbon
  • gemini-3.8-flash
  • ‫gemini-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-hydrogen
  • gemini-4-argon
  • gemini-4-carbon
  • gemini-3.8-flash
  • ‫gemini-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


ارائه بازخورد درباره دسترسی به Gemini API ازطریق چارچوب «مدل‌های بنیادی» Apple