| यह सुविधा सिर्फ़ तब उपलब्ध होती है, जब एपीआई उपलब्ध कराने वाली कंपनी के तौर पर Agent Platform Gemini API (formerly Vertex AI) का इस्तेमाल किया जा रहा हो. |
Firebase AI Logic एसडीके का इस्तेमाल करके, अपने ऐप्लिकेशन से Agent Platform Gemini API (formerly Vertex AI) को कॉल करते समय, Gemini मॉडल को टेक्स्ट जनरेट करने के लिए प्रॉम्प्ट दिया जा सकता है. यह टेक्स्ट, मल्टीमॉडल इनपुट के आधार पर जनरेट किया जा सकता है. जैसे, इमेज, PDF, वीडियो, और ऑडियो.
इनपुट के नॉन-टेक्स्ट वाले हिस्सों (जैसे, मीडिया फ़ाइलें) के लिए, अनुरोध में फ़ाइलें शामिल करने के लिए, Cloud Storage for Firebase का इस्तेमाल किया जा सकता है. इस सुविधा के बारे में आपको ये बातें ज़रूर जाननी चाहिए:
अगर Agent Platform Gemini API (formerly Vertex AI) का इस्तेमाल किया जा रहा है, तो Cloud Storage for Firebase का इस्तेमाल किसी भी मल्टीमॉडल अनुरोध के साथ किया जा सकता है. जैसे, टेक्स्ट जनरेट करना और चैट करना. इस गाइड में दिए गए उदाहरणों में, टेक्स्ट और इमेज के बुनियादी इनपुट को दिखाया गया है.
अनुरोध के इनपुट में, फ़ाइल का एमआईएमई टाइप और उसका 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, ब्लॉब और फ़ाइलों को सेव करने के लिए, Google Cloud Storage की तरह ही तेज़, सुरक्षित, और ज़रूरत के हिसाब से बढ़ाए जा सकने वाले इन्फ़्रास्ट्रक्चर का इस्तेमाल करता है. साथ ही, इसके क्लाइंट एसडीके खास तौर पर मोबाइल और वेब ऐप्लिकेशन के लिए बनाए गए हैं.
Firebase AI Logic एसडीके के लिए, अनुरोध का साइज़ ज़्यादा से ज़्यादा 20 एमबी हो सकता है. अगर अनुरोध बहुत बड़ा है, तो आपको एचटीटीपी 413 गड़बड़ी का मैसेज मिलता है. अगर किसी फ़ाइल के साइज़ की वजह से, अनुरोध का कुल साइज़ 20 एमबी से ज़्यादा हो जाता है, तो मल्टीमोडल अनुरोध में फ़ाइल को शामिल करने के लिए, Cloud Storage for Firebase यूआरएल का इस्तेमाल करें. हालांकि, अगर कोई फ़ाइल छोटी है, तो उसे अक्सर इनलाइन डेटा के तौर पर सीधे पास किया जा सकता है. ध्यान दें कि इनलाइन डेटा के तौर पर दी गई फ़ाइल, ट्रांज़िट के दौरान base64 में एन्कोड की जाती है. इससे अनुरोध का साइज़ बढ़ जाता है.
Cloud Storage for Firebase का इस्तेमाल करने के कुछ और फ़ायदे यहां दिए गए हैं:
असली उपयोगकर्ता, आपके ऐप्लिकेशन से सीधे Cloud Storage for Firebase बकेट में इमेज अपलोड कर सकते हैं. इसके बाद, उन इमेज को टेक्स्ट, इमेज वग़ैरह का एक साथ इस्तेमाल करके प्रॉम्प्ट डालने में शामिल किया जा सकता है. इसके लिए, आपको फ़ाइल का एमआईएमई टाइप और Cloud Storage for Firebase यूआरएल (जो फ़ाइल का आइडेंटिफ़ायर होता है) बताना होगा.
अगर असली उपयोगकर्ताओं को इमेज उपलब्ध करानी हैं, तो उनका समय और बैंडविड्थ बचाया जा सकता है. खास तौर पर, तब जब उनके नेटवर्क की क्वालिटी खराब या कमज़ोर हो.
- अगर किसी फ़ाइल को अपलोड या डाउनलोड करने में कोई रुकावट आती है, तो Cloud Storage for Firebase एसडीके, उस कार्रवाई को वहीं से अपने-आप फिर से शुरू कर देते हैं जहां वह रुकी थी.
- अपलोड की गई फ़ाइल का इस्तेमाल कई बार किया जा सकता है. इसके लिए, असली उपयोगकर्ता को हर बार उस फ़ाइल को अपलोड करने की ज़रूरत नहीं पड़ती, जब भी आपके ऐप्लिकेशन में उसकी ज़रूरत होती है. जैसे, किसी नए मल्टीमोडल अनुरोध में.
Firebase Security Rules का इस्तेमाल करके, असली उपयोगकर्ता के लिए Cloud Storage for Firebase में सेव की गई फ़ाइलों का ऐक्सेस सीमित किया जा सकता है. इससे सिर्फ़ अनुमति वाला उपयोगकर्ता ही फ़ाइलें अपलोड, डाउनलोड या मिटा सकता है.
Firebase या Google Cloud से, अपनी बकेट में मौजूद फ़ाइलों को ऐक्सेस किया जा सकता है. इससे आपको Google Cloud Storage एपीआई का इस्तेमाल करके, सर्वर-साइड प्रोसेसिंग करने की सुविधा मिलती है. जैसे, इमेज फ़िल्टर करना या वीडियो ट्रांसकोडिंग करना.
किस तरह की फ़ाइलें और यूआरएल इस्तेमाल किए जा सकते हैं?
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 यह जांच करेगा कि साइन इन किए हुए उपयोगकर्ता या क्लाइंट के पास फ़ाइल का ऐक्सेस है या नहीं. इसके बाद ही, दिए गए यूआरएल के साथ कॉल करने की अनुमति दी जाएगी.
Firebase AI Logic के साथ Cloud Storage for Firebase यूआरएल इस्तेमाल करें
| यह सुविधा सिर्फ़ तब उपलब्ध होती है, जब एपीआई उपलब्ध कराने वाली कंपनी के तौर पर Agent Platform Gemini API (formerly Vertex AI) का इस्तेमाल किया जा रहा हो. |
पहला चरण: Cloud Storage for Firebase को सेट अप करना
Cloud Storage for Firebase को सेट अप करने के बारे में ज़्यादा जानकारी, इसके शुरुआती निर्देश में मिल सकती है: iOS+ | Android | वेब | Flutter | Unity
यहां उन ज़रूरी कामों की जानकारी दी गई है जो आपको करने होंगे:
अपने Firebase प्रोजेक्ट में Cloud Storage for Firebase बकेट बनाएं या इंपोर्ट करें.
इस बकेट पर Firebase Security Rules लागू करें. Security Rules की मदद से, अपनी फ़ाइलों को सुरक्षित रखा जा सकता है. इसके लिए, सिर्फ़ अनुमति पा चुके असली उपयोगकर्ताओं को ही फ़ाइलों का ऐक्सेस दिया जाता है.
अपने ऐप्लिकेशन में 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 के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन हैं. इन्हें 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.");
दूसरा विकल्प: एमआईएमई टाइप और यूआरएल को साफ़ तौर पर शामिल करना
| इस उदाहरण को आज़माने से पहले, पक्का करें कि आपने 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 के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन हैं. इन्हें 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.");