จัดหาเครื่องมือให้กับโมเดลเมื่อเข้าถึง 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.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

การกราวด์ด้วย 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