เริ่มต้นใช้งาน Gemini API โดยใช้ Vertex AI ใน Firebase SDK

คู่มือนี้จะแสดงวิธีเริ่มต้นการเรียกใช้ Vertex AI Gemini API โดยตรงจากแอปโดยใช้ SDK Vertex AI in Firebase สําหรับแพลตฟอร์มที่เลือก

โปรดทราบว่าคุณยังใช้คู่มือนี้เพื่อเริ่มต้นการเข้าถึงImagenโมเดลโดยใช้ Vertex AI in FirebaseSDK ได้ด้วย

ข้อกำหนดเบื้องต้น

Swift

คู่มือนี้ถือว่าคุณคุ้นเคยกับการใช้ Xcode เพื่อพัฒนาแอปสำหรับแพลตฟอร์ม Apple (เช่น iOS)

  • ตรวจสอบว่าสภาพแวดล้อมการพัฒนาและแอปแพลตฟอร์ม Apple ของคุณเป็นไปตามข้อกำหนดต่อไปนี้

    • Xcode 16.2 ขึ้นไป
    • แอปของคุณกำหนดเป้าหมายเป็น iOS 15 ขึ้นไปหรือ macOS 12 ขึ้นไป
  • (ไม่บังคับ) ดูแอปตัวอย่าง

    ดาวน์โหลดแอปเริ่มต้นใช้งาน

    คุณสามารถลองใช้ SDK ได้อย่างรวดเร็ว ดูการใช้งานที่สมบูรณ์ของกรณีการใช้งานต่างๆ หรือใช้แอปตัวอย่างหากไม่มีแอปแพลตฟอร์ม Apple ของคุณเอง หากต้องการใช้แอปตัวอย่าง คุณจะต้องเชื่อมต่อแอปกับโปรเจ็กต์ Firebase

Kotlin

คู่มือนี้จะถือว่าคุณคุ้นเคยกับการใช้ Android Studio เพื่อพัฒนาแอปสำหรับ Android

  • ตรวจสอบว่าสภาพแวดล้อมการพัฒนาและแอป Android ของคุณเป็นไปตามข้อกำหนดต่อไปนี้

    • Android Studio (เวอร์ชันล่าสุด)
    • แอปของคุณกำหนดเป้าหมายเป็น API ระดับ 21 ขึ้นไป
  • (ไม่บังคับ) ดูแอปตัวอย่าง

    ดาวน์โหลดแอปตัวอย่าง

    คุณสามารถลองใช้ SDK ได้อย่างรวดเร็ว ดูการใช้งานที่สมบูรณ์ของกรณีการใช้งานต่างๆ หรือใช้แอปตัวอย่างหากไม่มีแอป Android ของคุณเอง หากต้องการใช้แอปตัวอย่าง คุณจะต้องเชื่อมต่อแอปกับโปรเจ็กต์ Firebase

Java

คู่มือนี้จะถือว่าคุณคุ้นเคยกับการใช้ Android Studio เพื่อพัฒนาแอปสำหรับ Android

  • ตรวจสอบว่าสภาพแวดล้อมการพัฒนาและแอป Android ของคุณเป็นไปตามข้อกำหนดต่อไปนี้

    • Android Studio (เวอร์ชันล่าสุด)
    • แอปของคุณกำหนดเป้าหมายเป็น API ระดับ 21 ขึ้นไป
  • (ไม่บังคับ) ดูแอปตัวอย่าง

    ดาวน์โหลดแอปตัวอย่าง

    คุณสามารถลองใช้ SDK ได้อย่างรวดเร็ว ดูการใช้งานที่สมบูรณ์ของกรณีการใช้งานต่างๆ หรือใช้แอปตัวอย่างหากไม่มีแอป Android ของคุณเอง หากต้องการใช้แอปตัวอย่าง คุณจะต้องเชื่อมต่อแอปกับโปรเจ็กต์ Firebase

Web

