تتيح ميزة تحديد مصدر المعلومات من خلال "بحث Google" ربط نموذج Gemini بمحتوى الويب في الوقت الفعلي، وهي تعمل مع جميع اللغات المتاحة. يتيح ذلك لـ Gemini تقديم إجابات أكثر دقة والاستشهاد بمصادر يمكن التحقّق منها تتجاوز تاريخ آخر تحديث لمعلوماته.
تساعدك عملية التأسيس على إنشاء تطبيقات يمكنها إجراء ما يلي:
- زيادة الدقة الواقعية: تقليل الهلوسة في النماذج من خلال استناد الردود إلى معلومات واقعية
- الوصول إلى معلومات في الوقت الفعلي: يمكنك الإجابة عن أسئلة حول الأحداث والمواضيع الحديثة.
توفير اقتباسات: يمكنك كسب ثقة المستخدمين من خلال عرض مصادر المعلومات التي يقدّمها النموذج.
Python
from google import genai
from google.genai import types
client = genai.Client()
grounding_tool = types.Tool(
google_search=types.GoogleSearch()
)
config = types.GenerateContentConfig(
tools=[grounding_tool]
)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Who won the euro 2024?",
config=config,
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const groundingTool = {
googleSearch: {},
};
const config = {
tools: [groundingTool],
};
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Who won the euro 2024?",
config,
});
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"
عند تفعيل أداة google_search، يتولّى النموذج تلقائيًا سير العمل الكامل المتمثل في البحث عن المعلومات ومعالجتها والاقتباس منها.

- طلب المستخدم: يرسل تطبيقك طلب المستخدم إلى Gemini API مع تفعيل أداة
google_search. - تحليل الطلب: يحلّل النموذج الطلب ويحدّد ما إذا كان بإمكان "بحث Google" تحسين الإجابة.
- بحث Google: إذا لزم الأمر، ينشئ النموذج تلقائيًا طلب بحث واحدًا أو أكثر وينفّذها.
- معالجة نتائج البحث: يعالج النموذج نتائج البحث، ويصنّف المعلومات، ويصوغ ردًا.
- الردّ المستند إلى معلومات موثوقة: تعرض واجهة برمجة التطبيقات ردًا نهائيًا سهل الاستخدام يستند إلى نتائج البحث. يتضمّن هذا الردّ الإجابة النصية التي قدّمها النموذج و
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: يحتوي على HTML وCSS لعرض "اقتراحات البحث" المطلوبة. يمكن الاطّلاع على متطلبات الاستخدام الكاملة في بنود الخدمة. -
groundingChunks: مصفوفة من العناصر التي تحتوي على مصادر الويب (uriوtitle). -
groundingSupports: مصفوفة من الأجزاء لربط ردّ النموذجtextبالمستندات المصدر فيgroundingChunks. يربط كل جزء نصًاsegment(محدّدًا بواسطةstartIndexوendIndex) بواحد أو أكثر منgroundingChunkIndices. هذا هو المفتاح لإنشاء استشهادات مضمّنة.
يمكن أيضًا استخدام ميزة "الاستناد إلى معلومات من بحث Google" مع أداة سياق عنوان URL للاستناد إلى بيانات الويب المتاحة للجميع وعناوين URL المحدّدة التي تقدّمها.
تحديد مصادر المحتوى من خلال اقتباسات مضمّنة
تعرض واجهة برمجة التطبيقات بيانات منظَّمة للاقتباسات، ما يمنحك تحكّمًا كاملاً في طريقة عرض المصادر في واجهة المستخدم. يمكنك استخدام الحقلَين 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:/...)
الأسعار
عند استخدام Grounding with Google Search، يتم تحصيل رسوم من مشروعك مقابل كل طلب من واجهة برمجة التطبيقات يتضمّن أداة google_search. إذا قرر النموذج تنفيذ طلبات بحث متعددة للإجابة عن طلب واحد (على سبيل المثال، البحث عن "UEFA Euro 2024 winner" و"Spain vs England Euro 2024 final score" ضمن طلب واحد من واجهة برمجة التطبيقات)، سيتم احتساب ذلك كاستخدام واحد للأداة قابل للفوترة لهذا الطلب.
للحصول على معلومات مفصّلة حول الأسعار، يُرجى الاطّلاع على صفحة أسعار Gemini API.
النماذج المتوافقة
لا يتم تضمين النماذج التجريبية ونماذج المعاينة. يمكنك الاطّلاع على إمكانات كل نموذج في صفحة نظرة عامة على النموذج.
| الطراز | تحديد المصادر باستخدام "بحث Google" |
|---|---|
| Gemini 2.5 Pro | ✔️ |
| Gemini 2.5 Flash | ✔️ |
| Gemini 2.5 Flash-Lite | ✔️ |
| Gemini 2.0 Flash | ✔️ |
| Gemini 1.5 Pro | ✔️ |
| Gemini 1.5 Flash | ✔️ |
مجموعات الأدوات المتوافقة
يمكنك استخدام ميزة "الاستناد إلى معلومات من بحث Google" مع أدوات أخرى، مثل تنفيذ الرموز البرمجية وسياق عنوان URL، لتنفيذ حالات استخدام أكثر تعقيدًا.
الاستناد إلى مصادر خارجية باستخدام نماذج Gemini 1.5 (إصدار قديم)
مع أنّ أداة google_search يُنصح باستخدامها مع الإصدار 2.0 من Gemini والإصدارات الأحدث، يتيح الإصدار 1.5 من Gemini استخدام أداة قديمة تُعرف باسم 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
}
}
}]
}'
الخطوات التالية
- جرِّب كتاب الطبخ الخاص بميزة "الاستناد إلى معلومات من بحث Google" في Gemini API.
- تعرَّف على الأدوات الأخرى المتاحة، مثل استدعاء الدوال.
- تعرَّف على كيفية تحسين الطلبات باستخدام عناوين URL محدّدة من خلال أداة سياق عنوان URL.