ใช้ได้เฉพาะเมื่อใช้ Vertex AI Gemini API เป็นผู้ให้บริการ API |
เมื่อเรียก Vertex AI Gemini API จากแอปโดยใช้ Firebase AI Logic SDK คุณสามารถแจ้งให้โมเดล 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 ของไคลเอ็นต์สร้างขึ้นเพื่อแอปบนอุปกรณ์เคลื่อนที่และเว็บโดยเฉพาะ
สำหรับ Firebase AI Logic SDK ขนาดคำขอสูงสุดคือ 20 MB คุณจะได้รับข้อผิดพลาด HTTP 413 หากคำขอมีขนาดใหญ่เกินไป หากขนาดของไฟล์จะทำให้คำขอทั้งหมดมีขนาดใหญ่เกิน 20 MB ให้ใช้ Cloud Storage for FirebaseURL เพื่อรวมไฟล์ไว้ในคำขอแบบหลายรูปแบบ อย่างไรก็ตาม หากไฟล์มีขนาดเล็ก คุณมักจะส่งไฟล์เป็นข้อมูลในบรรทัดได้โดยตรง (โปรดทราบว่าไฟล์ที่ส่งเป็นข้อมูลในบรรทัดจะได้รับการเข้ารหัสเป็น Base64 ระหว่างการส่ง ซึ่งจะเพิ่มขนาดคำขอ)
ประโยชน์เพิ่มเติมของการใช้ Cloud Storage for Firebase มีดังนี้
คุณสามารถให้ผู้ใช้ปลายทางอัปโหลดรูปภาพจากแอปของคุณไปยังที่เก็บข้อมูล Cloud Storage for Firebase ได้โดยตรง จากนั้นคุณก็สามารถใส่รูปภาพเหล่านั้นในพรอมต์แบบมัลติโมเดลได้โดยระบุประเภท MIME ของไฟล์และ URL Cloud Storage for Firebase (ซึ่งเป็นตัวระบุไฟล์)
คุณช่วยประหยัดเวลาและแบนด์วิดท์ของผู้ใช้ปลายทางได้หากผู้ใช้ต้องส่งรูปภาพ โดยเฉพาะอย่างยิ่งในกรณีที่ผู้ใช้มีเครือข่ายคุณภาพต่ำหรือไม่เสถียร
- หากการอัปโหลดหรือดาวน์โหลดไฟล์ถูกขัดจังหวะ 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 เรียบร้อยแล้ว |
ใช้ตัวเลือกนี้หากคุณเพิ่งอัปโหลดไฟล์ไปยังที่เก็บข้อมูล และต้องการรวมไฟล์นั้น (ผ่านข้อมูลอ้างอิงพื้นที่เก็บข้อมูล) ไว้ในคำขอทันที การเรียกใช้ต้องใช้ทั้งประเภท 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 และ URL Cloud Storage for Firebase และต้องการนำค่าเหล่านั้นไปใส่ในคำขอแบบมัลติโมดัลอย่างชัดเจน การเรียกใช้ต้องใช้ทั้งประเภท 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.");