Apple の Foundation Models フレームワークを介して Gemini API にアクセスするときに、モデルにツールを提供する


このページの例は、スタートガイド: Apple の Foundation Models フレームワークを介して 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 の Foundation Models フレームワークを介した Gemini API へのアクセスについてフィードバックをお寄せください