| ตัวอย่างในหน้านี้จะถือว่าคุณได้ทำตามขั้นตอนในหัวข้อ เริ่มต้นใช้งาน: เข้าถึง Gemini API ผ่านเฟรมเวิร์ก Foundation Models ของ Apple แล้ว |
คุณสามารถระบุเครื่องมือในตัวของ Gemini ให้กับโมเดล Gemini เมื่อ เข้าถึง Gemini API ผ่านเฟรมเวิร์ก Foundation Models ของ Apple เพื่อ เชื่อมต่อโมเดลกับแหล่งข้อมูลภายนอก
หน้านี้จะแสดงวิธีใช้เครื่องมือในตัวต่อไปนี้สำหรับ Gemini โมเดล
การอ้างอิงข้อมูลจาก Google Search
การอ้างอิงข้อมูลจาก
โปรดดูรายละเอียด แนวทางปฏิบัติแนะนำ และกรณีการใช้งานได้ที่คู่มือทั่วไปเกี่ยวกับ
การอ้างอิงข้อมูลจาก
โมเดลที่รองรับ
gemini-3.1-pro-previewgemini-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")
เปิดใช้เครื่องมือ Google Search
ระบุเครื่องมือ 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
การอ้างอิงข้อมูลจาก
โปรดดูรายละเอียด แนวทางปฏิบัติแนะนำ และกรณีการใช้งานได้ที่คู่มือทั่วไปเกี่ยวกับ
การอ้างอิงข้อมูลจาก
โมเดลที่รองรับ
gemini-3.1-pro-previewgemini-3.5-flashgemini-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