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


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

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

คู่มือนี้จะถือว่าคุณคุ้นเคยกับการใช้ 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 in Firebase เพื่อเปิดเวิร์กโฟลว์ที่จะช่วยคุณ ทำงานต่อไปนี้ให้เสร็จ (โปรดทราบว่าหากคุณเห็นแท็บในคอนโซลของ Vertex AI เท่านี้ก็เรียบร้อย)

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

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


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

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

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

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

Kotlin+KTX

dependencies {
  // ... other androidx dependencies

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

Java

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

dependencies {
  // ... other androidx dependencies

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

  // 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 in Firebase