पहले से मौजूद टूल और फ़ंक्शन कॉलिंग को एक साथ इस्तेमाल करना

Gemini, एक ही इंटरैक्शन में बिल्ट-इन टूल, जैसे कि google_search, और फ़ंक्शन कॉलिंग (इसे कस्टम टूल भी कहा जाता है) को एक साथ इस्तेमाल करने की सुविधा देता है. इसके लिए, यह टूल कॉल के कॉन्टेक्स्ट के इतिहास को सेव करता है और उसे दिखाता है. इसमें पहले से मौजूद और कस्टम टूल के कॉम्बिनेशन का इस्तेमाल किया जा सकता है. इससे, एजेंट के तौर पर काम करने वाले जटिल वर्कफ़्लो बनाए जा सकते हैं. उदाहरण के लिए, मॉडल आपके कारोबार के लॉजिक को कॉल करने से पहले, रीयल-टाइम वेब डेटा के आधार पर खुद को तैयार कर सकता है.

यहां एक उदाहरण दिया गया है, जिसमें google_search और कस्टम फ़ंक्शन getWeather की मदद से, बिल्ट-इन और कस्टम टूल के कॉम्बिनेशन को चालू किया गया है:

Python

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-flash-preview",
    input="What is the northernmost city in the United States? What's the weather like there today?",
    tools=[
        {"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

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-flash-preview",
    input: "What is the northernmost city in the United States? What's the weather like there today?",
    tools: [
        { googleSearch: {} },
        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

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-flash-preview",
  "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 places
google_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 चरणों का इस्तेमाल करता है)

आगे क्या करना है