| 本页中的示例假定您已完成 入门:通过 Apple 的基础模型框架访问 Gemini API。 |
通过 Apple 的基础模型框架访问 Gemini API 时,您可以向 Gemini 模型提供 Gemini 内置工具,以将模型连接到外部数据源。
本页介绍了如何使用 Gemini 模型的以下内置工具:
- 依托
Google Search 进行接地 - 依托
Google Maps 进行接地
依托 Google Search 进行接地
依托
如需了解详情、最佳实践和用例,请参阅通用
依托
支持的模型
gemini-3.1-pro-previewgemini-3.5-flashgemini-3.1-flash-litegemini-3-pro-image-preview(又称“Nano Banana Pro”)gemini-3.1-flash-image-preview(又称“Nano Banana 2”)
启用 Google Search 工具
在创建 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 进行接地
依托
如需了解详情、最佳实践和用例,请参阅通用
依托
支持的模型
gemini-3.1-pro-previewgemini-3.5-flashgemini-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