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


คู่มือนี้จะแสดงวิธีเริ่มต้นโทรไปยัง Vertex AI Gemini API โดยตรงจากแอปของคุณโดยใช้ Vertex AI สำหรับ Firebase SDK

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

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

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

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

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

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

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

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

  1. ในคอนโซล Firebase ให้ไปที่ส่วน หน้า Build with Gemini แล้วคลิกการ์ดที่ 2 เพื่อเปิดเวิร์กโฟลว์ที่ช่วยให้คุณดำเนินการ งานต่อไปนี้ หากคุณเห็นแท็บในคอนโซลสำหรับ Vertex AI ให้ทำดังนี้ ขั้นตอนเหล่านี้เสร็จสมบูรณ์แล้ว

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

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


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

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

Vertex AI for Firebase SDK สำหรับ Android (firebase-vertexai) มี สิทธิ์เข้าถึง Vertex AI Gemini API

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

Kotlin+KTX

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Vertex AI for Firebase SDK for Android
  implementation("com.google.firebase:firebase-vertexai:16.0.0-beta03")
}

Java

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

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Vertex AI for Firebase SDK for Android
  implementation("com.google.firebase:firebase-vertexai:16.0.0-beta03")

  // 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")
}

ขั้นตอนที่ 3: เริ่มต้นบริการ Vertex AI และโมเดล Generative

ก่อนเรียก API คุณต้องเริ่มต้น Vertex AI และโมเดล Generative

Kotlin+KTX

สำหรับ Kotlin เมธอดใน SDK นี้คือฟังก์ชันระงับและต้องมีการเรียกใช้ จากขอบเขต Coroutine
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash")

Java

สำหรับ Java เมธอดสตรีมมิงใน SDK นี้จะแสดงค่า Publisher ประเภทจากคลังสตรีมแบบรีแอคทีฟ
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-1.5-flash");

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

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

ขั้นตอนที่ 4: เรียกใช้ Vertex AI Gemini API

ตอนนี้คุณได้เชื่อมต่อแอปกับ Firebase เพิ่ม SDK และเริ่มต้นแล้ว บริการ Vertex AI และโมเดล Generative คุณก็พร้อมที่จะเรียกใช้ Vertex AI Gemini API แล้ว

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

Kotlin+KTX

สำหรับ Kotlin เมธอดใน SDK นี้คือฟังก์ชันระงับและต้องมีการเรียกใช้ จากขอบเขต Coroutine
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-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 the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-1.5-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);

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

ดูข้อมูลเพิ่มเติมเกี่ยวกับโมเดล Gemini

ดูข้อมูลเกี่ยวกับ รุ่นที่เหมาะกับกรณีการใช้งานที่หลากหลาย และ โควต้าและการกำหนดราคา

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

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

คุณยังสามารถทดสอบข้อความแจ้งและการกำหนดค่าโมเดลโดยใช้ Vertex AI Studio


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