通过 Apple 的基础模型框架访问 Gemini API 时可用的功能


本页中的示例假定您已完成 入门:通过 Apple 的 Foundation Models 框架访问 Gemini API


本指南介绍了如何使用适用于 Apple 平台的 Firebase AI Logic SDK,通过 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 提供商,以查看此页面上特定于提供商的内容 和代码。

您可以要求 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


提供反馈 ,说明如何通过 Apple 的 Foundation Models 框架访问 Gemini API