हमारा सुझाव है कि Android डेवलपर, Gemini Pro और Flash मॉडल को ऐक्सेस करने के लिए, Firebase AI Logic का इस्तेमाल करके Gemini Developer API का इस्तेमाल करें. इससे बिना क्रेडिट कार्ड के शुरुआत की जा सकती है. साथ ही, इसमें बिना किसी शुल्क के इस्तेमाल की जा सकने वाली सुविधाएं भी मिलती हैं. जब आपको यह पता चल जाए कि इंटिग्रेशन सही तरीके से काम कर रहा है, तब इसे ज़्यादा उपयोगकर्ताओं के लिए उपलब्ध कराया जा सकता है. इसके लिए, आपको पैसे चुकाकर सदस्यता लेनी होगी.
शुरू करना
अपने ऐप्लिकेशन से सीधे तौर पर Gemini API का इस्तेमाल करने से पहले, आपको कुछ काम करने होंगे. जैसे, प्रॉम्प्ट लिखने के बारे में जानना. साथ ही, SDK टूल का इस्तेमाल करने के लिए, Firebase और अपने ऐप्लिकेशन को सेट अप करना.
प्रॉम्प्ट के साथ एक्सपेरिमेंट करना
प्रॉम्प्ट आज़माने से, आपको अपने Android ऐप्लिकेशन के लिए सबसे सही फ़्रेज़, कॉन्टेंट, और फ़ॉर्मैट ढूंढने में मदद मिल सकती है. Google AI Studio एक आईडीई है. इसका इस्तेमाल, अपने ऐप्लिकेशन के इस्तेमाल के उदाहरणों के लिए प्रॉम्प्ट के प्रोटोटाइप बनाने और उन्हें डिज़ाइन करने के लिए किया जा सकता है.
अपने इस्तेमाल के उदाहरण के लिए सही प्रॉम्प्ट बनाना, विज्ञान से ज़्यादा कला है. इसलिए, एक्सपेरिमेंट करना बहुत ज़रूरी है. Firebase के दस्तावेज़ में जाकर, प्रॉम्प्ट के बारे में ज़्यादा जानें.
जब आपको लगे कि आपका प्रॉम्प्ट सही है, तब "<>" बटन पर क्लिक करके, कोड स्निपेट पाएं. इन स्निपेट को अपने कोड में जोड़ा जा सकता है.
Firebase प्रोजेक्ट सेट अप करना और अपने ऐप्लिकेशन को Firebase से कनेक्ट करना
जब आपका ऐप्लिकेशन एपीआई को कॉल करने के लिए तैयार हो जाए, तब Firebase के एआई लॉजिक का इस्तेमाल शुरू करने के लिए गाइड में दिए गए "पहला चरण" में दिए गए निर्देशों का पालन करें. इससे, आपको अपने ऐप्लिकेशन में Firebase और SDK टूल सेट अप करने में मदद मिलेगी.
Gradle डिपेंडेंसी जोड़ना
अपने ऐप्लिकेशन मॉड्यूल में यह Gradle डिपेंडेंसी जोड़ें:
Kotlin
dependencies {
// ... other androidx dependencies
// Import the BoM for the Firebase platform
implementation(platform("com.google.firebase:firebase-bom:34.1.0"))
// Add the dependency for the Firebase AI Logic library When using the BoM,
// you don't specify versions in Firebase library dependencies
implementation("com.google.firebase:firebase-ai")
}
Java
dependencies {
// Import the BoM for the Firebase platform
implementation(platform("com.google.firebase:34.1.0"))
// Add the dependency for the Firebase AI Logic library When using the BoM,
// you don't specify versions in Firebase library dependencies
implementation("com.google.firebase:firebase-ai")
// Required for one-shot operations (to use `ListenableFuture` from Guava
// Android)
implementation("com.google.guava:guava:31.0.1-android")
// Required for streaming operations (to use `Publisher` from Reactive
// Streams)
implementation("org.reactivestreams:reactive-streams:1.0.4")
}
जनरेटिव मॉडल को शुरू करना
GenerativeModel
को इंस्टैंटिएट करके और मॉडल का नाम तय करके शुरू करें:
Kotlin
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
Java
GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);
Gemini Developer API के साथ इस्तेमाल किए जा सकने वाले उपलब्ध मॉडल के बारे में ज़्यादा जानें. मॉडल पैरामीटर कॉन्फ़िगर करने के बारे में भी ज़्यादा जानें.
अपने ऐप्लिकेशन से Gemini Developer API का इस्तेमाल करना
SDK टूल का इस्तेमाल करने के लिए, Firebase और ऐप्लिकेशन सेट अप करने के बाद, अब आपके पास अपने ऐप्लिकेशन से Gemini Developer API के साथ इंटरैक्ट करने का विकल्प है.
टेक्स्ट जनरेट करना
टेक्स्ट वाला जवाब जनरेट करने के लिए, अपने प्रॉम्प्ट के साथ generateContent()
को कॉल करें.
Kotlin
scope.launch {
val response = model.generateContent("Write a story about a magic backpack.")
}
Java
Content prompt = new Content.Builder()
.addText("Write a story about a magic backpack.")
.build();
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
[...]
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
इमेज और अन्य मीडिया से टेक्स्ट जनरेट करना
टेक्स्ट के साथ-साथ इमेज या अन्य मीडिया वाले प्रॉम्प्ट से भी टेक्स्ट जनरेट किया जा सकता है. generateContent()
को कॉल करते समय, मीडिया को इनलाइन डेटा के तौर पर पास किया जा सकता है.
उदाहरण के लिए, बिटमैप का इस्तेमाल करने के लिए, image
कॉन्टेंट टाइप का इस्तेमाल करें:
Kotlin
scope.launch {
val response = model.generateContent(
content {
image(bitmap)
text("what is the object in the picture?")
}
)
}
Java
Content content = new Content.Builder()
.addImage(bitmap)
.addText("what is the object in the picture?")
.build();
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
[...]
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
ऑडियो फ़ाइल पास करने के लिए, inlineData
कॉन्टेंट टाइप का इस्तेमाल करें:
Kotlin
val contentResolver = applicationContext.contentResolver
val inputStream = contentResolver.openInputStream(audioUri).use { stream ->
stream?.let {
val bytes = stream.readBytes()
val prompt = content {
inlineData(bytes, "audio/mpeg") // Specify the appropriate audio MIME type
text("Transcribe this audio recording.")
}
val response = model.generateContent(prompt)
}
}
Java
ContentResolver resolver = getApplicationContext().getContentResolver();
try (InputStream stream = resolver.openInputStream(audioUri)) {
File audioFile = new File(new URI(audioUri.toString()));
int audioSize = (int) audioFile.length();
byte audioBytes = new byte[audioSize];
if (stream != null) {
stream.read(audioBytes, 0, audioBytes.length);
stream.close();
// Provide a prompt that includes audio specified earlier and text
Content prompt = new Content.Builder()
.addInlineData(audioBytes, "audio/mpeg") // Specify the appropriate audio MIME type
.addText("Transcribe what's said in this audio recording.")
.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 text = result.getText();
Log.d(TAG, (text == null) ? "" : text);
}
@Override
public void onFailure(Throwable t) {
Log.e(TAG, "Failed to generate a response", t);
}
}, executor);
} else {
Log.e(TAG, "Error getting input stream for file.");
// Handle the error appropriately
}
} catch (IOException e) {
Log.e(TAG, "Failed to read the audio file", e);
} catch (URISyntaxException e) {
Log.e(TAG, "Invalid audio file", e);
}
वीडियो फ़ाइल देने के लिए, inlineData
कॉन्टेंट टाइप का इस्तेमाल जारी रखें:
Kotlin
val contentResolver = applicationContext.contentResolver
contentResolver.openInputStream(videoUri).use { stream ->
stream?.let {
val bytes = stream.readBytes()
val prompt = content {
inlineData(bytes, "video/mp4") // Specify the appropriate video MIME type
text("Describe the content of this video")
}
val response = model.generateContent(prompt)
}
}
Java
ContentResolver resolver = getApplicationContext().getContentResolver();
try (InputStream stream = resolver.openInputStream(videoUri)) {
File videoFile = new File(new URI(videoUri.toString()));
int videoSize = (int) videoFile.length();
byte[] videoBytes = new byte[videoSize];
if (stream != null) {
stream.read(videoBytes, 0, videoBytes.length);
stream.close();
// Provide a prompt that includes video specified earlier and text
Content prompt = new Content.Builder()
.addInlineData(videoBytes, "video/mp4")
.addText("Describe the content of this video")
.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);
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
इसी तरह, PDF (application/pdf
) और सादे टेक्स्ट (text/plain
) वाले दस्तावेज़ों को भी पैरामीटर के तौर पर उनके MIME टाइप के साथ पास किया जा सकता है.
एक से ज़्यादा बार की गई बातचीत
इसमें एक से ज़्यादा बार बातचीत करने की सुविधा भी उपलब्ध है. startChat()
फ़ंक्शन का इस्तेमाल करके, चैट शुरू करें. विकल्प के तौर पर, मॉडल को मैसेज का इतिहास दिया जा सकता है. इसके बाद, चैट मैसेज भेजने के लिए sendMessage()
फ़ंक्शन को कॉल करें.
Kotlin
val chat = model.startChat(
history = listOf(
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
content(role = "model") { text("Great to meet you. What would you like to know?") }
)
)
scope.launch {
val response = chat.sendMessage("How many paws are in my house?")
}
Java
Content.Builder userContentBuilder = new Content.Builder();
userContentBuilder.setRole("user");
userContentBuilder.addText("Hello, I have 2 dogs in my house.");
Content userContent = userContentBuilder.build();
Content.Builder modelContentBuilder = new Content.Builder();
modelContentBuilder.setRole("model");
modelContentBuilder.addText("Great to meet you. What would you like to know?");
Content modelContent = userContentBuilder.build();
List<Content> history = Arrays.asList(userContent, modelContent);
// Initialize the chat
ChatFutures chat = model.startChat(history);
// Create a new user message
Content.Builder messageBuilder = new Content.Builder();
messageBuilder.setRole("user");
messageBuilder.addText("How many paws are in my house?");
Content message = messageBuilder.build();
// Send the message
ListenableFuture<GenerateContentResponse> response = chat.sendMessage(message);
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);
ज़्यादा जानकारी के लिए, Firebase का दस्तावेज़ देखें.
अगले चरण
- GitHub पर, Android Quickstart Firebase सैंपल ऐप्लिकेशन और Android AI सैंपल कैटलॉग देखें.
- अपने ऐप्लिकेशन को प्रोडक्शन के लिए तैयार करें. इसमें, Firebase App Check सेट अप करना भी शामिल है. इससे, Gemini API को बिना अनुमति वाले क्लाइंट के गलत इस्तेमाल से बचाया जा सकता है.
- Firebase के दस्तावेज़ में, Firebase के एआई लॉजिक के बारे में ज़्यादा जानें.