คู่มือนี้จะแสดงวิธีเริ่มต้นเรียกใช้ Vertex AI Gemini API จากแอปของคุณโดยตรงโดยใช้ SDK ของ Vertex AI สำหรับ Firebase
สิ่งที่ต้องดำเนินการก่อน
คู่มือนี้จะถือว่าคุณคุ้นเคยกับการใช้ Android Studio เพื่อพัฒนาแอปสำหรับ Android
ตรวจสอบว่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์และแอป Android เป็นไปตามข้อกำหนดต่อไปนี้
- Android Studio (เวอร์ชันล่าสุด)
- แอป Android ต้องกำหนดเป้าหมายเป็น API ระดับ 21 ขึ้นไป
(ไม่บังคับ) ดูแอปตัวอย่าง
คุณสามารถลองใช้ SDK ได้อย่างรวดเร็ว ดูการใช้งานกรณีการใช้งานที่หลากหลาย หรือใช้แอปตัวอย่างหากไม่มีแอป Android ของคุณเอง หากต้องการใช้แอปตัวอย่าง คุณจะต้อง เชื่อมต่อกับโปรเจ็กต์ Firebase
ขั้นตอนที่ 1: สร้างโปรเจ็กต์ Firebase และเชื่อมต่อแอปกับ Firebase
หากคุณมีโปรเจ็กต์ Firebase และแอปที่เชื่อมต่อกับ Firebase อยู่แล้ว
ในคอนโซล Firebase ให้ไปที่หน้าสร้างด้วย Gemini แล้วคลิกการ์ดใบที่ 2 เพื่อเปิดเวิร์กโฟลว์ที่ช่วยให้คุณทำงานต่อไปนี้ได้ หากคุณเห็นแท็บในคอนโซลสำหรับ Vertex AI แสดงว่างานเหล่านี้เสร็จสมบูรณ์แล้ว
อัปเกรดโปรเจ็กต์เพื่อใช้แพ็กเกจราคาแบบจ่ายเมื่อใช้ Blaze
เปิดใช้ API ทั้ง 2 รายการต่อไปนี้สำหรับโปรเจ็กต์ของคุณ
aiplatform.googleapis.com
และfirebaseml.googleapis.com
ทำขั้นตอนถัดไปในคู่มือนี้เพื่อเพิ่ม SDK ลงในแอป
หากคุณยังไม่มีโปรเจ็กต์ Firebase และแอปที่เชื่อมต่อกับ Firebase
ขั้นตอนที่ 2: เพิ่ม SDK
เมื่อตั้งค่าโปรเจ็กต์ Firebase และเชื่อมต่อแอปกับ Firebase แล้ว (ดูขั้นตอนก่อนหน้า) ตอนนี้คุณเพิ่ม Vertex AI สำหรับ Firebase SDK ลงในแอปได้แล้ว
Vertex AI สำหรับ Firebase SDK สำหรับ Android (firebase-vertexai
) ให้สิทธิ์เข้าถึง Vertex AI Gemini API
ในไฟล์การกำหนดค่า Gradle ของโมดูล (ระดับแอป)
(เช่น <project>/<app-module>/build.gradle.kts
) ให้เพิ่ม Dependency สำหรับ SDK ของ Vertex AI สำหรับ Firebase สำหรับ 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-beta01")
}
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-beta01")
// 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-preview-0514")
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-preview-0514");
// 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-preview-0514")
// 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-preview-0514");
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
- ดูข้อมูลเพิ่มเติมเกี่ยวกับการสร้างข้อความจากพรอมต์แบบข้อความเท่านั้น รวมถึงวิธีสตรีมคำตอบ
- สร้างข้อความจากพรอมต์หลายรูปแบบ (รวมถึงข้อความ, รูปภาพ, PDF, วิดีโอ และเสียง)
- สร้างการสนทนาแบบผลัดกันเล่น (แชท)
- ใช้การเรียกใช้ฟังก์ชันเพื่อเชื่อมต่อโมเดล Generative กับระบบและข้อมูลภายนอก
เรียนรู้วิธีควบคุมการสร้างเนื้อหา
- ทำความเข้าใจการออกแบบพรอมต์ รวมถึงแนวทางปฏิบัติแนะนำ กลยุทธ์ และตัวอย่างพรอมต์
- กำหนดค่าพารามิเตอร์โมเดล เช่น โทเค็นอุณหภูมิและเอาต์พุตสูงสุด
- ใช้การตั้งค่าความปลอดภัยเพื่อปรับ โอกาสในการได้รับคำตอบที่อาจถือว่าเป็นอันตราย
แสดงความคิดเห็นเกี่ยวกับประสบการณ์การใช้งาน Vertex AI สำหรับ Firebase