Google Search के साथ ग्राउंडिंग की सुविधा, Gemini मॉडल को रीयल-टाइम में वेब कॉन्टेंट से कनेक्ट करती है. यह उपलब्ध सभी भाषाओं में काम करती है. इससे Gemini को ज़्यादा सटीक जवाब देने में मदद मिलती है. साथ ही, वह अपनी जानकारी के कटऑफ़ के बाद के ऐसे सोर्स का हवाला दे पाता है जिनकी पुष्टि की जा सकती है.
ग्राउंडिंग की मदद से, ऐसे ऐप्लिकेशन बनाए जा सकते हैं जो ये काम कर सकते हैं:
- जवाब में तथ्यों को ज़्यादा सटीक तरीके से शामिल करना: मॉडल के भ्रम को कम करें. इसके लिए, जवाबों को असल दुनिया की जानकारी के आधार पर तैयार करें.
- रीयल-टाइम में जानकारी ऐक्सेस करना: हाल की घटनाओं और विषयों के बारे में सवालों के जवाब पाना.
साइटेशन दें: मॉडल के दावों के सोर्स दिखाकर, लोगों का भरोसा जीतें.
Python
from google import genai
from google.genai import types
# Configure the client
client = genai.Client()
# Define the grounding tool
grounding_tool = types.Tool(
google_search=types.GoogleSearch()
)
# Configure generation settings
config = types.GenerateContentConfig(
tools=[grounding_tool]
)
# Make the request
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Who won the euro 2024?",
config=config,
)
# Print the grounded response
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
// Configure the client
const ai = new GoogleGenAI({});
// Define the grounding tool
const groundingTool = {
googleSearch: {},
};
// Configure generation settings
const config = {
tools: [groundingTool],
};
// Make the request
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Who won the euro 2024?",
config,
});
// Print the grounded response
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"contents": [
{
"parts": [
{"text": "Who won the euro 2024?"}
]
}
],
"tools": [
{
"google_search": {}
}
]
}'
खोज टूल नोटबुक का इस्तेमाल करके, इस बारे में ज़्यादा जानें.
Google Search की मदद से जवाब पाने की सुविधा कैसे काम करती है
google_search
टूल को चालू करने पर, मॉडल अपने-आप जानकारी खोजने, प्रोसेस करने, और उसका हवाला देने का पूरा वर्कफ़्लो मैनेज करता है.
- उपयोगकर्ता का प्रॉम्प्ट: आपका ऐप्लिकेशन, उपयोगकर्ता के प्रॉम्प्ट को Gemini API पर भेजता है. इसके लिए,
google_search
टूल चालू होना चाहिए. - प्रॉम्प्ट का विश्लेषण: मॉडल, प्रॉम्प्ट का विश्लेषण करता है. इससे यह तय किया जाता है कि Google Search से जवाब को बेहतर बनाया जा सकता है या नहीं.
- Google Search: अगर ज़रूरत होती है, तो मॉडल अपने-आप एक या उससे ज़्यादा सर्च क्वेरी जनरेट करता है और उन्हें पूरा करता है.
- खोज के नतीजों को प्रोसेस करना: मॉडल, खोज के नतीजों को प्रोसेस करता है, जानकारी को इकट्ठा करता है, और जवाब तैयार करता है.
- भरोसेमंद स्रोतों से मिली जानकारी के आधार पर जवाब देना: एपीआई, खोज के नतीजों के आधार पर उपयोगकर्ता को ऐसा जवाब देता है जो उसके लिए फ़ायदेमंद हो. इस जवाब में, मॉडल का टेक्स्ट वाला जवाब और
groundingMetadata
शामिल है. इसमें खोज क्वेरी, वेब नतीजे, और उद्धरण शामिल होते हैं.
जवाब के लिए भरोसेमंद स्रोत से जानकारी लेने के बारे में समझना
जब किसी जवाब में भरोसेमंद सोर्स से जानकारी शामिल की जाती है, तो उस जवाब में groundingMetadata
फ़ील्ड शामिल होता है. दावों की पुष्टि करने और अपने ऐप्लिकेशन में उद्धरणों को ज़्यादा बेहतर तरीके से दिखाने के लिए, यह स्ट्रक्चर्ड डेटा ज़रूरी है.
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Spain won Euro 2024, defeating England 2-1 in the final. This victory marks Spain's record fourth European Championship title."
}
],
"role": "model"
},
"groundingMetadata": {
"webSearchQueries": [
"UEFA Euro 2024 winner",
"who won euro 2024"
],
"searchEntryPoint": {
"renderedContent": "<!-- HTML and CSS for the search widget -->"
},
"groundingChunks": [
{"web": {"uri": "https://vertexaisearch.cloud.google.com.....", "title": "aljazeera.com"}},
{"web": {"uri": "https://vertexaisearch.cloud.google.com.....", "title": "uefa.com"}}
],
"groundingSupports": [
{
"segment": {"startIndex": 0, "endIndex": 85, "text": "Spain won Euro 2024, defeatin..."},
"groundingChunkIndices": [0]
},
{
"segment": {"startIndex": 86, "endIndex": 210, "text": "This victory marks Spain's..."},
"groundingChunkIndices": [0, 1]
}
]
}
}
]
}
Gemini API, groundingMetadata
के साथ यह जानकारी दिखाता है:
webSearchQueries
: इस्तेमाल की गई खोज क्वेरी का कलेक्शन. यह मॉडल, डीबग करने और मॉडल की तर्क प्रक्रिया को समझने के लिए उपयोगी है.searchEntryPoint
: इसमें खोज के सुझाव दिखाने के लिए ज़रूरी एचटीएमएल और सीएसएस शामिल होती है. इस्तेमाल से जुड़ी सभी ज़रूरी शर्तों के बारे में, सेवा की शर्तों में बताया गया है.groundingChunks
: यह ऑब्जेक्ट का ऐसा कलेक्शन है जिसमें वेब सोर्स (uri
औरtitle
) शामिल होते हैं.groundingSupports
: यह चंक का ऐसा कलेक्शन है जो मॉडल के जवाबtext
कोgroundingChunks
में मौजूद सोर्स से कनेक्ट करता है. हर चंक, टेक्स्टsegment
को एक या उससे ज़्यादाgroundingChunkIndices
से लिंक करता है. इस टेक्स्ट कोstartIndex
औरendIndex
से तय किया जाता है. इनलाइन उद्धरण बनाने के लिए, यह ज़रूरी है.
Google Search से मिली जानकारी का इस्तेमाल, यूआरएल के कॉन्टेक्स्ट वाले टूल के साथ भी किया जा सकता है. इससे, जवाबों को सार्वजनिक वेब डेटा और आपके दिए गए यूआरएल, दोनों के हिसाब से तैयार किया जा सकता है.
इनलाइन उद्धरणों की मदद से सोर्स एट्रिब्यूट करना
यह एपीआई, स्ट्रक्चर्ड उद्धरण डेटा दिखाता है. इससे आपको यह तय करने का पूरा कंट्रोल मिलता है कि आपको अपने यूज़र इंटरफ़ेस में सोर्स कैसे दिखाने हैं. groundingSupports
और groundingChunks
फ़ील्ड का इस्तेमाल करके, मॉडल के जवाबों को सीधे तौर पर उनके सोर्स से लिंक किया जा सकता है. यहां मेटाडेटा को प्रोसेस करने का सामान्य तरीका बताया गया है, ताकि इनलाइन और क्लिक किए जा सकने वाले उद्धरणों के साथ जवाब तैयार किया जा सके.
Python
def add_citations(response):
text = response.text
supports = response.candidates[0].grounding_metadata.grounding_supports
chunks = response.candidates[0].grounding_metadata.grounding_chunks
# Sort supports by end_index in descending order to avoid shifting issues when inserting.
sorted_supports = sorted(supports, key=lambda s: s.segment.end_index, reverse=True)
for support in sorted_supports:
end_index = support.segment.end_index
if support.grounding_chunk_indices:
# Create citation string like [1](link1)[2](link2)
citation_links = []
for i in support.grounding_chunk_indices:
if i < len(chunks):
uri = chunks[i].web.uri
citation_links.append(f"[{i + 1}]({uri})")
citation_string = ", ".join(citation_links)
text = text[:end_index] + citation_string + text[end_index:]
return text
# Assuming response with grounding metadata
text_with_citations = add_citations(response)
print(text_with_citations)
JavaScript
function addCitations(response) {
let text = response.text;
const supports = response.candidates[0]?.groundingMetadata?.groundingSupports;
const chunks = response.candidates[0]?.groundingMetadata?.groundingChunks;
// Sort supports by end_index in descending order to avoid shifting issues when inserting.
const sortedSupports = [...supports].sort(
(a, b) => (b.segment?.endIndex ?? 0) - (a.segment?.endIndex ?? 0),
);
for (const support of sortedSupports) {
const endIndex = support.segment?.endIndex;
if (endIndex === undefined || !support.groundingChunkIndices?.length) {
continue;
}
const citationLinks = support.groundingChunkIndices
.map(i => {
const uri = chunks[i]?.web?.uri;
if (uri) {
return `[${i + 1}](${uri})`;
}
return null;
})
.filter(Boolean);
if (citationLinks.length > 0) {
const citationString = citationLinks.join(", ");
text = text.slice(0, endIndex) + citationString + text.slice(endIndex);
}
}
return text;
}
const textWithCitations = addCitations(response);
console.log(textWithCitations);
इनलाइन उद्धरणों के साथ नया जवाब इस तरह दिखेगा:
Spain won Euro 2024, defeating England 2-1 in the final.[1](https:/...), [2](https:/...), [4](https:/...), [5](https:/...) This victory marks Spain's record-breaking fourth European Championship title.[5]((https:/...), [2](https:/...), [3](https:/...), [4](https:/...)
कीमत
Google Search के साथ ग्राउंडिंग की सुविधा का इस्तेमाल करने पर, आपके प्रोजेक्ट से हर एपीआई अनुरोध के लिए शुल्क लिया जाता है. इस अनुरोध में google_search
टूल शामिल होता है. अगर मॉडल किसी एक प्रॉम्प्ट का जवाब देने के लिए, एक से ज़्यादा खोज क्वेरी चलाने का फ़ैसला करता है (उदाहरण के लिए, एक ही एपीआई कॉल में "UEFA Euro 2024 winner"
और "Spain vs England Euro 2024 final score"
खोजना), तो इसे उस अनुरोध के लिए, टूल के बिल किए जाने वाले इस्तेमाल के तौर पर गिना जाता है.
शुल्क के बारे में ज़्यादा जानकारी के लिए, Gemini API के शुल्क वाला पेज देखें.
काम करने वाले मॉडल
इसमें एक्सपेरिमेंट के तौर पर उपलब्ध मॉडल और झलक देखने के लिए उपलब्ध मॉडल शामिल नहीं हैं. इनकी क्षमताओं के बारे में जानने के लिए, मॉडल की खास जानकारी पेज पर जाएं.
मॉडल | Google Search से जानकारी पाना |
---|---|
Gemini 2.5 Pro | ✔️ |
Gemini 2.5 Flash | ✔️ |
Gemini 2.0 Flash | ✔️ |
Gemini 1.5 Pro | ✔️ |
Gemini 1.5 Flash | ✔️ |
Gemini 1.5 मॉडल के साथ ग्राउंडिंग (लेगसी)
google_search
टूल का इस्तेमाल, Gemini 2.0 और इसके बाद के वर्शन के लिए किया जा सकता है. हालांकि, Gemini 1.5 में google_search_retrieval
नाम का लेगसी टूल इस्तेमाल किया जा सकता है. इस टूल में dynamic
मोड उपलब्ध है. इससे मॉडल यह तय कर पाता है कि उसे खोज करनी है या नहीं. यह इस बात पर निर्भर करता है कि मॉडल को लगता है कि प्रॉम्प्ट में नई जानकारी की ज़रूरत है या नहीं. अगर मॉडल का कॉन्फ़िडेंस, आपकी सेट की गई dynamic_threshold
(0.0 और 1.0 के बीच की वैल्यू) से ज़्यादा है, तो वह खोज करेगा.
Python
# Note: This is a legacy approach for Gemini 1.5 models.
# The 'google_search' tool is recommended for all new development.
import os
from google import genai
from google.genai import types
client = genai.Client()
retrieval_tool = types.Tool(
google_search_retrieval=types.GoogleSearchRetrieval(
dynamic_retrieval_config=types.DynamicRetrievalConfig(
mode=types.DynamicRetrievalConfigMode.MODE_DYNAMIC,
dynamic_threshold=0.7 # Only search if confidence > 70%
)
)
)
config = types.GenerateContentConfig(
tools=[retrieval_tool]
)
response = client.models.generate_content(
model='gemini-1.5-flash',
contents="Who won the euro 2024?",
config=config,
)
print(response.text)
if not response.candidates[0].grounding_metadata:
print("\nModel answered from its own knowledge.")
JavaScript
// Note: This is a legacy approach for Gemini 1.5 models.
// The 'googleSearch' tool is recommended for all new development.
import { GoogleGenAI, DynamicRetrievalConfigMode } from "@google/genai";
const ai = new GoogleGenAI({});
const retrievalTool = {
googleSearchRetrieval: {
dynamicRetrievalConfig: {
mode: DynamicRetrievalConfigMode.MODE_DYNAMIC,
dynamicThreshold: 0.7, // Only search if confidence > 70%
},
},
};
const config = {
tools: [retrievalTool],
};
const response = await ai.models.generateContent({
model: "gemini-1.5-flash",
contents: "Who won the euro 2024?",
config,
});
console.log(response.text);
if (!response.candidates?.[0]?.groundingMetadata) {
console.log("\nModel answered from its own knowledge.");
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"contents": [
{"parts": [{"text": "Who won the euro 2024?"}]}
],
"tools": [{
"google_search_retrieval": {
"dynamic_retrieval_config": {
"mode": "MODE_DYNAMIC",
"dynamic_threshold": 0.7
}
}
}]
}'
आगे क्या करना है
- Gemini API की कुकबुक में, Google Search की मदद से जानकारी को भरोसेमंद बनाने की सुविधा आज़माएं.
- उपलब्ध अन्य टूल के बारे में जानें. जैसे, फ़ंक्शन कॉलिंग.
- यूआरएल के कॉन्टेक्स्ट वाले टूल का इस्तेमाल करके, प्रॉम्प्ट में यूआरएल जोड़ने का तरीका जानें.