Gemini API की मदद से फ़ंक्शन कॉल करना

फ़ंक्शन कॉलिंग की सुविधा की मदद से, मॉडल को बाहरी टूल और एपीआई से कनेक्ट किया जा सकता है. टेक्स्ट वाले जवाब जनरेट करने के बजाय, मॉडल यह तय करता है कि कब खास फ़ंक्शन को कॉल करना है. साथ ही, असल ज़िंदगी में होने वाली कार्रवाइयों को पूरा करने के लिए ज़रूरी पैरामीटर उपलब्ध कराता है. इससे मॉडल, नेचुरल लैंग्वेज और असल दुनिया की कार्रवाइयों और डेटा के बीच एक पुल की तरह काम कर पाता है. फ़ंक्शन कॉलिंग का इस्तेमाल इन तीन मुख्य कामों के लिए किया जाता है:

  • ज्ञान बढ़ाना: डेटाबेस, एपीआई, और नॉलेज बेस जैसे बाहरी सोर्स से जानकारी ऐक्सेस करना.
  • ज़्यादा सुविधाएं: कैलकुलेशन करने और मॉडल की सीमाओं को बढ़ाने के लिए, बाहरी टूल का इस्तेमाल करें. जैसे, कैलकुलेटर का इस्तेमाल करना या चार्ट बनाना.
  • कार्रवाइयां करना: एपीआई का इस्तेमाल करके, बाहरी सिस्टम के साथ इंटरैक्ट करना. जैसे, अपॉइंटमेंट शेड्यूल करना, इनवॉइस बनाना, ईमेल भेजना या स्मार्ट होम डिवाइसों को कंट्रोल करना.

फ़ंक्शन कॉलिंग की सुविधा कैसे काम करती है

फ़ंक्शन कॉलिंग के बारे में खास जानकारी

फ़ंक्शन कॉलिंग में, आपके ऐप्लिकेशन, मॉडल, और बाहरी फ़ंक्शन के बीच स्ट्रक्चर्ड इंटरैक्शन शामिल होता है. इस प्रोसेस के बारे में यहां बताया गया है:

  1. फ़ंक्शन का एलान तय करें: अपने ऐप्लिकेशन कोड में फ़ंक्शन का एलान तय करें. फ़ंक्शन के बारे में जानकारी देने वाले फ़ॉर्मैट में, मॉडल को फ़ंक्शन के नाम, पैरामीटर, और मकसद के बारे में बताया जाता है.
  2. फ़ंक्शन के एलान के साथ एलएलएम को कॉल करना: मॉडल को फ़ंक्शन के एलान के साथ उपयोगकर्ता का प्रॉम्प्ट भेजें. यह अनुरोध का विश्लेषण करता है और यह तय करता है कि फ़ंक्शन कॉल करना मददगार होगा या नहीं. अगर ऐसा है, तो यह स्ट्रक्चर्ड JSON ऑब्जेक्ट के साथ जवाब देता है.
  3. फ़ंक्शन कोड को लागू करना (आपकी ज़िम्मेदारी): मॉडल, फ़ंक्शन को लागू नहीं करता है. जवाब को प्रोसेस करने और फ़ंक्शन कॉल की जांच करने की ज़िम्मेदारी आपके ऐप्लिकेशन की है. ऐसा तब करना होता है, जब
    • हां: फ़ंक्शन का नाम और आर्ग्युमेंट निकालें. इसके बाद, अपने ऐप्लिकेशन में उससे जुड़ा फ़ंक्शन लागू करें.
    • नहीं: मॉडल ने प्रॉम्प्ट का सीधा जवाब टेक्स्ट के तौर पर दिया है (उदाहरण में इस फ़्लो पर कम ज़ोर दिया गया है, लेकिन यह एक संभावित नतीजा है).
  4. उपयोगकर्ता के लिए आसान जवाब तैयार करना: अगर कोई फ़ंक्शन पूरा हो गया है, तो उसके नतीजे को कैप्चर करें और बातचीत के अगले चरण में उसे मॉडल को वापस भेजें. यह नतीजे का इस्तेमाल करके, उपयोगकर्ता के लिए एक आसान जवाब जनरेट करेगा. इसमें फ़ंक्शन कॉल से मिली जानकारी शामिल होगी.

इस प्रोसेस को कई बार दोहराया जा सकता है. इससे मुश्किल इंटरैक्शन और वर्कफ़्लो को पूरा किया जा सकता है. यह मॉडल, एक ही टर्न में कई फ़ंक्शन कॉल करने की सुविधा भी देता है. इसे पैरलल फ़ंक्शन कॉलिंग कहते हैं. साथ ही, यह मॉडल एक के बाद एक फ़ंक्शन कॉल करने की सुविधा भी देता है. इसे कंपोज़िशनल फ़ंक्शन कॉलिंग कहते हैं.

पहला चरण: फ़ंक्शन का एलान तय करना

अपने ऐप्लिकेशन कोड में एक फ़ंक्शन और उसके एलान को तय करें. इससे उपयोगकर्ता, लाइट की वैल्यू सेट कर पाएंगे और एपीआई अनुरोध कर पाएंगे. यह फ़ंक्शन, बाहरी सेवाओं या एपीआई को कॉल कर सकता है.

Python

# Define a function that the model can call to control smart lights
set_light_values_declaration = {
    "name": "set_light_values",
    "description": "Sets the brightness and color temperature of a light.",
    "parameters": {
        "type": "object",
        "properties": {
            "brightness": {
                "type": "integer",
                "description": "Light level from 0 to 100. Zero is off and 100 is full brightness",
            },
            "color_temp": {
                "type": "string",
                "enum": ["daylight", "cool", "warm"],
                "description": "Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.",
            },
        },
        "required": ["brightness", "color_temp"],
    },
}

# This is the actual function that would be called based on the model's suggestion
def set_light_values(brightness: int, color_temp: str) -> dict[str, int | str]:
    """Set the brightness and color temperature of a room light. (mock API).

    Args:
        brightness: Light level from 0 to 100. Zero is off and 100 is full brightness
        color_temp: Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.

    Returns:
        A dictionary containing the set brightness and color temperature.
    """
    return {"brightness": brightness, "colorTemperature": color_temp}

JavaScript

import { Type } from '@google/genai';

