| ใช้ได้เฉพาะเมื่อใช้ Agent Platform Gemini API (formerly Vertex AI) เป็นผู้ให้บริการ API |
เมื่อเรียกใช้ Agent Platform Gemini API (formerly Vertex AI) จากแอปโดยใช้ SDK Firebase AI Logic คุณสามารถแจ้งให้โมเดล Gemini สร้างข้อความที่สร้างขึ้นตามอินพุตแบบสื่อผสม เช่น รูปภาพ, PDF, วิดีโอ และเสียง
สำหรับส่วนที่ไม่ใช่ข้อความของอินพุต (เช่น ไฟล์สื่อ) คุณสามารถเลือกใช้ Cloud Storage for Firebase เพื่อรวมไฟล์ไว้ในคำขอได้ ข้อมูลระดับสูงที่คุณจำเป็นต้องทราบเกี่ยวกับฟีเจอร์นี้มีดังนี้
คุณสามารถใช้ Cloud Storage for Firebase กับคำขอแบบหลายโมดัล (เช่น ทั้งการสร้างข้อความและแชท) หากใช้ Agent Platform Gemini API (formerly Vertex AI) ตัวอย่างในคู่มือนี้แสดงการป้อนข้อความและรูปภาพพื้นฐาน
คุณระบุประเภท MIME ของไฟล์และ URL Cloud Storage for Firebase (ซึ่งขึ้นต้นด้วย
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 ให้ใช้ URL ของ Cloud Storage for Firebase เพื่อรวมไฟล์ไว้ในคำขอแบบสื่อผสม อย่างไรก็ตาม หากไฟล์มีขนาดเล็ก คุณมักจะส่งไฟล์เป็นข้อมูลในบรรทัดได้โดยตรง (โปรดทราบว่าไฟล์ที่ระบุเป็นข้อมูลในบรรทัดจะได้รับการเข้ารหัสเป็น base64 ระหว่างการส่ง ซึ่งจะเพิ่มขนาดของคำขอ)
ประโยชน์เพิ่มเติมของการใช้ Cloud Storage for Firebase มีดังนี้
คุณสามารถให้ผู้ใช้ปลายทางอัปโหลดรูปภาพจากแอปของคุณไปยังบักเก็ต Cloud Storage for Firebase ได้โดยตรง จากนั้นคุณสามารถรวมรูปภาพเหล่านั้นไว้ในพรอมต์แบบหลายรูปแบบได้โดยระบุประเภท MIME ของไฟล์และ URL ของ Cloud Storage for Firebase (ซึ่งเป็นตัวระบุไฟล์)
คุณสามารถประหยัดเวลาและแบนด์วิดท์ของผู้ใช้ปลายทางได้หากผู้ใช้ต้องการส่งรูปภาพ โดยเฉพาะอย่างยิ่งหากผู้ใช้มีคุณภาพเครือข่ายที่ไม่ดีหรือไม่เสถียร
- หากการอัปโหลดหรือดาวน์โหลดไฟล์ถูกขัดจังหวะ SDK ของ Cloud Storage for Firebase จะเริ่มการทำงานใหม่โดยอัตโนมัติจากจุดที่ค้างไว้
- คุณสามารถใช้ไฟล์ที่อัปโหลดเดียวกันได้หลายครั้งโดยที่ผู้ใช้ปลายทางไม่ต้องอัปโหลดไฟล์เดียวกันทุกครั้งที่จำเป็นต้องใช้ในแอป (เช่น ในคำขอแบบหลายโมดัลใหม่)
คุณสามารถจำกัดสิทธิ์เข้าถึงของผู้ใช้ปลายทางสำหรับไฟล์ที่จัดเก็บไว้ใน Cloud Storage for Firebase ได้โดยใช้ Firebase Security Rules ซึ่งจะอนุญาตให้เฉพาะผู้ใช้ที่ได้รับอนุญาตเท่านั้นที่สามารถอัปโหลด ดาวน์โหลด หรือลบไฟล์ได้
คุณสามารถเข้าถึงไฟล์ในที่เก็บข้อมูลจาก Firebase หรือจาก Google Cloud ซึ่งช่วยให้คุณมีความยืดหยุ่นในการประมวลผลฝั่งเซิร์ฟเวอร์ เช่น การกรองรูปภาพหรือการแปลงวิดีโอโดยใช้ API ของ Google Cloud Storage
ระบบรองรับไฟล์และ URL ประเภทใดบ้าง
ข้อกำหนดสำหรับไฟล์และ URL เมื่อต้องการใช้ URL Cloud Storage for Firebase กับ 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
| ใช้ได้เฉพาะเมื่อใช้ Agent Platform Gemini API (formerly Vertex AI) เป็นผู้ให้บริการ API |
ขั้นตอนที่ 1: ตั้งค่า Cloud Storage for Firebase
ดูวิธีการโดยละเอียดสำหรับการตั้งค่า Cloud Storage for Firebase ได้ในคู่มือเริ่มต้นใช้งานต่อไปนี้ iOS+ | Android | เว็บ | Flutter | Unity
งานระดับสูงที่คุณจะต้องทำมีดังนี้
สร้างหรือนำเข้าบักเก็ต Cloud Storage for Firebase ในโปรเจ็กต์ Firebase
ใช้ Firebase Security Rules กับที่เก็บข้อมูลนี้ Security Rules ช่วยรักษาความปลอดภัยให้ไฟล์ของคุณโดยจำกัดการเข้าถึงไว้เฉพาะผู้ใช้ปลายทางที่ได้รับอนุญาต
เพิ่มไลบรารีของไคลเอ็นต์สำหรับ Cloud Storage for Firebase ลงในแอป
โปรดทราบว่าคุณสามารถข้ามขั้นตอนนี้ได้ แต่คุณต้องระบุประเภท MIME และค่า URL ในคำขออย่างชัดเจน เสมอ
ขั้นตอนที่ 2: อัปโหลดไฟล์ไปยังที่เก็บข้อมูล
ในเอกสารประกอบของ Cloud Storage คุณสามารถดูวิธีต่างๆ ทั้งหมดในการอัปโหลดไฟล์ไปยังที่เก็บข้อมูล ตัวอย่างเช่น คุณสามารถอัปโหลดไฟล์ในเครื่องจากอุปกรณ์ของผู้ใช้ปลายทาง เช่น รูปภาพและวิดีโอจากกล้อง ดูข้อมูลเพิ่มเติม iOS+ | Android | เว็บ | Flutter | Unity
เมื่อคุณอัปโหลดไฟล์ไปยังที่เก็บข้อมูล Cloud Storage จะใช้ข้อมูล 2 รายการต่อไปนี้กับไฟล์โดยอัตโนมัติ คุณจะต้องระบุค่าเหล่านี้ในคำขอ (ตามที่แสดงในขั้นตอนถัดไปของคู่มือนี้)
ประเภท MIME: ประเภทสื่อของไฟล์ (เช่น
image/png) เราจะพยายามตรวจหาประเภท MIME โดยอัตโนมัติระหว่างการอัปโหลดและใช้ข้อมูลเมตานั้นกับออบเจ็กต์ในที่เก็บข้อมูล อย่างไรก็ตาม คุณสามารถระบุประเภท MIME ในระหว่างการอัปโหลดได้ (ไม่บังคับ)URL ของ Cloud Storage for Firebase: ตัวระบุที่ไม่ซ้ำกันสำหรับไฟล์ URL ต้องขึ้นต้นด้วย
gs://
ขั้นตอนที่ 3: ระบุประเภท MIME และ URL ของไฟล์ในคำขอแบบสื่อผสม
เมื่อมีไฟล์ที่จัดเก็บไว้ในที่เก็บข้อมูลแล้ว คุณสามารถระบุประเภท MIME และ URL ของไฟล์ในคำขอได้ โปรดทราบว่าตัวอย่างเหล่านี้แสดงคำขอ generateContent ที่ไม่ใช่การสตรีม แต่คุณยังใช้ URL กับการสตรีมและแชทได้ด้วย
หากต้องการรวมไฟล์ในคำขอ คุณสามารถใช้ตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้
ตัวเลือกที่ 1: ระบุประเภท MIME และ URL โดยใช้ข้อมูลอ้างอิงของ Storage
ตัวเลือกที่ 2: ระบุประเภท MIME และ URL อย่างชัดเจน
ตัวเลือกที่ 1: ใส่ประเภท MIME และ URL โดยใช้ข้อมูลอ้างอิง Storage
| ก่อนลองใช้ตัวอย่างนี้ โปรดอ่านคู่มือเริ่มต้นใช้งาน Firebase AI Logic SDK ให้เสร็จสมบูรณ์ |
ใช้ตัวเลือกนี้หากคุณเพิ่งอัปโหลดไฟล์ไปยังที่เก็บข้อมูล และต้องการรวมไฟล์ (ผ่านการอ้างอิงพื้นที่เก็บข้อมูล) ไว้ในคำขอทันที การเรียกใช้ต้องมีทั้งประเภท 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 = model.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 = { type: "fileData", 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 Logic SDK ให้ครบถ้วน |
ใช้ตัวเลือกนี้หากคุณทราบค่าสำหรับประเภท 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 = model.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 = { type: "fileData", 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.");