Gemini मॉडल से, दस्तावेज़ वाली फ़ाइलों (जैसे, PDF और सादे टेक्स्ट वाली फ़ाइलें) का विश्लेषण करने के लिए कहा जा सकता है. इसके लिए, आपको इन फ़ाइलों को इनलाइन (base64-एन्कोड की गई) या यूआरएल के ज़रिए उपलब्ध कराना होगा. जब आप Firebase AI Logic, का इस्तेमाल करते हैं, तो अपने ऐप्लिकेशन से सीधे यह अनुरोध किया जा सकता है.
इस सुविधा की मदद से, ये काम किए जा सकते हैं:
- दस्तावेज़ों में मौजूद डायग्राम, चार्ट, और टेबल का विश्लेषण करना
- जानकारी को स्ट्रक्चर्ड आउटपुट फ़ॉर्मैट में निकालना
- दस्तावेज़ों में मौजूद विज़ुअल और टेक्स्ट कॉन्टेंट के बारे में सवालों के जवाब देना
- दस्तावेज़ों की खास जानकारी देना
- दस्तावेज़ के कॉन्टेंट को ट्रांसक्रिप्ट करना. उदाहरण के लिए, एचटीएमएल में. ऐसा करते समय, लेआउट और फ़ॉर्मैटिंग को बनाए रखना, ताकि डाउनस्ट्रीम ऐप्लिकेशन (जैसे, RAG पाइपलाइन) में इसका इस्तेमाल किया जा सके
इस गाइड में, दस्तावेज़ के इनपुट (जैसे, PDF) से टेक्स्ट जनरेट करने के बारे में बताया गया है. हालांकि, दस्तावेज़ के इनपुट से इमेज भी जनरेट की जा सकती हैं.
कोड के सैंपल पर जाएं स्ट्रीम किए गए जवाबों के लिए कोड पर जाएं
|
दस्तावेज़ों (जैसे, PDF) के साथ काम करने के अन्य विकल्पों के बारे में जानने के लिए, अन्य गाइड देखें स्ट्रक्चर्ड आउटपुट जनरेट करना सिलसिलेवार बातचीत |
शुरू करने से पहले
|
इस पेज पर, Gemini API के अपने चुने गए प्रोवाइडर के हिसाब से कॉन्टेंट और कोड देखने के लिए, उस पर क्लिक करें. |
अगर आपने अब तक, शुरू करने के लिए
गाइड में दिए गए निर्देशों का पालन नहीं किया है, तो उन्हें पूरा करें. इसमें बताया गया है कि Firebase प्रोजेक्ट को कैसे
सेट अप करें, अपने ऐप्लिकेशन को Firebase से कैसे कनेक्ट करें, एसडीके टूल कैसे जोड़ें,
अपने चुने गए Gemini API प्रोवाइडर के लिए बैकएंड सेवा को कैसे शुरू करें, और
GenerativeModel इंस्टेंस कैसे बनाएं.
हम आपको Google AI Studio का इस्तेमाल करने का सुझाव देते हैं, ताकि आप अपने प्रॉम्प्ट की जांच कर सकें और उनमें बदलाव कर सकें. Google AI Studio.
इस सुविधा के साथ काम करने वाले मॉडल
इस गाइड में, दस्तावेज़ के इनपुट (जैसे, PDF) से टेक्स्ट जनरेट करने के बारे में बताया गया है. यह सुविधा, इन Gemini मॉडल के लिए उपलब्ध है:
gemini-3.1-pro-previewgemini-3.6-flash(और पुरानाgemini-3.5-flash)gemini-3.5-flash-lite(और पुरानाgemini-3.1-flash-lite)
आम तौर पर इस्तेमाल किए जाने वाले Gemini 2.5 मॉडल, इस सुविधा के साथ काम करते हैं. हालांकि, इन सभी मॉडल को बंद कर दिया गया है.
base64-एन्कोड की गई PDF फ़ाइलों से टेक्स्ट जनरेट करना
|
इस सैंपल को आज़माने से पहले, अपने प्रोजेक्ट और ऐप्लिकेशन को सेट अप करने के लिए, इस गाइड का
शुरू करने से पहले सेक्शन पूरा करें. उस सेक्शन में, आपको अपने चुने हुए 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-3.6-flash")
// 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-3.6-flash")
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-3.6-flash");
// 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-3.6-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(','));
reader.readAsDataURL(file);
});
return {
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-3.6-flash');
// 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-3.6-flash");
// 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 गड़बड़ी मिलती है.
इनके बारे में ज़्यादा जानकारी पाने के लिए, "इनपुट के तौर पर इस्तेमाल की जा सकने वाली फ़ाइलें और ज़रूरी शर्तें" पेज देखें:
- अनुरोध में फ़ाइल उपलब्ध कराने के अलग-अलग विकल्प . जैसे, इनलाइन या फ़ाइल के यूआरएल या यूआरआई का इस्तेमाल करना
- दस्तावेज़ वाली फ़ाइलों के लिए ज़रूरी शर्तें और सबसे सही तरीके
दस्तावेज़ों के लिए इस्तेमाल किए जा सकने वाले MIME टाइप
Gemini के मल्टीमोडल मॉडल, दस्तावेज़ों के इन MIME टाइप के साथ काम करते हैं:
- PDF -
application/pdf - टेक्स्ट -
text/plain
हर अनुरोध के लिए सीमाएं
PDF को इमेज माना जाता है. इसलिए, PDF का एक पेज, एक इमेज माना जाता है. किसी प्रॉम्प्ट में इस्तेमाल किए जा सकने वाले पेजों की संख्या, इमेज की संख्या तक सीमित होती है, जिनके साथ Gemini मल्टीमोडल मॉडल काम कर सकते हैं.
- हर अनुरोध में ज़्यादा से ज़्यादा इतनी फ़ाइलें इस्तेमाल की जा सकती हैं: 3,000 फ़ाइलें
- हर फ़ाइल में ज़्यादा से ज़्यादा इतने पेज इस्तेमाल किए जा सकते हैं: 1,000 पेज
- हर फ़ाइल का ज़्यादा से ज़्यादा साइज़: 50 एमबी
तुम और क्या कर सकती हो?
- मॉडल को लंबे प्रॉम्प्ट भेजने से पहले, टोकन की गिनती करने का तरीका जानें.
- सेट अप करें Cloud Storage for Firebase ताकि मल्टीमोडल अनुरोधों में बड़ी फ़ाइलें शामिल की जा सकें. साथ ही, प्रॉम्प्ट में फ़ाइलें उपलब्ध कराने के लिए, बेहतर तरीके से मैनेज किया गया समाधान मिल सके. फ़ाइलों में इमेज, PDF, वीडियो, और ऑडियो शामिल हो सकते हैं.
-
प्रोडक्शन के लिए तैयारी करने के बारे में सोचें. इसके लिए, प्रोडक्शन की चेकलिस्ट देखें:
- अनधिकृत क्लाइंट से Gemini API को गलत तरीके से इस्तेमाल होने से बचाने के लिए, Firebase App Check जितनी जल्दी हो सके सेट अप करें.
- अपने ऐप्लिकेशन में वैल्यू (जैसे, मॉडल का नाम) अपडेट करने के लिए, को इंटिग्रेट करें. इसके लिए, ऐप्लिकेशन का नया वर्शन रिलीज़ करने की ज़रूरत नहीं है.Firebase Remote Config
अन्य सुविधाओं को आज़माएं
- सिलसिलेवार बातचीत (चैट) की सुविधा बनाएं.
- सिर्फ़ टेक्स्ट वाले प्रॉम्प्ट से टेक्स्ट जनरेट करें.
- टेक्स्ट और मल्टीमोडल, दोनों तरह के प्रॉम्प्ट से स्ट्रक्चर्ड आउटपुट (जैसे, JSON) जनरेट करें.
- टेक्स्ट और मल्टीमॉडल, दोनों तरह के प्रॉम्प्ट से इमेज जनरेट करें और उनमें बदलाव करें.
- Gemini मॉडल को अपने ऐप्लिकेशन के अन्य हिस्सों और बाहरी सिस्टम और जानकारी से कनेक्ट करने के लिए, टूल (जैसे, फ़ंक्शन कॉलिंग और Google Search के साथ ग्राउंडिंग) का इस्तेमाल करें.Gemini
कॉन्टेंट जनरेट करने की सुविधा को कंट्रोल करने का तरीका जानें
- प्रॉम्प्ट डिज़ाइन को समझें. इसमें सबसे सही तरीके, रणनीतियां, और प्रॉम्प्ट के उदाहरण शामिल हैं.
- मॉडल के पैरामीटर कॉन्फ़िगर करें. जैसे, ज़्यादा से ज़्यादा आउटपुट टोकन, बार-बार इस्तेमाल होने वाले आउटपुट टोकन की संभावना वगैरह.
- सुरक्षा सेटिंग का इस्तेमाल करके, ऐसे जवाब मिलने की संभावना को कम करें जिन्हें नुकसान पहुंचाने वाला माना जा सकता है.
इस्तेमाल किए जा सकने वाले मॉडल के बारे में ज़्यादा जानें
अलग-अलग इस्तेमाल के उदाहरणों के लिए उपलब्ध मॉडल , उनके कोटे और कीमत के बारे में जानें.सुझाव/राय देना या शिकायत करना के बारे में अपने अनुभव के साथ Firebase AI Logic