คู่มือนี้จะถือว่าคุณคุ้นเคยกับการใช้ JavaScript เพื่อพัฒนาเว็บแอป คู่มือนี้ใช้ได้กับเฟรมเวิร์กทุกประเภท

  • ตรวจสอบว่าสภาพแวดล้อมการพัฒนาและเว็บแอปเป็นไปตามข้อกำหนดต่อไปนี้

    • (ไม่บังคับ) Node.js
    • เว็บเบราว์เซอร์สมัยใหม่
  • (ไม่บังคับ) ดูแอปตัวอย่าง

    ดาวน์โหลดแอปตัวอย่าง

    คุณสามารถลองใช้ SDK ได้อย่างรวดเร็ว ดูการใช้งาน Use Case ต่างๆ อย่างครบถ้วน หรือใช้แอปตัวอย่างหากไม่มีเว็บแอปของคุณเอง หากต้องการใช้แอปตัวอย่าง คุณจะต้องเชื่อมต่อแอปกับโปรเจ็กต์ Firebase

Dart

คู่มือนี้จะถือว่าคุณคุ้นเคยกับการพัฒนาแอปด้วย Flutter

  • ตรวจสอบว่าสภาพแวดล้อมการพัฒนาและแอป Flutter เป็นไปตามข้อกำหนดต่อไปนี้

    • Dart 3.2.0 ขึ้นไป
  • (ไม่บังคับ) ดูแอปตัวอย่าง

    ดาวน์โหลดแอปตัวอย่าง

    คุณสามารถลองใช้ SDK ได้อย่างรวดเร็ว ดูการใช้งานที่สมบูรณ์ของ Use Case ต่างๆ หรือใช้แอปตัวอย่างหากไม่มีแอป Flutter ของคุณเอง หากต้องการใช้แอปตัวอย่าง คุณจะต้องเชื่อมต่อแอปกับโปรเจ็กต์ Firebase

ขั้นตอนที่ 1: ตั้งค่าโปรเจ็กต์ Firebase และเชื่อมต่อแอปกับ Firebase

หากคุณมีโปรเจ็กต์ Firebase และแอปที่เชื่อมต่อกับ Firebase อยู่แล้ว

  1. ในคอนโซล Firebase ให้ไปที่หน้า Vertex AI

  2. คลิกการ์ด Vertex AI in Firebase เพื่อเปิดใช้งานเวิร์กโฟลว์ที่จะช่วยคุณทำงานต่อไปนี้

  3. ไปยังขั้นตอนถัดไปในคู่มือนี้เพื่อเพิ่ม SDK ลงในแอป

หากคุณไม่มีโปรเจ็กต์ Firebase และแอปที่เชื่อมต่อกับ Firebase อยู่แล้ว


ขั้นตอนที่ 2: เพิ่ม SDK

เมื่อตั้งค่าโปรเจ็กต์ Firebase และเชื่อมต่อแอปกับ Firebase แล้ว (ดูขั้นตอนก่อนหน้า) ตอนนี้คุณก็เพิ่ม Vertex AI in Firebase SDK ลงในแอปได้แล้ว

Swift

ใช้ Swift Package Manager เพื่อติดตั้งและจัดการทรัพยากร Dependency ของ Firebase

ไลบรารี Vertex AI in Firebase ให้สิทธิ์เข้าถึง API สําหรับการโต้ตอบกับโมเดล Gemini และ Imagen ไลบรารีนี้รวมอยู่ใน Firebase SDK สําหรับแพลตฟอร์ม Apple (firebase-ios-sdk)

  1. เปิดโปรเจ็กต์แอปใน Xcode แล้วไปที่ไฟล์ > เพิ่มแพ็กเกจ

  2. เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ Firebase SDK สําหรับแพลตฟอร์ม Apple ดังนี้

    https://github.com/firebase/firebase-ios-sdk
    
  3. เลือก SDK เวอร์ชันล่าสุด

  4. เลือกคลัง FirebaseVertexAI