// Define a function that the model can call to control smart lights
const setLightValuesFunctionDeclaration = {
  name: 'set_light_values',
  description: 'Sets the brightness and color temperature of a light.',
  parameters: {
    type: Type.OBJECT,
    properties: {
      brightness: {
        type: Type.NUMBER,
        description: 'Light level from 0 to 100. Zero is off and 100 is full brightness',
      },
      color_temp: {
        type: Type.STRING,
        enum: ['daylight', 'cool', 'warm'],
        description: 'Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.',
      },
    },
    required: ['brightness', 'color_temp'],
  },
};

/**

*   Set the brightness and color temperature of a room light. (mock API)
*   @param {number} brightness - Light level from 0 to 100. Zero is off and 100 is full brightness
*   @param {string} color_temp - Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.
*   @return {Object} A dictionary containing the set brightness and color temperature.
*/
function setLightValues(brightness, color_temp) {
  return {
    brightness: brightness,
    colorTemperature: color_temp
  };
}

दूसरा चरण: फ़ंक्शन के एलान के साथ मॉडल को कॉल करना

फ़ंक्शन के एलान तय करने के बाद, मॉडल को उनका इस्तेमाल करने के लिए कहा जा सकता है. यह प्रॉम्प्ट और फ़ंक्शन के एलान का विश्लेषण करता है. साथ ही, यह तय करता है कि सीधे जवाब देना है या किसी फ़ंक्शन को कॉल करना है. अगर किसी फ़ंक्शन को कॉल किया जाता है, तो जवाब वाले ऑब्जेक्ट में फ़ंक्शन कॉल का सुझाव शामिल होगा.

Python

from google.genai import types

# Configure the client and tools
client = genai.Client()
tools = types.Tool(function_declarations=[set_light_values_declaration])
config = types.GenerateContentConfig(tools=[tools])

# Define user prompt
contents = [
    types.Content(
        role="user", parts=[types.Part(text="Turn the lights down to a romantic level")]
    )
]

# Send request with function declarations
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents=contents
    config=config,
)

print(response.candidates[0].content.parts[0].function_call)

JavaScript

import { GoogleGenAI } from '@google/genai';

// Generation config with function declaration
const config = {
  tools: [{
    functionDeclarations: [setLightValuesFunctionDeclaration]
  }]
};

// Configure the client
const ai = new GoogleGenAI({});

// Define user prompt
const contents = [
  {
    role: 'user',
    parts: [{ text: 'Turn the lights down to a romantic level' }]
  }
];

// Send request with function declarations
const response = await ai.models.generateContent({
  model: 'gemini-2.5-flash',
  contents: contents,
  config: config
});

console.log(response.functionCalls[0]);

इसके बाद, मॉडल OpenAPI के साथ काम करने वाले स्कीमा में functionCall ऑब्जेक्ट दिखाता है. इसमें यह बताया जाता है कि उपयोगकर्ता के सवाल का जवाब देने के लिए, एलान किए गए एक या उससे ज़्यादा फ़ंक्शन को कैसे कॉल किया जाए.

Python

id=None args={'color_temp': 'warm', 'brightness': 25} name='set_light_values'

JavaScript

{
  name: 'set_light_values',
  args: { brightness: 25, color_temp: 'warm' }
}

तीसरा चरण: set_light_values फ़ंक्शन कोड को लागू करना

मॉडल के जवाब से फ़ंक्शन कॉल की जानकारी निकालें, आर्ग्युमेंट पार्स करें, और set_light_values फ़ंक्शन को लागू करें.

Python

# Extract tool call details, it may not be in the first part.
tool_call = response.candidates[0].content.parts[0].function_call

if tool_call.name == "set_light_values":
    result = set_light_values(**tool_call.args)
    print(f"Function execution result: {result}")

JavaScript

// Extract tool call details
const tool_call = response.functionCalls[0]

let result;
if (tool_call.name === 'set_light_values') {
  result = setLightValues(tool_call.args.brightness, tool_call.args.color_temp);
  console.log(`Function execution result: ${JSON.stringify(result)}`);
}

चौथा चरण: फ़ंक्शन के नतीजे के साथ उपयोगकर्ता के लिए आसान जवाब जनरेट करना और मॉडल को फिर से कॉल करना

आखिर में, फ़ंक्शन के नतीजे को वापस मॉडल को भेजें, ताकि वह इस जानकारी को उपयोगकर्ता को दिए जाने वाले अपने फ़ाइनल जवाब में शामिल कर सके.

Python

# Create a function response part
function_response_part = types.Part.from_function_response(
    name=tool_call.name,
    response={"result": result},
)

# Append function call and result of the function execution to contents
contents.append(response.candidates[0].content) # Append the content from the model's response.
contents.append(types.Content(role="user", parts=[function_response_part])) # Append the function response

final_response = client.models.generate_content(
    model="gemini-2.5-flash",
    config=config,
    contents=contents,
)

print(final_response.text)

JavaScript

// Create a function response part
const function_response_part = {
  name: tool_call.name,
  response: { result }
}

// Append function call and result of the function execution to contents
contents.push(response.candidates[0].content);
contents.push({ role: 'user', parts: [{ functionResponse: function_response_part }] });

// Get the final response from the model
const final_response = await ai.models.generateContent({
  model: 'gemini-2.5-flash',
  contents: contents,
  config: config
});

console.log(final_response.text);

इससे फ़ंक्शन कॉल करने की प्रोसेस पूरी हो जाती है. मॉडल ने उपयोगकर्ता के अनुरोध को पूरा करने के लिए, set_light_values फ़ंक्शन का इस्तेमाल किया.

फ़ंक्शन के एलान

