मल्टीमॉडल अनुरोधों में बड़ी फ़ाइलें शामिल करें और 'Firebase के लिए Cloud Storage' का इस्तेमाल करके फ़ाइलें मैनेज करें

यह सुविधा सिर्फ़ तब उपलब्ध होती है, जब Vertex AI Gemini API को एपीआई उपलब्ध कराने वाली सेवा के तौर पर इस्तेमाल किया जा रहा हो.

Firebase AI Logic एसडीके का इस्तेमाल करके, अपने ऐप्लिकेशन से Vertex AI Gemini API को कॉल करते समय, Gemini मॉडल को मल्टीमॉडल इनपुट के आधार पर टेक्स्ट जनरेट करने के लिए प्रॉम्प्ट किया जा सकता है. जैसे, इमेज, PDF, वीडियो, और ऑडियो.

इनपुट के नॉन-टेक्स्ट वाले हिस्सों (जैसे कि मीडिया फ़ाइलें) के लिए, अनुरोध में फ़ाइलें शामिल करने के लिए Cloud Storage for Firebase का इस्तेमाल किया जा सकता है. हालांकि, ऐसा करना ज़रूरी नहीं है. इस सुविधा के बारे में आपको ये बातें जानना ज़रूरी है:

  • Cloud Storage for Firebase का इस्तेमाल करने पर, मल्टीमॉडल अनुरोध (जैसे, टेक्स्ट जनरेट करना और चैट करना) के साथ Cloud Storage for Firebase का इस्तेमाल किया जा सकता है.Vertex AI Gemini API इस गाइड में दिए गए उदाहरणों में, टेक्स्ट और इमेज के बुनियादी इनपुट दिखाए गए हैं.

  • अनुरोध के इनपुट में, फ़ाइल का MIME टाइप और उसका Cloud Storage for Firebase यूआरएल दिया जाता है. यह हमेशा gs:// से शुरू होता है. ये वैल्यू, Cloud Storageबकेट में अपलोड की गई किसी भी फ़ाइल को अपने-आप असाइन किया गया मेटाडेटा होती हैं.

  • आपको फ़ाइल टाइप और यूआरएल का इस्तेमाल करना होगा.


इस समाधान गाइड में, Cloud Storage for Firebase को सेट अप करने, अपने ऐप्लिकेशन से Cloud Storage for Firebase बकेट में कोई फ़ाइल अपलोड करने, और फिर Gemini API को किए गए मल्टीमॉडल अनुरोध में फ़ाइल का MIME टाइप और Cloud Storage for Firebase यूआरएल शामिल करने का तरीका बताया गया है.

क्या आपको कोड के उदाहरण देखने हैं? या क्या आपने पहले से ही Cloud Storage for Firebase सेट अप कर लिया है और अब आपको इसका इस्तेमाल मल्टीमॉडल अनुरोधों के साथ करना है?

कोड के उदाहरणों पर जाएं

अपने ऐप्लिकेशन के साथ Cloud Storage for Firebase का इस्तेमाल क्यों करें?

Cloud Storage for Firebase, Google Cloud Storage की तरह ही तेज़, सुरक्षित, और बड़े पैमाने पर इस्तेमाल किए जा सकने वाले इंफ़्रास्ट्रक्चर का इस्तेमाल करता है. इसका इस्तेमाल, ब्लोब और फ़ाइलों को सेव करने के लिए किया जाता है. साथ ही, इसके क्लाइंट एसडीके खास तौर पर मोबाइल और वेब ऐप्लिकेशन के लिए बनाए गए हैं.

Firebase AI Logic एसडीके के लिए, अनुरोध का ज़्यादा से ज़्यादा साइज़ 20 एमबी होता है. अगर कोई अनुरोध बहुत बड़ा है, तो आपको एचटीटीपी 413 गड़बड़ी का मैसेज मिलता है. अगर किसी फ़ाइल का साइज़ इतना बड़ा है कि अनुरोध का कुल साइज़ 20 एमबी से ज़्यादा हो जाएगा, तो मल्टीमॉडल अनुरोध में फ़ाइल शामिल करने के लिए, Cloud Storage for Firebase यूआरएल का इस्तेमाल करें. हालांकि, अगर कोई फ़ाइल छोटी है, तो उसे सीधे तौर पर इनलाइन डेटा के तौर पर पास किया जा सकता है. ध्यान दें कि इनलाइन डेटा के तौर पर दी गई फ़ाइल को ट्रांज़िट में base64 में कोड किया जाता है. इससे अनुरोध का साइज़ बढ़ जाता है.

