Gemini, एक ही जनरेशन में पहले से मौजूद टूल, जैसे कि google_search, और फ़ंक्शन कॉलिंग (इसे कस्टम टूल भी कहा जाता है) को एक साथ इस्तेमाल करने की सुविधा देता है. इसके लिए, यह टूल कॉल के कॉन्टेक्स्ट के इतिहास को सुरक्षित रखता है और उसे दिखाता है. इसमें पहले से मौजूद और कस्टम टूल के कॉम्बिनेशन का इस्तेमाल किया जा सकता है. इससे, एजेंट के तौर पर काम करने वाले जटिल वर्कफ़्लो बनाए जा सकते हैं. उदाहरण के लिए, मॉडल आपके कारोबार के लॉजिक को कॉल करने से पहले, रीयल-टाइम वेब डेटा के आधार पर खुद को तैयार कर सकता है.
यहां एक उदाहरण दिया गया है, जिसमें google_search और कस्टम फ़ंक्शन getWeather की मदद से, बिल्ट-इन और कस्टम टूल के कॉम्बिनेशन को चालू किया गया है:
Python
from google import genai
from google.genai import types
client = genai.Client()
getWeather = {
"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"],
},
}
# Turn 1: Initial request with Google Search (built-in) and getWeather (custom) tools enabled
response = client.models.generate_content(
model="gemini-3-flash-preview",
contents="What is the northernmost city in the United States? What's the weather like there today?",
config=types.GenerateContentConfig(
tools=[
types.Tool(
google_search=types.ToolGoogleSearch(), # Built-in tool
function_declarations=[getWeather] # Custom tool
),
],
include_server_side_tool_invocations=True
),
)
for part in response.candidates[0].content.parts:
if part.tool_call:
print(f"Tool call: {part.tool_call.tool_type} (ID: {part.tool_call.id})")
if part.tool_response:
print(f"Tool response: {part.tool_response.tool_type} (ID: {part.tool_response.id})")
if part.function_call:
print(f"Function call: {part.function_call.name} (ID: {part.function_call.id})")
# Turn 2: Manually build history to circulate both tool and function context
history = [
types.Content(
role="user",
parts=[types.Part(text="What is the northernmost city in the United States? What's the weather like there today?")]
),
# Response from Turn 1 includes tool_call, tool_response, and thought_signatures
response.candidates[0].content,
# Return the function_response
types.Content(
role="user",
parts=[types.Part(
function_response=types.FunctionResponse(
name="getWeather",
response={"response": "Very cold. 22 degrees Fahrenheit."},
id=response.candidates[0].content.parts[2].function_call.id # Match the ID from the function_call
)
)]
)
]
response_2 = client.models.generate_content(
model="gemini-3-flash-preview",
contents=history,
config=types.GenerateContentConfig(
tools=[
types.Tool(
google_search=types.ToolGoogleSearch(),
function_declarations=[getWeather]
),
],
# This flag needs to be enabled for built-in tool context circulation and tool combination
include_server_side_tool_invocations=True
),
)
for part in response_2.candidates[0].content.parts:
if part.text:
print(part.text)
JavaScript
import { GoogleGenAI } from '@google/genai';
const client = new GoogleGenAI({});
const getWeather = {
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"]
}
};
async function run() {
const model = client.getGenerativeModel({
model: "gemini-3-flash-preview",
});
const tools = [
{ googleSearch: {} },
{ functionDeclarations: [getWeather] }
];
// This flag needs to be enabled for built-in tool context circulation and tool combination
const toolConfig = { includeServerSideToolInvocations: true };
// Turn 1: Initial request with Google Search (built-in) and getWeather (custom) tools enabled
const result1 = await model.generateContent({
contents: [{role: "user", parts: [{text: "What is the northernmost city in the United States? What's the weather like there today?"}]}],
tools: tools,
toolConfig: toolConfig,
});
const response1 = result1.response;
for (const part of response1.candidates[0].content.parts) {
if (part.toolCall) {
console.log(`Tool call: ${part.toolCall.toolType} (ID: ${part.toolCall.id})`);
}
if (part.toolResponse) {
console.log(`Tool response: ${part.toolResponse.toolType} (ID: ${part.toolResponse.id})`);
}
if (part.functionCall) {
console.log(`Function call: ${part.functionCall.name} (ID: ${part.functionCall.id})`);
}
}
const functionCallId = response1.candidates[0].content.parts.find(p => p.functionCall)?.functionCall?.id;
// Turn 2: Manually build history to circulate both tool and function context
const history = [
{
role: "user",
parts:[{text: "What is the northernmost city in the United States? What's the weather like there today?"}]
},
// Response from Turn 1 includes tool_call, tool_response, and thought_signatures
response1.candidates[0].content,
// Return the function_response
{
role: "user",
parts: [{
functionResponse: {
name: "getWeather",
response: {response: "Very cold. 22 degrees Fahrenheit."},
id: functionCallId // Match the ID from the function_call
}
}]
}
];
const result2 = await model.generateContent({
contents: history,
tools: tools,
toolConfig: toolConfig,
});
for (const part of result2.response.candidates[0].content.parts) {
if (part.text) {
console.log(part.text);
}
}
}
run();
ऐप पर जाएं
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Exit(err)
}
defer client.Close()
getWeather := &genai.FunctionDeclaration{
Name: "getWeather",
Description: "Get the weather in a given location",
Parameters: &genai.Schema{
Type: genai.Object,
Properties: map[string]*genai.Schema{
"location": {
Type: genai.String,
Description: "The city and state, e.g. San Francisco, CA",
},
},
Required: []string{"location"},
},
}
model := client.GenerativeModel("gemini-3-flash-preview")
model.Tools = []*genai.Tool{
{GoogleSearch: &genai.GoogleSearch{}}, // Built-in tool
{FunctionDeclarations: []*genai.FunctionDeclaration{getWeather}}, // Custom tool
}
ist := true
model.ToolConfig = &genai.ToolConfig{
IncludeServerSideToolInvocations: &ist, // This flag needs to be enabled for built-in tool context circulation and tool combination
}
chat := model.StartChat()
// Turn 1: Initial request with Google Search (built-in) and getWeather (custom) tools enabled
prompt := genai.Text("What is the northernmost city in the United States? What's the weather like there today?")
resp1, err := chat.SendMessage(ctx, prompt)
if err != nil {
log.Exitf("SendMessage failed: %v", err)
}
if resp1 == nil || len(resp1.Candidates) == 0 || resp1.Candidates[0].Content == nil {
log.Exit("empty response from model")
}
var functionCallID string
for _, part := range resp1.Candidates[0].Content.Parts {
switch p := part.(type) {
case genai.FunctionCall:
fmt.Printf("Function call: %s (ID: %s)\n", p.Name, p.ID)
if p.Name == "getWeather" {
functionCallID = p.ID
}
case genai.ToolCallPart:
fmt.Printf("Tool call: %s (ID: %s)\n", p.ToolType, p.ID)
case genai.ToolResponsePart:
fmt.Printf("Tool response: %s (ID: %s)\n", p.ToolType, p.ID)
}
}
if functionCallID == "" {
log.Exit("no getWeather function call in response")
}
// Turn 2: Provide function result back to model.
// Chat history automatically includes tool_call, tool_response, and thought_signatures from Turn 1.
fr := genai.FunctionResponse{
Name: "getWeather",
ID: functionCallID,
Response: map[string]any{
"response": "Very cold. 22 degrees Fahrenheit.",
},
}
resp2, err := chat.SendMessage(ctx, fr)
if err != nil {
log.Exitf("SendMessage for turn 2 failed: %v", err)
}
if resp2 == nil || len(resp2.Candidates) == 0 || resp2.Candidates[0].Content == nil {
log.Exit("empty response from model in turn 2")
}
for _, part := range resp2.Candidates[0].Content.Parts {
if txt, ok := part.(genai.Text); ok {
fmt.Println(string(txt))
}
}
}
REST
# Turn 1: Initial request with Google Search (built-in) and getWeather (custom) tools enabled
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"contents": [{
"role": "user",
"parts": [{
"text": "What is the northernmost city in the United States? What'\''s the weather like there today?"
}]
}],
"tools": [{
"googleSearch": {}
}, {
"functionDeclarations": [{
"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"]
}
}]
}],
"toolConfig": {
"includeServerSideToolInvocations": true
}
}'
# Turn 2: Manually build history to circulate both tool and function context
# The following request assumes you have captured candidates[0].content from Turn 1 response,
# and extracted function_call.id for getWeather.
# Replace FUNCTION_CALL_ID and insert candidate content from turn 1.
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"contents": [
{
"role": "user",
"parts": [{"text": "What is the northernmost city in the United States? What'\''s the weather like there today?"}]
},
YOUR_CANDIDATE_CONTENT_FROM_TURN_1_RESPONSE,
{
"role": "user",
"parts": [{
"functionResponse": {
"name": "getWeather",
"id": "FUNCTION_CALL_ID",
"response": {"response": "Very cold. 22 degrees Fahrenheit."}
}
}]
}
],
"tools": [{
"googleSearch": {}
}, {
"functionDeclarations": [{
"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"]
}
}]
}],
"toolConfig": {
"includeServerSideToolInvocations": true
}
}'
यह कैसे काम करता है
Gemini 3 मॉडल, टूल के कॉन्टेक्स्ट को शेयर करने की सुविधा का इस्तेमाल करते हैं. इससे, पहले से मौजूद और कस्टम टूल के कॉम्बिनेशन को चालू किया जा सकता है. टूल के कॉन्टेक्स्ट को सर्कुलेट करने की सुविधा की मदद से, पहले से मौजूद टूल के कॉन्टेक्स्ट को सुरक्षित रखा जा सकता है और उसे दिखाया जा सकता है. साथ ही, इसे एक ही कॉल में कस्टम टूल के साथ शेयर किया जा सकता है.
टूल कॉम्बिनेशन की सुविधा चालू करना
- टूल के कॉन्टेक्स्ट को शेयर करने की सुविधा चालू करने के लिए, आपको
include_server_side_tool_invocationsफ़्लैग कोtrueपर सेट करना होगा. - कॉम्बिनेशन के तौर पर काम करने की सुविधा को ट्रिगर करने के लिए,
function_declarationsके साथ-साथ, उन बिल्ट-इन टूल को शामिल करें जिनका आपको इस्तेमाल करना है.- अगर आपने
function_declarationsशामिल नहीं किया है, तो भी टूल कॉन्टेक्स्ट सर्कुलेशन, शामिल किए गए बिल्ट-इन टूल पर काम करेगा. हालांकि, ऐसा तब तक होगा, जब तक फ़्लैग सेट है.
- अगर आपने
एपीआई, पार्ट दिखाता है
एक ही जवाब में, एपीआई, टूल कॉल के लिए toolCall और toolResponse हिस्से दिखाता है. फ़ंक्शन (कस्टम टूल) कॉल के लिए, एपीआई functionCall कॉल वाला हिस्सा दिखाता है. इसके जवाब में उपयोगकर्ता, अगले टर्न में functionResponse वाला हिस्सा देता है.
toolCallऔरtoolResponse: एपीआई इन हिस्सों को इसलिए दिखाता है, ताकि यह पता चल सके कि सर्वर साइड पर कौनसे टूल इस्तेमाल किए गए हैं. साथ ही, अगले टर्न के लिए उनके इस्तेमाल के नतीजे भी मिल सकें.functionCallऔरfunctionResponse: एपीआई, फ़ंक्शन कॉल को उपयोगकर्ता को भेजता है, ताकि वह इसे भर सके. इसके बाद, उपयोगकर्ता फ़ंक्शन के जवाब में नतीजे वापस भेजता है. ये हिस्से, Gemini API में फ़ंक्शन कॉल करने की सुविधा के लिए स्टैंडर्ड हैं. ये टूल कॉम्बिनेशन की सुविधा के लिए खास नहीं हैं.- (सिर्फ़ कोड एक्ज़ीक्यूशन टूल के लिए)
executableCodeऔरcodeExecutionResult: कोड एक्ज़ीक्यूशन टूल का इस्तेमाल करते समय, एपीआईfunctionCallऔरfunctionResponseके बजायexecutableCode(मॉडल से जनरेट किया गया वह कोड जिसे एक्ज़ीक्यूट किया जाना है) औरcodeExecutionResult(एक्ज़ीक्यूटेबल कोड का नतीजा) दिखाता है.
आपको हर बार मॉडल को सभी हिस्सों को वापस भेजना होगा. इनमें वे सभी फ़ील्ड भी शामिल हैं जिनमें ये हिस्से मौजूद हैं. ऐसा करने से, कॉन्टेक्स्ट को बनाए रखने और टूल के कॉम्बिनेशन को चालू करने में मदद मिलती है.
वापस किए गए हिस्सों में ज़रूरी फ़ील्ड
एपीआई से मिले कुछ हिस्सों में id, tool_type, और thought_signature फ़ील्ड शामिल होंगे. इन फ़ील्ड से, टूल के कॉन्टेक्स्ट को बनाए रखने में मदद मिलती है. इसलिए, ये फ़ील्ड टूल के कॉम्बिनेशन के लिए ज़रूरी हैं. आपको अपने अगले अनुरोधों में, जवाब में दिए गए सभी हिस्सों को वापस भेजना होगा.
id: यह एक यूनीक आइडेंटिफ़ायर है, जो कॉल को उसके रिस्पॉन्स से मैप करता है.idको सभी फ़ंक्शन कॉल के जवाबों पर सेट किया जाता है. भले ही, टूल के कॉन्टेक्स्ट का सर्कुलेशन कैसा भी हो. आपको फ़ंक्शन के जवाब में वहीidदेना होगा जो एपीआई, फ़ंक्शन कॉल में देता है. ऐसा करना ज़रूरी है. बिल्ट-इन टूल, टूल कॉल और टूल के जवाब के बीचidको अपने-आप शेयर करते हैं.- यह टूल से जुड़े सभी हिस्सों में मौजूद है:
toolCall,toolResponse,functionCall,functionResponse,executableCode,codeExecutionResult
- यह टूल से जुड़े सभी हिस्सों में मौजूद है:
tool_type: इससे इस्तेमाल किए जा रहे टूल की पहचान होती है. यह लिटरल बिल्ट-इन टूल या (जैसे,URL_CONTEXT) या फ़ंक्शन (जैसे,getWeather) का नाम होता है.- यह
toolCallऔरtoolResponseहिस्सों में मौजूद है.
- यह
thought_signature: यह एपीआई से मिले हर जवाब में एम्बेड किया गया एन्क्रिप्ट (सुरक्षित) किया गया कॉन्टेक्स्ट होता है. थॉट सिग्नेचर के बिना कॉन्टेक्स्ट को फिर से नहीं बनाया जा सकता. अगर हर टर्न में सभी हिस्सों के लिए थॉट सिग्नेचर नहीं दिए जाते हैं, तो मॉडल में गड़बड़ी होगी.- यह सभी हिस्सों में मौजूद होता है.
टूल के हिसाब से डेटा
कुछ बिल्ट-इन टूल, उपयोगकर्ता को दिखने वाले डेटा आर्ग्युमेंट दिखाते हैं. ये आर्ग्युमेंट, टूल टाइप के हिसाब से अलग-अलग होते हैं.
| टूल | उपयोगकर्ता को दिखने वाले टूल कॉल आर्ग्युमेंट (अगर कोई है) | उपयोगकर्ता को दिखने वाला टूल रिस्पॉन्स (अगर कोई हो) |
|---|---|---|
| GOOGLE_SEARCH | queries |
search_suggestions |
| GOOGLE_MAPS | queries |
placesgoogle_maps_widget_context_token |
| URL_CONTEXT | urlsब्राउज़ किए जाने वाले यूआरएल |
urls_metadataretrieved_url: ब्राउज़ किए गए यूआरएलurl_retrieval_status: ब्राउज़ करने की स्थिति |
| FILE_SEARCH | कोई नहीं | कोई नहीं |
टूल के कॉम्बिनेशन के अनुरोध के स्ट्रक्चर का उदाहरण
नीचे दिए गए अनुरोध के स्ट्रक्चर में, प्रॉम्प्ट के अनुरोध का स्ट्रक्चर दिखाया गया है: "अमेरिका का सबसे उत्तरी शहर कौनसा है? वहां आज मौसम कैसा है?". इसमें तीन टूल शामिल हैं: Gemini के पहले से मौजूद टूल google_search और code_execution, और कस्टम फ़ंक्शन get_weather.
{
"model": "models/gemini-3-flash-preview",
"contents": [{
"parts": [{
"text": "What is the northernmost city in the United States? What's the weather like there today?"
}],
"role": "user"
}, {
"parts": [{
"thoughtSignature": "...",
"toolCall": {
"toolType": "GOOGLE_SEARCH_WEB",
"args": {
"queries": ["northernmost city in the United States"]
},
"id": "a7b3k9p2"
}
}, {
"thoughtSignature": "...",
"toolResponse": {
"toolType": "GOOGLE_SEARCH_WEB",
"response": {
"search_suggestions": "..."
},
"id": "a7b3k9p2"
}
}, {
"functionCall": {
"name": "getWeather",
"args": {
"city": "Utqiaġvik, Alaska"
},
"id": "m4q8z1v6"
},
"thoughtSignature": "..."
}],
"role": "model"
}, {
"parts": [{
"functionResponse": {
"name": "getWeather",
"response": {
"response": "Very cold. 22 degrees Fahrenheit."
},
"id": "m4q8z1v6"
}
}],
"role": "user"
}],
"tools": [{
"functionDeclarations": [{
"name": "getWeather"
}]
}, {
"googleSearch": {
}
}, {
"codeExecution": {
}
}],
"toolConfig": {
"includeServerSideToolInvocations": true
}
}
टोकन और कीमत
ध्यान दें कि अनुरोधों में मौजूद toolCall और toolResponse हिस्सों को prompt_token_count में गिना जाता है. टूल के इस्तेमाल से जुड़े ये इंटरमीडिएट चरण अब आपको दिखते हैं और आपको वापस भेजे जाते हैं. इसलिए, ये बातचीत के इतिहास का हिस्सा होते हैं. ऐसा सिर्फ़ अनुरोधों के मामले में होता है, जवाबों के मामले में नहीं.
Google Search टूल, इस नियम का अपवाद है. Google Search, क्वेरी के लेवल पर पहले से ही अपना प्राइसिंग मॉडल लागू करता है. इसलिए, टोकन के लिए दो बार शुल्क नहीं लिया जाता (कीमत पेज देखें).
ज़्यादा जानकारी के लिए, टोकन पेज पढ़ें.
सीमाएं
include_server_side_tool_invocationsफ़्लैग चालू होने पर, डिफ़ॉल्ट रूप सेVALIDATEDमोड चालू होता है.AUTOमोड काम नहीं करताgoogle_searchजैसे बिल्ट-इन टूल, जगह और समय की जानकारी पर निर्भर करते हैं. इसलिए, अगर आपकेsystem_instructionयाfunction_declaration.descriptionमें जगह और समय की जानकारी अलग-अलग है, तो हो सकता है कि टूल कॉम्बिनेशन की सुविधा ठीक से काम न करे.
इन टूल के साथ काम करता है
टूल के कॉन्टेक्स्ट को शेयर करने की सामान्य सुविधा, सर्वर-साइड (पहले से मौजूद) टूल पर लागू होती है. कोड एक्ज़ीक्यूशन भी सर्वर-साइड टूल है. हालांकि, इसमें कॉन्टेक्स्ट सर्कुलेशन के लिए, पहले से मौजूद समाधान होता है. कंप्यूटर का इस्तेमाल करने और फ़ंक्शन कॉल करने की सुविधा, क्लाइंट-साइड टूल हैं. साथ ही, इनमें कॉन्टेक्स्ट को शेयर करने के लिए पहले से मौजूद समाधान भी होते हैं.
| टूल | एक्ज़ीक्यूशन साइड | कॉन्टेक्स्ट को एक से दूसरी जगह ले जाने की सुविधा |
|---|---|---|
| Google Search | सर्वर-साइड | समर्थित |
| Google Maps | सर्वर-साइड | समर्थित |
| यूआरएल का कॉन्टेक्स्ट | सर्वर-साइड | समर्थित |
| फ़ाइल खोजना | सर्वर-साइड | समर्थित |
| कोड को लागू करना | सर्वर-साइड | समर्थित (पहले से मौजूद, executableCode और codeExecutionResult का इस्तेमाल करता है) |
| कंप्यूटर का इस्तेमाल | क्लाइंट-साइड | समर्थित (पहले से मौजूद, functionCall और functionResponse का इस्तेमाल करता है) |
| कस्टम फ़ंक्शन | क्लाइंट-साइड | समर्थित (पहले से मौजूद, functionCall और functionResponse का इस्तेमाल करता है) |
आगे क्या करना है
- Gemini API में फ़ंक्शन कॉलिंग के बारे में ज़्यादा जानें.
- इन टूल के बारे में जानें: