透過 Apple 的 Foundation Models 架構存取 Gemini API 時,為模型提供工具


本頁範例假設您已完成「入門指南:透過 Apple 的基礎模型架構存取 Gemini API」。


透過 Apple 的 Foundation Models 架構存取 Gemini API 時,您可以為 Gemini 模型提供 Gemini 內建工具,將模型連結至外部資料來源。

本頁說明如何使用下列 Gemini 模型內建工具:

使用 Google Search 建立基準,可將 Gemini 模型連結至即時公開的網路內容。這可讓模型提供更準確、符合現況的回覆,並引用可驗證的來源。

如需詳細資料、最佳做法和用途,請參閱使用 Google Search 建立基準的一般指南。

支援的模型

  • gemini-4-argon
  • gemini-3.8-flash
  • gemini-3.5-flash-lite (以及舊版 gemini-3.1-flash-lite)
  • gemini-nano-banana-2.1 (Nano Banana 2.1)
  • gemini-3.1-flash-image (Nano Banana 2)
  • gemini-3-pro-image (Nano Banana Pro)

舊版一般用途 Gemini 3.x Flash 模型支援這項功能,但由於即將停用和/或自動轉送至新版模型,因此不建議使用。

在建立 geminiLanguageModel 時提供 googleSearch 工具:

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_MODEL_NAME",
  // Provide Google Search as a tool that the model can use to generate its response.
  serverTools: [GeminiTool.googleSearch()]
)

let session = LanguageModelSession(model: model)
let response = try await session.respond(to: "What is the weather in Toronto today?")
for entry in response.transcriptEntries {
  if case let .response(responseEntry) = entry {
    if let groundingMetadata = responseEntry
        .metadata["groundingMetadata"] as? GroundingMetadata {
      for chunk in groundingMetadata.groundingChunks {
        let webChunk = chunk.web
        // use the webChunk
      }
    }
  }
}

// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result

使用Google Maps建立基準

透過 Google Maps 建立基準,可將 Gemini 模型連結至 Google Maps 的地理空間資料,以便在應用程式中建構位置感知功能。

如需詳細資料、最佳做法和用途,請參閱使用 Google Maps 建立基準的一般指南。

支援的模型

  • gemini-4-argon
  • gemini-3.8-flash
  • gemini-3.5-flash-lite (以及舊版 gemini-3.1-flash-lite)

舊版一般用途 Gemini 3.x Flash 模型支援這項功能,但由於即將停用和/或自動轉送至新版模型,因此不建議使用。

啟用「Google Maps」工具

在建立 geminiLanguageModel 時提供 googleMaps 工具。您也可以選擇在工具設定中提供座標。

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_MODEL_NAME",
  // Provide Google Maps as a tool that the model can use to generate its response.
  serverTools: [GeminiTool.googleMaps()]
)

let session = LanguageModelSession(model: model)

let response = try await session
      respond(to: "Where is a good place to grab a coffee near Alameda, CA?")

for entry in response.transcriptEntries {
  if case let .response(responseEntry) = entry {
    if let groundingMetadata = responseEntry
        .metadata["groundingMetadata"] as? GroundingMetadata {
      for chunk in groundingMetadata.groundingChunks {
        let mapsChunk = chunk.maps
        // use the mapsChunk
      }
    }
  }
}

// Make sure to comply with the "Grounding with Google Maps" usage requirements,
// which includes how you meet service usage requirements


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