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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

  • फ़ाइल, 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 का इस्तेमाल करना

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

पहला चरण: सेट अप करना 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 = { 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.");

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

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

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

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 = { 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.");