Fornire strumenti al modello quando si accede all'API Gemini tramite il framework Foundation Models di Apple


Gli esempi in questa pagina presuppongono che tu abbia completato la sezione Inizia: accedi a Gemini API tramite il framework Foundation Models di Apple.


Puoi fornire strumenti integrati Gemini ai modelli Gemini quando accedi a Gemini API tramite il framework Foundation Models di Apple per collegare il modello a origini dati esterne.

La pagina mostra come utilizzare i seguenti strumenti integrati per i modelli Gemini:

Il grounding con Google Search collega un modello Gemini a contenuti web disponibili pubblicamente in tempo reale. In questo modo, il modello può fornire risposte più accurate e aggiornate e citare fonti verificabili.

Per dettagli, best practice e casi d'uso, consulta la guida generale Grounding con Google Search.

Modelli supportati

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-3-pro-image-preview (noto anche come "Nano Banana Pro")
  • gemini-3.1-flash-image-preview (ovvero "Nano Banana 2")

Fornisci lo strumento googleSearch durante la creazione di 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

Grounding con Google Maps

Il grounding con Google Maps collega un modello Gemini ai dati geospaziali di Google Maps, in modo da poter integrare funzionalità basate sulla posizione nelle tue app.

Per dettagli, best practice e casi d'uso, consulta la guida generale Grounding con Google Maps.

Modelli supportati

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite

Attivare lo strumento Google Maps

Fornisci lo strumento googleMaps nell'ambito della creazione di geminiLanguageModel. Puoi anche fornire facoltativamente le coordinate nella configurazione dello strumento.

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


Invia feedback sull'accesso a Gemini API tramite il framework Foundation Models di Apple