किसी प्रॉम्प्ट में फ़ंक्शन कॉलिंग लागू करने पर, आपको एक tools ऑब्जेक्ट बनाना होता है. इसमें एक या उससे ज़्यादा function declarations होते हैं. फ़ंक्शन को JSON का इस्तेमाल करके तय किया जाता है. खास तौर पर, OpenAPI स्कीमा फ़ॉर्मैट के चुने गए सबसेट का इस्तेमाल करके. किसी फ़ंक्शन के एलान में ये पैरामीटर शामिल किए जा सकते हैं:

  • name (string): फ़ंक्शन के लिए यूनीक नाम (get_weather_forecast, send_email). ऐसे नाम इस्तेमाल करें जिनमें खाली जगहें या खास वर्ण न हों. साथ ही, नाम से फ़ंक्शन के बारे में जानकारी मिलती हो (अंडरस्कोर या कैमल केस का इस्तेमाल करें).
  • description (string): फ़ंक्शन के मकसद और क्षमताओं के बारे में साफ़ तौर पर पूरी जानकारी. इससे मॉडल को यह समझने में मदद मिलती है कि फ़ंक्शन का इस्तेमाल कब करना है. जवाब में सटीक जानकारी दें. अगर ज़रूरी हो, तो उदाहरण भी दें ("यह सुविधा, जगह के हिसाब से थिएटर ढूंढती है. इसके अलावा, यह फ़िलहाल थिएटर में चल रही फ़िल्म का टाइटल भी ढूंढ सकती है.").
  • parameters (object): यह फ़ंक्शन के लिए, इनपुट पैरामीटर तय करता है.
    • type (string): यह पूरे डेटा टाइप के बारे में बताता है. जैसे, object.
    • properties (object): इसमें अलग-अलग पैरामीटर की सूची होती है. हर पैरामीटर में यह जानकारी होती है:
      • type (string): पैरामीटर का डेटा टाइप, जैसे कि string, integer, boolean, array.
      • description (string): पैरामीटर के मकसद और फ़ॉर्मैट की जानकारी. उदाहरण और पाबंदियां ("शहर और राज्य, जैसे कि 'सैन फ़्रांसिस्को, कैलिफ़ोर्निया' या कोई पिन कोड, जैसे कि '95616'.").
      • enum (ऐरे, ज़रूरी नहीं): अगर पैरामीटर की वैल्यू एक तय सेट से ली गई हैं, तो "enum" का इस्तेमाल करके, अनुमति वाली वैल्यू की सूची बनाएं. इसके बजाय, सिर्फ़ ब्यौरे में उनका ब्यौरा न दें. इससे सटीक जानकारी मिलती है ("enum": ["daylight", "cool", "warm"]).
    • required (ऐरे): यह स्ट्रिंग का एक ऐरे है. इसमें उन पैरामीटर के नाम दिए गए हैं जो फ़ंक्शन के काम करने के लिए ज़रूरी हैं.

types.FunctionDeclaration.from_callable(client=client, callable=your_function) का इस्तेमाल करके, Python फ़ंक्शन से सीधे तौर पर FunctionDeclarations भी बनाए जा सकते हैं.

सोच-विचार करके फ़ंक्शन कॉल करने की सुविधा

"सोचना" सुविधा चालू करने से, फ़ंक्शन कॉल की परफ़ॉर्मेंस को बेहतर बनाया जा सकता है. ऐसा इसलिए, क्योंकि इससे मॉडल को फ़ंक्शन कॉल के सुझाव देने से पहले, अनुरोध के बारे में सोचने का समय मिल जाता है. Gemini API स्टेटलेस है. इसलिए, एक से ज़्यादा बार की जाने वाली बातचीत में, मॉडल के तर्क का कॉन्टेक्स्ट हर बार बदलता रहेगा. इस कॉन्टेक्स्ट को बनाए रखने के लिए, थॉट सिग्नेचर का इस्तेमाल किया जा सकता है. थॉट सिग्नेचर, मॉडल की इंटरनल थॉट प्रोसेस का एन्क्रिप्ट (सुरक्षित) किया गया वर्शन होता है. इसे आपको बाद के टर्न में मॉडल को वापस भेजना होता है.

एक से ज़्यादा बार बातचीत करने वाले टूल के लिए स्टैंडर्ड पैटर्न यह है कि मॉडल के पिछले जवाब को बातचीत के इतिहास में जोड़ दिया जाता है. content ऑब्जेक्ट में, thought_signatures अपने-आप शामिल हो जाता है. इस पैटर्न का इस्तेमाल करने पर, कोड में कोई बदलाव करने की ज़रूरत नहीं होती.

विचारों के सिग्नेचर को मैन्युअल तरीके से मैनेज करना

अगर आपको बातचीत के इतिहास में मैन्युअल तरीके से बदलाव करना है, तो पिछले जवाब को पूरा भेजने के बजाय, मॉडल के टर्न में शामिल thought_signature को सही तरीके से मैनेज करें.

यह पक्का करने के लिए कि मॉडल का कॉन्टेक्स्ट बना रहे, इन नियमों का पालन करें:

  • thought_signature को हमेशा उसके ओरिजनल Part में मौजूद मॉडल पर वापस भेजें.
  • सिग्नेचर वाले Part को ऐसे Part के साथ न मर्ज करें जिसमें सिग्नेचर नहीं है. इससे विचार के पोज़िशनल कॉन्टेक्स्ट में रुकावट आती है.
  • दो ऐसे Parts को न मिलाएं जिनमें हस्ताक्षर शामिल हों, क्योंकि हस्ताक्षर वाली स्ट्रिंग को मर्ज नहीं किया जा सकता.

सोच के आधार पर किए गए हस्ताक्षर की जांच करना

इसे लागू करना ज़रूरी नहीं है. हालांकि, डीबग करने या शिक्षा के मकसद से, thought_signature देखने के लिए जवाब की जांच की जा सकती है.

Python

import base64
# After receiving a response from a model with thinking enabled
# response = client.models.generate_content(...)

# The signature is attached to the response part containing the function call
part = response.candidates[0].content.parts[0]
if part.thought_signature:
  print(base64.b64encode(part.thought_signature).decode("utf-8"))

JavaScript

// After receiving a response from a model with thinking enabled
// const response = await ai.models.generateContent(...)

// The signature is attached to the response part containing the function call
const part = response.candidates[0].content.parts[0];
if (part.thoughtSignature) {
  console.log(part.thoughtSignature);
}

सोचने की प्रक्रिया वाले पेज पर जाकर, थॉट सिग्नेचर के इस्तेमाल और इसकी सीमाओं के बारे में ज़्यादा जानें. साथ ही, सोचने के मॉडल के बारे में सामान्य जानकारी पाएं.

पैरलल फ़ंक्शन कॉलिंग

एक बार में एक फ़ंक्शन कॉल करने के अलावा, एक साथ कई फ़ंक्शन कॉल भी किए जा सकते हैं. पैरलल फ़ंक्शन कॉलिंग की मदद से, एक साथ कई फ़ंक्शन लागू किए जा सकते हैं. इसका इस्तेमाल तब किया जाता है, जब फ़ंक्शन एक-दूसरे पर निर्भर न हों. यह कई स्थितियों में काम आता है. जैसे, अलग-अलग सोर्स से डेटा इकट्ठा करना. जैसे, अलग-अलग डेटाबेस से ग्राहक की जानकारी पाना या अलग-अलग वेयरहाउस में इन्वेंट्री के लेवल की जांच करना. इसके अलावा, कई कार्रवाइयां करना. जैसे, अपने अपार्टमेंट को डिस्को में बदलना.

Python