เมื่อเสร็จแล้ว Xcode จะเริ่มจับคู่ข้อมูลและดาวน์โหลดทรัพยากร Dependency ในเบื้องหลังโดยอัตโนมัติ

Kotlin

Vertex AI in Firebase SDK สําหรับ Android (firebase-vertexai) ให้สิทธิ์เข้าถึง API สําหรับการโต้ตอบกับรุ่น Gemini และ Imagen

ในไฟล์ Gradle ของโมดูล (ระดับแอป) (เช่น <project>/<app-module>/build.gradle.kts) ให้เพิ่มทรัพยากร Dependency สำหรับคลัง Vertex AI in Firebase สำหรับ Android เราขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันของไลบรารี

dependencies {
  // ... other androidx dependencies

  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:33.13.0"))

  // Add the dependency for the Vertex AI in Firebase library
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-vertexai")
}

การใช้ Firebase Android BoM จะทำให้แอปใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้อยู่เสมอ

Java

Vertex AI in Firebase SDK สําหรับ Android (firebase-vertexai) ให้สิทธิ์เข้าถึง API สําหรับการโต้ตอบกับรุ่น Gemini และ Imagen

ในไฟล์ Gradle ของโมดูล (ระดับแอป) (เช่น <project>/<app-module>/build.gradle.kts) ให้เพิ่มทรัพยากร Dependency สำหรับคลัง Vertex AI in Firebase สำหรับ Android เราขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันของไลบรารี

สำหรับ Java คุณต้องเพิ่มไลบรารีอีก 2 รายการ

dependencies {
  // ... other androidx dependencies

  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:33.13.0"))

  // Add the dependency for the Vertex AI in Firebase library
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-vertexai")

  // Required for one-shot operations (to use `ListenableFuture` from Guava Android)
  implementation("com.google.guava:guava:31.0.1-android")

  // Required for streaming operations (to use `Publisher` from Reactive Streams)
  implementation("org.reactivestreams:reactive-streams:1.0.4")
}

การใช้ Firebase Android BoM จะทำให้แอปใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้อยู่เสมอ

Web

ไลบรารี Vertex AI in Firebase ให้สิทธิ์เข้าถึง API สําหรับการโต้ตอบกับโมเดล Gemini และ Imagen ไลบรารีนี้รวมอยู่ใน Firebase JavaScript SDK สําหรับเว็บ

  1. ติดตั้ง Firebase JS SDK สําหรับเว็บโดยใช้ npm

    npm install firebase
    
  2. เริ่มต้นใช้งาน Firebase ในแอป

    import { initializeApp } from "firebase/app";
    
    // TODO(developer) Replace the following with your app's Firebase configuration
    // See: https://firebase.google.com/docs/web/learn-more#config-object
    const firebaseConfig = {
      // ...
    };
    
    // Initialize FirebaseApp
    const firebaseApp = initializeApp(firebaseConfig);
    

Dart

ปลั๊กอิน Vertex AI in Firebase สําหรับ Flutter (firebase_vertexai) ให้สิทธิ์เข้าถึง API สําหรับการโต้ตอบกับรูปแบบ Gemini และ Imagen

  1. จากไดเรกทอรีโปรเจ็กต์ Flutter ให้เรียกใช้คำสั่งต่อไปนี้เพื่อติดตั้งปลั๊กอินหลักและปลั๊กอิน Vertex AI in Firebase

    flutter pub add firebase_core && flutter pub add firebase_vertexai
    
  2. ในไฟล์ lib/main.dart ให้นําเข้าปลั๊กอินหลักของ Firebase, ปลั๊กอิน Vertex AI in Firebase และไฟล์กําหนดค่าที่คุณสร้างขึ้นก่อนหน้านี้ โดยทําดังนี้

    import 'package:firebase_core/firebase_core.dart';
    import 'package:firebase_vertexai/firebase_vertexai.dart';
    import 'firebase_options.dart';
    
  3. นอกจากนี้ ในไฟล์ lib/main.dart ให้เริ่มต้น Firebase โดยใช้ออบเจ็กต์ DefaultFirebaseOptions ที่ส่งออกโดยไฟล์การกําหนดค่า

    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
    
  4. สร้างแอปพลิเคชัน Flutter อีกครั้งโดยทำดังนี้

    flutter run
    

