Gemini मॉडल से, आपकी दी गई इमेज फ़ाइलों का विश्लेषण करने के लिए कहा जा सकता है. इन फ़ाइलों को इनलाइन (base64-encoded) या यूआरएल के ज़रिए दिया जा सकता है. Firebase AI Logic का इस्तेमाल करते समय, यह अनुरोध सीधे अपने ऐप्लिकेशन से किया जा सकता है.
इस सुविधा की मदद से, ये काम किए जा सकते हैं:
- इमेज के लिए कैप्शन जनरेट करना या उनसे जुड़े सवालों के जवाब देना
- किसी इमेज के बारे में एक छोटी कहानी या कविता लिखना
- किसी इमेज में मौजूद ऑब्जेक्ट का पता लगाना और उनके लिए बाउंडिंग बॉक्स के निर्देशांक दिखाना
- भावना, स्टाइल या अन्य विशेषताओं के आधार पर, इमेज के सेट को लेबल करना या कैटगरी में बांटना
कोड के सैंपल पर जाएं स्ट्रीम किए गए जवाबों के कोड पर जाएं
इमेज के साथ काम करने के ज़्यादा विकल्पों के लिए, अन्य गाइड देखें स्ट्रक्चर्ड आउटपुट जनरेट करना कई बार बातचीत करना डिवाइस पर मौजूद इमेज का विश्लेषण करना इमेज जनरेट करना |
शुरू करने से पहले
इस पेज पर, Gemini API उपलब्ध कराने वाली कंपनी के हिसाब से कॉन्टेंट और कोड देखने के लिए, उस कंपनी पर क्लिक करें. |
अगर आपने अब तक शुरुआती गाइड नहीं पढ़ी है, तो इसे पढ़ें. इसमें बताया गया है कि Firebase प्रोजेक्ट कैसे सेट अप करें, अपने ऐप्लिकेशन को Firebase से कैसे कनेक्ट करें, एसडीके कैसे जोड़ें, चुने गए Gemini API प्रोवाइडर के लिए बैकएंड सेवा को कैसे शुरू करें, और GenerativeModel
इंस्टेंस कैसे बनाएं.
हमारा सुझाव है कि अपने प्रॉम्प्ट की जांच करने और उन्हें बेहतर बनाने के लिए, Google AI Studio का इस्तेमाल करें. इससे आपको जनरेट किया गया कोड स्निपेट भी मिल सकता है.
इमेज फ़ाइलों (base64-encoded) से टेक्स्ट जनरेट करना
इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें. उस सेक्शन में, आपको Gemini API सेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे. |
Gemini मॉडल से, टेक्स्ट और इमेज का इस्तेमाल करके टेक्स्ट जनरेट करने के लिए कहा जा सकता है. इसके लिए, हर इनपुट फ़ाइल का mimeType
और फ़ाइल खुद देनी होगी. इस पेज पर नीचे, इनपुट फ़ाइलों के लिए ज़रूरी शर्तें और सुझाव देखें.
Swift
टेक्स्ट और इमेज के मल्टीमोडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent()
को कॉल किया जा सकता है.
एक फ़ाइल का इनपुट
import FirebaseAI
// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(modelName: "gemini-2.5-flash")
guard let image = UIImage(systemName: "bicycle") else { fatalError() }
// Provide a text prompt to include with the image
let prompt = "What's in this picture?"
// To generate text output, call generateContent and pass in the prompt
let response = try await model.generateContent(image, prompt)
print(response.text ?? "No text in response.")
एक से ज़्यादा फ़ाइलें इनपुट करना
import FirebaseAI
// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(modelName: "gemini-2.5-flash")
guard let image1 = UIImage(systemName: "car") else { fatalError() }
guard let image2 = UIImage(systemName: "car.2") else { fatalError() }
// Provide a text prompt to include with the images
let prompt = "What's different between these pictures?"
// To generate text output, call generateContent and pass in the prompt
let response = try await model.generateContent(image1, image2, prompt)
print(response.text ?? "No text in response.")
Kotlin
टेक्स्ट और इमेज के मल्टीमोडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent()
को कॉल किया जा सकता है.
एक फ़ाइल का इनपुट
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
// Loads an image from the app/res/drawable/ directory
val bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky)
// Provide a prompt that includes the image specified above and text
val prompt = content {
image(bitmap)
text("What developer tool is this mascot from?")
}
// To generate text output, call generateContent with the prompt
val response = generativeModel.generateContent(prompt)
print(response.text)
एक से ज़्यादा फ़ाइलें इनपुट करना
Kotlin के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन होते हैं. इन्हें कोरूटीन स्कोप से कॉल करना होता है.
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
// Loads an image from the app/res/drawable/ directory
val bitmap1: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky)
val bitmap2: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky_eats_pizza)
// Provide a prompt that includes the images specified above and text
val prompt = content {
image(bitmap1)
image(bitmap2)
text("What is different between these pictures?")
}
// To generate text output, call generateContent with the prompt
val response = generativeModel.generateContent(prompt)
print(response.text)
Java
टेक्स्ट और इमेज के मल्टीमोडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent()
को कॉल किया जा सकता है.
ListenableFuture
दिखाते हैं.
एक फ़ाइल का इनपुट
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sparky);
// Provide a prompt that includes the image specified above and text
Content content = new Content.Builder()
.addImage(bitmap)
.addText("What developer tool is this mascot from?")
.build();
// To generate text output, call generateContent with the prompt
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
एक से ज़्यादा फ़ाइलें इनपुट करना
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.sparky);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.sparky_eats_pizza);
// Provide a prompt that includes the images specified above and text
Content prompt = new Content.Builder()
.addImage(bitmap1)
.addImage(bitmap2)
.addText("What's different between these pictures?")
.build();
// To generate text output, call generateContent with the prompt
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
टेक्स्ट और इमेज के मल्टीमोडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent()
को कॉल किया जा सकता है.
एक फ़ाइल का इनपुट
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, { model: "gemini-2.5-flash" });
// Converts a File object to a Part object.
async function fileToGenerativePart(file) {
const base64EncodedDataPromise = new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(file);
});
return {
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
};
}
async function run() {
// Provide a text prompt to include with the image
const prompt = "What do you see?";
const fileInputEl = document.querySelector("input[type=file]");
const imagePart = await fileToGenerativePart(fileInputEl.files[0]);
// To generate text output, call generateContent with the text and image
const result = await model.generateContent([prompt, imagePart]);
const response = result.response;
const text = response.text();
console.log(text);
}
run();
एक से ज़्यादा फ़ाइलें इनपुट करना
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, { model: "gemini-2.5-flash" });
// Converts a File object to a Part object.
async function fileToGenerativePart(file) {
const base64EncodedDataPromise = new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(file);
});
return {
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
};
}
async function run() {
// Provide a text prompt to include with the images
const prompt = "What's different between these pictures?";
// Prepare images for input
const fileInputEl = document.querySelector("input[type=file]");
const imageParts = await Promise.all(
[...fileInputEl.files].map(fileToGenerativePart)
);
// To generate text output, call generateContent with the text and images
const result = await model.generateContent([prompt, ...imageParts]);
const response = result.response;
const text = response.text();
console.log(text);
}
run();
Dart
टेक्स्ट और इमेज के मल्टीमोडल इनपुट से टेक्स्ट जनरेट करने के लिए, generateContent()
को कॉल किया जा सकता है.
एक फ़ाइल का इनपुट
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
FirebaseAI.googleAI().generativeModel(model: 'gemini-2.5-flash');
// Provide a text prompt to include with the image
final prompt = TextPart("What's in the picture?");
// Prepare images for input
final image = await File('image0.jpg').readAsBytes();
final imagePart = InlineDataPart('image/jpeg', image);
// To generate text output, call generateContent with the text and image
final response = await model.generateContent([
Content.multi([prompt,imagePart])
]);
print(response.text);
एक से ज़्यादा फ़ाइलें इनपुट करना
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
FirebaseAI.googleAI().generativeModel(model: 'gemini-2.5-flash');
final (firstImage, secondImage) = await (
File('image0.jpg').readAsBytes(),
File('image1.jpg').readAsBytes()
).wait;
// Provide a text prompt to include with the images
final prompt = TextPart("What's different between these pictures?");
// Prepare images for input
final imageParts = [
InlineDataPart('image/jpeg', firstImage),
InlineDataPart('image/jpeg', secondImage),
];
// To generate text output, call generateContent with the text and images
final response = await model.generateContent([
Content.multi([prompt, ...imageParts])
]);
print(response.text);
Unity
टेक्स्ट और इमेज के मल्टीमोडल इनपुट से टेक्स्ट जनरेट करने के लिए, GenerateContentAsync()
को कॉल किया जा सकता है.
एक फ़ाइल का इनपुट
using Firebase;
using Firebase.AI;
// Initialize the Gemini Developer API backend service
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports your use case
var model = ai.GetGenerativeModel(modelName: "gemini-2.5-flash");
// Convert a Texture2D into InlineDataParts
var grayImage = ModelContent.InlineData("image/png",
UnityEngine.ImageConversion.EncodeToPNG(UnityEngine.Texture2D.grayTexture));
// Provide a text prompt to include with the image
var prompt = ModelContent.Text("What's in this picture?");
// To generate text output, call GenerateContentAsync and pass in the prompt
var response = await model.GenerateContentAsync(new [] { grayImage, prompt });
UnityEngine.Debug.Log(response.Text ?? "No text in response.");
एक से ज़्यादा फ़ाइलें इनपुट करना
using Firebase;
using Firebase.AI;
// Initialize the Gemini Developer API backend service
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports your use case
var model = ai.GetGenerativeModel(modelName: "gemini-2.5-flash");
// Convert Texture2Ds into InlineDataParts
var blackImage = ModelContent.InlineData("image/png",
UnityEngine.ImageConversion.EncodeToPNG(UnityEngine.Texture2D.blackTexture));
var whiteImage = ModelContent.InlineData("image/png",
UnityEngine.ImageConversion.EncodeToPNG(UnityEngine.Texture2D.whiteTexture));
// Provide a text prompt to include with the images
var prompt = ModelContent.Text("What's different between these pictures?");
// To generate text output, call GenerateContentAsync and pass in the prompt
var response = await model.GenerateContentAsync(new [] { blackImage, whiteImage, prompt });
UnityEngine.Debug.Log(response.Text ?? "No text in response.");
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के लिए, सही मॉडल चुनने का तरीका जानें.
जवाब को स्ट्रीम करना
इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें. उस सेक्शन में, आपको Gemini API सेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे. |
मॉडल जनरेशन से पूरा नतीजा मिलने का इंतज़ार न करके, आपको तेज़ी से इंटरैक्शन मिल सकते हैं. इसके बजाय, कुछ नतीजों को हैंडल करने के लिए स्ट्रीमिंग का इस्तेमाल करें.
जवाब को स्ट्रीम करने के लिए, generateContentStream
को कॉल करें.
इनपुट इमेज फ़ाइलों के लिए ज़रूरी शर्तें और सुझाव
ध्यान दें कि इनलाइन डेटा के तौर पर दी गई फ़ाइल को ट्रांज़िट में base64 में एन्कोड किया जाता है. इससे अनुरोध का साइज़ बढ़ जाता है. अगर कोई अनुरोध बहुत बड़ा है, तो आपको एचटीटीपी 413 गड़बड़ी दिखेगी.
नीचे दी गई चीज़ों के बारे में ज़्यादा जानकारी पाने के लिए, "Vertex AI Gemini API के लिए इस्तेमाल की जा सकने वाली इनपुट फ़ाइलें और ज़रूरी शर्तें" देखें:
- अनुरोध में फ़ाइल देने के अलग-अलग विकल्प (इनलाइन या फ़ाइल के यूआरएल का इस्तेमाल करके)
- इमेज फ़ाइलों के लिए ज़रूरी शर्तें और सबसे सही तरीके
इमेज के लिए इस्तेमाल किए जा सकने वाले MIME टाइप
Gemini मल्टीमॉडल मॉडल, इमेज के इन एमआईएमई टाइप के साथ काम करते हैं:
- PNG -
image/png
- JPEG -
image/jpeg
- WebP -
image/webp
हर अनुरोध के लिए सीमाएं
किसी इमेज में पिक्सल की संख्या तय नहीं होती. हालांकि, बड़ी इमेज को छोटा किया जाता है और उनके चारों ओर पैडिंग जोड़ी जाती है, ताकि वे 3072 x 3072 के ज़्यादा से ज़्यादा रिज़ॉल्यूशन में फ़िट हो सकें. ऐसा करते समय, उनके ओरिजनल आसपेक्ट रेशियो (लंबाई-चौड़ाई का अनुपात) को बनाए रखा जाता है.
हर अनुरोध में ज़्यादा से ज़्यादा फ़ाइलें: 3,000 इमेज फ़ाइलें
तुम और क्या कर सकती हो?
- मॉडल को लंबे प्रॉम्प्ट भेजने से पहले, टोकन गिनने का तरीका जानें.
- Cloud Storage for Firebase सेट अप करें, ताकि मल्टीमॉडल अनुरोधों में बड़ी फ़ाइलें शामिल की जा सकें. साथ ही, आपको प्रॉम्प्ट में फ़ाइलें उपलब्ध कराने के लिए, बेहतर तरीके से मैनेज किया गया समाधान मिल सके. फ़ाइलों में इमेज, PDF, वीडियो, और ऑडियो शामिल हो सकते हैं.
-
प्रोडक्शन के लिए तैयारी शुरू करें. इसके लिए, प्रोडक्शन की चेकलिस्ट देखें. इसमें ये चीज़ें शामिल हैं:
- Firebase App Check सेट अप करना ताकि बिना अनुमति वाले क्लाइंट, Gemini API का गलत इस्तेमाल न कर पाएं.
- Firebase Remote Config को इंटिग्रेट करना ताकि ऐप्लिकेशन का नया वर्शन रिलीज़ किए बिना, ऐप्लिकेशन में वैल्यू अपडेट की जा सकें. जैसे, मॉडल का नाम.
अन्य सुविधाएं आज़माएं
- एक से ज़्यादा बार बातचीत (चैट) करने की सुविधा बनाएं.
- सिर्फ़ टेक्स्ट वाले प्रॉम्प्ट से टेक्स्ट जनरेट करना.
- टेक्स्ट और मल्टीमॉडल, दोनों तरह के प्रॉम्प्ट से स्ट्रक्चर्ड आउटपुट (जैसे कि JSON) जनरेट करना.
- टेक्स्ट प्रॉम्प्ट से इमेज जनरेट करें (Gemini या Imagen).
- Gemini मॉडल को अपने ऐप्लिकेशन के अन्य हिस्सों और बाहरी सिस्टम और जानकारी से कनेक्ट करने के लिए, टूल (जैसे, फ़ंक्शन कॉलिंग और Google Search के साथ ग्राउंडिंग) का इस्तेमाल करें.
कॉन्टेंट जनरेट करने की सुविधा को कंट्रोल करने का तरीका जानें
- प्रॉम्प्ट डिज़ाइन को समझें. इसमें सबसे सही तरीके, रणनीतियां, और उदाहरण के तौर पर दिए गए प्रॉम्प्ट शामिल हैं.
- मॉडल के पैरामीटर कॉन्फ़िगर करें. जैसे, Gemini के लिए तापमान और ज़्यादा से ज़्यादा आउटपुट टोकन या Imagen के लिए आसपेक्ट रेशियो (लंबाई-चौड़ाई का अनुपात) और व्यक्ति की जनरेशन.
- सुरक्षा सेटिंग का इस्तेमाल करें, ताकि आपको ऐसे जवाब मिलने की संभावना को कम किया जा सके जिन्हें नुकसान पहुंचाने वाला माना जा सकता है.
साथ काम करने वाले मॉडल के बारे में ज़्यादा जानें
अलग-अलग कामों के लिए उपलब्ध मॉडल और उनके कोटे और कीमत के बारे में जानें.Firebase AI Logic के साथ अपने अनुभव के बारे में सुझाव/राय दें या शिकायत करें