Cung cấp các công cụ cho mô hình khi truy cập vào Gemini API thông qua khung Foundation Models của Apple


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:

Tính năng Bám sát nguồn bằng Google Search kết nối mô hình Gemini với nội dung trên web theo thời gian thực và có sẵn công khai. Nhờ đó, mô hình có thể đưa ra câu trả lời chính xác và mới nhất, đồng thời trích dẫn các nguồn có thể xác minh.

Để 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 Google Search.

Các mô hình được hỗ trợ

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-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")

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 Google Maps kết nối một mô hình Gemini với dữ liệu không gian địa lý từ Google Maps để bạn có thể tạo chức năng nhận biết vị trí trong các ứng dụng của mình.

Để 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 Google Maps.

Các mô hình được hỗ trợ

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-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