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


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


通过 Apple 的 Foundation Models 框架访问 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(又称“Nano Banana Pro”)
  • gemini-3.1-flash-image(又称“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