power_disco_ball = {
    "name": "power_disco_ball",
    "description": "Powers the spinning disco ball.",
    "parameters": {
        "type": "object",
        "properties": {
            "power": {
                "type": "boolean",
                "description": "Whether to turn the disco ball on or off.",
            }
        },
        "required": ["power"],
    },
}

start_music = {
    "name": "start_music",
    "description": "Play some music matching the specified parameters.",
    "parameters": {
        "type": "object",
        "properties": {
            "energetic": {
                "type": "boolean",
                "description": "Whether the music is energetic or not.",
            },
            "loud": {
                "type": "boolean",
                "description": "Whether the music is loud or not.",
            },
        },
        "required": ["energetic", "loud"],
    },
}

dim_lights = {
    "name": "dim_lights",
    "description": "Dim the lights.",
    "parameters": {
        "type": "object",
        "properties": {
            "brightness": {
                "type": "number",
                "description": "The brightness of the lights, 0.0 is off, 1.0 is full.",
            }
        },
        "required": ["brightness"],
    },
}

JavaScript

import { Type } from '@google/genai';

const powerDiscoBall = {
  name: 'power_disco_ball',
  description: 'Powers the spinning disco ball.',
  parameters: {
    type: Type.OBJECT,
    properties: {
      power: {
        type: Type.BOOLEAN,
        description: 'Whether to turn the disco ball on or off.'
      }
    },
    required: ['power']
  }
};

const startMusic = {
  name: 'start_music',
  description: 'Play some music matching the specified parameters.',
  parameters: {
    type: Type.OBJECT,
    properties: {
      energetic: {
        type: Type.BOOLEAN,
        description: 'Whether the music is energetic or not.'
      },
      loud: {
        type: Type.BOOLEAN,
        description: 'Whether the music is loud or not.'
      }
    },
    required: ['energetic', 'loud']
  }
};

const dimLights = {
  name: 'dim_lights',
  description: 'Dim the lights.',
  parameters: {
    type: Type.OBJECT,
    properties: {
      brightness: {
        type: Type.NUMBER,
        description: 'The brightness of the lights, 0.0 is off, 1.0 is full.'
      }
    },
    required: ['brightness']
  }
};

फ़ंक्शन कॉल करने के मोड को कॉन्फ़िगर करें, ताकि बताए गए सभी टूल का इस्तेमाल किया जा सके. ज़्यादा जानने के लिए, फ़ंक्शन कॉलिंग को कॉन्फ़िगर करने के बारे में पढ़ें.

Python

from google import genai
from google.genai import types

# Configure the client and tools
client = genai.Client()
house_tools = [
    types.Tool(function_declarations=[power_disco_ball, start_music, dim_lights])
]
config = types.GenerateContentConfig(
    tools=house_tools,
    automatic_function_calling=types.AutomaticFunctionCallingConfig(
        disable=True
    ),
    # Force the model to call 'any' function, instead of chatting.
    tool_config=types.ToolConfig(
        function_calling_config=types.FunctionCallingConfig(mode='ANY')
    ),
)

chat = client.chats.create(model="gemini-2.5-flash", config=config)
response = chat.send_message("Turn this place into a party!")

# Print out each of the function calls requested from this single call
print("Example 1: Forced function calling")
for fn in response.function_calls:
    args = ", ".join(f"{key}={val}" for key, val in fn.args.items())
    print(f"{fn.name}({args})")

JavaScript

import { GoogleGenAI } from '@google/genai';

// Set up function declarations
const houseFns = [powerDiscoBall, startMusic, dimLights];

const config = {
    tools: [{
        functionDeclarations: houseFns
    }],
    // Force the model to call 'any' function, instead of chatting.
    toolConfig: {
        functionCallingConfig: {
            mode: 'any'
        }
    }
};

// Configure the client
const ai = new GoogleGenAI({});

// Create a chat session
const chat = ai.chats.create({
    model: 'gemini-2.5-flash',
    config: config
});
const response = await chat.sendMessage({message: 'Turn this place into a party!'});

// Print out each of the function calls requested from this single call
console.log("Example 1: Forced function calling");
for (const fn of response.functionCalls) {
    const args = Object.entries(fn.args)
        .map(([key, val]) => `${key}=${val}`)
        .join(', ');
    console.log(`${fn.name}(${args})`);
}

प्रिंट किए गए हर नतीजे में, फ़ंक्शन कॉल को दिखाया गया है. मॉडल ने इसके लिए अनुरोध किया था. नतीजे वापस भेजने के लिए, जवाबों को उसी क्रम में शामिल करें जिस क्रम में अनुरोध किए गए थे.

Python SDK, फ़ंक्शन को अपने-आप कॉल करने की सुविधा के साथ काम करता है. यह सुविधा, Python फ़ंक्शन को अपने-आप डिक्लेरेशन में बदल देती है. साथ ही, फ़ंक्शन कॉल के एक्ज़ीक्यूशन और रिस्पॉन्स साइकल को मैनेज करती है. डिस्को के इस्तेमाल का उदाहरण यहां दिया गया है.

Python

from google import genai
from google.genai import types

# Actual function implementations
def power_disco_ball_impl(power: bool) -> dict:
    """Powers the spinning disco ball.

    Args:
        power: Whether to turn the disco ball on or off.

    Returns:
        A status dictionary indicating the current state.
    """
    return {"status": f"Disco ball powered {'on' if power else 'off'}"}

def start_music_impl(energetic: bool, loud: bool) -> dict:
    """Play some music matching the specified parameters.

    Args:
        energetic: Whether the music is energetic or not.
        loud: Whether the music is loud or not.

    Returns:
        A dictionary containing the music settings.
    """
    music_type = "energetic" if energetic else "chill"
    volume = "loud" if loud else "quiet"
    return {"music_type": music_type, "volume": volume}

def dim_lights_impl(brightness: float) -> dict:
    """Dim the lights.

    Args:
        brightness: The brightness of the lights, 0.0 is off, 1.0 is full.

    Returns:
        A dictionary containing the new brightness setting.
    """
    return {"brightness": brightness}

# Configure the client
client = genai.Client()
config = types.GenerateContentConfig(
    tools=[power_disco_ball_impl, start_music_impl, dim_lights_impl]
)

# Make the request
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Do everything you need to this place into party!",
    config=config,
)

print("\nExample 2: Automatic function calling")
print(response.text)
# I've turned on the disco ball, started playing loud and energetic music, and dimmed the lights to 50% brightness. Let's get this party started!

कंपोज़िशनल फ़ंक्शन कॉलिंग

