Gemini में, पहले से मौजूद टूल, जैसे कि google_search, और फ़ंक्शन कॉलिंग (इसे कस्टम टूल भी कहा जाता है) को एक साथ इस्तेमाल किया जा सकता है. इसके लिए, टूल कॉल के कॉन्टेक्स्ट इतिहास को सेव किया जाता है और उसे दिखाया जाता है. पहले से मौजूद और कस्टम टूल के कॉम्बिनेशन की मदद से, जटिल और एजेंटिक वर्कफ़्लो बनाए जा सकते हैं. उदाहरण के लिए, मॉडल आपके कारोबार के लॉजिक को कॉल करने से पहले, रीयल-टाइम वेब डेटा के आधार पर खुद को तैयार कर सकता है.
यहां एक उदाहरण दिया गया है, जिसमें google_search और कस्टम फ़ंक्शन getWeather की मदद से, बिल्ट-इन और कस्टम टूल के कॉम्बिनेशन को चालू किया गया है:
Python
# This will only work for SDK newer than 2.0.0
from google import genai
client = genai.Client()
getWeather = {
"type": "function",
"name": "getWeather",
"description": "Gets the weather for a requested city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city and state, e.g. Utqiaġvik, Alaska",
},
},
"required": ["city"],
},
}
# The Interactions API manages context automatically across tool calls.
# The model will first use Google Search, then call getWeather.
interaction = client.interactions.create(
model="gemini-3.5-flash",
input="What is the northernmost city in the United States? What's the weather like there today?",
tools=[
{"type": "google_search"},
getWeather,
],
)
# Process steps: the interaction contains search results and a function call
for step in interaction.steps:
if step.type == "function_call":
print(f"Function call: {step.name} with args: {step.arguments}")
# In a real application, you would execute the function here
# and provide the result back to the model.
JavaScript
// This will only work for SDK newer than 2.0.0
import { GoogleGenAI } from '@google/genai';
const client = new GoogleGenAI({});
const getWeather = {
type: "function",
name: "getWeather",
description: "Get the weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA"
}
},
required: ["location"]
}
};
// The Interactions API manages context automatically across tool calls.
// The model will first use Google Search, then call getWeather.
const interaction = await client.interactions.create({
model: "gemini-3.5-flash",
input: "What is the northernmost city in the United States? What's the weather like there today?",
tools: [
{ type: "google_search" },
getWeather,
],
});
// Process steps: the interaction contains search results and a function call
for (const step of interaction.steps) {
if (step.type === "function_call") {
console.log(`Function call: ${step.name} with args: ${JSON.stringify(step.arguments)}`);
// In a real application, you would execute the function here
// and provide the result back to the model.
}
}
REST
# Specifies the API revision to avoid breaking changes when they become default
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"model": "gemini-3.5-flash",
"input": "What is the northernmost city in the United States? What'\''s the weather like there today?",
"tools": [
{ "type": "google_search" },
{
"type": "function",
"name": "getWeather",
"description": "Get the weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
]
}'
यह कैसे काम करता है
Gemini 3 मॉडल, टूल कॉन्टेक्स्ट सर्कुलेशन का इस्तेमाल करते हैं. इससे, पहले से मौजूद और कस्टम टूल के कॉम्बिनेशन को चालू किया जा सकता है. टूल के कॉन्टेक्स्ट को सर्कुलेट करने की सुविधा की मदद से, पहले से मौजूद टूल के कॉन्टेक्स्ट को सुरक्षित रखा जा सकता है और उसे दिखाया जा सकता है. साथ ही, उसे एक ही इंटरैक्शन में कस्टम टूल के साथ शेयर किया जा सकता है.
टूल कॉम्बिनेशन की सुविधा चालू करना
- कॉम्बिनेशन के व्यवहार को ट्रिगर करने के लिए,
function_declarationsऔर इस्तेमाल किए जाने वाले बिल्ट-इन टूल शामिल करें.
एपीआई से मिले जवाब में शामिल चरणों की जानकारी
इंटरैक्शन के जवाब में, एपीआई, बिल्ट-इन टूल कॉल और फ़ंक्शन (कस्टम टूल) कॉल के लिए अलग-अलग चरण दिखाता है:
- पहले से मौजूद टूल के चरण: एपीआई, इन्हें अपने-आप मैनेज करता है. इससे बातचीत के दौरान संदर्भ बना रहता है.
- फ़ंक्शन कॉल करने के चरण: एपीआई, आपके कस्टम फ़ंक्शन के लिए
function_callचरण दिखाता है. फ़ंक्शन को लागू करना और उसका नतीजा वापस देना.
जवाब में शामिल चरणों में मौजूद ज़रूरी फ़ील्ड
जवाब में मिले चरणों में मौजूद कुछ फ़ील्ड, टूल के कॉन्टेक्स्ट को बनाए रखने और टूल के कॉम्बिनेशन को चालू करने के लिए ज़रूरी होते हैं:
id: यहfunction_callऔरfunction_responseचरणों में मिलता है. यह एक यूनीक आइडेंटिफ़ायर होता है, जो कॉल को उसके जवाब से मैप करता है.signature: यहthoughtचरणों के साथ-साथ, Gemini 3 और इसके बाद के मॉडल के लिए, टूल कॉल (जैसे,function_call) और नतीजे (जैसे,function_response) के सभी चरणों में मौजूद होता है. इस एन्क्रिप्ट (सुरक्षित) किए गए कॉन्टेक्स्ट की मदद से, सभी इंटरैक्शन में टूल के कॉन्टेक्स्ट को शेयर किया जा सकता है.
इन फ़ील्ड को मैनेज करना:
- स्टेटफ़ुल मोड (सुझाया गया):
previous_interaction_idका इस्तेमाल करने पर, सर्वरidऔरsignatureफ़ील्ड, दोनों को अपने-आप मैनेज करता है. - स्टेटलेस मोड: बातचीत के इतिहास को मैन्युअल तरीके से मैनेज करते समय, आपको यह पक्का करना होगा कि आपने
idऔरsignature, दोनों फ़ील्ड को मॉडल को वापस भेज दिया हो. ऐसा आपको बाद के अनुरोधों में करना होगा, ताकि पुष्टि की जा सके और कॉन्टेक्स्ट को बनाए रखा जा सके. अगर आपने पूरे जवाब वाले ऑब्जेक्ट को इतिहास में वापस भेज दिया है, तो आधिकारिक एसडीके इसे अपने-आप मैनेज कर लेते हैं.
टूल के हिसाब से डेटा
कुछ बिल्ट-इन टूल, उपयोगकर्ता को दिखने वाले डेटा आर्ग्युमेंट दिखाते हैं. ये आर्ग्युमेंट, टूल टाइप के हिसाब से अलग-अलग होते हैं.
| टूल | उपयोगकर्ता को दिखने वाले टूल कॉल आर्ग्युमेंट (अगर कोई है) | उपयोगकर्ता को दिखने वाला टूल रिस्पॉन्स (अगर कोई हो) |
|---|---|---|
| google_search | queries |
search_suggestions |
| google_maps | queries |
placesgoogle_maps_widget_context_token |
| url_context | urlsब्राउज़ किए जाने वाले यूआरएल |
status: ब्राउज़ करने की स्थितिretrieved_url: ब्राउज़ किए गए यूआरएल |
| file_search | कोई नहीं | कोई नहीं |
टोकन और कीमत
ध्यान दें कि अनुरोधों में शामिल, टूल कॉल के बिल्ट-इन पार्ट को prompt_token_count में गिना जाता है. टूल के इस्तेमाल से जुड़े ये इंटरमीडिएट चरण अब आपको दिखते हैं और आपको वापस भेजे जाते हैं. इसलिए, ये बातचीत के इतिहास का हिस्सा होते हैं. ऐसा सिर्फ़ अनुरोधों के मामले में होता है, जवाबों के मामले में नहीं.
Google Search टूल पर यह नियम लागू नहीं होता. Google Search, क्वेरी के लेवल पर पहले से ही अपना प्राइसिंग मॉडल लागू करता है. इसलिए, टोकन के लिए दो बार शुल्क नहीं लिया जाता (कीमत पेज देखें).
ज़्यादा जानकारी के लिए, टोकन पेज पढ़ें.
सीमाएं
- टूल के कॉन्टेक्स्ट को शेयर करने की सुविधा चालू होने पर, डिफ़ॉल्ट रूप से
validatedमोड चालू हो जाता है.autoमोड काम नहीं करता. google_searchजैसे बिल्ट-इन टूल, जगह और समय की जानकारी पर निर्भर करते हैं. इसलिए, अगर आपकेsystem_instructionयाfunction_declaration.descriptionमें जगह और समय की जानकारी अलग-अलग है, तो हो सकता है कि टूल कॉम्बिनेशन की सुविधा ठीक से काम न करे.
इन टूल के साथ काम करता है
टूल के कॉन्टेक्स्ट को शेयर करने की सामान्य सुविधा, सर्वर-साइड (पहले से मौजूद) टूल पर लागू होती है. कोड एक्ज़ीक्यूशन भी सर्वर-साइड टूल है. हालांकि, इसमें कॉन्टेक्स्ट सर्कुलेशन के लिए, पहले से मौजूद समाधान होता है. कंप्यूटर का इस्तेमाल और फ़ंक्शन कॉलिंग, क्लाइंट-साइड टूल हैं. इनमें कॉन्टेक्स्ट को शेयर करने के लिए, पहले से मौजूद समाधान भी होते हैं.
| टूल | एक्ज़ीक्यूशन साइड | कॉन्टेक्स्ट सर्कुलेशन की सुविधा के लिए सहायता |
|---|---|---|
| Google Search | सर्वर-साइड | समर्थित |
| Google Maps | सर्वर-साइड | समर्थित |
| यूआरएल का कॉन्टेक्स्ट | सर्वर-साइड | समर्थित |
| फ़ाइल खोजना | सर्वर-साइड | समर्थित |
| कोड चलाने की सुविधा | सर्वर-साइड | काम करता है (पहले से मौजूद है, code_execution और code_execution_result चरणों का इस्तेमाल करता है) |
| कंप्यूटर का इस्तेमाल | क्लाइंट-साइड | काम करता है (पहले से मौजूद है, function_call और function_response चरणों का इस्तेमाल करता है) |
| कस्टम फ़ंक्शन | क्लाइंट-साइड | काम करता है (पहले से मौजूद है, function_call और function_response चरणों का इस्तेमाल करता है) |
आगे क्या करना है
- Gemini API में फ़ंक्शन कॉलिंग के बारे में ज़्यादा जानें.
- इन टूल के बारे में जानें: