| Các ví dụ trên trang này giả định rằng bạn đã hoàn tất phần Bắt đầu: Truy cập vào Gemini API thông qua khung Foundation Models của Apple. |
Bạn có thể cung cấp các công cụ tích hợp Gemini cho các mô hình Gemini khi truy cập vào Gemini API thông qua khung Foundation Models của Apple để kết nối mô hình với các nguồn dữ liệu bên ngoài.
Trang này hướng dẫn bạn cách sử dụng các công cụ tích hợp sau đây cho các mô hình Gemini:
Bám sát nguồn bằng Google Search
Tính năng Bám sát nguồn bằng
Để biết thông tin chi tiết, các phương pháp hay nhất và trường hợp sử dụng, hãy xem hướng dẫn chung về Căn cứ vào
Các mô hình được hỗ trợ
gemini-3.1-pro-previewgemini-3.5-flashgemini-3.1-flash-litegemini-3-pro-image-preview(còn gọi là "Nano Banana Pro")gemini-3.1-flash-image-preview(còn gọi là "Nano Banana 2")
Bật công cụ Google Search
Cung cấp công cụ googleSearch trong quá trình tạo geminiLanguageModel:
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
Bám sát nguồn bằng Google Maps
Tính năng cơ sở hoá bằng
Để biết thông tin chi tiết, các phương pháp hay nhất và trường hợp sử dụng, hãy xem hướng dẫn chung về Căn cứ vào
Các mô hình được hỗ trợ
gemini-3.1-pro-previewgemini-3.5-flashgemini-3.1-flash-lite
Bật công cụ Google Maps
Cung cấp công cụ googleMaps trong quá trình tạo geminiLanguageModel.
Bạn cũng có thể cung cấp toạ độ trong cấu hình của công cụ (không bắt buộc).
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
Gửi ý kiến phản hồi về việc truy cập vào Gemini API thông qua khung Mô hình nền tảng của Apple