कंपोज़िशनल या सीक्वेंशियल फ़ंक्शन कॉलिंग की मदद से, Gemini एक साथ कई फ़ंक्शन कॉल कर सकता है, ताकि किसी मुश्किल अनुरोध को पूरा किया जा सके. उदाहरण के लिए, "मेरी मौजूदा जगह का तापमान बताओ" सवाल का जवाब देने के लिए, Gemini API पहले get_current_location() फ़ंक्शन को कॉल कर सकता है. इसके बाद, वह get_weather() फ़ंक्शन को कॉल कर सकता है, जो जगह की जानकारी को पैरामीटर के तौर पर लेता है.

यहां दिए गए उदाहरण में, Python SDK और फ़ंक्शन को अपने-आप कॉल करने की सुविधा का इस्तेमाल करके, कंपोज़िशनल फ़ंक्शन कॉल करने का तरीका बताया गया है.

Python

इस उदाहरण में, google-genai Python SDK की फ़ंक्शन कॉल करने की सुविधा का इस्तेमाल किया गया है. SDK, Python फ़ंक्शन को ज़रूरी स्कीमा में अपने-आप बदल देता है. साथ ही, मॉडल के अनुरोध करने पर फ़ंक्शन कॉल को लागू करता है और टास्क पूरा करने के लिए, नतीजे वापस मॉडल को भेजता है.

import os
from google import genai
from google.genai import types

# Example Functions
def get_weather_forecast(location: str) -> dict:
    """Gets the current weather temperature for a given location."""
    print(f"Tool Call: get_weather_forecast(location={location})")
    # TODO: Make API call
    print("Tool Response: {'temperature': 25, 'unit': 'celsius'}")
    return {"temperature": 25, "unit": "celsius"}  # Dummy response

def set_thermostat_temperature(temperature: int) -> dict:
    """Sets the thermostat to a desired temperature."""
    print(f"Tool Call: set_thermostat_temperature(temperature={temperature})")
    # TODO: Interact with a thermostat API
    print("Tool Response: {'status': 'success'}")
    return {"status": "success"}

# Configure the client and model
client = genai.Client()
config = types.GenerateContentConfig(
    tools=[get_weather_forecast, set_thermostat_temperature]
)

# Make the request
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="If it's warmer than 20°C in London, set the thermostat to 20°C, otherwise set it to 18°C.",
    config=config,
)

# Print the final, user-facing response
print(response.text)

अनुमानित आउटपुट

कोड चलाने पर, आपको दिखेगा कि SDK फ़ंक्शन कॉल को व्यवस्थित कर रहा है. मॉडल पहले get_weather_forecast को कॉल करता है, तापमान की जानकारी पाता है, और फिर प्रॉम्प्ट में दिए गए लॉजिक के आधार पर सही वैल्यू के साथ set_thermostat_temperature को कॉल करता है.

Tool Call: get_weather_forecast(location=London)
Tool Response: {'temperature': 25, 'unit': 'celsius'}
Tool Call: set_thermostat_temperature(temperature=20)
Tool Response: {'status': 'success'}
OK. I've set the thermostat to 20°C.

JavaScript

इस उदाहरण में, मैन्युअल एक्ज़ीक्यूशन लूप का इस्तेमाल करके, कंपोज़िशनल फ़ंक्शन कॉलिंग करने के लिए JavaScript/TypeScript SDK का इस्तेमाल करने का तरीका बताया गया है.

import { GoogleGenAI, Type } from "@google/genai";

// Configure the client
const ai = new GoogleGenAI({});

// Example Functions
function get_weather_forecast({ location }) {
  console.log(`Tool Call: get_weather_forecast(location=${location})`);
  // TODO: Make API call
  console.log("Tool Response: {'temperature': 25, 'unit': 'celsius'}");
  return { temperature: 25, unit: "celsius" };
}

function set_thermostat_temperature({ temperature }) {
  console.log(
    `Tool Call: set_thermostat_temperature(temperature=${temperature})`,
  );
  // TODO: Make API call
  console.log("Tool Response: {'status': 'success'}");
  return { status: "success" };
}

const toolFunctions = {
  get_weather_forecast,
  set_thermostat_temperature,
};

const tools = [
  {
    functionDeclarations: [
      {
        name: "get_weather_forecast",
        description:
          "Gets the current weather temperature for a given location.",
        parameters: {
          type: Type.OBJECT,
          properties: {
            location: {
              type: Type.STRING,
            },
          },
          required: ["location"],
        },
      },
      {
        name: "set_thermostat_temperature",
        description: "Sets the thermostat to a desired temperature.",
        parameters: {
          type: Type.OBJECT,
          properties: {
            temperature: {
              type: Type.NUMBER,
            },
          },
          required: ["temperature"],
        },
      },
    ],
  },
];

// Prompt for the model
let contents = [
  {
    role: "user",
    parts: [
      {
        text: "If it's warmer than 20°C in London, set the thermostat to 20°C, otherwise set it to 18°C.",
      },
    ],
  },
];

// Loop until the model has no more function calls to make
while (true) {
  const result = await ai.models.generateContent({
    model: "gemini-2.5-flash",
    contents,
    config: { tools },
  });

  if (result.functionCalls && result.functionCalls.length > 0) {
    const functionCall = result.functionCalls[0];

    const { name, args } = functionCall;

    if (!toolFunctions[name]) {
      throw new Error(`Unknown function call: ${name}`);
    }

    // Call the function and get the response.
    const toolResponse = toolFunctions[name](args);

    const functionResponsePart = {
      name: functionCall.name,
      response: {
        result: toolResponse,
      },
    };

    // Send the function response back to the model.
    contents.push({
      role: "model",
      parts: [
        {
          functionCall: functionCall,
        },
      ],
    });
    contents.push({
      role: "user",
      parts: [
        {
          functionResponse: functionResponsePart,
        },
      ],
    });
  } else {
    // No more function calls, break the loop.
    console.log(result.text);
    break;
  }
}

अनुमानित आउटपुट

कोड चलाने पर, आपको दिखेगा कि SDK फ़ंक्शन कॉल को व्यवस्थित कर रहा है. मॉडल पहले get_weather_forecast को कॉल करता है, तापमान की जानकारी पाता है, और फिर प्रॉम्प्ट में दिए गए लॉजिक के आधार पर सही वैल्यू के साथ set_thermostat_temperature को कॉल करता है.

Tool Call: get_weather_forecast(location=London)
Tool Response: {'temperature': 25, 'unit': 'celsius'}
Tool Call: set_thermostat_temperature(temperature=20)
Tool Response: {'status': 'success'}
OK. It's 25°C in London, so I've set the thermostat to 20°C.

