ความสามารถที่พร้อมใช้งานเมื่อเข้าถึง Gemini API ผ่านเฟรมเวิร์กโมเดลพื้นฐานของ Apple


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


คู่มือนี้จะแสดงวิธีส่งคำขอประเภทต่างๆ ไปยัง Gemini API ผ่านเฟรมเวิร์กโมเดลพื้นฐานของ Apple โดยใช้ SDK ของ Firebase AI Logic สำหรับแพลตฟอร์ม Apple

หน้านี้แสดงตัวอย่างวิธีส่งคำขอประเภทต่อไปนี้



สร้างข้อความ

โมเดล Gemini รองรับความสามารถต่อไปนี้ในการสร้างข้อความ

โมเดลที่รองรับความสามารถนี้

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite

สร้างข้อความจากอินพุตที่เป็นข้อความเท่านั้น

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการ ในหน้านี้

คุณขอให้Geminiโมเดลสร้างข้อความได้โดยการป้อนพรอมต์ด้วยข้อความเท่านั้น

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-3.5-flash")

// Provide a prompt that contains text.
let prompt = "Write a story about a magic backpack."

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// For a single-turn interaction, create a new session each time you call the model.
let session = LanguageModelSession(model: model)

// Generate a text response to the prompt.
let response = try await session.respond(to: prompt)
print(response.content)

สตรีมคำตอบ

คุณสามารถโต้ตอบได้เร็วขึ้นโดยไม่ต้องรอผลลัพธ์ทั้งหมดจากการสร้างโมเดล แต่ให้ใช้การสตรีมเพื่อจัดการผลลัพธ์บางส่วนแทน หากต้องการ สตรีมคำตอบ ให้ใช้ streamResponse(to:) แทน respond(to:)

// imports
// initialization of Gemini API backend service and a `geminiLanguageModel`

// Provide a prompt that contains text.
let prompt = "Write a story about a magic backpack."

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// For a single-turn interaction, create a new session each time you call the model.
let session = LanguageModelSession(model: model)

// Generate a text response to the prompt.
// To stream the response, use `streamResponse(to:)` instead of `respond(to:)`
let stream = session.streamResponse(to: "Write a story about a magic backpack.")
var response = ""
for try await snapshot in stream {
  // The snapshot contains *all* content generated so far.
  response = snapshot.content
}

สร้างข้อความระหว่างการสนทนาไปมา (แชท)

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการ ในหน้านี้

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-3.5-flash")

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// The session maintains state between each request.
let session = LanguageModelSession(model: model)

// Generate a text response to an initial prompt.
let response = try await session.respond(to: "Hello! I'd like to learn more about Albert Einstein.")
print(response.content)  // Example response from model: "What would you like to know?"

// Continue using the existing session. Each prompt and response is added to the transcript.
let response2 = try await session.respond(to: "When was he born?")
print(response2.content)  // Example response from model: "March 14, 1879"

สร้างข้อความจากอินพุตแบบหลายรูปแบบ (เช่น รูปภาพ)

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการ ในหน้านี้

คุณขอให้Geminiโมเดลสร้างข้อความได้โดยป้อนพรอมต์ด้วยข้อความและ ไฟล์ เช่น รูปภาพหรือ PDF

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-3.5-flash")

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// For a single-turn interaction, create a new session each time you call the model.
let session = LanguageModelSession(model: model)

let cgImage: CGImage = // ... fetch CGImage from your datasource.
let response = try await session.respond {
  "What are the dominant colors of this image, in order?"
  Attachment(cgImage)
}
print(response.content)

สตรีมคำตอบ

คุณสามารถโต้ตอบได้เร็วขึ้นโดยไม่ต้องรอผลลัพธ์ทั้งหมดจากการสร้างโมเดล แต่ให้ใช้การสตรีมเพื่อจัดการผลลัพธ์บางส่วนแทน หากต้องการ สตรีมคำตอบ ให้ใช้ streamResponse แทน respond

// imports
// initialization of Gemini API backend service and a `geminiLanguageModel`

// Create a session by injecting the model into Apple's `LanguageModelSession`.
// For a single-turn interaction, create a new session each time you call the model.
let session = LanguageModelSession(model: model)

let cgImage: CGImage = // ... fetch CGImage from your datasource.
let stream = session.streamResponse {
  "What are the dominant colors of this image, in order?"
  Attachment(cgImage)
}

var response = ""
for try await snapshot in stream {
  // The snapshot contains *all* content generated so far.
  response = snapshot.content
}
print(response)



สร้างรูปภาพ (โดยใช้โมเดล "Nano Banana")

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการ ในหน้านี้

โมเดลที่รองรับความสามารถนี้

  • gemini-3-pro-image (หรือที่รู้จักในชื่อ "Nano Banana Pro")
  • gemini-3.1-flash-image (หรือที่เรียกว่า "Nano Banana 2")

คุณขอให้Geminiโมเดลสร้างรูปภาพ (เช่น โมเดล "Nano Banana" ) สร้างรูปภาพได้โดยการป้อนพรอมต์ด้วยข้อความเท่านั้น

ตัวอย่างต่อไปนี้แสดงวิธีสร้างเฉพาะรูปภาพ แต่Gemini โมเดลสร้างรูปภาพสามารถสร้างได้ทั้งรูปภาพและข้อความ

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 image-generating model that supports your use case.
let model = ai.geminiLanguageModel(name: "gemini-3.1-flash-image"
    options:
      GeminiGenerationOptions(responseModalities: .image)
)

let session = LanguageModelSession(model: model)
let response = try await session.respond(
          to: "Generate an image of the Eiffel tower with fireworks in the background."
        )

var generatedImage: CIImage?
// Find the image in the transcriptEntries.
for entry in response.transcriptEntries {
  if case let .response(response) = entry {
    for segment in response.segments {
      if case let .attachment(attachment) = segment,
          case let .image(image) = attachment.content {
        generatedImage = image.ciImage
      }
    }
  }
}



สร้างเอาต์พุต JSON ที่มีโครงสร้าง

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการ ในหน้านี้

โมเดลที่รองรับความสามารถนี้

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-3-pro-image

Gemini โมเดลจะแสดงผลการตอบกลับเป็นข้อความที่ไม่มีโครงสร้างโดยค่าเริ่มต้น อย่างไรก็ตาม กรณีการใช้งานบางอย่างต้องใช้ข้อความที่มีโครงสร้าง เช่น JSON เช่น คุณอาจใช้คำตอบสำหรับงานอื่นๆ ที่ต้องใช้สคีมาข้อมูลที่กำหนดไว้

คุณกำหนดค่าโมเดลให้จัดรูปแบบคำตอบตามสคีมา JSON ที่คุณระบุได้ ดูรายละเอียด แนวทางปฏิบัติแนะนำ และกรณีการใช้งานสำหรับการสร้างเอาต์พุต JSON ที่มีโครงสร้างได้ที่คำแนะนำทั่วไปเกี่ยวกับสร้างเอาต์พุตที่มีโครงสร้าง

import FoundationModels
import FirebaseCore
import FirebaseAILogic

@Generable(description: "Basic profile information about a cat")
struct CatProfile {
  var name: String
  @Guide(description: "The age of the cat", .range(0 ... 20))
  var age: Int
  @Guide(description: "A one sentence profile about the cat's personality")
  var profile: String
}

// 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-3.5-flash")
let session = LanguageModelSession(model: model)

let response = try await session.respond(
  to: "Generate a cute rescue cat profile with an Elvish theme",
  generating: CatProfile.self
)
let cat = response.content


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