Cloud Storage for Firebase का इस्तेमाल करने के कुछ और फ़ायदे यहाँ दिए गए हैं:

  • असली उपयोगकर्ताओं के पास, सीधे आपके ऐप्लिकेशन से Cloud Storage for Firebase बकेट में इमेज अपलोड करने का विकल्प होता है. इसके बाद, उन इमेज को अपने मल्टीमॉडल प्रॉम्प्ट में शामिल किया जा सकता है. इसके लिए, आपको सिर्फ़ फ़ाइल का MIME टाइप और Cloud Storage for Firebase यूआरएल (जो फ़ाइल के लिए आइडेंटिफ़ायर होता है) बताना होगा.

  • अगर असली उपयोगकर्ताओं को इमेज उपलब्ध करानी हैं, तो उनके समय और बैंडविड्थ को बचाया जा सकता है. ऐसा खास तौर पर तब किया जा सकता है, जब नेटवर्क की क्वालिटी खराब हो.

    • अगर फ़ाइल अपलोड या डाउनलोड करने में कोई रुकावट आती है, तो Cloud Storage for Firebase SDK, कार्रवाई को अपने-आप वहीं से शुरू कर देते हैं जहां वह रुकी थी.
    • अपलोड की गई एक ही फ़ाइल का इस्तेमाल कई बार किया जा सकता है. इसके लिए, उपयोगकर्ता को हर बार फ़ाइल अपलोड करने की ज़रूरत नहीं होती. जैसे, मल्टीमॉडल वाले नए अनुरोध में.
  • Firebase Security Rules का इस्तेमाल करके, Cloud Storage for Firebase में सेव की गई फ़ाइलों को ऐक्सेस करने से असली उपयोगकर्ता को रोका जा सकता है. Firebase Security Rules की मदद से, सिर्फ़ अनुमति पा चुके उपयोगकर्ता को फ़ाइलें अपलोड, डाउनलोड या मिटाने की अनुमति मिलती है.

  • Firebase या Google Cloud से, अपने बकेट में मौजूद फ़ाइलों को ऐक्सेस किया जा सकता है. इससे आपको सर्वर-साइड प्रोसेसिंग करने की सुविधा मिलती है. जैसे, Google Cloud Storage एपीआई का इस्तेमाल करके इमेज फ़िल्टर करना या वीडियो ट्रांसकोड करना.

किन फ़ाइल टाइप और यूआरएल का इस्तेमाल किया जा सकता है?

Firebase AI Logic SDK टूल के साथ Cloud Storage for Firebase यूआरएल इस्तेमाल करने के लिए, फ़ाइलों और यूआरएल से जुड़ी ये ज़रूरी शर्तें पूरी होनी चाहिए:

  • फ़ाइल को मल्टीमॉडल अनुरोधों के लिए इनपुट फ़ाइलों की ज़रूरी शर्तों को पूरा करना होगा. इसमें MIME टाइप और फ़ाइल साइज़ जैसी ज़रूरी शर्तें शामिल हैं.

  • फ़ाइल को Cloud Storage for Firebase बकेट में सेव किया जाना चाहिए (इसका मतलब है कि बकेट को Firebase की सेवाओं, जैसे कि Firebase Security Rules से ऐक्सेस किया जा सकता है). अगर आपको Firebase कंसोल में अपनी बकेट दिखती है, तो इसका मतलब है कि यह Cloud Storage for Firebase बकेट है.

  • Cloud Storage for Firebase बकेट उसी Firebase प्रोजेक्ट में होना चाहिए जिसमें आपने अपना ऐप्लिकेशन रजिस्टर किया है.

  • फ़ाइल के Cloud Storage for Firebase यूआरएल की शुरुआत gs:// से होनी चाहिए. यह Google Cloud Storage यूआरएल बनाने का तरीका है.

  • फ़ाइल का यूआरएल, "ब्राउज़र" यूआरएल नहीं होना चाहिए. उदाहरण के लिए, इंटरनेट पर मिली किसी इमेज का यूआरएल.

साथ ही, आपके बकेट के लिए Firebase Security Rules को फ़ाइल का सही ऐक्सेस देना होगा. उदाहरण के लिए:

  • अगर आपके पास सार्वजनिक नियम हैं, तो कोई भी उपयोगकर्ता या क्लाइंट फ़ाइल को ऐक्सेस कर सकता है.

  • अगर आपके पास सख्त नियम हैं, तो Firebase यह जांच करेगा कि साइन इन किए गए उपयोगकर्ता या क्लाइंट के पास फ़ाइल का ऐक्सेस है या नहीं. इसके बाद ही, वह दिए गए यूआरएल से कॉल को आगे बढ़ने देगा. हमारा सुझाव है कि आप सख्त नियमों का इस्तेमाल करें.

Firebase AI Logic के साथ Cloud Storage for Firebase यूआरएल का इस्तेमाल करना

यह सुविधा सिर्फ़ तब उपलब्ध होती है, जब Vertex AI Gemini API को एपीआई उपलब्ध कराने वाली सेवा के तौर पर इस्तेमाल किया जा रहा हो.

पहला चरण: Cloud Storage for Firebase सेट अप करना

Cloud Storage for Firebase को सेट अप करने के बारे में ज़्यादा जानकारी, इसकी शुरुआती गाइड में दी गई है: iOS+ | Android | वेब | Flutter | Unity

