قابلیت‌های موجود هنگام دسترسی به رابط برنامه‌نویسی نرم‌افزار Gemini از طریق چارچوب مدل‌های بنیادی اپل


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


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

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



تولید متن

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

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

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

تولید متن از ورودی فقط متنی

برای مشاهده محتوا و کد مخصوص ارائه‌دهنده در این صفحه، روی ارائه‌دهنده API 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 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:) 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
}

تولید متن در طول یک جلسه چند نوبتی (چت)

برای مشاهده محتوا و کد مخصوص ارائه‌دهنده در این صفحه، روی ارائه‌دهنده 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`.
// 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"

تولید متن از ورودی چندوجهی (مانند تصاویر)

برای مشاهده محتوا و کد مخصوص ارائه‌دهنده در این صفحه، روی ارائه‌دهنده API Gemini خود کلیک کنید.

شما می‌توانید از یک مدل 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)

پاسخ را پخش کنید

شما می‌توانید با منتظر نماندن برای کل نتیجه از تولید مدل، به تعاملات سریع‌تری دست یابید و در عوض از streaming برای مدیریت نتایج جزئی استفاده کنید. برای streaming پاسخ، به جای 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)



تولید تصاویر (با استفاده از مدل‌های «نانو موز»)

برای مشاهده محتوا و کد مخصوص ارائه‌دهنده در این صفحه، روی ارائه‌دهنده API Gemini خود کلیک کنید.

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

  • gemini-3-pro-image (معروف به "نانو موز پرو")
  • gemini-3.1-flash-image (معروف به "نانو موز ۲")

شما می‌توانید از یک مدل تولیدکننده تصویر 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 ساختاریافته

برای مشاهده محتوا و کد مخصوص ارائه‌دهنده در این صفحه، روی ارائه‌دهنده API Gemini خود کلیک کنید.

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

  • 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


در مورد دسترسی به API Gemini از طریق چارچوب مدل‌های بنیادی اپل، بازخورد خود را ارائه دهید.