Gemini मॉडल से, दस्तावेज़ फ़ाइलों (जैसे, PDF और सादे टेक्स्ट वाली फ़ाइलें) का विश्लेषण करने के लिए कहा जा सकता है. ये फ़ाइलें, इनलाइन (base64-एन्कोडेड) या यूआरएल के ज़रिए दी जा सकती हैं. Firebase AI Logic का इस्तेमाल करके, सीधे अपने मोबाइल या वेब ऐप्लिकेशन से यह अनुरोध किया जा सकता है. जैसे, Apple (Swift), Android (Kotlin / Java), वेब (JavaScript), Flutter (Dart) या Unity ऐप्लिकेशन.
इस सुविधा की मदद से, ये काम किए जा सकते हैं:
- दस्तावेज़ों में मौजूद डायग्राम, चार्ट, और टेबल का विश्लेषण करना
- जानकारी को स्ट्रक्चर्ड आउटपुट फ़ॉर्मैट में एक्सट्रैक्ट करना
- दस्तावेज़ों में मौजूद विज़ुअल और टेक्स्ट कॉन्टेंट के बारे में सवालों के जवाब देना
- दस्तावेज़ों की खास जानकारी देना
- दस्तावेज़ के कॉन्टेंट को ट्रांसक्राइब करना (उदाहरण के लिए, एचटीएमएल में), लेआउट और फ़ॉर्मैटिंग को सुरक्षित रखना, ताकि डाउनस्ट्रीम ऐप्लिकेशन (जैसे कि आरएजी पाइपलाइन में) में इस्तेमाल किया जा सके
इस गाइड में, PDF जैसे दस्तावेज़ इनपुट से टेक्स्ट जनरेट करने के बारे में बताया गया है. हालांकि, दस्तावेज़ इनपुट से इमेज भी जनरेट की जा सकती हैं.
कोड सैंपल पर जाएं स्ट्रीम किए गए रिस्पॉन्स के लिए कोड पर जाएं
|
PDF जैसे दस्तावेज़ों के साथ काम करने के अन्य विकल्पों के बारे में जानने के लिए, अन्य गाइड देखें स्ट्रक्चर्ड आउटपुट जनरेट करना मल्टी-टर्न चैट |
शुरू करने से पहले
|
इस पेज पर, सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट और कोड देखने के लिए, Gemini API की सेवा देने वाली कंपनी पर क्लिक करें. |
अगर आपने अब तक शुरुआती निर्देश नहीं पढ़े हैं, तो उन्हें पढ़ें. इनमें Firebase प्रोजेक्ट सेट अप करने, अपने ऐप्लिकेशन को Firebase से कनेक्ट करने, और SDK टूल जोड़ने का तरीका बताया गया है. साथ ही, इनमें आपके चुने गए Gemini API प्रोवाइडर के लिए बैकएंड सेवा को शुरू करने और GenerativeModel इंस्टेंस बनाने का तरीका भी बताया गया है.
अपने प्रॉम्प्ट को टेस्ट करने और उनमें बदलाव करने के लिए, हमारा सुझाव है कि आप Google AI Studio का इस्तेमाल करें.
इस सुविधा के साथ काम करने वाले मॉडल
इस गाइड में, PDF जैसे दस्तावेज़ों से टेक्स्ट जनरेट करने के बारे में बताया गया है. यह गाइड, इन Gemini मॉडल पर लागू होती है:
gemini-4-hydrogengemini-4-argongemini-4-carbongemini-3.8-flashgemini-3.5-flash-lite(और इससे पुरानाgemini-3.1-flash-lite)
सामान्य इस्तेमाल के लिए उपलब्ध पुराने Gemini 3.x Flash मॉडल में यह सुविधा काम करती है. हालांकि, अब इनका इस्तेमाल करने का सुझाव नहीं दिया जाता. इसकी वजह यह है कि इन्हें बंद करने की तारीखें आने वाली हैं और/या नए मॉडल पर अपने-आप रूट होने की सुविधा उपलब्ध है.
PDF फ़ाइलों (base64-एन्कोडेड) से टेक्स्ट जनरेट करना
|
इस सैंपल को आज़माने से पहले, इस गाइड का
शुरू करने से पहले सेक्शन पूरा करें.
इससे आपको अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करने में मदद मिलेगी. इस सेक्शन में, आपको Gemini APIसेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे. |
Gemini मॉडल से जनरेट किया गया टेक्स्ट पाने के लिए कहा जा सकता है. इसके लिए, टेक्स्ट और PDF फ़ाइलें प्रॉम्प्ट के तौर पर दी जा सकती हैं. आपको हर इनपुट फ़ाइल का mimeType और फ़ाइल खुद देनी होगी. इस पेज पर आगे जाकर, इनपुट फ़ाइलों के लिए ज़रूरी शर्तें और सुझाव देखें.
Swift
टेक्स्ट और PDF के मल्टीमोडल इनपुट से जनरेट किया गया टेक्स्ट बनाने के लिए, generateContent() को कॉल किया जा सकता है.
import FirebaseAILogic
// 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-4-hydrogen")
// Provide the PDF as `Data` with the appropriate MIME type
let pdf = try InlineDataPart(data: Data(contentsOf: pdfURL), mimeType: "application/pdf")
// Provide a text prompt to include with the PDF file
let prompt = "Summarize the important results in this report."
// To generate text output, call `generateContent` with the PDF file and text prompt
let response = try await model.generateContent(pdf, prompt)
// Print the generated text, handling the case where it might be nil
print(response.text ?? "No text in response.")
Kotlin
टेक्स्ट और PDF के मल्टीमोडल इनपुट से जनरेट किया गया टेक्स्ट बनाने के लिए, 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-4-hydrogen")
val contentResolver = applicationContext.contentResolver
// Provide the URI for the PDF file you want to send to the model
val inputStream = contentResolver.openInputStream(pdfUri)
if (inputStream != null) { // Check if the PDF file loaded successfully
inputStream.use { stream ->
// Provide a prompt that includes the PDF file specified above and text
val prompt = content {
inlineData(
bytes = stream.readBytes(),
mimeType = "application/pdf" // Specify the appropriate PDF file MIME type
)
text("Summarize the important results in this report.")
}
// To generate text output, call `generateContent` with the prompt
val response = model.generateContent(prompt)
// Log the generated text, handling the case where it might be null
Log.d(TAG, response.text ?: "")
}
} else {
Log.e(TAG, "Error getting input stream for file.")
// Handle the error appropriately
}
Java
टेक्स्ट और PDF के मल्टीमोडल इनपुट से जनरेट किया गया टेक्स्ट बनाने के लिए, 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-4-hydrogen");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
ContentResolver resolver = getApplicationContext().getContentResolver();
// Provide the URI for the PDF file you want to send to the model
try (InputStream stream = resolver.openInputStream(pdfUri)) {
if (stream != null) {
byte[] audioBytes = stream.readAllBytes();
stream.close();
// Provide a prompt that includes the PDF file specified above and text
Content prompt = new Content.Builder()
.addInlineData(audioBytes, "application/pdf") // Specify the appropriate PDF file MIME type
.addText("Summarize the important results in this report.")
.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 pdf file", e);
} catch (URISyntaxException e) {
Log.e(TAG, "Invalid pdf file", e);
}
Web
टेक्स्ट और PDF के मल्टीमोडल इनपुट से जनरेट किया गया टेक्स्ट बनाने के लिए, 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-4-hydrogen" });
// 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(','));
reader.readAsDataURL(file);
});
return {
type: "inlineData",
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
};
}
async function run() {
// Provide a text prompt to include with the PDF file
const prompt = "Summarize the important results in this report.";
// Prepare PDF file for input
const fileInputEl = document.querySelector("input[type=file]");
const pdfPart = await fileToGenerativePart(fileInputEl.files);
// To generate text output, call `generateContent` with the text and PDF file
const result = await model.generateContent([prompt, pdfPart]);
// Log the generated text, handling the case where it might be undefined
console.log(result.response.text() ?? "No text in response.");
}
run();
Dart
टेक्स्ट और PDF के मल्टीमोडल इनपुट से टेक्स्ट जनरेट करने के लिए, 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-4-hydrogen');
// Provide a text prompt to include with the PDF file
final prompt = TextPart("Summarize the important results in this report.");
// Prepare the PDF file for input
final doc = await File('document0.pdf').readAsBytes();
// Provide the PDF file as `Data` with the appropriate PDF file MIME type
final docPart = InlineDataPart('application/pdf', doc);
// To generate text output, call `generateContent` with the text and PDF file
final response = await model.generateContent([
Content.multi([prompt,docPart])
]);
// Print the generated text
print(response.text);
Unity
टेक्स्ट और PDF के मल्टीमोडल इनपुट से जनरेट किया गया टेक्स्ट बनाने के लिए, 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-4-hydrogen");
// Provide a text prompt to include with the PDF file
var prompt = ModelContent.Text("Summarize the important results in this report.");
// Provide the PDF file as `data` with the appropriate PDF file MIME type
var doc = ModelContent.InlineData("application/pdf",
System.IO.File.ReadAllBytes(System.IO.Path.Combine(
UnityEngine.Application.streamingAssetsPath, "document0.pdf")));
// To generate text output, call `GenerateContentAsync` with the text and PDF file
var response = await model.GenerateContentAsync(new [] { prompt, doc });
// Print the generated text
UnityEngine.Debug.Log(response.Text ?? "No text in response.");
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के लिए, सही मॉडल चुनने का तरीका जानें.भी चुनी जा सकती है.
जवाब को स्ट्रीम करें
|
इस सैंपल को आज़माने से पहले, इस गाइड का
शुरू करने से पहले सेक्शन पूरा करें.
इससे आपको अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करने में मदद मिलेगी. इस सेक्शन में, आपको Gemini APIसेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे. |
मॉडल जनरेशन से पूरा नतीजा मिलने का इंतज़ार करने के बजाय, अधूरे नतीजों को हैंडल करने के लिए स्ट्रीमिंग का इस्तेमाल करके, तेज़ी से इंटरैक्शन किए जा सकते हैं.
जवाब को स्ट्रीम करने के लिए, generateContentStream को कॉल करें.
इनपुट दस्तावेज़ों के लिए ज़रूरी शर्तें और सुझाव
ध्यान दें कि इनलाइन डेटा के तौर पर दी गई फ़ाइल, ट्रांज़िट के दौरान base64 में एन्कोड की जाती है. इससे अनुरोध का साइज़ बढ़ जाता है. अगर अनुरोध बहुत बड़ा है, तो आपको एचटीटीपी 413 गड़बड़ी का मैसेज मिलता है.
इनके बारे में ज़्यादा जानने के लिए, "इस्तेमाल की जा सकने वाली इनपुट फ़ाइलें और ज़रूरी शर्तें" पेज देखें:
- अनुरोध में फ़ाइल उपलब्ध कराने के अलग-अलग विकल्प (इनलाइन या फ़ाइल के यूआरएल या यूआरआई का इस्तेमाल करके)
- दस्तावेज़ वाली फ़ाइलों के लिए ज़रूरी शर्तें और सबसे सही तरीके
दस्तावेज़ के लिए इस्तेमाल किए जा सकने वाले एमआईएमई टाइप
Gemini मल्टीमोडल मॉडल, इन दस्तावेज़ों के एमआईएमई टाइप के साथ काम करते हैं:
- PDF -
application/pdf - टेक्स्ट -
text/plain
हर अनुरोध के लिए सीमाएं
PDF को इमेज माना जाता है. इसलिए, PDF के एक पेज को एक इमेज माना जाता है. किसी प्रॉम्प्ट में उतने ही पेज शामिल किए जा सकते हैं जितनी इमेज Gemini मल्टीमोडल मॉडल में इस्तेमाल की जा सकती हैं.
- हर अनुरोध में ज़्यादा से ज़्यादा इतनी फ़ाइलें जोड़ी जा सकती हैं: 3,000 फ़ाइलें
- हर फ़ाइल में ज़्यादा से ज़्यादा पेज: हर फ़ाइल में 1,000 पेज
- हर फ़ाइल का ज़्यादा से ज़्यादा साइज़: 50 एमबी
तुम और क्या कर सकती हो?
- मॉडल को लंबे प्रॉम्प्ट भेजने से पहले, टोकन की गिनती करने का तरीक़ा जानें.
- Cloud Storage for Firebase सेट अप करें, ताकि मल्टीमोडल अनुरोधों में बड़ी फ़ाइलें शामिल की जा सकें. साथ ही, प्रॉम्प्ट में फ़ाइलें उपलब्ध कराने के लिए, बेहतर तरीके से मैनेज किया गया समाधान मिल सके. फ़ाइलों में इमेज, PDF, वीडियो, और ऑडियो शामिल हो सकते हैं.
-
प्रोडक्शन की तैयारी शुरू करें. इसके लिए, प्रोडक्शन चेकलिस्ट देखें:
- जितनी जल्दी हो सके Firebase App Check को लागू करें, ताकि Gemini API को बिना अनुमति वाले क्लाइंट के गलत इस्तेमाल से बचाया जा सके.
- Firebase Remote Config या सर्वर प्रॉम्प्ट टेंप्लेट का इस्तेमाल करें, ताकि आप अपने ऐप्लिकेशन का नया वर्शन रिलीज़ किए बिना, एआई की सुविधा (जैसे, मॉडल का नाम) के कॉन्फ़िगरेशन में मांग पर बदलाव कर सकें.
अन्य सुविधाएं आज़माना
- मल्टी-टर्न बातचीत (चैट) बनाएं.
- सिर्फ़ टेक्स्ट वाले प्रॉम्प्ट से टेक्स्ट जनरेट करना.
- टेक्स्ट और मल्टीमोडल प्रॉम्प्ट, दोनों से JSON जैसा स्ट्रक्चर्ड आउटपुट जनरेट करना.
- टेक्स्ट और टेक्स्ट, इमेज वग़ैरह का एक साथ इस्तेमाल करके प्रॉम्प्ट डालने की सुविधा की मदद से, इमेज जनरेट करना और उनमें बदलाव करना.
- Gemini लिखे गए शब्दों को सुनने की सुविधा (टीटीएस) वाले मॉडल का इस्तेमाल करके, स्पीच जनरेट करना (एक और कई स्पीकर, दोनों के लिए).
-
Gemini मॉडल को अपने ऐप्लिकेशन के अन्य हिस्सों, बाहरी सिस्टम, और जानकारी से कनेक्ट करने के लिए, फ़ंक्शन कॉलिंग और
Google Search याGoogle Maps के साथ ग्राउंडिंग जैसे टूल इस्तेमाल करें.
कॉन्टेंट जनरेट करने की सुविधा को कंट्रोल करने का तरीका जानें
- प्रॉम्प्ट डिज़ाइन करने के बारे में जानें. इसमें सबसे सही तरीके, रणनीतियां, और उदाहरण के तौर पर दिए गए प्रॉम्प्ट शामिल हैं.
- मॉडल के पैरामीटर कॉन्फ़िगर करें. जैसे, ज़्यादा से ज़्यादा आउटपुट टोकन, आउटपुट टोकन के दोहराए जाने की संभावना वगैरह.
- सुरक्षा सेटिंग का इस्तेमाल करें, ताकि नुकसान पहुंचाने वाले जवाब मिलने की संभावना को कम किया जा सके.
इस्तेमाल किए जा सकने वाले मॉडल के बारे में ज़्यादा जानें
अलग-अलग कामों के लिए उपलब्ध मॉडल, उनके कोटा, और कीमत के बारे में जानें.Firebase AI Logic के साथ अपने अनुभव के बारे में सुझाव/राय देना या शिकायत करना