الميزات المتاحة عند استخدام واجهة برمجة تطبيقات Gemini من خلال إطار عمل Foundation Models من Apple


تفترض الأمثلة الواردة في هذه الصفحة أنّك أكملت مقالة البدء: الوصول إلى Gemini API من خلال إطار عمل Apple's Foundation Models.


يوضّح هذا الدليل كيفية إرسال أنواع مختلفة من الطلبات إلى الـ Gemini API من خلال إطار عمل Apple's Foundation Models باستخدام الـ Firebase AI Logic SDK لمنصّات Apple.

تعرِض هذه الصفحة أمثلة على كيفية إرسال الأنواع التالية من الطلبات:



إنشاء نص

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)

عرض الردّ تدريجيًا

يمكنك تحقيق تفاعلات أسرع من خلال عدم انتظار النتيجة الكاملة من إنشاء النموذج، واستخدام عرض الردّ تدريجيًا بدلاً من ذلك للتعامل مع النتائج الجزئية. لعرض الردّ تدريجيًا، استخدِم 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")

يمكنك أن تطلب من نموذج 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


إرسال ملاحظات حول الوصول إلى Gemini API من خلال إطار عمل Apple's Foundation Models