通过 Apple 的基础模型框架访问 Gemini API 时,向模型提供工具


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


通过 Apple 的基础模型框架访问 Gemini API 时,您可以向 Gemini 模型提供 Gemini 内置工具,以将模型连接到外部数据源。

本页介绍了如何使用 Gemini 模型的以下内置工具:

依托 Google Search 进行接地会将 Gemini 模型连接到 实时、公开提供的 Web 内容。这使模型能够提供更准确、最新的回答,并引用可验证的来源。

如需了解详情、最佳实践和用例,请参阅通用 依托 Google Search 指南。

支持的模型

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-3-pro-image-preview(又称“Nano Banana Pro”)
  • gemini-3.1-flash-image-preview(又称“Nano Banana 2”)

在创建 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-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite

启用 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