توفير أدوات للنموذج عند الوصول إلى Gemini API من خلال إطار عمل Foundation Models من Apple


تفترض الأمثلة الواردة في هذه الصفحة أنّك أكملت الخطوات الواردة في البدء: الوصول إلى Gemini API من خلال إطار عمل Foundation Models من Apple.


يمكنك توفير Gemini أدوات مدمجة Gemini للنماذج عند الوصول إلى Gemini API من خلال إطار عمل Foundation Models من Apple لربط النموذج بمصادر البيانات الخارجية.

توضّح لك الصفحة كيفية استخدام الأدوات المضمّنة التالية لنماذج Gemini:

تتيح ميزة "تحديد المصدر من خلال Google Search" ربط نموذج Gemini بمحتوى الويب المتاح بشكل علني في الوقت الفعلي. يتيح ذلك للنموذج تقديم إجابات أحدث وأكثر دقة والإشارة إلى مصادر يمكن التحقّق منها.

للاطّلاع على التفاصيل وأفضل الممارسات وحالات الاستخدام، يُرجى الرجوع إلى الدليل العام حول الاستناد إلى Google Search.

النماذج المتوافقة

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-3-pro-image-preview (المعروف أيضًا باسم "Nano Banana Pro")
  • gemini-3.1-flash-image-preview (المعروف أيضًا باسم "Nano Banana 2")

قدِّم الأداة googleSearch كجزء من عملية إنشاء 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

تحديد المصدر من خلال Google Maps

تتيح ميزة Grounding with Google Maps ربط نموذج Gemini ببيانات جغرافية مكانية من Google Maps، ما يتيح لك إنشاء وظائف تستند إلى الموقع الجغرافي في تطبيقاتك.

للاطّلاع على التفاصيل وأفضل الممارسات وحالات الاستخدام، يُرجى الرجوع إلى الدليل العام حول الاستناد إلى Google Maps.

النماذج المتوافقة

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

تفعيل أداة Google Maps

توفير أداة googleMaps كجزء من إنشاء 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 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


تقديم ملاحظات حول الوصول إلى Gemini API من خلال إطار عمل "النماذج الأساسية" من Apple