หากต้องการเข้าถึง Gemini API และกลุ่มโมเดล Gemini จากแอปโดยตรง เราขอแนะนําให้ใช้ Vertex AI ใน Firebase SDK สําหรับ Android SDK นี้เป็นส่วนหนึ่งของแพลตฟอร์ม Firebase ที่ใหญ่ขึ้น ซึ่งช่วยให้คุณสร้างและเรียกใช้แอปแบบ Full Stack ได้
ย้ายข้อมูลจาก SDK ของไคลเอ็นต์ Google AI
Vertex AI ใน Firebase SDK คล้ายกับ SDK ไคลเอ็นต์ Google AI แต่Vertex AI ใน Firebase SDK มีตัวเลือกด้านความปลอดภัยที่สำคัญและฟีเจอร์อื่นๆ สําหรับกรณีการใช้งานจริง เช่น เมื่อใช้ Vertex AI ใน Firebase คุณจะใช้สิ่งต่อไปนี้ได้ด้วย
- Firebase App Check เพื่อปกป้อง Gemini API จากการละเมิดโดยไคลเอ็นต์ที่ไม่ได้รับอนุญาต
- การกำหนดค่าระยะไกลของ Firebase เพื่อตั้งค่าและเปลี่ยนแปลงค่าของแอปในระบบคลาวด์แบบไดนามิก (เช่น ชื่อรุ่น) โดยไม่ต้องเปิดตัวแอปเวอร์ชันใหม่
- Cloud Storage for Firebase เพื่อรวมไฟล์สื่อขนาดใหญ่ในคำขอไปยัง Gemini API
หากผสานรวม SDK ไคลเอ็นต์ AI ของ Google ในแอปแล้ว คุณจะย้ายข้อมูลไปยัง Vertex AI ใน Firebase ได้
เริ่มต้นใช้งาน
ก่อนที่จะโต้ตอบกับ Gemini API จากแอปโดยตรง คุณจะต้องทําสิ่งต่างๆ ก่อน ซึ่งรวมถึงทำความคุ้นเคยกับการแจ้งเตือน รวมถึงตั้งค่า Firebase และแอปเพื่อใช้ SDK
ทดลองใช้พรอมต์
คุณสามารถทดลองใช้พรอมต์ใน Vertex AI Studio Vertex AI Studio เป็น IDE สําหรับการออกแบบพรอมต์และการสร้างต้นแบบ ซึ่งจะช่วยให้คุณอัปโหลดไฟล์เพื่อทดสอบพรอมต์ด้วยข้อความและรูปภาพ รวมถึงบันทึกพรอมต์เพื่อกลับมาดูในภายหลังได้
การสร้างพรอมต์ที่เหมาะสมสำหรับ Use Case ของคุณเป็นศิลปะมากกว่าวิทยาศาสตร์ ซึ่งทำให้การทดสอบมีความสําคัญ ดูข้อมูลเพิ่มเติมเกี่ยวกับการแจ้งได้ในเอกสารประกอบของ Firebase
ตั้งค่าโปรเจ็กต์ Firebase และเชื่อมต่อแอปกับ Firebase
เมื่อพร้อมเรียกใช้ Gemini API จากแอปแล้ว ให้ทําตามวิธีการในคู่มือเริ่มต้นใช้งาน Vertex AI ใน Firebase เพื่อตั้งค่า Firebase และ SDK ในแอป คู่มือเริ่มต้นใช้งานจะช่วยคุณทํางานต่อไปนี้ทั้งหมดในคู่มือนี้
- ตั้งค่าโปรเจ็กต์ Firebase ใหม่หรือที่มีอยู่ รวมถึงใช้แพ็กเกจราคาแบบจ่ายตามการใช้งานจริงของ Blaze และเปิดใช้ API ที่จําเป็น
- เชื่อมต่อแอปกับ Firebase ซึ่งรวมถึงการลงทะเบียนแอปและเพิ่มไฟล์การกําหนดค่า Firebase (
google-services.json
) ลงในแอป
เพิ่มการพึ่งพา Gradle
เพิ่ม Dependency ของ Gradle ต่อไปนี้ลงในโมดูลแอป
Kotlin
dependencies {
...
// Import the BoM for the Firebase platform
implementation(platform("com.google.firebase:firebase-bom:33.9.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")
}
Java
dependencies {
[...]
implementation("com.google.firebase:firebase-vertexai:16.1.0")
// Required to use `ListenableFuture` from Guava Android for one-shot generation
implementation("com.google.guava:guava:31.0.1-android")
// Required to use `Publisher` from Reactive Streams for streaming operations
implementation("org.reactivestreams:reactive-streams:1.0.4")
}
เริ่มต้นบริการ Vertex AI และโมเดล Generative
เริ่มต้นด้วยการสร้างอินสแตนซ์ GenerativeModel
และระบุชื่อโมเดล
Kotlin
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")
Java
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel("gemini-2.0-flash");
ดูข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบที่ใช้ได้สําหรับใช้กับ Vertex AI ใน Firebase ได้ในเอกสารประกอบของ Firebase นอกจากนี้ คุณยังดูข้อมูลเกี่ยวกับการกําหนดค่าพารามิเตอร์รูปแบบได้ด้วย
โต้ตอบกับ Gemini API จากแอป
เมื่อตั้งค่า Firebase และแอปให้ใช้ SDK แล้ว คุณก็พร้อมที่จะโต้ตอบกับ Gemini API จากแอปแล้ว
สร้างข้อความ
หากต้องการสร้างคำตอบที่เป็นข้อความ ให้เรียกใช้ generateContent()
พร้อมพรอมต์
Kotlin
// Note: `generateContent()` is a `suspend` function, which integrates well
// with existing Kotlin code.
scope.launch {
val response = model.generateContent("Write a story about the green robot")
}
Java
// In Java, create a `GenerativeModelFutures` from the `GenerativeModel`.
// Note that `generateContent()` returns a `ListenableFuture`. Learn more:
// https://developer.android.com/develop/background-work/background-tasks/asynchronous/listenablefuture
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
Content prompt = new Content.Builder()
.addText("Write a story about a green robot.")
.build();
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
สร้างข้อความจากรูปภาพและสื่ออื่นๆ
นอกจากนี้ คุณยังสร้างข้อความจากพรอมต์ที่มีข้อความและรูปภาพหรือสื่ออื่นๆ ได้ด้วย เมื่อเรียกใช้ generateContent()
คุณสามารถส่งสื่อเป็นข้อมูลในบรรทัด (ดังที่แสดงในตัวอย่างด้านล่าง) หรือจะรวมไฟล์สื่อขนาดใหญ่ในคำขอโดยใช้ Cloud Storage สำหรับ URL ของ Firebase ก็ได้
Kotlin
scope.launch {
val response = model.generateContent(
content {
image(bitmap)
text("what is the object in the picture?")
}
)
}
Java
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sparky);
Content prompt = new Content.Builder()
.addImage(bitmap)
.addText("What developer tool is this mascot from?")
.build();
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
แชทแบบหลายรอบ
นอกจากนี้ คุณยังรองรับการสนทนาแบบหลายรอบได้ด้วย เริ่มต้นใช้งานแชทด้วยฟังก์ชัน startChat()
คุณระบุประวัติข้อความได้ (ไม่บังคับ) จากนั้นเรียกใช้ฟังก์ชัน sendMessage()
เพื่อส่งข้อความแชท
Kotlin
val chat = generativeModel.startChat(
history = listOf(
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
content(role = "model") { text("Great to meet you. What would you like to know?") }
)
)
scope.launch {
val response = chat.sendMessage("How many paws are in my house?")
}
Java
// (Optional) create message history
Content.Builder userContentBuilder = new Content.Builder();
userContentBuilder.setRole("user");
userContentBuilder.addText("Hello, I have 2 dogs in my house.");
Content userContent = userContentBuilder.build();
Content.Builder modelContentBuilder = new Content.Builder();
modelContentBuilder.setRole("model");
modelContentBuilder.addText("Great to meet you. What would you like to know?");
Content modelContent = userContentBuilder.build();
List<Content> history = Arrays.asList(userContent, modelContent);
// Initialize the chat
ChatFutures chat = model.startChat(history);
// Create a new user message
Content.Builder messageBuilder = new Content.Builder();
messageBuilder.setRole("user");
messageBuilder.addText("How many paws are in my house?");
Content message = messageBuilder.build();
Publisher<GenerateContentResponse> streamingResponse =
chat.sendMessageStream(message);
StringBuilder outputContent = new StringBuilder();
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
@Override
public void onNext(GenerateContentResponse generateContentResponse) {
String chunk = generateContentResponse.getText();
outputContent.append(chunk);
}
@Override
public void onComplete() {
// ...
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
});
สตรีมคำตอบ
คุณสามารถโต้ตอบได้เร็วขึ้นโดยไม่ต้องรอผลลัพธ์ทั้งหมดจากการสร้างโมเดล และใช้การสตรีมเพื่อจัดการผลลัพธ์บางส่วนแทน ใช้ generateContentStream()
เพื่อสตรีมคำตอบ
Kotlin
scope.launch {
var outputContent = ""
generativeModel.generateContentStream(inputContent)
.collect { response ->
outputContent += response.text
}
}
Java
// Note that in Java the method `generateContentStream()` returns a
// Publisher from the Reactive Streams library.
// https://www.reactive-streams.org/
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
// Provide a prompt that contains text
Content prompt = new Content.Builder()
.addText("Write a story about a green robot.")
.build();
Publisher<GenerateContentResponse> streamingResponse =
model.generateContentStream(prompt);
StringBuilder outputContent = new StringBuilder();
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
@Override
public void onNext(GenerateContentResponse generateContentResponse) {
String chunk = generateContentResponse.getText();
outputContent.append(chunk);
}
@Override
public void onComplete() {
// ...
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
});
ขั้นตอนถัดไป
- ตรวจสอบแอปตัวอย่าง Vertex AI ใน Firebase บน GitHub
- เริ่มคิดถึงการเตรียมความพร้อมสําหรับเวอร์ชันที่ใช้งานจริง ซึ่งรวมถึงการตั้งค่า Firebase App Check เพื่อปกป้อง Gemini API จากการละเมิดโดยไคลเอ็นต์ที่ไม่ได้รับอนุญาต
- ดูข้อมูลเพิ่มเติมเกี่ยวกับ Vertex AI ใน Firebase ในเอกสารประกอบของ Firebase