ขั้นตอนที่ 3: เริ่มต้นบริการ Vertex AI และสร้างอินสแตนซ์ GenerativeModel

คุณต้องเริ่มต้นบริการ Vertex AI และสร้างอินสแตนซ์ GenerativeModel ก่อนจึงจะเรียก API และส่งพรอมต์ไปยังโมเดล Gemini ได้

Swift

import FirebaseVertexAI

// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()

// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-flash")

Kotlin

สำหรับ Kotlin เมธอดใน SDK นี้จะถือเป็นฟังก์ชันที่ระงับและต้องมีการเรียกใช้จากขอบเขต Coroutine
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")

Java

สําหรับ Java เมธอดสตรีมมิงใน SDK นี้จะแสดงผลประเภท Publisher จากไลบรารี Reactive Streams
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-2.0-flash");

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Web

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-flash" });

Dart

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
      FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-flash');

หลังจากอ่านคู่มือเริ่มต้นใช้งานนี้จบแล้ว โปรดดูวิธีเลือกโมเดลและ (ไม่บังคับ) ตำแหน่งที่เหมาะสมกับกรณีการใช้งานและแอป

ขั้นตอนที่ 4: ส่งคําขอพรอมต์ไปยังโมเดล

เมื่อเชื่อมต่อแอปกับ Firebase, เพิ่ม SDK และเริ่มต้นบริการ Vertex AI และโมเดล Generative แล้ว คุณก็พร้อมส่งคําขอพรอมต์ไปยังโมเดล Gemini

คุณสามารถใช้ generateContent() เพื่อสร้างข้อความจากพรอมต์แบบข้อความเท่านั้น โดยทำดังนี้

Swift

import FirebaseVertexAI

// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()

// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-flash")

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

// To generate text output, call generateContent with the text input
let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")

Kotlin

สำหรับ Kotlin เมธอดใน SDK นี้จะถือเป็นฟังก์ชันที่ระงับและต้องมีการเรียกใช้จากขอบเขต Coroutine
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")

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

// To generate text output, call generateContent with the text input
val response = generativeModel.generateContent(prompt)
print(response.text)

Java

สําหรับ Java เมธอดใน SDK นี้จะแสดงผลเป็น ListenableFuture
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-2.0-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// Provide a prompt that contains text
Content prompt = new Content.Builder()
    .addText("Write a story about a magic backpack.")
    .build();

// To generate text output, call generateContent with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-flash" });

// Wrap in an async function so you can use await
async function run() {
  // Provide a prompt that contains text
  const prompt = "Write a story about a magic backpack."

  // To generate text output, call generateContent with the text input
  const result = await model.generateContent(prompt);

  const response = result.response;
  const text = response.text();
  console.log(text);
}

run();

Dart

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
      FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-flash');

// Provide a prompt that contains text
final prompt = [Content.text('Write a story about a magic backpack.')];

// To generate text output, call generateContent with the text input
final response = await model.generateContent(prompt);
print(response.text);

คุณทำอะไรได้อีกบ้าง

ดูข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบที่รองรับ

ดูข้อมูลเกี่ยวกับรูปแบบที่ใช้ได้กับกรณีการใช้งานต่างๆ รวมถึงโควต้าและราคา

ลองใช้ความสามารถอื่นๆ

ดูวิธีควบคุมการสร้างเนื้อหา

คุณยังทดสอบพรอมต์และการกําหนดค่ารูปแบบได้โดยใช้ Vertex AI Studio


แสดงความคิดเห็นเกี่ยวกับประสบการณ์การใช้งาน Vertex AI in Firebase