Gemini API का इस्तेमाल करके, मल्टीमोडल प्रॉम्प्ट से टेक्स्ट जनरेट करें
Vertex AI in Firebase SDK टूल का इस्तेमाल करके, अपने ऐप्लिकेशन से Gemini API को कॉल करते समय,
Gemini मॉडल को कई तरह के इनपुट के आधार पर टेक्स्ट जनरेट करने के लिए कहा जा सकता है.
मल्टीमोडल प्रॉम्प्ट में कई मोड (या इनपुट के टाइप) शामिल हो सकते हैं. जैसे, इमेज के साथ टेक्स्ट, PDF, प्लैन-टेक्स्ट फ़ाइलें, वीडियो, और ऑडियो.
हर मल्टीमोडल अनुरोध में, आपको हमेशा यह जानकारी देनी होगी:
फ़ाइल. फ़ाइल को इनलाइन डेटा के तौर पर (जैसा कि इस पेज पर दिखाया गया है) या उसके यूआरएल या यूआरआई का इस्तेमाल करके दिया जा सकता है.
हमारा सुझाव है कि कई मोड वाले प्रॉम्प्ट को टेस्ट करने और उनमें बदलाव करने के लिए, Vertex AI Studio का इस्तेमाल करें.
Gemini API के साथ काम करने के अन्य विकल्प
वैकल्पिक तौर पर, Gemini API के किसी अन्य वर्शन "Google AI" के साथ एक्सपेरिमेंट करें Google AI Studio और
Google AI क्लाइंट SDK टूल का इस्तेमाल करके, सीमाओं के अंदर और जहां उपलब्ध हो वहां बिना किसी शुल्क के ऐक्सेस पाएं.
इन SDK टूल का इस्तेमाल, मोबाइल और वेब ऐप्लिकेशन में सिर्फ़ प्रोटोटाइप बनाने के लिए किया जाना चाहिए.
अगर आपने अब तक ऐसा नहीं किया है, तो शुरू करने से जुड़ी गाइड पढ़ें. इसमें, Firebase प्रोजेक्ट सेट अप करने, अपने ऐप्लिकेशन को Firebase से कनेक्ट करने, SDK टूल जोड़ने, Vertex AI सेवा को शुरू करने, और GenerativeModel इंस्टेंस बनाने का तरीका बताया गया है.
अगर आपके पास पहले से मीडिया फ़ाइलें नहीं हैं, तो सार्वजनिक तौर पर उपलब्ध इन फ़ाइलों का इस्तेमाल किया जा सकता है. ये फ़ाइलें उन बकेट में सेव होती हैं जो आपके Firebase प्रोजेक्ट में नहीं होतीं. इसलिए, आपको यूआरएल के लिए https://storage.googleapis.com/BUCKET_NAME/PATH/TO/FILE फ़ॉर्मैट का इस्तेमाल करना होगा.
इमेज: https://storage.googleapis.com/cloud-samples-data/generative-ai/image/scones.jpgimage/jpeg MIME टाइप वाली.
इस इमेज को देखें या डाउनलोड करें.
PDF: https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/2403.05530.pdfapplication/pdf MIME टाइप वाला.
यह PDF देखें या डाउनलोड करें.
वीडियो: https://storage.googleapis.com/cloud-samples-data/video/animals.mp4video/mp4 MIME टाइप वाला.
यह वीडियो देखें या डाउनलोड करें.
ऑडियो: https://storage.googleapis.com/cloud-samples-data/generative-ai/audio/pixel.mp3audio/mp3 MIME टाइप वाला.
इस ऑडियो को सुनें या डाउनलोड करें.
टेक्स्ट और एक इमेज से टेक्स्ट जनरेट करना
इस सैंपल को आज़माने से पहले, पक्का करें कि आपने इस गाइड का शुरू करने से पहले वाला सेक्शन पूरा कर लिया हो.
Gemini API को मल्टीमोडल प्रॉम्प्ट के साथ कॉल किया जा सकता है. इन प्रॉम्प्ट में टेक्स्ट और एक फ़ाइल, जैसे कि इमेज (जैसा कि इस उदाहरण में दिखाया गया है) शामिल होती है.
टेक्स्ट और एक इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
importFirebaseVertexAI// Initialize the Vertex AI serviceletvertex=VertexAI.vertexAI()// Create a `GenerativeModel` instance with a model that supports your use caseletmodel=vertex.generativeModel(modelName:"gemini-2.0-flash")guardletimage=UIImage(systemName:"bicycle")else{fatalError()}// Provide a text prompt to include with the imageletprompt="What's in this picture?"// To generate text output, call generateContent and pass in the promptletresponse=tryawaitmodel.generateContent(image,prompt)print(response.text??"No text in response.")
Kotlin
टेक्स्ट और एक इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
Kotlin के लिए, इस SDK टूल में मौजूद मैथड, सस्पेंड फ़ंक्शन हैं. इन्हें कोरूटीन स्कोप से कॉल किया जाना चाहिए.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casevalgenerativeModel=Firebase.vertexAI.generativeModel("gemini-2.0-flash")// Loads an image from the app/res/drawable/ directoryvalbitmap:Bitmap=BitmapFactory.decodeResource(resources,R.drawable.sparky)// Provide a prompt that includes the image specified above and textvalprompt=content{image(bitmap)text("What developer tool is this mascot from?")}// To generate text output, call generateContent with the promptvalresponse=generativeModel.generateContent(prompt)print(response.text)
Java
टेक्स्ट और एक इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use caseGenerativeModelgm=FirebaseVertexAI.getInstance().generativeModel("gemini-2.0-flash");GenerativeModelFuturesmodel=GenerativeModelFutures.from(gm);Bitmapbitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sparky);// Provide a prompt that includes the image specified above and textContentcontent=newContent.Builder().addImage(bitmap).addText("What developer tool is this mascot from?").build();// To generate text output, call generateContent with the promptListenableFuture<GenerateContentResponse>response=model.generateContent(content);Futures.addCallback(response,newFutureCallback<GenerateContentResponse>(){@OverridepublicvoidonSuccess(GenerateContentResponseresult){StringresultText=result.getText();System.out.println(resultText);}@OverridepublicvoidonFailure(Throwablet){t.printStackTrace();}},executor);
Web
टेक्स्ट और एक इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
import{initializeApp}from"firebase/app";import{getVertexAI,getGenerativeModel}from"firebase/vertexai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Vertex AI serviceconstvertexAI=getVertexAI(firebaseApp);// Create a `GenerativeModel` instance with a model that supports your use caseconstmodel=getGenerativeModel(vertexAI,{model:"gemini-2.0-flash"});// Converts a File object to a Part object.asyncfunctionfileToGenerativePart(file){constbase64EncodedDataPromise=newPromise((resolve)=>{constreader=newFileReader();reader.onloadend=()=>resolve(reader.result.split(',')[1]);reader.readAsDataURL(file);});return{inlineData:{data:awaitbase64EncodedDataPromise,mimeType:file.type},};}asyncfunctionrun(){// Provide a text prompt to include with the imageconstprompt="What's different between these pictures?";constfileInputEl=document.querySelector("input[type=file]");constimagePart=awaitfileToGenerativePart(fileInputEl.files[0]);// To generate text output, call generateContent with the text and imageconstresult=awaitmodel.generateContent([prompt,imagePart]);constresponse=result.response;consttext=response.text();console.log(text);}run();
Dart
टेक्स्ट और एक इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
import'package:firebase_vertexai/firebase_vertexai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';awaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casefinalmodel=FirebaseVertexAI.instance.generativeModel(model:'gemini-2.0-flash');// Provide a text prompt to include with the imagefinalprompt=TextPart("What's in the picture?");// Prepare images for inputfinalimage=awaitFile('image0.jpg').readAsBytes();finalimagePart=InlineDataPart('image/jpeg',image);// To generate text output, call generateContent with the text and imagefinalresponse=awaitmodel.generateContent([Content.multi([prompt,imagePart])]);print(response.text);
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल और जगह चुनने का तरीका जानें.
टेक्स्ट और कई इमेज से टेक्स्ट जनरेट करना
इस सैंपल को आज़माने से पहले, पक्का करें कि आपने इस गाइड का शुरू करने से पहले वाला सेक्शन पूरा कर लिया हो.
Gemini API को मल्टीमोडल प्रॉम्प्ट के साथ कॉल किया जा सकता है. इन प्रॉम्प्ट में टेक्स्ट और कई फ़ाइलें, जैसे कि इमेज (जैसा कि इस उदाहरण में दिखाया गया है) शामिल होती हैं.
टेक्स्ट और कई इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
importFirebaseVertexAI// Initialize the Vertex AI serviceletvertex=VertexAI.vertexAI()// Create a `GenerativeModel` instance with a model that supports your use caseletmodel=vertex.generativeModel(modelName:"gemini-2.0-flash")guardletimage1=UIImage(systemName:"car")else{fatalError()}guardletimage2=UIImage(systemName:"car.2")else{fatalError()}// Provide a text prompt to include with the imagesletprompt="What's different between these pictures?"// To generate text output, call generateContent and pass in the promptletresponse=tryawaitmodel.generateContent(image1,image2,prompt)print(response.text??"No text in response.")
Kotlin
टेक्स्ट और कई इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
Kotlin के लिए, इस SDK टूल में मौजूद मैथड, सस्पेंड फ़ंक्शन हैं. इन्हें कोरूटीन स्कोप से कॉल किया जाना चाहिए.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casevalgenerativeModel=Firebase.vertexAI.generativeModel("gemini-2.0-flash")// Loads an image from the app/res/drawable/ directoryvalbitmap1:Bitmap=BitmapFactory.decodeResource(resources,R.drawable.sparky)valbitmap2:Bitmap=BitmapFactory.decodeResource(resources,R.drawable.sparky_eats_pizza)// Provide a prompt that includes the images specified above and textvalprompt=content{image(bitmap1)image(bitmap2)text("What is different between these pictures?")}// To generate text output, call generateContent with the promptvalresponse=generativeModel.generateContent(prompt)print(response.text)
Java
टेक्स्ट और कई इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use caseGenerativeModelgm=FirebaseVertexAI.getInstance().generativeModel("gemini-2.0-flash");GenerativeModelFuturesmodel=GenerativeModelFutures.from(gm);Bitmapbitmap1=BitmapFactory.decodeResource(getResources(),R.drawable.sparky);Bitmapbitmap2=BitmapFactory.decodeResource(getResources(),R.drawable.sparky_eats_pizza);// Provide a prompt that includes the images specified above and textContentprompt=newContent.Builder().addImage(bitmap1).addImage(bitmap2).addText("What's different between these pictures?").build();// To generate text output, call generateContent with the promptListenableFuture<GenerateContentResponse>response=model.generateContent(prompt);Futures.addCallback(response,newFutureCallback<GenerateContentResponse>(){@OverridepublicvoidonSuccess(GenerateContentResponseresult){StringresultText=result.getText();System.out.println(resultText);}@OverridepublicvoidonFailure(Throwablet){t.printStackTrace();}},executor);
Web
टेक्स्ट और कई इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
import{initializeApp}from"firebase/app";import{getVertexAI,getGenerativeModel}from"firebase/vertexai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Vertex AI serviceconstvertexAI=getVertexAI(firebaseApp);// Create a `GenerativeModel` instance with a model that supports your use caseconstmodel=getGenerativeModel(vertexAI,{model:"gemini-2.0-flash"});// Converts a File object to a Part object.asyncfunctionfileToGenerativePart(file){constbase64EncodedDataPromise=newPromise((resolve)=>{constreader=newFileReader();reader.onloadend=()=>resolve(reader.result.split(',')[1]);reader.readAsDataURL(file);});return{inlineData:{data:awaitbase64EncodedDataPromise,mimeType:file.type},};}asyncfunctionrun(){// Provide a text prompt to include with the imagesconstprompt="What's different between these pictures?";// Prepare images for inputconstfileInputEl=document.querySelector("input[type=file]");constimageParts=awaitPromise.all([...fileInputEl.files].map(fileToGenerativePart));// To generate text output, call generateContent with the text and imagesconstresult=awaitmodel.generateContent([prompt,...imageParts]);constresponse=result.response;consttext=response.text();console.log(text);}run();
Dart
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है. इसमें टेक्स्ट और कई इमेज शामिल होती हैं:
import'package:firebase_vertexai/firebase_vertexai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';awaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casefinalmodel=FirebaseVertexAI.instance.generativeModel(model:'gemini-2.0-flash');final(firstImage,secondImage)=await(File('image0.jpg').readAsBytes(),File('image1.jpg').readAsBytes()).wait;// Provide a text prompt to include with the imagesfinalprompt=TextPart("What's different between these pictures?");// Prepare images for inputfinalimageParts=[InlineDataPart('image/jpeg',firstImage),InlineDataPart('image/jpeg',secondImage),];// To generate text output, call generateContent with the text and imagesfinalresponse=awaitmodel.generateContent([Content.multi([prompt,...imageParts])]);print(response.text);
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल और जगह चुनने का तरीका जानें.
टेक्स्ट और वीडियो से टेक्स्ट जनरेट करना
इस सैंपल को आज़माने से पहले, पक्का करें कि आपने इस गाइड का शुरू करने से पहले वाला सेक्शन पूरा कर लिया हो.
Gemini API को ऐसे मल्टीमोडल प्रॉम्प्ट के साथ कॉल किया जा सकता है जिनमें टेक्स्ट और वीडियो फ़ाइल, दोनों शामिल हों (जैसा कि इस उदाहरण में दिखाया गया है).
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
importFirebaseVertexAI// Initialize the Vertex AI serviceletvertex=VertexAI.vertexAI()// Create a `GenerativeModel` instance with a model that supports your use caseletmodel=vertex.generativeModel(modelName:"gemini-2.0-flash")// Provide the video as `Data` with the appropriate MIME type.letvideo=InlineDataPart(data:tryData(contentsOf:videoURL),mimeType:"video/mp4")// Provide a text prompt to include with the videoletprompt="What is in the video?"// To generate text output, call generateContent with the text and videoletresponse=tryawaitmodel.generateContent(video,prompt)print(response.text??"No text in response.")
Kotlin
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
Kotlin के लिए, इस SDK टूल में मौजूद मैथड, सस्पेंड फ़ंक्शन हैं. इन्हें कोरूटीन स्कोप से कॉल किया जाना चाहिए.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casevalgenerativeModel=Firebase.vertexAI.generativeModel("gemini-2.0-flash")valcontentResolver=applicationContext.contentResolvercontentResolver.openInputStream(videoUri).use{stream->stream?.let{valbytes=stream.readBytes()// Provide a prompt that includes the video specified above and textvalprompt=content{inlineData(bytes,"video/mp4")text("What is in the video?")}// To generate text output, call generateContent with the promptvalresponse=generativeModel.generateContent(prompt)Log.d(TAG,response.text?:"")}}
Java
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use caseGenerativeModelgm=FirebaseVertexAI.getInstance().generativeModel("gemini-2.0-flash");GenerativeModelFuturesmodel=GenerativeModelFutures.from(gm);ContentResolverresolver=getApplicationContext().getContentResolver();try(InputStreamstream=resolver.openInputStream(videoUri)){FilevideoFile=newFile(newURI(videoUri.toString()));intvideoSize=(int)videoFile.length();byte[]videoBytes=newbyte[videoSize];if(stream!=null){stream.read(videoBytes,0,videoBytes.length);stream.close();// Provide a prompt that includes the video specified above and textContentprompt=newContent.Builder().addInlineData(videoBytes,"video/mp4").addText("What is in the video?").build();// To generate text output, call generateContent with the promptListenableFuture<GenerateContentResponse>response=model.generateContent(prompt);Futures.addCallback(response,newFutureCallback<GenerateContentResponse>(){@OverridepublicvoidonSuccess(GenerateContentResponseresult){StringresultText=result.getText();System.out.println(resultText);}@OverridepublicvoidonFailure(Throwablet){t.printStackTrace();}},executor);}}catch(IOExceptione){e.printStackTrace();}catch(URISyntaxExceptione){e.printStackTrace();}
Web
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
import{initializeApp}from"firebase/app";import{getVertexAI,getGenerativeModel}from"firebase/vertexai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Vertex AI serviceconstvertexAI=getVertexAI(firebaseApp);// Create a `GenerativeModel` instance with a model that supports your use caseconstmodel=getGenerativeModel(vertexAI,{model:"gemini-2.0-flash"});// Converts a File object to a Part object.asyncfunctionfileToGenerativePart(file){constbase64EncodedDataPromise=newPromise((resolve)=>{constreader=newFileReader();reader.onloadend=()=>resolve(reader.result.split(',')[1]);reader.readAsDataURL(file);});return{inlineData:{data:awaitbase64EncodedDataPromise,mimeType:file.type},};}asyncfunctionrun(){// Provide a text prompt to include with the videoconstprompt="What do you see?";constfileInputEl=document.querySelector("input[type=file]");constvideoPart=awaitfileToGenerativePart(fileInputEl.files[0]);// To generate text output, call generateContent with the text and videoconstresult=awaitmodel.generateContent([prompt,videoPart]);constresponse=result.response;consttext=response.text();console.log(text);}run();
Dart
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से टेक्स्ट जनरेट करने के लिए, generateContent() को कॉल किया जा सकता है:
import'package:firebase_vertexai/firebase_vertexai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';awaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casefinalmodel=FirebaseVertexAI.instance.generativeModel(model:'gemini-2.0-flash');// Provide a text prompt to include with the videofinalprompt=TextPart("What's in the video?");// Prepare video for inputfinalvideo=awaitFile('video0.mp4').readAsBytes();// Provide the video as `Data` with the appropriate mimetypefinalvideoPart=InlineDataPart('video/mp4',video);// To generate text output, call generateContent with the text and imagesfinalresponse=awaitmodel.generateContent([Content.multi([prompt,...videoPart])]);print(response.text);
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल और जगह चुनने का तरीका जानें.
जवाब स्ट्रीम करना
इन सैंपल को आज़माने से पहले, पक्का करें कि आपने इस गाइड का शुरू करने से पहले वाला सेक्शन पूरा कर लिया हो.
मॉडल जनरेशन के पूरे नतीजे का इंतज़ार किए बिना, तेज़ी से इंटरैक्शन हासिल किए जा सकते हैं. इसके बजाय, कुछ नतीजों को मैनेज करने के लिए स्ट्रीमिंग का इस्तेमाल करें.
जवाब को स्ट्रीम करने के लिए, generateContentStream को कॉल करें.
उदाहरण देखें: टेक्स्ट और एक इमेज से जनरेट किया गया टेक्स्ट स्ट्रीम करना
Swift
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट के अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और एक इमेज शामिल होती है:
importFirebaseVertexAI// Initialize the Vertex AI serviceletvertex=VertexAI.vertexAI()// Create a `GenerativeModel` instance with a model that supports your use caseletmodel=vertex.generativeModel(modelName:"gemini-2.0-flash")guardletimage=UIImage(systemName:"bicycle")else{fatalError()}// Provide a text prompt to include with the imageletprompt="What's in this picture?"// To stream generated text output, call generateContentStream and pass in the promptletcontentStream=trymodel.generateContentStream(image,prompt)fortryawaitchunkincontentStream{iflettext=chunk.text{print(text)}}
Kotlin
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट के अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और एक इमेज शामिल होती है:
Kotlin के लिए, इस SDK टूल में मौजूद मैथड, सस्पेंड फ़ंक्शन हैं. इन्हें कोरूटीन स्कोप से कॉल किया जाना चाहिए.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casevalgenerativeModel=Firebase.vertexAI.generativeModel("gemini-2.0-flash")// Loads an image from the app/res/drawable/ directoryvalbitmap:Bitmap=BitmapFactory.decodeResource(resources,R.drawable.sparky)// Provide a prompt that includes the image specified above and textvalprompt=content{image(bitmap)text("What developer tool is this mascot from?")}// To stream generated text output, call generateContentStream with the promptvarfullResponse=""generativeModel.generateContentStream(prompt).collect{chunk->print(chunk.text)fullResponse+=chunk.text}
Java
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट के अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और एक इमेज शामिल होती है:
Java के लिए, इस SDK टूल में स्ट्रीमिंग के तरीके, Reactive Streams लाइब्रेरी से Publisher टाइप दिखाते हैं.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use caseGenerativeModelgm=FirebaseVertexAI.getInstance().generativeModel("gemini-2.0-flash");GenerativeModelFuturesmodel=GenerativeModelFutures.from(gm);Bitmapbitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sparky);// Provide a prompt that includes the image specified above and textContentprompt=newContent.Builder().addImage(bitmap).addText("What developer tool is this mascot from?").build();// To stream generated text output, call generateContentStream with the promptPublisher<GenerateContentResponse>streamingResponse=model.generateContentStream(prompt);finalString[]fullResponse={""};streamingResponse.subscribe(newSubscriber<GenerateContentResponse>(){@OverridepublicvoidonNext(GenerateContentResponsegenerateContentResponse){Stringchunk=generateContentResponse.getText();fullResponse[0]+=chunk;}@OverridepublicvoidonComplete(){System.out.println(fullResponse[0]);}@OverridepublicvoidonError(Throwablet){t.printStackTrace();}@OverridepublicvoidonSubscribe(Subscriptions){}});
Web
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट के अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और एक इमेज शामिल होती है:
import{initializeApp}from"firebase/app";import{getVertexAI,getGenerativeModel}from"firebase/vertexai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Vertex AI serviceconstvertexAI=getVertexAI(firebaseApp);// Create a `GenerativeModel` instance with a model that supports your use caseconstmodel=getGenerativeModel(vertexAI,{model:"gemini-2.0-flash"});// Converts a File object to a Part object.asyncfunctionfileToGenerativePart(file){constbase64EncodedDataPromise=newPromise((resolve)=>{constreader=newFileReader();reader.onloadend=()=>resolve(reader.result.split(',')[1]);reader.readAsDataURL(file);});return{inlineData:{data:awaitbase64EncodedDataPromise,mimeType:file.type},};}asyncfunctionrun(){// Provide a text prompt to include with the imageconstprompt="What do you see?";// Prepare image for inputconstfileInputEl=document.querySelector("input[type=file]");constimagePart=awaitfileToGenerativePart(fileInputEl.files[0]);// To stream generated text output, call generateContentStream with the text and imageconstresult=awaitmodel.generateContentStream([prompt,imagePart]);forawait(constchunkofresult.stream){constchunkText=chunk.text();console.log(chunkText);}}run();
Dart
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट के अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और एक इमेज शामिल होती है:
import'package:firebase_vertexai/firebase_vertexai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';awaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casefinalmodel=FirebaseVertexAI.instance.generativeModel(model:'gemini-2.0-flash');// Provide a text prompt to include with the imagefinalprompt=TextPart("What's in the picture?");// Prepare images for inputfinalimage=awaitFile('image0.jpg').readAsBytes();finalimagePart=InlineDataPart('image/jpeg',image);// To stream generated text output, call generateContentStream with the text and imagefinalresponse=awaitmodel.generateContentStream([Content.multi([prompt,imagePart])]);awaitfor(finalchunkinresponse){print(chunk.text);}
उदाहरण देखें: टेक्स्ट और कई इमेज से जनरेट किया गया टेक्स्ट स्ट्रीम करना
Swift
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और कई इमेज शामिल होती हैं:
importFirebaseVertexAI// Initialize the Vertex AI serviceletvertex=VertexAI.vertexAI()// Create a `GenerativeModel` instance with a model that supports your use caseletmodel=vertex.generativeModel(modelName:"gemini-2.0-flash")guardletimage1=UIImage(systemName:"car")else{fatalError()}guardletimage2=UIImage(systemName:"car.2")else{fatalError()}// Provide a text prompt to include with the imagesletprompt="What's different between these pictures?"// To stream generated text output, call generateContentStream and pass in the promptletcontentStream=trymodel.generateContentStream(image1,image2,prompt)fortryawaitchunkincontentStream{iflettext=chunk.text{print(text)}}
Kotlin
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और कई इमेज शामिल होती हैं:
Kotlin के लिए, इस SDK टूल में मौजूद मैथड, सस्पेंड फ़ंक्शन हैं. इन्हें कोरूटीन स्कोप से कॉल किया जाना चाहिए.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casevalgenerativeModel=Firebase.vertexAI.generativeModel("gemini-2.0-flash")// Loads an image from the app/res/drawable/ directoryvalbitmap1:Bitmap=BitmapFactory.decodeResource(resources,R.drawable.sparky)valbitmap2:Bitmap=BitmapFactory.decodeResource(resources,R.drawable.sparky_eats_pizza)// Provide a prompt that includes the images specified above and textvalprompt=content{image(bitmap1)image(bitmap2)text("What's different between these pictures?")}// To stream generated text output, call generateContentStream with the promptvarfullResponse=""generativeModel.generateContentStream(prompt).collect{chunk->print(chunk.text)fullResponse+=chunk.text}
Java
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और कई इमेज शामिल होती हैं:
Java के लिए, इस SDK टूल में स्ट्रीमिंग के तरीके, Reactive Streams लाइब्रेरी से Publisher टाइप दिखाते हैं.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use caseGenerativeModelgm=FirebaseVertexAI.getInstance().generativeModel("gemini-2.0-flash");GenerativeModelFuturesmodel=GenerativeModelFutures.from(gm);Bitmapbitmap1=BitmapFactory.decodeResource(getResources(),R.drawable.sparky);Bitmapbitmap2=BitmapFactory.decodeResource(getResources(),R.drawable.sparky_eats_pizza);// Provide a prompt that includes the images specified above and textContentprompt=newContent.Builder().addImage(bitmap1).addImage(bitmap2).addText("What's different between these pictures?").build();// To stream generated text output, call generateContentStream with the promptPublisher<GenerateContentResponse>streamingResponse=model.generateContentStream(prompt);finalString[]fullResponse={""};streamingResponse.subscribe(newSubscriber<GenerateContentResponse>(){@OverridepublicvoidonNext(GenerateContentResponsegenerateContentResponse){Stringchunk=generateContentResponse.getText();fullResponse[0]+=chunk;}@OverridepublicvoidonComplete(){System.out.println(fullResponse[0]);}@OverridepublicvoidonError(Throwablet){t.printStackTrace();}@OverridepublicvoidonSubscribe(Subscriptions){}});
Web
टेक्स्ट, इमेज, और वीडियो वगैरह का इस्तेमाल करके किए गए प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है. इस अनुरोध में टेक्स्ट और कई इमेज शामिल होती हैं:
import{initializeApp}from"firebase/app";import{getVertexAI,getGenerativeModel}from"firebase/vertexai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Vertex AI serviceconstvertexAI=getVertexAI(firebaseApp);// Create a `GenerativeModel` instance with a model that supports your use caseconstmodel=getGenerativeModel(vertexAI,{model:"gemini-2.0-flash"});// Converts a File object to a Part object.asyncfunctionfileToGenerativePart(file){constbase64EncodedDataPromise=newPromise((resolve)=>{constreader=newFileReader();reader.onloadend=()=>resolve(reader.result.split(',')[1]);reader.readAsDataURL(file);});return{inlineData:{data:awaitbase64EncodedDataPromise,mimeType:file.type},};}asyncfunctionrun(){// Provide a text prompt to include with the imagesconstprompt="What's different between these pictures?";constfileInputEl=document.querySelector("input[type=file]");constimageParts=awaitPromise.all([...fileInputEl.files].map(fileToGenerativePart));// To stream generated text output, call generateContentStream with the text and imagesconstresult=awaitmodel.generateContentStream([prompt,...imageParts]);forawait(constchunkofresult.stream){constchunkText=chunk.text();console.log(chunkText);}}run();
Dart
इस उदाहरण में, टेक्स्ट और कई इमेज वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream का इस्तेमाल करने का तरीका बताया गया है:
import'package:firebase_vertexai/firebase_vertexai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';awaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casefinalmodel=FirebaseVertexAI.instance.generativeModel(model:'gemini-2.0-flash');final(firstImage,secondImage)=await(File('image0.jpg').readAsBytes(),File('image1.jpg').readAsBytes()).wait;// Provide a text prompt to include with the imagesfinalprompt=TextPart("What's different between these pictures?");// Prepare images for inputfinalimageParts=[InlineDataPart('image/jpeg',firstImage),InlineDataPart('image/jpeg',secondImage),];// To stream generated text output, call generateContentStream with the text and imagesfinalresponse=awaitmodel.generateContentStream([Content.multi([prompt,...imageParts])]);awaitfor(finalchunkinresponse){print(chunk.text);}
उदाहरण देखें: टेक्स्ट और वीडियो से जनरेट किया गया टेक्स्ट स्ट्रीम करना
Swift
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है:
importFirebaseVertexAI// Initialize the Vertex AI serviceletvertex=VertexAI.vertexAI()// Create a `GenerativeModel` instance with a model that supports your use caseletmodel=vertex.generativeModel(modelName:"gemini-2.0-flash")// Provide the video as `Data` with the appropriate MIME typeletvideo=InlineDataPart(data:tryData(contentsOf:videoURL),mimeType:"video/mp4")// Provide a text prompt to include with the videoletprompt="What is in the video?"// To stream generated text output, call generateContentStream with the text and videoletcontentStream=trymodel.generateContentStream(video,prompt)fortryawaitchunkincontentStream{iflettext=chunk.text{print(text)}}
Kotlin
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है:
Kotlin के लिए, इस SDK टूल में मौजूद मैथड, सस्पेंड फ़ंक्शन हैं. इन्हें कोरूटीन स्कोप से कॉल किया जाना चाहिए.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casevalgenerativeModel=Firebase.vertexAI.generativeModel("gemini-2.0-flash")valcontentResolver=applicationContext.contentResolvercontentResolver.openInputStream(videoUri).use{stream->stream?.let{valbytes=stream.readBytes()// Provide a prompt that includes the video specified above and textvalprompt=content{inlineData(bytes,"video/mp4")text("What is in the video?")}// To stream generated text output, call generateContentStream with the promptvarfullResponse=""generativeModel.generateContentStream(prompt).collect{chunk->Log.d(TAG,chunk.text?:"")fullResponse+=chunk.text}}}
Java
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है:
Java के लिए, इस SDK टूल में स्ट्रीमिंग के तरीके, Reactive Streams लाइब्रेरी से Publisher टाइप दिखाते हैं.
// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use caseGenerativeModelgm=FirebaseVertexAI.getInstance().generativeModel("gemini-2.0-flash");GenerativeModelFuturesmodel=GenerativeModelFutures.from(gm);ContentResolverresolver=getApplicationContext().getContentResolver();try(InputStreamstream=resolver.openInputStream(videoUri)){FilevideoFile=newFile(newURI(videoUri.toString()));intvideoSize=(int)videoFile.length();byte[]videoBytes=newbyte[videoSize];if(stream!=null){stream.read(videoBytes,0,videoBytes.length);stream.close();// Provide a prompt that includes the video specified above and textContentprompt=newContent.Builder().addInlineData(videoBytes,"video/mp4").addText("What is in the video?").build();// To stream generated text output, call generateContentStream with the promptPublisher<GenerateContentResponse>streamingResponse=model.generateContentStream(prompt);finalString[]fullResponse={""};streamingResponse.subscribe(newSubscriber<GenerateContentResponse>(){@OverridepublicvoidonNext(GenerateContentResponsegenerateContentResponse){Stringchunk=generateContentResponse.getText();fullResponse[0]+=chunk;}@OverridepublicvoidonComplete(){System.out.println(fullResponse[0]);}@OverridepublicvoidonError(Throwablet){t.printStackTrace();}@OverridepublicvoidonSubscribe(Subscriptions){}});}}catch(IOExceptione){e.printStackTrace();}catch(URISyntaxExceptione){e.printStackTrace();}
Web
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है:
import{initializeApp}from"firebase/app";import{getVertexAI,getGenerativeModel}from"firebase/vertexai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Vertex AI serviceconstvertexAI=getVertexAI(firebaseApp);// Create a `GenerativeModel` instance with a model that supports your use caseconstmodel=getGenerativeModel(vertexAI,{model:"gemini-2.0-flash"});// Converts a File object to a Part object.asyncfunctionfileToGenerativePart(file){constbase64EncodedDataPromise=newPromise((resolve)=>{constreader=newFileReader();reader.onloadend=()=>resolve(reader.result.split(',')[1]);reader.readAsDataURL(file);});return{inlineData:{data:awaitbase64EncodedDataPromise,mimeType:file.type},};}asyncfunctionrun(){// Provide a text prompt to include with the videoconstprompt="What do you see?";constfileInputEl=document.querySelector("input[type=file]");constvideoPart=awaitfileToGenerativePart(fileInputEl.files[0]);// To stream generated text output, call generateContentStream with the text and videoconstresult=awaitmodel.generateContentStream([prompt,videoPart]);forawait(constchunkofresult.stream){constchunkText=chunk.text();console.log(chunkText);}}run();
Dart
टेक्स्ट और एक वीडियो वाले मल्टीमोडल प्रॉम्प्ट अनुरोध से जनरेट किए गए टेक्स्ट को स्ट्रीम करने के लिए, generateContentStream() को कॉल किया जा सकता है:
import'package:firebase_vertexai/firebase_vertexai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';awaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Vertex AI service and create a `GenerativeModel` instance// Specify a model that supports your use casefinalmodel=FirebaseVertexAI.instance.generativeModel(model:'gemini-2.0-flash');// Provide a text prompt to include with the videofinalprompt=TextPart("What's in the video?");// Prepare video for inputfinalvideo=awaitFile('video0.mp4').readAsBytes();// Provide the video as `Data` with the appropriate mimetypefinalvideoPart=InlineDataPart('video/mp4',video);// To stream generated text output, call generateContentStream with the text and imagefinalresponse=awaitmodel.generateContentStream([Content.multi([prompt,videoPart])]);awaitfor(finalchunkinresponse){print(chunk.text);}
इस्तेमाल किए जा सकने वाले MIME टाइप और उन्हें बताने का तरीका
फ़ाइलों और अलग-अलग तरीकों से किए जाने वाले अनुरोधों के लिए ज़रूरी शर्तें और सबसे सही तरीके
तुम और क्या कर सकती हो?
मॉडल को लंबे प्रॉम्प्ट भेजने से पहले, टोकन की गिनती करने का तरीका जानें.
Cloud Storage for Firebase को सेट अप करें,
ताकि आप अपने कई मोड वाले अनुरोधों में बड़ी फ़ाइलें शामिल कर सकें. साथ ही, प्रॉम्प्ट में फ़ाइलें उपलब्ध कराने के लिए, बेहतर तरीके से मैनेज किया जा सके.
फ़ाइलों में इमेज, PDF, वीडियो, और ऑडियो शामिल हो सकते हैं.
प्रोडक्शन के लिए तैयारी करना शुरू करें. इसमें, Gemini API को बिना अनुमति वाले क्लाइंट के गलत इस्तेमाल से बचाने के लिए, Firebase App Check सेट अप करना भी शामिल है.
साथ ही, प्रोडक्शन की चेकलिस्ट को ज़रूर देखें.