จัดหาเครื่องมือให้กับโมเดลเมื่อเข้าถึง Gemini API ผ่านเฟรมเวิร์กโมเดลพื้นฐานของ Apple


ตัวอย่างในหน้านี้ถือว่าคุณได้ทำตามขั้นตอนในเริ่มต้นใช้งาน: เข้าถึง Gemini API ผ่านเฟรมเวิร์ก Foundation Models ของ Apple เสร็จแล้ว


คุณสามารถจัดหาเครื่องมือในตัว Gemini รายการให้กับโมเดล Gemini เมื่อเข้าถึง Gemini API ผ่านเฟรมเวิร์กโมเดลพื้นฐานของ Apple เพื่อเชื่อมต่อโมเดลกับแหล่งข้อมูลภายนอก

หน้านี้จะแสดงวิธีใช้เครื่องมือในตัวต่อไปนี้สำหรับโมเดล Gemini

การเชื่อมต่อข้อมูลกับ Google Search จะเชื่อมต่อโมเดล Gemini กับเนื้อหาเว็บแบบเรียลไทม์ที่เปิดเผยต่อสาธารณะ ซึ่งจะช่วยให้โมเดลให้คำตอบที่แม่นยำและอัปเดตยิ่งขึ้น รวมถึงอ้างอิงแหล่งข้อมูลที่ตรวจสอบได้

ดูรายละเอียด แนวทางปฏิบัติแนะนำ และกรณีการใช้งานได้ที่คู่มือการอ้างอิงข้อมูลใน Google Search ทั่วไป

โมเดลที่รองรับ

  • gemini-4-argon
  • gemini-3.8-flash
  • gemini-3.5-flash-lite (และ gemini-3.1-flash-lite รุ่นเก่า)
  • gemini-nano-banana-2.1 (Nano Banana 2.1)
  • gemini-3.1-flash-image (Nano Banana 2)
  • gemini-3-pro-image (Nano Banana Pro)

โมเดล Gemini 3.x Flash สำหรับการใช้งานทั่วไปที่เก่ากว่ารองรับความสามารถนี้ แต่เราไม่แนะนำให้ใช้อีกต่อไปเนื่องจากวันที่ปิดตัวที่กำลังจะมาถึงและ/หรือการกำหนดเส้นทางอัตโนมัติไปยังโมเดลที่ใหม่กว่า

ระบุเครื่องมือ 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

การอ้างอิงข้อมูลด้วย Google Maps จะเชื่อมต่อโมเดล Gemini กับข้อมูลเชิงพื้นที่จาก Google Maps เพื่อให้คุณสร้างฟังก์ชันการทำงานที่รับรู้ถึงตำแหน่งลงในแอปได้

ดูรายละเอียด แนวทางปฏิบัติแนะนำ และกรณีการใช้งานได้ที่คู่มือการอ้างอิงข้อมูลใน Google Maps ทั่วไป

โมเดลที่รองรับ

  • gemini-4-argon
  • gemini-3.8-flash
  • gemini-3.5-flash-lite (และ gemini-3.1-flash-lite รุ่นเก่า)

โมเดล Gemini 3.x Flash สำหรับการใช้งานทั่วไปที่เก่ากว่ารองรับความสามารถนี้ แต่เราไม่แนะนำให้ใช้อีกต่อไปเนื่องจากวันที่ปิดตัวที่กำลังจะมาถึงและ/หรือการกำหนดเส้นทางอัตโนมัติไปยังโมเดลที่ใหม่กว่า

เปิดใช้เครื่องมือ 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