จัดหาเครื่องมือให้กับโมเดลเมื่อเข้าถึง Gemini API ผ่านเฟรมเวิร์กโมเดลพื้นฐานของ 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.6-flash (และ gemini-3.5-flash เวอร์ชันเก่า)
  • gemini-3.5-flash-lite (และ gemini-3.1-flash-lite เวอร์ชันเก่า)
  • gemini-3-pro-image (หรือ "Nano Banana Pro")
  • gemini-3.1-flash-image (หรือ "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

การอ้างอิงข้อมูลจาก 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 ผ่านเฟรมเวิร์ก Foundation Models ของ Apple