ใช้ได้เมื่อใช้ Vertex AI Gemini API เป็นผู้ให้บริการ API เท่านั้น |
เมื่อเรียกใช้ Vertex AI Gemini API จากแอปโดยใช้ SDK ของ Firebase AI Logic คุณสามารถแจ้งให้โมเดล Gemini สร้างข้อความ ตามอินพุตมัลติโมดัล เช่น รูปภาพ, PDF, วิดีโอ และเสียง
สำหรับส่วนที่ไม่ใช่ข้อความของอินพุต (เช่น ไฟล์สื่อ) คุณสามารถใช้ Cloud Storage for Firebase เพื่อรวมไฟล์ในคำขอได้ (ไม่บังคับ) โดยสรุป สิ่งที่คุณต้องทราบเกี่ยวกับฟีเจอร์นี้มีดังนี้
คุณใช้ Cloud Storage for Firebase กับคำขอแบบมัลติโมดอลได้ (เช่น การสร้างข้อความและการแชท) หากใช้ Vertex AI Gemini API ตัวอย่างในคำแนะนำนี้แสดงอินพุตข้อความและรูปภาพพื้นฐาน
คุณระบุประเภท MIME ของไฟล์และ Cloud Storage for Firebase URL (ซึ่งขึ้นต้นด้วย
gs://
เสมอ) ในอินพุตคำขอ ค่าเหล่านี้คือข้อมูลเมตาที่กำหนดให้กับไฟล์ที่อัปโหลดไปยังที่เก็บข้อมูล Cloud Storage โดยอัตโนมัติคุณต้องใช้ประเภทไฟล์และ URL ที่รองรับ
คู่มือโซลูชันนี้อธิบายวิธีตั้งค่า Cloud Storage for Firebase อัปโหลดไฟล์ไปยังที่เก็บข้อมูล Cloud Storage for Firebase จากแอป แล้วระบุประเภท MIME และ URL ของ Cloud Storage for Firebase ในคำขอแบบมัลติโมดอลไปยัง Gemini API
คุณต้องการดูตัวอย่างโค้ดไหม หรือคุณตั้งค่า Cloud Storage for Firebase แล้วและพร้อมเริ่มใช้กับ คำขอแบบมัลติโมดัล
เหตุใดจึงควรใช้ Cloud Storage for Firebase กับแอป
Cloud Storage for Firebase ใช้โครงสร้างพื้นฐานที่รวดเร็ว ปลอดภัย และปรับขนาดได้เหมือนกับ Google Cloud Storage ในการจัดเก็บ Blob และไฟล์ และ SDK ของไคลเอ็นต์ได้รับการออกแบบมาโดยเฉพาะสำหรับแอปบนอุปกรณ์เคลื่อนที่และเว็บ
สำหรับ SDK Firebase AI Logic ขนาดคำขอสูงสุดคือ 20 MB คุณจะได้รับข้อผิดพลาด HTTP 413 หากคำขอมีขนาดใหญ่เกินไป หากขนาดไฟล์จะทำให้ขนาดคำขอทั้งหมดเกิน 20 MB ให้ใช้Cloud Storage for Firebase URL เพื่อรวมไฟล์ไว้ในคำขอแบบมัลติโมดัล อย่างไรก็ตาม หากไฟล์มีขนาดเล็ก คุณมักจะส่งไฟล์เป็นข้อมูลแบบอินไลน์ได้โดยตรง (โปรดทราบว่าไฟล์ที่ระบุเป็นข้อมูลแบบอินไลน์จะได้รับการเข้ารหัสเป็น base64 ใน ระหว่างการรับส่ง ซึ่งจะเพิ่มขนาดของคำขอ)
ประโยชน์เพิ่มเติมบางส่วนของการใช้ Cloud Storage for Firebase มีดังนี้
คุณสามารถให้ผู้ใช้ปลายทางอัปโหลดรูปภาพจากแอปของคุณไปยัง Cloud Storage for Firebaseที่เก็บข้อมูลโดยตรง จากนั้นคุณก็สามารถรวมรูปภาพเหล่านั้นไว้ใน พรอมต์มัลติโมดัลได้เพียงแค่ระบุประเภท MIME ของไฟล์และ Cloud Storage for FirebaseURL (ซึ่งเป็นตัวระบุสำหรับไฟล์)
คุณสามารถประหยัดเวลาและแบนด์วิดท์ของผู้ใช้ปลายทางได้หากผู้ใช้จำเป็นต้องระบุรูปภาพ โดยเฉพาะในกรณีที่ผู้ใช้มีคุณภาพเครือข่ายไม่ดีหรือไม่เสถียร
- หากการอัปโหลดหรือดาวน์โหลดไฟล์ถูกขัดจังหวะ Cloud Storage for Firebase SDK จะเริ่มการดำเนินการต่อโดยอัตโนมัติจากจุดที่หยุดไว้
- คุณสามารถใช้ไฟล์ที่อัปโหลดเดียวกันได้หลายครั้งโดยไม่ต้องให้ผู้ใช้ปลายทาง อัปโหลดไฟล์เดียวกันทุกครั้งที่จำเป็นในแอป (เช่น ในคำขอแบบมัลติโมดัลใหม่)
คุณสามารถจำกัดการเข้าถึงไฟล์ที่จัดเก็บไว้ใน Cloud Storage for Firebase ของผู้ใช้ปลายทางได้โดยใช้ Firebase Security Rules ซึ่งจะอนุญาตให้เฉพาะผู้ใช้ที่ได้รับอนุญาตเท่านั้นที่อัปโหลด ดาวน์โหลด หรือลบไฟล์ได้
คุณเข้าถึงไฟล์ในที่เก็บข้อมูลจาก Firebase หรือจาก Google Cloud ได้ ซึ่งจะช่วยให้คุณมีความยืดหยุ่นในการประมวลผลฝั่งเซิร์ฟเวอร์ เช่น การกรองรูปภาพหรือการแปลงรหัสวิดีโอโดยใช้ Google Cloud Storage API
ระบบรองรับไฟล์และ URL ประเภทใดบ้าง
ต่อไปนี้คือข้อกำหนดสำหรับไฟล์และ URL เมื่อคุณต้องการใช้ Cloud Storage for Firebase URL กับ SDK Firebase AI Logic
ไฟล์ต้องเป็นไปตามข้อกำหนดของไฟล์อินพุตสำหรับคำขอแบบมัลติโมดัล ซึ่งรวมถึงข้อกำหนดต่างๆ เช่น ประเภท MIME และขนาดไฟล์
ไฟล์ต้องจัดเก็บไว้ในที่เก็บข้อมูล Cloud Storage for Firebase (ซึ่งหมายความว่าบริการ Firebase เช่น Firebase Security Rules จะเข้าถึงที่เก็บข้อมูลได้) หากคุณดูที่เก็บข้อมูลในFirebase คอนโซลได้ แสดงว่าเป็นที่เก็บข้อมูล Cloud Storage for Firebase
Cloud Storage for Firebaseที่เก็บข้อมูลต้องอยู่ในโปรเจ็กต์ Firebase เดียวกันกับที่ คุณลงทะเบียนแอป
URL ของไฟล์ Cloud Storage for Firebase ต้องขึ้นต้นด้วย
gs://
ซึ่งเป็นวิธีสร้าง URL ของ Google Cloud Storage ทั้งหมดURL ของไฟล์ต้องไม่ใช่ URL ของ "เบราว์เซอร์" (เช่น URL ของรูปภาพ ที่คุณพบในอินเทอร์เน็ต)
นอกจากนี้ Firebase Security Rules สำหรับที่เก็บข้อมูลต้องอนุญาต การเข้าถึงไฟล์ที่เหมาะสม เช่น
หากคุณมีกฎแบบสาธารณะ ผู้ใช้หรือไคลเอ็นต์ใดก็ได้จะเข้าถึงไฟล์ได้
หากคุณมีกฎที่รัดกุม (ขอแนะนำอย่างยิ่ง) Firebase จะตรวจสอบว่าผู้ใช้ที่ลงชื่อเข้าใช้หรือ ไคลเอ็นต์มีสิทธิ์เข้าถึงไฟล์เพียงพอก่อนที่จะอนุญาตให้เรียกใช้ ผ่าน URL ที่ระบุ
ใช้ URL ของ Cloud Storage for Firebase กับ Firebase AI Logic
ใช้ได้เมื่อใช้ Vertex AI Gemini API เป็นผู้ให้บริการ API เท่านั้น |
ขั้นตอนที่ 1: ตั้งค่า Cloud Storage for Firebase
คุณดูวิธีการตั้งค่า Cloud Storage for Firebase แบบละเอียดได้ใน คู่มือเริ่มต้นใช้งาน iOS+ | Android | เว็บ | Flutter | Unity
งานระดับสูงที่คุณจะต้องทำมีดังนี้
สร้างหรือนำเข้าที่เก็บข้อมูล Cloud Storage for Firebase ในโปรเจ็กต์ Firebase
ใช้ Firebase Security Rules กับที่เก็บข้อมูลนี้ Rules ช่วยรักษาความปลอดภัยไฟล์โดยจำกัดการเข้าถึงไว้สำหรับผู้ใช้ที่ได้รับอนุญาตเท่านั้น
เพิ่มไลบรารีของไคลเอ็นต์สำหรับ Cloud Storage for Firebase ลงในแอป
โปรดทราบว่าคุณจะข้ามงานนี้ก็ได้ แต่ในกรณีนี้คุณต้องระบุประเภท MIME และค่า URL ในคำขออย่างชัดเจนเสมอ
ขั้นตอนที่ 2: อัปโหลดไฟล์ไปยังที่เก็บข้อมูล
ในเอกสารประกอบของ Cloud Storage คุณจะดูวิธีต่างๆ ในการอัปโหลดไฟล์ไปยังที่เก็บข้อมูลได้ เช่น คุณสามารถอัปโหลดไฟล์ในเครื่องจากอุปกรณ์ของผู้ใช้ปลายทาง เช่น รูปภาพและวิดีโอจากกล้อง ดูข้อมูลเพิ่มเติม iOS+ | Android | เว็บ | Flutter | Unity
เมื่ออัปโหลดไฟล์ไปยังที่เก็บข้อมูล Cloud Storage จะใช้ข้อมูล 2 รายการต่อไปนี้กับไฟล์โดยอัตโนมัติ คุณจะต้องรวมค่าต่อไปนี้ในคำขอ (ดังที่แสดงในขั้นตอนถัดไปของคู่มือนี้)
ประเภท MIME: นี่คือประเภทสื่อของไฟล์ (เช่น
image/png
) เราจะพยายามตรวจหาประเภท MIME โดยอัตโนมัติในระหว่างการอัปโหลดและใช้ข้อมูลเมตาดังกล่าวกับออบเจ็กต์ในที่เก็บข้อมูล อย่างไรก็ตาม คุณเลือกระบุ ประเภท MIME ในระหว่างการอัปโหลดได้Cloud Storage for Firebase URL: ตัวระบุที่ไม่ซ้ำกันสำหรับไฟล์ URL ต้องขึ้นต้นด้วย
gs://
ขั้นตอนที่ 3: ระบุประเภท MIME และ URL ของไฟล์ในคำขอแบบมัลติโมดัล
เมื่อจัดเก็บไฟล์ไว้ในที่เก็บข้อมูลแล้ว คุณจะระบุประเภท MIME และ URL
ในคำขอได้ โปรดทราบว่าตัวอย่างเหล่านี้แสดงคำขอ generateContent
ที่ไม่ใช่การสตรีม แต่คุณยังใช้ URL กับการสตรีมและการแชทได้ด้วย
หากต้องการรวมไฟล์ในคำขอ คุณสามารถใช้ตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้
ตัวเลือกที่ 1: ระบุประเภท MIME และ URL โดยใช้การอ้างอิงพื้นที่เก็บข้อมูล
ตัวเลือกที่ 2: ระบุประเภท MIME และ URL อย่างชัดเจน
ตัวเลือกที่ 1: ระบุประเภท MIME และ URL โดยใช้การอ้างอิงพื้นที่เก็บข้อมูล
ก่อนที่จะลองใช้ตัวอย่างนี้ โปรดตรวจสอบว่าคุณได้ทำตาม คู่มือการเริ่มต้นใช้งานสำหรับ Firebase AI LogicSDK เสร็จแล้ว |
ใช้ตัวเลือกนี้หากเพิ่งอัปโหลดไฟล์ไปยังที่เก็บข้อมูล และต้องการรวมไฟล์ (ผ่านการอ้างอิง Storage) ในคำขอทันที การเรียกใช้ ต้องใช้ทั้งประเภท MIME และ URL ของ Cloud Storage for Firebase
Swift
// Upload an image file using Cloud Storage for Firebase.
let storageRef = Storage.storage().reference(withPath: "images/image.jpg")
guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else {
fatalError("File 'image.jpg' not found in main bundle.")
}
let metadata = try await storageRef.putFileAsync(from: imageURL)
// Get the MIME type and Cloud Storage for Firebase URL.
guard let mimeType = metadata.contentType else {
fatalError("The MIME type of the uploaded image is nil.")
}
// Construct a URL in the required format.
let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)"
let prompt = "What's in this picture?"
// Construct the imagePart with the MIME type and the URL.
let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType)
// To generate text output, call generateContent with the prompt and the imagePart.
let result = try await model.generateContent(prompt, imagePart)
if let text = result.text {
print(text)
}
Kotlin
สำหรับ Kotlin เมธอดใน SDK นี้เป็นฟังก์ชันระงับและต้องเรียกใช้ จากขอบเขตของ Coroutine// Upload an image file using Cloud Storage for Firebase.
val storageRef = Firebase.storage.reference.child("images/image.jpg")
val fileUri = Uri.fromFile(File("image.jpg"))
try {
val taskSnapshot = storageRef.putFile(fileUri).await()
// Get the MIME type and Cloud Storage for Firebase file path.
val mimeType = taskSnapshot.metadata?.contentType
val bucket = taskSnapshot.metadata?.bucket
val filePath = taskSnapshot.metadata?.path
if (mimeType != null && bucket != null) {
// Construct a URL in the required format.
val storageUrl = "gs://$bucket/$filePath"
// Construct a prompt that includes text, the MIME type, and the URL.
val prompt = content {
fileData(mimeType = mimeType, uri = storageUrl)
text("What's in this picture?")
}
// To generate text output, call generateContent with the prompt.
val response = generativeModel.generateContent(prompt)
println(response.text)
}
} catch (e: StorageException) {
// An error occurred while uploading the file.
} catch (e: GoogleGenerativeAIException) {
// An error occurred while generating text.
}
Java
สำหรับ Java เมธอดใน SDK นี้จะแสดงผลเป็นListenableFuture
// Upload an image file using Cloud Storage for Firebase.
StorageReference storage = FirebaseStorage.getInstance().getReference("images/image.jpg");
Uri fileUri = Uri.fromFile(new File("images/image.jpg"));
storage.putFile(fileUri).addOnSuccessListener(taskSnapshot -> {
// Get the MIME type and Cloud Storage for Firebase file path.
String mimeType = taskSnapshot.getMetadata().getContentType();
String bucket = taskSnapshot.getMetadata().getBucket();
String filePath = taskSnapshot.getMetadata().getPath();
if (mimeType != null && bucket != null) {
// Construct a URL in the required format.
String storageUrl = "gs://" + bucket + "/" + filePath;
// Create a prompt that includes text, the MIME type, and the URL.
Content prompt = new Content.Builder()
.addFileData(storageUrl, mimeType)
.addText("What's in this picture?")
.build();
// To generate text output, call generateContent with the prompt.
GenerativeModelFutures modelFutures = GenerativeModelFutures.from(model);
ListenableFuture<GenerateContentResponse> response = modelFutures.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(@NonNull Throwable t) {
t.printStackTrace();
}
}, executor);
}
}).addOnFailureListener(e -> {
// An error occurred while uploading the file.
e.printStackTrace();
});
Web
// Upload an image file using Cloud Storage for Firebase.
const storageRef = ref(storage, "image.jpg");
const uploadResult = await uploadBytes(storageRef, file);
// Get the MIME type and Cloud Storage for Firebase URL.
// toString() is the simplest way to construct the Cloud Storage for Firebase URL
// in the required format.
const mimeType = uploadResult.metadata.contentType;
const storageUrl = uploadResult.ref.toString();
// Construct the imagePart with the MIME type and the URL.
const imagePart = { fileData: { mimeType, fileUri: storageUrl }};
// To generate text output, call generateContent with the prompt and imagePart.
const result = await model.generateContent([prompt, imagePart]);
console.log(result.response.text());
Dart
// Upload an image file using Cloud Storage for Firebase.
final storageRef = FirebaseStorage.instance.ref();
final imageRef = storageRef.child("images/image.jpg");
await imageRef.putData(data);
// Get the MIME type and Cloud Storage for Firebase file path.
final metadata = await imageRef.getMetadata();
final mimeType = metadata.contentType;
final bucket = imageRef.bucket;
final fullPath = imageRef.fullPath;
final prompt = TextPart("What's in the picture?");
// Construct a URL in the required format.
final storageUrl = 'gs://$bucket/$fullPath';
// Construct the filePart with the MIME type and the URL.
final filePart = FileData(mimeType, storageUrl);
// To generate text output, call generateContent with the text and the filePart.
final response = await model.generateContent([
Content.multi([prompt, filePart])
]);
print(response.text);
Unity
var storageRef = FirebaseStorage.DefaultInstance.GetReference("images/image.jpg");
var metadata = await storageRef.PutFileAsync(filePathToJpg);
// Get the MIME type and Cloud Storage for Firebase URL.
var mimeType = metadata.ContentType;
// Construct a URL in the required format.
var storageURL = new Uri($"gs://{storageRef.Bucket}/{storageRef.Path}");
var prompt = ModelContent.Text("What's in this picture?");
// Construct a FileData that explicitly includes the MIME type and
// Cloud Storage for Firebase URL values.
var fileData = ModelContent.FileData(mimeType, storageURL);
// To generate text output, call GenerateContentAsync with the prompt and fileData.
var response = await model.GenerateContentAsync(new [] { prompt, fileData });
UnityEngine.Debug.Log(response.Text ?? "No text in response.");
ตัวเลือกที่ 2: ระบุประเภท MIME และ URL อย่างชัดเจน
ก่อนที่จะลองใช้ตัวอย่างนี้ โปรดตรวจสอบว่าคุณได้ทำตาม คู่มือการเริ่มต้นใช้งานสำหรับ Firebase AI LogicSDK เสร็จแล้ว |
ใช้ตัวเลือกนี้หากคุณทราบค่าสำหรับประเภท MIME และ Cloud Storage for Firebase URL และต้องการรวมค่าดังกล่าวอย่างชัดเจนใน คำขอแบบมัลติโมดัล การเรียกใช้ต้องมีทั้งประเภท MIME และ URL
Swift
let prompt = "What's in this picture?"
// Construct an imagePart that explicitly includes the MIME type and
// Cloud Storage for Firebase URL values.
let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg")
// To generate text output, call generateContent with the prompt and imagePart.
let result = try await model.generateContent(prompt, imagePart)
if let text = result.text {
print(text)
}
Kotlin
สำหรับ Kotlin เมธอดใน SDK นี้เป็นฟังก์ชันระงับและต้องเรียกใช้ จากขอบเขตของ Coroutine// Construct a prompt that explicitly includes the MIME type and Cloud Storage for Firebase URL values.
val prompt = content {
fileData(mimeType = "image/jpeg", uri = "gs://bucket-name/path/image.jpg")
text("What's in this picture?")
}
// To generate text output, call generateContent with the prompt.
val response = generativeModel.generateContent(prompt)
println(response.text)
Java
สำหรับ Java เมธอดใน SDK นี้จะแสดงผลเป็นListenableFuture
// Construct a prompt that explicitly includes the MIME type and Cloud Storage for Firebase URL values.
Content prompt = new Content.Builder()
.addFilePart("gs://bucket-name/path/image.jpg", "image/jpeg")
.addText("What's in this picture?")
.build();
// To generate text output, call generateContent with the prompt
GenerativeModelFutures modelFutures = GenerativeModelFutures.from(model);
ListenableFuture<GenerateContentResponse> response = modelFutures.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(@NonNull Throwable t) {
t.printStackTrace();
}
}, executor);
Web
const prompt = "What's in this picture?";
// Construct an imagePart that explicitly includes the MIME type and Cloud Storage for Firebase URL values.
const imagePart = { fileData: { mimeType: "image/jpeg", fileUri: "gs://bucket-name/path/image.jpg" }};
// To generate text output, call generateContent with the prompt and imagePart.
const result = await model.generateContent([prompt, imagePart]);
console.log(result.response.text());
Dart
final prompt = TextPart("What's in the picture?");
// Construct a filePart that explicitly includes the MIME type and Cloud Storage for Firebase URL values.
final filePart = FileData('image/jpeg', 'gs://bucket-name/path/image.jpg'),
// To generate text output, call generateContent with the prompt and filePart.
final response = await model.generateContent([
Content.multi([prompt, filePart])
]);
print(response.text);
Unity
var prompt = ModelContent.Text("What's in this picture?");
// Construct a FileData that explicitly includes the MIME type and
// Cloud Storage for Firebase URL values.
var fileData = ModelContent.FileData(
mimeType: "image/jpeg",
uri: new Uri("gs://bucket-name/path/image.jpg")
);
// To generate text output, call GenerateContentAsync with the prompt and fileData.
var response = await model.GenerateContentAsync(new [] { prompt, fileData });
UnityEngine.Debug.Log(response.Text ?? "No text in response.");