«پایهگذاری» با
پایهگذاری با
- افزایش دقت واقعی: با اتکا به اطلاعات دنیای واقعی، توهمات مدل را کاهش دهید.
- دسترسی به اطلاعات همزمان: به سؤالات مربوط به رویدادها و موضوعات اخیر پاسخ دهید.
- ارائه منابع: با نشان دادن منابع ادعاهای مدل، اعتماد کاربر را جلب کنید یا به کاربران اجازه دهید سایتهای مرتبط را مرور کنند.
- تکمیل تکالیف پیچیدهتر: بازیابی آرتیفکتها و تصاویر مرتبط، ویدیوها، یا رسانههای دیگر برای کمک به تکالیف استدلال.
- بهبود پاسخهای مختص منطقه یا زبان: اطلاعات مختص منطقه را پیدا کنید یا در ترجمه دقیق محتوا کمک کنید.
مدلهای پشتیبانیشده
gemini-3.1-pro-preview-
gemini-3.8-flash(وgemini-3.7-flash،gemini-3.6-flash، وgemini-3.5-flashبزرگتر) -
gemini-3.5-flash-lite(وgemini-3.1-flash-liteقدیمیتر) -
gemini-3-pro-image(با نام مستعار «Nano Banana Pro») gemini-3.1-flash-image(با نام مستعار «Nano Banana 2»)
مدلهای Gemini 2.5 با کاربرد عمومی از این قابلیت پشتیبانی میکنند، اما همه آنها منسوخ شدهاند.
مدلهای Gemini Live API نیز از این قابلیت پشتیبانی میکنند، اما همه نمونههای کد در این راهنما برای مدلهای Gemini با کاربرد عمومی است.
زبانهای پشتیبانیشده
زبانهای پشتیبانیشده برای مدلهای Gemini را ببینید.
مدل را با Google Search زمینگیر کنید
|
روی ارائهدهنده Gemini API خود کلیک کنید تا محتوا و کد مخصوص ارائهدهنده را در این صفحه مشاهده کنید. |
وقتی نمونه GenerativeModel را ایجاد میکنید، GoogleSearch را بهعنوان
tool ارائه دهید تا مدل بتواند از آن برای تولید پاسخ خود استفاده کند.
Swift
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_MODEL_NAME",
// Provide Google Search as a tool that the model can use to generate its response.
tools: [Tool.googleSearch()]
)
let response = try await model.generateContent("Who won the euro 2024?")
print(response.text ?? "No text in response.")
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Kotlin
// 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(
modelName = "GEMINI_MODEL_NAME",
// Provide Google Search as a tool that the model can use to generate its response
tools = listOf(Tool.googleSearch())
)
val response = model.generateContent("Who won the euro 2024?")
print(response.text)
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Java
// 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_MODEL_NAME",
null,
null,
// Provide Google Search as a tool that the model can use to generate its response
List.of(Tool.GoogleSearch()));
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
ListenableFuture response = model.generateContent("Who won the euro 2024?");
Futures.addCallback(response, new FutureCallback() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Web
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_MODEL_NAME",
// Provide Google Search as a tool that the model can use to generate its response
tools: [{ googleSearch: {} }]
}
);
const result = await model.generateContent("Who won the euro 2024?");
console.log(result.response.text());
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Dart
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_ai/firebase_ai.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_MODEL_NAME',
// Provide Google Search as a tool that the model can use to generate its response.
tools: [
Tool.googleSearch(),
],
);
final response = await model.generateContent([Content.text("Who won the euro 2024?")]);
print(response.text);
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Unity
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_MODEL_NAME",
// Provide Google Search as a tool that the model can use to generate its response.
tools: new[] { new Tool(new GoogleSearch()) }
);
var response = await model.GenerateContentAsync("Who won the euro 2024?");
UnityEngine.Debug.Log(response.Text ?? "No text in response.");
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
با نحوه انتخاب مدل مناسب برای مورد استفاده و برنامه خود آشنا شوید.
نحوه عملکرد «پایهگذاری با Google Search »
وقتی از ابزار GoogleSearch استفاده میکنید، مدل کل گردش کار جستجو، پردازش، و استناد به اطلاعات را بهطور خودکار انجام میدهد.
گردش کار مدل به این صورت است:
- دریافت پیامواره: برنامه شما پیاموارهای به مدل Gemini
با ابزار
GoogleSearchفعال ارسال میکند. - تجزیهوتحلیل پیامواره: مدل پیامواره را تجزیهوتحلیل میکند و تعیین میکند که آیا
Google Search میتواند پاسخ خود را بهبود دهد یا نه. - ارسال پُرسمان به
Google Search : درصورت نیاز، مدل بهطور خودکار یک یا چند پُرسمان جستجو تولید میکند و آنها را اجرا میکند. - پردازش نتایج «جستجو»: مدل
Google Search نتیجه را پردازش میکند و به پیامواره اصلی پاسخ میدهد. - «نتیجه مستند» برگردانده میشود: مدل یک پاسخ نهایی و کاربرپسند برمیگرداند که براساس نتایج
Google Search مستند شده است. این پاسخ شامل پاسخ نوشتاری مدل وgroundingMetadataبا پُرسمانهای جستجو، نتایج وب، و منابع است.
توجه داشته باشید که ارائه groundingMetadata
شیء نخواهد بود و بنابراین نتیجه زمینهای نیست.

