透過 Apple 的基礎模型架構存取 Gemini API 時可用的功能


本頁面的範例假設您已完成「開始使用:透過 Apple 的 Foundation Models 架構存取 Gemini API」。


本指南說明如何使用 Firebase AI Logic SDK for Apple 平台,透過 Apple 的 Foundation Models 架構,將各種要求傳送至 Gemini API

本頁提供範例,說明如何傳送下列類型的要求:



生成文字

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 供應商,即可在這個頁面查看供應商專屬內容和程式碼。

你可以透過文字和檔案 (例如圖片或 PDF) 提示 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`.
// 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


提供意見 說明如何透過 Apple 的基礎模型架構存取 Gemini API