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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

  • फ़ाइल, 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, दिए गए यूआरएल के साथ कॉल को प्रोसेस करने की अनुमति देने से पहले, यह जांच करेगा कि साइन इन किए हुए उपयोगकर्ता या क्लाइंट के पास फ़ाइल को ऐक्सेस करने की अनुमति है या नहीं.

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

यह सुविधा, 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 लागू करना. Security Rules की मदद से, अनुमति वाले एंड यूज़र के लिए ऐक्सेस सीमित करके, अपनी फ़ाइलों को सुरक्षित किया जा सकता है.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

इस विकल्प का इस्तेमाल तब करें, जब आपको एमआईएमई टाइप और 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 = model.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.");