फ़ंक्शन कॉलिंग की सुविधा, LiveAPI की नेटिव सुविधा है. इसका मतलब है कि Live API, Python SDK की तरह फ़ंक्शन कॉल को हैंडल कर सकता है.

Python

# Light control schemas
turn_on_the_lights_schema = {'name': 'turn_on_the_lights'}
turn_off_the_lights_schema = {'name': 'turn_off_the_lights'}

prompt = """
  Hey, can you write run some python code to turn on the lights, wait 10s and then turn off the lights?
  """

tools = [
    {'code_execution': {}},
    {'function_declarations': [turn_on_the_lights_schema, turn_off_the_lights_schema]}
]

await run(prompt, tools=tools, modality="AUDIO")

JavaScript

// Light control schemas
const turnOnTheLightsSchema = { name: 'turn_on_the_lights' };
const turnOffTheLightsSchema = { name: 'turn_off_the_lights' };

const prompt = `
  Hey, can you write run some python code to turn on the lights, wait 10s and then turn off the lights?
`;

const tools = [
  { codeExecution: {} },
  { functionDeclarations: [turnOnTheLightsSchema, turnOffTheLightsSchema] }
];

await run(prompt, tools=tools, modality="AUDIO")

फ़ंक्शन कॉल करने के मोड

Gemini API की मदद से, यह कंट्रोल किया जा सकता है कि मॉडल, दिए गए टूल (फ़ंक्शन के बारे में जानकारी) का इस्तेमाल कैसे करे. खास तौर पर, function_calling_config में जाकर मोड सेट किया जा सकता है.

  • AUTO (Default): मॉडल यह तय करता है कि प्रॉम्प्ट और कॉन्टेक्स्ट के आधार पर, नैचुरल लैंग्वेज में जवाब जनरेट करना है या फ़ंक्शन कॉल का सुझाव देना है. यह सबसे ज़्यादा फ़्लेक्सिबल मोड है. हमारा सुझाव है कि ज़्यादातर स्थितियों में इसका इस्तेमाल करें.
  • ANY: इस मॉडल को हमेशा फ़ंक्शन कॉल का अनुमान लगाने के लिए बाध्य किया जाता है. साथ ही, यह फ़ंक्शन स्कीमा के मुताबिक काम करने की गारंटी देता है. अगर allowed_function_names तय नहीं किया गया है, तो मॉडल, दिए गए किसी भी फ़ंक्शन के एलान में से कोई एक चुन सकता है. अगर allowed_function_names को सूची के तौर पर दिया जाता है, तो मॉडल सिर्फ़ उस सूची में मौजूद फ़ंक्शन चुन सकता है. इस मोड का इस्तेमाल तब करें, जब आपको हर प्रॉम्प्ट के लिए फ़ंक्शन कॉल के जवाब की ज़रूरत हो (अगर लागू हो).
  • NONE: मॉडल को फ़ंक्शन कॉल करने की अनुमति नहीं है. यह फ़ंक्शन के बारे में कोई जानकारी दिए बिना अनुरोध भेजने के बराबर है. इसका इस्तेमाल, टूल की परिभाषाओं को हटाए बिना, फ़ंक्शन कॉलिंग को कुछ समय के लिए बंद करने के लिए करें.

Python

from google.genai import types

# Configure function calling mode
tool_config = types.ToolConfig(
    function_calling_config=types.FunctionCallingConfig(
        mode="ANY", allowed_function_names=["get_current_temperature"]
    )
)

# Create the generation config
config = types.GenerateContentConfig(
    tools=[tools],  # not defined here.
    tool_config=tool_config,
)

JavaScript

import { FunctionCallingConfigMode } from '@google/genai';

// Configure function calling mode
const toolConfig = {
  functionCallingConfig: {
    mode: FunctionCallingConfigMode.ANY,
    allowedFunctionNames: ['get_current_temperature']
  }
};

// Create the generation config
const config = {
  tools: tools, // not defined here.
  toolConfig: toolConfig,
};

फ़ंक्शन को अपने-आप कॉल करने की सुविधा (सिर्फ़ Python के लिए)

Python SDK का इस्तेमाल करते समय, Python फ़ंक्शन को सीधे तौर पर टूल के तौर पर इस्तेमाल किया जा सकता है. SDK, Python फ़ंक्शन को अपने-आप डिक्लेरेशन में बदल देता है. साथ ही, आपके लिए फ़ंक्शन कॉल को एक्ज़ीक्यूट करता है और रिस्पॉन्स साइकल को मैनेज करता है. इसके बाद, Python SDK अपने-आप ये काम करता है:

  1. यह मॉडल से मिले फ़ंक्शन कॉल के जवाबों का पता लगाता है.
  2. अपने कोड में, उससे जुड़े Python फ़ंक्शन को कॉल करें.
  3. इस फ़ंक्शन से, मॉडल को जवाब वापस भेजा जाता है.
  4. यह फ़ंक्शन, मॉडल से मिले जवाब का आखिरी टेक्स्ट दिखाता है.

इसका इस्तेमाल करने के लिए, टाइप हिंट और डॉकस्ट्रिंग के साथ अपना फ़ंक्शन तय करें. इसके बाद, फ़ंक्शन को टूल के तौर पर पास करें. JSON डिक्लेरेशन को टूल के तौर पर पास न करें:

Python

from google import genai
from google.genai import types

# Define the function with type hints and docstring
def get_current_temperature(location: str) -> dict:
    """Gets the current temperature for a given location.

    Args:
        location: The city and state, e.g. San Francisco, CA

    Returns:
        A dictionary containing the temperature and unit.
    """
    # ... (implementation) ...
    return {"temperature": 25, "unit": "Celsius"}

# Configure the client
client = genai.Client()
config = types.GenerateContentConfig(
    tools=[get_current_temperature]
)  # Pass the function itself

# Make the request
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="What's the temperature in Boston?",
    config=config,
)

print(response.text)  # The SDK handles the function call and returns the final text

इन तरीकों से, फ़ंक्शन को अपने-आप कॉल करने की सुविधा बंद की जा सकती है:

Python

config = types.GenerateContentConfig(
    tools=[get_current_temperature],
    automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True)
)

फ़ंक्शन स्कीमा का अपने-आप एलान होना

