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