यहां उन मुख्य कामों के बारे में बताया गया है जिन्हें आपको पूरा करना होगा:

  1. अपने Firebase प्रोजेक्ट में Cloud Storage for Firebase बकेट बनाएं या इंपोर्ट करें.

  2. इस बकेट में Firebase Security Rules लागू करें. Rules इससे आपको अपनी फ़ाइलों को सुरक्षित रखने में मदद मिलती है. ऐसा इसलिए, क्योंकि यह सिर्फ़ उन असली उपयोगकर्ताओं को फ़ाइलों का ऐक्सेस देता है जिनके पास इसकी अनुमति होती है.

  3. अपने ऐप्लिकेशन में Cloud Storage for Firebase के लिए क्लाइंट लाइब्रेरी जोड़ें.

    ध्यान दें कि इस टास्क को छोड़ा जा सकता है. हालांकि, ऐसा करने पर आपको हमेशा अपने अनुरोधों में MIME टाइप और यूआरएल की वैल्यू साफ़ तौर पर शामिल करनी होंगी.

दूसरा चरण: किसी बकेट में फ़ाइल अपलोड करना

Cloud Storage के दस्तावेज़ में, बकेट में फ़ाइलें अपलोड करने के सभी तरीकों के बारे में जानें. उदाहरण के लिए, उपयोगकर्ता के डिवाइस से स्थानीय फ़ाइलें अपलोड की जा सकती हैं. जैसे, कैमरे से ली गई फ़ोटो और वीडियो. ज़्यादा जानें: iOS+ | Android | वेब | Flutter | Unity

किसी बकेट में फ़ाइल अपलोड करने पर, Cloud Storage फ़ाइल में अपने-आप यह दो तरह की जानकारी जोड़ देता है. आपको अनुरोध में ये वैल्यू शामिल करनी होंगी. इस गाइड के अगले चरण में यह दिखाया गया है.

  • एमआईएमई टाइप: यह फ़ाइल का मीडिया टाइप होता है. उदाहरण के लिए, image/png. अपलोड करने के दौरान, हम एमआईएमई टाइप का पता अपने-आप लगाने की कोशिश करेंगे. साथ ही, उस मेटाडेटा को बकेट में मौजूद ऑब्जेक्ट पर लागू करेंगे. हालांकि, अपलोड करते समय MIME टाइप बताना ज़रूरी नहीं है.

  • Cloud Storage for Firebase यूआरएल: यह फ़ाइल के लिए यूनीक आइडेंटिफ़ायर होता है. यूआरएल gs:// से शुरू होना चाहिए.

तीसरा चरण: मल्टीमॉडल अनुरोध में फ़ाइल का एमआईएमई टाइप और यूआरएल शामिल करें

किसी बकेट में फ़ाइल सेव करने के बाद, अनुरोध में उसका MIME टाइप और यूआरएल शामिल किया जा सकता है. ध्यान दें कि इन उदाहरणों में, बिना स्ट्रीमिंग वाला generateContent अनुरोध दिखाया गया है. हालांकि, स्ट्रीमिंग और चैट के साथ भी यूआरएल का इस्तेमाल किया जा सकता है.

अनुरोध में फ़ाइल शामिल करने के लिए, इनमें से किसी एक विकल्प का इस्तेमाल किया जा सकता है:

पहला विकल्प: स्टोरेज रेफ़रंस का इस्तेमाल करके, MIME टाइप और यूआरएल शामिल करना

इस उदाहरण को आज़माने से पहले, पक्का करें कि आपने Firebase AI Logic SDK टूल के लिए, शुरू करने से जुड़ी गाइड पढ़ ली हो.

अगर आपने फ़ाइल को अभी-अभी बकेट में अपलोड किया है और आपको फ़ाइल को तुरंत अनुरोध में शामिल करना है, तो इस विकल्प का इस्तेमाल करें. इसके लिए, स्टोरेज रेफ़रंस का इस्तेमाल करें. कॉल के लिए, MIME टाइप और 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 के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन होते हैं. इन्हें कोरूटीन स्कोप से कॉल करना होता है.
// 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 के लिए, इस एसडीके टूल में मौजूद तरीके 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.");

दूसरा विकल्प: MIME टाइप और यूआरएल को साफ़ तौर पर शामिल करना

इस उदाहरण को आज़माने से पहले, पक्का करें कि आपने Firebase AI Logic SDK टूल के लिए, शुरू करने से जुड़ी गाइड पढ़ ली हो.

अगर आपको MIME टाइप और Cloud Storage for Firebase यूआरएल की वैल्यू पता हैं और आपको उन्हें मल्टीमॉडल अनुरोध में साफ़ तौर पर शामिल करना है, तो इस विकल्प का इस्तेमाल करें. कॉल के लिए, MIME टाइप और यूआरएल, दोनों ज़रूरी हैं.

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 के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन होते हैं. इन्हें कोरूटीन स्कोप से कॉल करना होता है.
// 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 के लिए, इस एसडीके टूल में मौजूद तरीके 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.");