Python फ़ंक्शन से स्कीमा अपने-आप निकालने की सुविधा, हर मामले में काम नहीं करती. उदाहरण के लिए, यह उन मामलों को हैंडल नहीं करता है जहां आपको नेस्ट किए गए डिक्शनरी-ऑब्जेक्ट के फ़ील्ड के बारे में बताना होता है. यह एपीआई, इनमें से किसी भी टाइप के बारे में जानकारी दे सकता है:

Python

AllowedType = (int | float | bool | str | list['AllowedType'] | dict[str, AllowedType])

अनुमानित स्कीमा देखने के लिए, इसे from_callable का इस्तेमाल करके बदला जा सकता है:

Python

def multiply(a: float, b: float):
    """Returns a * b."""
    return a * b

fn_decl = types.FunctionDeclaration.from_callable(callable=multiply, client=client)

# to_json_dict() provides a clean JSON representation.
print(fn_decl.to_json_dict())

एक से ज़्यादा टूल का इस्तेमाल करना: नेटिव टूल को फ़ंक्शन कॉलिंग के साथ जोड़ना

एक ही समय पर, कई टूल चालू किए जा सकते हैं. इनमें नेटिव टूल के साथ-साथ फ़ंक्शन कॉलिंग की सुविधा वाले टूल भी शामिल हैं. यहां एक ऐसा उदाहरण दिया गया है जिसमें Live API का इस्तेमाल करके किए गए अनुरोध में, दो टूल चालू किए गए हैं: Google Search से मिली जानकारी का इस्तेमाल करना और कोड को लागू करना.

Python

# Multiple tasks example - combining lights, code execution, and search
prompt = """
  Hey, I need you to do three things for me.

    1.  Turn on the lights.
    2.  Then compute the largest prime palindrome under 100000.
    3.  Then use Google Search to look up information about the largest earthquake in California the week of Dec 5 2024.

  Thanks!
  """

tools = [
    {'google_search': {}},
    {'code_execution': {}},
    {'function_declarations': [turn_on_the_lights_schema, turn_off_the_lights_schema]} # not defined here.
]

# Execute the prompt with specified tools in audio modality
await run(prompt, tools=tools, modality="AUDIO")

JavaScript

// Multiple tasks example - combining lights, code execution, and search
const prompt = `
  Hey, I need you to do three things for me.

    1.  Turn on the lights.
    2.  Then compute the largest prime palindrome under 100000.
    3.  Then use Google Search to look up information about the largest earthquake in California the week of Dec 5 2024.

  Thanks!
`;

const tools = [
  { googleSearch: {} },
  { codeExecution: {} },
  { functionDeclarations: [turnOnTheLightsSchema, turnOffTheLightsSchema] } // not defined here.
];

// Execute the prompt with specified tools in audio modality
await run(prompt, {tools: tools, modality: "AUDIO"});

Python डेवलपर, लाइव एपीआई टूल का इस्तेमाल करने से जुड़ी नोटबुक में जाकर इसे आज़मा सकते हैं.

मॉडल कॉन्टेक्स्ट प्रोटोकॉल (एमसीपी)

मॉडल कॉन्टेक्स्ट प्रोटोकॉल (एमसीपी), एआई ऐप्लिकेशन को बाहरी टूल और डेटा से कनेक्ट करने के लिए एक ओपन स्टैंडर्ड है. एमसीपी, मॉडल को कॉन्टेक्स्ट ऐक्सेस करने के लिए एक सामान्य प्रोटोकॉल उपलब्ध कराता है. जैसे, फ़ंक्शन (टूल), डेटा सोर्स (संसाधन) या पहले से तय किए गए प्रॉम्प्ट.

Gemini SDK में, एमसीपी के लिए पहले से ही सहायता उपलब्ध है. इससे बॉयलरप्लेट कोड कम हो जाता है. साथ ही, एमसीपी टूल के लिए टूल अपने-आप कॉल होने की सुविधा मिलती है. जब मॉडल, एमसीपी टूल कॉल जनरेट करता है, तब Python और JavaScript क्लाइंट एसडीके, एमसीपी टूल को अपने-आप लागू कर सकते हैं. साथ ही, जवाब को अगले अनुरोध में मॉडल को वापस भेज सकते हैं. यह लूप तब तक जारी रहता है, जब तक मॉडल कोई और टूल कॉल नहीं करता.

यहां Gemini और mcp एसडीके के साथ लोकल एमसीपी सर्वर इस्तेमाल करने का एक उदाहरण दिया गया है.

Python

पक्का करें कि आपके चुने हुए प्लैटफ़ॉर्म पर, mcp SDK का नया वर्शन इंस्टॉल हो.

pip install mcp
import os
import asyncio
from datetime import datetime
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from google import genai

client = genai.Client()

# Create server parameters for stdio connection
server_params = StdioServerParameters(
    command="npx",  # Executable
    args=["-y", "@philschmid/weather-mcp"],  # MCP Server
    env=None,  # Optional environment variables
)

async def run():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            # Prompt to get the weather for the current day in London.
            prompt = f"What is the weather in London in {datetime.now().strftime('%Y-%m-%d')}?"

            # Initialize the connection between client and server
            await session.initialize()

            # Send request to the model with MCP function declarations
            response = await client.aio.models.generate_content(
                model="gemini-2.5-flash",
                contents=prompt,
                config=genai.types.GenerateContentConfig(
                    temperature=0,
                    tools=[session],  # uses the session, will automatically call the tool
                    # Uncomment if you **don't** want the SDK to automatically call the tool
                    # automatic_function_calling=genai.types.AutomaticFunctionCallingConfig(
                    #     disable=True
                    # ),
                ),
            )
            print(response.text)

# Start the asyncio event loop and run the main function
asyncio.run(run())

JavaScript

पक्का करें कि आपके चुने हुए प्लैटफ़ॉर्म पर mcp SDK का नया वर्शन इंस्टॉल हो.

npm install @modelcontextprotocol/sdk
import { GoogleGenAI, FunctionCallingConfigMode , mcpToTool} from '@google/genai';
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";

// Create server parameters for stdio connection
const serverParams = new StdioClientTransport({
  command: "npx", // Executable
  args: ["-y", "@philschmid/weather-mcp"] // MCP Server
});

const client = new Client(
  {
    name: "example-client",
    version: "1.0.0"
  }
);

// Configure the client
const ai = new GoogleGenAI({});

// Initialize the connection between client and server
await client.connect(serverParams);

// Send request to the model with MCP tools
const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: `What is the weather in London in ${new Date().toLocaleDateString()}?`,
  config: {
    tools: [mcpToTool(client)],  // uses the session, will automatically call the tool
    // Uncomment if you **don't** want the sdk to automatically call the tool
    // automaticFunctionCalling: {
    //   disable: true,
    // },
  },
});
console.log(response.text)