فهمیدن نتیجه مستند
اگر مدل پاسخ خود را براساس groundingMetadata است که حاوی دادههای ساختاری است
که برای درستیسنجی ادعاها و ایجاد یک تجربه منبع غنی در
برنامه شما ضروری است.
شیء groundingMetadata در «نتیجه مستند» حاوی اطلاعات زیر است:
webSearchQueries: آرایهای از پرسمانهای جستجو که بهGoogle Search ارسال شده است. این اطلاعات برای اشکالزدایی و درک فرایند استدلال مدل مفید است.searchEntryPoint: حاوی HTML و CSS برای پرداز کردن «Google Search پیشنهاد» موردنیاز است. ملزم هستید از الزامات استفاده «استناد بهGoogle Search » برای ارائهدهنده API انتخابیتان پیروی کنید: Gemini Developer API یا Agent Platform Gemini API (formerly Vertex AI) (بخش شرایط خدمات در «شرایط ویژه خدمات» را ببینید). در ادامه این صفحه، با نحوه استفاده و نمایش نتیجه زمینهای آشنا شوید.
groundingChunks: آرایهای از اشیایی که حاوی منابع وب است (uriوtitle).groundingSupports: آرایهای از تکهها برای اتصال پاسخ مدلtextبه منابع درgroundingChunks. هر بخش یک نوشتارsegment(تعریفشده توسطstartIndexوendIndex) را به یک یا چندgroundingChunkIndicesپیوند میدهد. این فیلد به شما کمک میکند پیوندهای منبع درونخطی بسازید. در ادامه این صفحه، با نحوه استفاده و نمایش نتیجه زمینهای آشنا شوید.
در اینجا نمونهای از پاسخ آورده شده است که شامل 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]
}
]
}
}
]
}
استفاده و نمایش نتیجه زمینهای
اگر مدل از ابزار groundingMetadata شیء ارائه خواهد کرد.
برای نمایش
پیشنهادهای
علاوهبر رعایت الزامات استفاده از ابزار
(الزامی) نمایش Google Search پیشنهاد
اگر پاسخی حاوی «
شیء groundingMetadata حاوی «searchEntryPoint که فیلد renderedContent دارد
و سبکبندی HTML و CSS سازگار ارائه میدهد که باید برای
نمایش پیشنهادهای «جستجو» در برنامهتان پیادهسازی کنید.
اطلاعات دقیق درباره
الزامات نمایش و رفتار برای پیشنهادهای
در ادامه این بخش، نمونههای کد نمونه را ببینید.
(الزامی) نمایش منابع
هدف groundingMetadata حاوی دادههای منبع ساختاریافته، بهویژه
فیلدهای groundingSupports و groundingChunks است. از این اطلاعات برای پیوند دادن اظهارات مدل مستقیماً به منابع آنها در میانای کاربریتان (درونخطی و انبوه) استفاده کنید.
در ادامه این بخش، نمونههای کد نمونه را ببینید.
نمونههای کد نمونه
این نمونههای کد الگوهای تعمیمیافتهای برای استفاده و نمایش نتیجه زمینهای ارائه میدهند. بااینحال، مسئولیت شما است که مطمئن شوید پیادهسازی خاص شما با الزامات رعایت استانداردها مطابقت دارد.
Swift
// ...
// Get the model's response
let text = response.text
// Get the grounding metadata
if let candidate = response.candidates.first,
let groundingMetadata = candidate.groundingMetadata {
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
if let renderedContent = groundingMetadata.searchEntryPoint?.renderedContent {
// TODO(developer): Display Google Search suggestions using a WebView
}
// REQUIRED - display sources
let groundingChunks = groundingMetadata.groundingChunks
for chunk in groundingMetadata.groundingChunks {
if let web = chunk.web {
let title = web.title // for example, "uefa.com"
let uri = web.uri // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show source in the UI
}
}
}
Kotlin
// ...
// Get the model's response
val text = response.text
// Get the grounding metadata
val groundingMetadata = response.candidates.firstOrNull()?.groundingMetadata
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
val renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent
if (renderedContent != null) {
// TODO(developer): Display Google Search suggestions using a WebView
}
// REQUIRED - display sources
val groundingChunks = groundingMetadata?.groundingChunks
groundingChunks?.let { chunks ->
for (chunk in chunks) {
val title = chunk.web?.title // for example, "uefa.com"
val uri = chunk.web?.uri // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show source in the UI
}
}
Java
// ...
Futures.addCallback(response, new FutureCallback() {
@Override
public void onSuccess(GenerateContentResponse result) {
// Get the model's response
String text = result.getText();
// Get the grounding metadata
GroundingMetadata groundingMetadata =
result.getCandidates()[0].getGroundingMetadata();
if (groundingMetadata != null) {
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
String renderedContent =
groundingMetadata.getSearchEntryPoint().getRenderedContent();
if (renderedContent != null) {
// TODO(developer): Display Google Search suggestions using a WebView
}
// REQUIRED - display sources
List chunks = groundingMetadata.getGroundingChunks();
if (chunks != null) {
for(GroundingChunk chunk : chunks) {
WebGroundingChunk web = chunk.getWeb();
if (web != null) {
String title = web.getTitle(); // for example, "uefa.com"
String uri = web.getUri(); // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show sources in the UI
}
}
}
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
// ...
// Get the model's text response
const text = result.response.text();
// Get the grounding metadata
const groundingMetadata = result.response.candidates?.[0]?.groundingMetadata;
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
const renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent;
if (renderedContent) {
// TODO(developer): render this HTML and CSS in the UI
}
// REQUIRED - display sources
const groundingChunks = groundingMetadata?.groundingChunks;
if (groundingChunks) {
for (const chunk of groundingChunks) {
const title = chunk.web?.title; // for example, "uefa.com"
const uri = chunk.web?.uri; // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show sources in the UI
}
}
Dart
// ...
// Get the model's response
final text = response.text;
// Get the grounding metadata
final groundingMetadata = response.candidates.first.groundingMetadata;
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
final renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent;
if (renderedContent != null) {
// TODO(developer): Display Google Search suggestions using a WebView
}
// REQUIRED - display sources
final groundingChunks = groundingMetadata?.groundingChunks;
if (groundingChunks != null) {
for (var chunk in groundingChunks) {
final title = chunk.web?.title; // for example, "uefa.com"
final uri = chunk.web?.uri; // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show sources in the UI
}
}
Unity
// ...
// Get the model's response.
var text = response.Text;
// Get the grounding metadata.
var groundingMetadata = response.Candidates.First().GroundingMetadata.Value;
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
if (groundingMetadata.SearchEntryPoint.HasValue) {
var renderedContent = groundingMetadata.SearchEntryPoint.Value.RenderedContent;
// TODO(developer): Display Google Search suggestions using a WebView
}
// REQUIRED - display sources
foreach(GroundingChunk chunk in groundingMetadata.GroundingChunks) {
var title = chunk.Web.Value.Title; // for example, "uefa.com"
var uri = chunk.Web.Value.Uri; // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show sources in the UI
}
نتایج مستند و پایش هوش مصنوعی در کنسول Firebase
اگر پایش هوش مصنوعی را در کنسول Firebase فعال کرده باشید، پاسخها در Cloud Logging ذخیره میشوند. بهطور پیشفرض، این دادهها ۳۰ روز دوره نگهداری دارند.
این مسئولیت شما است که مطمئن شوید این دوره نگهداری، یا هر دوره سفارشی که تنظیم میکنید، کاملاً با مورد استفاده خاص شما و هرگونه الزامات انطباق اضافی برای ارائهدهنده انتخابی Gemini API شما مطابقت دارد: Gemini Developer API یا Agent Platform Gemini API (formerly Vertex AI) (بخش شرایط خدمات در «شرایط خاص سرویس» را ببینید). برای برآورده کردن این الزامات، ممکن است لازم باشد دوره نگهداری را در Cloud Logging تغییر دهید.
قیمتگذاری و محدودیتها
حتماً قیمتگذاری، دردسترس بودن مدل، و محدودیتهای «پایهگذاری با»