// Close the connection
await client.close();

बिल्ट-इन एमसीपी की सुविधा से जुड़ी सीमाएं

हमारे एसडीके में, एमसीपी की सुविधा पहले से मौजूद होती है. हालांकि, यह एक्सपेरिमेंट के तौर पर उपलब्ध है. साथ ही, इसमें ये सीमाएं हैं:

  • सिर्फ़ टूल इस्तेमाल किए जा सकते हैं. संसाधन या प्रॉम्प्ट नहीं
  • यह Python और JavaScript/TypeScript SDK के लिए उपलब्ध है.
  • आने वाली रिलीज़ में, बड़े बदलाव हो सकते हैं.

अगर ये सर्वर आपकी ज़रूरत के हिसाब से काम नहीं कर रहे हैं, तो MCP सर्वर को मैन्युअल तरीके से इंटिग्रेट किया जा सकता है.

इन मॉडल के साथ काम करता है

इस सेक्शन में, मॉडल और उनके फ़ंक्शन कॉल करने की क्षमताओं के बारे में बताया गया है. इसमें एक्सपेरिमेंट के तौर पर उपलब्ध मॉडल शामिल नहीं हैं. मॉडल की खास जानकारी वाले पेज पर, आपको मॉडल की क्षमताओं के बारे में पूरी जानकारी मिल सकती है.

मॉडल फ़ंक्शन कॉलिंग पैरलल फ़ंक्शन कॉलिंग कॉम्पोज़िशनल फ़ंक्शन कॉलिंग
Gemini 2.5 Pro ✔️ ✔️ ✔️
Gemini 2.5 Flash ✔️ ✔️ ✔️
Gemini 2.5 Flash-Lite ✔️ ✔️ ✔️
Gemini 2.0 Flash ✔️ ✔️ ✔️
Gemini 2.0 Flash-Lite X X X

सबसे सही तरीके

  • फ़ंक्शन और पैरामीटर के बारे में जानकारी: जानकारी देते समय, साफ़ तौर पर और सटीक तरीके से बताएं. सही फ़ंक्शन चुनने और सही तर्क देने के लिए, मॉडल इन पर निर्भर करता है.
  • नाम: फ़ंक्शन के ऐसे नाम इस्तेमाल करें जिनसे उनके काम के बारे में पता चलता हो. इन नामों में स्पेस, अवधि या डैश नहीं होने चाहिए.
  • स्ट्रॉन्ग टाइपिंग: गड़बड़ियों को कम करने के लिए, पैरामीटर के लिए खास टाइप (पूर्णांक, स्ट्रिंग, एनम) का इस्तेमाल करें. अगर किसी पैरामीटर के लिए मान्य वैल्यू का सेट सीमित है, तो enum का इस्तेमाल करें.
  • टूल चुनना: मॉडल, कई टूल का इस्तेमाल कर सकता है. हालांकि, बहुत ज़्यादा टूल उपलब्ध कराने से, गलत या ज़रूरत के हिसाब से सही न होने वाले टूल को चुनने का जोखिम बढ़ सकता है. सबसे अच्छे नतीजे पाने के लिए, सिर्फ़ काम के टूल उपलब्ध कराएं. साथ ही, कोशिश करें कि ऐक्टिव टूल की संख्या 10 से 20 के बीच हो. अगर आपके पास कई टूल हैं, तो बातचीत के कॉन्टेक्स्ट के आधार पर डाइनैमिक टूल चुनने की सुविधा का इस्तेमाल करें.
  • प्रॉम्प्ट इंजीनियरिंग:
    • कॉन्टेक्स्ट दें: मॉडल को उसकी भूमिका के बारे में बताएं. जैसे, "तुम मौसम की जानकारी देने वाली एक मददगार असिस्टेंट हो.").
    • निर्देश देना: यह तय करें कि फ़ंक्शन का इस्तेमाल कब और कैसे करना है (जैसे, "तारीखों का अनुमान न लगाएं; अनुमानों के लिए हमेशा आने वाले समय की तारीख का इस्तेमाल करें.").
    • ज़्यादा जानकारी पाने के लिए सवाल पूछने को बढ़ावा देना: मॉडल को निर्देश दें कि अगर ज़रूरत हो, तो वह सवाल के बारे में ज़्यादा जानकारी पाने के लिए सवाल पूछे.
  • तापमान: कम तापमान का इस्तेमाल करें. जैसे, 0) का इस्तेमाल करें, ताकि फ़ंक्शन कॉल ज़्यादा भरोसेमंद तरीके से किए जा सकें.
  • पुष्टि करना: अगर फ़ंक्शन कॉल के गंभीर नतीजे होते हैं (जैसे कि ऑर्डर देना), तो उसे लागू करने से पहले उपयोगकर्ता से कॉल की पुष्टि करें.
  • गड़बड़ी ठीक करना: अपने फ़ंक्शन में गड़बड़ी ठीक करने की सुविधा लागू करें, ताकि अचानक मिलने वाले इनपुट या एपीआई के काम न करने की समस्या को आसानी से ठीक किया जा सके. ऐसे सूचना देने वाले गड़बड़ी के मैसेज दिखाएं जिनका इस्तेमाल मॉडल, उपयोगकर्ता को मददगार जवाब देने के लिए कर सके.
  • सुरक्षा: बाहरी एपीआई को कॉल करते समय सुरक्षा का ध्यान रखें. पुष्टि करने और अनुमति देने के सही तरीकों का इस्तेमाल करें. फ़ंक्शन कॉल में संवेदनशील डेटा को ज़ाहिर न करें.
  • टोकन की सीमाएं: फ़ंक्शन की जानकारी और पैरामीटर, इनपुट टोकन की सीमा में गिने जाते हैं. अगर टोकन की सीमाएं पूरी हो रही हैं, तो फ़ंक्शन की संख्या या ब्यौरे की लंबाई को सीमित करें. साथ ही, मुश्किल टास्क को छोटे और ज़्यादा फ़ोकस वाले फ़ंक्शन सेट में बांटें.

अहम जानकारी और सीमाएं

  • सिर्फ़ OpenAPI स्कीमा के सबसेट का इस्तेमाल किया जा सकता है.
  • Python में इस्तेमाल किए जा सकने वाले पैरामीटर टाइप सीमित हैं.
  • फ़ंक्शन को अपने-आप कॉल करने की सुविधा, सिर्फ़ Python SDK में उपलब्ध है.