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

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

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

फ़ंक्शन कॉलिंग के चालू उदाहरण के लिए, यहां देखें: "लाइट बॉट" Notebook.

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

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

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

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

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

  • name (स्ट्रिंग): एपीआई कॉल में फ़ंक्शन के लिए यूनीक आइडेंटिफ़ायर.
  • description (स्ट्रिंग): फ़ंक्शन के बारे में पूरी जानकारी मकसद और क्षमताओं को पूरा करता हो.
  • parameters (ऑब्जेक्ट): फ़ंक्शन के लिए ज़रूरी इनपुट डेटा तय करता है.
    • type (स्ट्रिंग): इससे, कुल डेटा टाइप की जानकारी मिलती है, जैसे कि object.
    • properties (ऑब्जेक्ट): अलग-अलग पैरामीटर की सूची बनाता है. हर पैरामीटर में ये चीज़ें होती हैं:
      • type (स्ट्रिंग): पैरामीटर का डेटा टाइप, जैसे कि string, integer, boolean.
      • description (स्ट्रिंग): पैरामीटर की साफ़ तौर पर जानकारी का इस्तेमाल करें.
    • required (अरे): स्ट्रिंग का कलेक्शन, जिसमें पैरामीटर के नाम होते हैं जो इस फ़ंक्शन के काम करने के लिए ज़रूरी हैं.

cURL निर्देशों का इस्तेमाल करके किए गए फ़ंक्शन की जानकारी के कोड के उदाहरणों के लिए, फ़ंक्शन कॉलिंग के उदाहरण. इसके उदाहरणों के लिए Gemini API SDK टूल का इस्तेमाल करके, फ़ंक्शन का एलान करने के बारे में ज़्यादा जानने के लिए, फ़ंक्शन कॉलिंग का ट्यूटोरियल.

फ़ंक्शन का एलान करने के लिए सबसे सही तरीके

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

  • name: बिना खाली जगह और सटीक जानकारी देने वाले नाम इस्तेमाल करें (.) या डैश (-) वर्ण इस्तेमाल करें. इसके बजाय, अंडरस्कोर (_) वर्णों का इस्तेमाल करें या ऊंट जैसा.

  • description: फ़ंक्शन के बारे में पूरी जानकारी दें. साथ ही, ज़रूरत पड़ने पर उदाहरण भी दें. उदाहरण के लिए, find theaters के बजाय, find theaters based on location and optionally movie title that is currently playing in theaters. का इस्तेमाल करें. ज़्यादा जानकारी या अस्पष्ट जानकारी देने से बचें.

  • properties > type: मॉडल में गड़बड़ियों को कम करने के लिए, स्ट्रॉन्गली टाइप किए गए पैरामीटर का इस्तेमाल करें. उदाहरण के लिए, अगर पैरामीटर की वैल्यू किसी सीमित सेट से हैं, तो ब्यौरे में वैल्यू की सूची बनाने के बजाय, enum फ़ील्ड का इस्तेमाल करें (उदाहरण के लिए, "type": "enum", "values": ["now_playing", "upcoming"]). अगर पैरामीटर का मान हमेशा एक पूर्णांक होता है. इसके बजाय, टाइप को integer पर सेट करें number.

  • properties > description: सटीक उदाहरण और शर्तें बताएं. उदाहरण के लिए, the location to search के बजाय The city and state, e.g. San Francisco, CA or a zip code e.g. 95616 का इस्तेमाल करें.

फ़ंक्शन कॉलिंग का इस्तेमाल करते समय, सबसे सही तरीके जानने के लिए सबसे सही तरीके सेक्शन.

फ़ंक्शन कॉलिंग मोड

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

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

ANY मोड ("फ़ोर्स्ड फ़ंक्शन कॉलिंग") का इस्तेमाल किया जा रहा है यह सुविधा सिर्फ़ Gemini 1.5 Pro और Gemini 1.5 Flash मॉडल के साथ काम करती है.

allowed_function_names का एक सेट भी पास किया जा सकता है. ऐसा करने पर, उन फ़ंक्शन की संख्या सीमित हो जाती है जिन्हें मॉडल कॉल करेगा. आपको सिर्फ़ तब allowed_function_names शामिल करना चाहिए, जब मोड ANY हो. फ़ंक्शन के नाम मेल खाने चाहिए फ़ंक्शन की जानकारी के लिए नाम. मोड को ANY और allowed_function_names सेट करने पर, मॉडल दिए गए फ़ंक्शन के नामों के सेट से फ़ंक्शन कॉल का अनुमान लगाएगा.

अनुरोध के उदाहरण से लिया गया यह कोड स्निपेट, mode को ANY पर सेट करने और अनुमति वाले फ़ंक्शन की सूची तय करने का तरीका दिखाता है:

"tool_config": {
  "function_calling_config": {
    "mode": "ANY",
    "allowed_function_names": ["find_theaters", "get_showtimes"]
  },
}

फ़ंक्शन कॉल करने के उदाहरण

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

इस सुविधा के साथ cURL निर्देशों का इस्तेमाल करने पर, फ़ंक्शन और पैरामीटर tools एलिमेंट में जानकारी शामिल होती है. इसमें हर फ़ंक्शन की जानकारी tools एलिमेंट में फ़ंक्शन का नाम होता है और आपने इसका इस्तेमाल करके पैरामीटर तय किए हैं OpenAPI के साथ काम करने वाला स्कीमा, और फ़ंक्शन का ब्यौरा.

एक बार के टर्न का उदाहरण

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

यहां दिया गया कर्ल सैंपल, किसी फ़ंक्शन के ब्यौरे को पास करने का उदाहरण है. यह फ़ंक्शन, यह जानकारी दिखाता है कि कोई फ़िल्म कहां चल रही है. अनुरोध में कई फ़ंक्शन एलान शामिल हैं, जैसे कि find_movies और find_theaters.

एक बार कॉल करने वाले फ़ंक्शन के उदाहरण का अनुरोध

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": {
      "role": "user",
      "parts": {
        "text": "Which theaters in Mountain View show Barbie movie?"
    }
  },
  "tools": [
    {
      "function_declarations": [
        {
          "name": "find_movies",
          "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "description": {
                "type": "string",
                "description": "Any kind of description including category or genre, title words, attributes, etc."
              }
            },
            "required": [
              "description"
            ]
          }
        },
        {
          "name": "find_theaters",
          "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              }
            },
            "required": [
              "location"
            ]
          }
        },
        {
          "name": "get_showtimes",
          "description": "Find the start times for movies playing in a specific theater",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              },
              "theater": {
                "type": "string",
                "description": "Name of the theater"
              },
              "date": {
                "type": "string",
                "description": "Date for requested showtime"
              }
            },
            "required": [
              "location",
              "movie",
              "theater",
              "date"
            ]
          }
        }
      ]
    }
  ]
}'
    

इस कर्ल के उदाहरण के लिए रिस्पॉन्स कुछ ऐसा हो सकता है.

एक बार में काम करने वाले फ़ंक्शन के ज़रिए, कर्ल को कॉल करने के उदाहरण का जवाब

[{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "find_theaters",
              "args": {
                "movie": "Barbie",
                "location": "Mountain View, CA"
              }
            }
          }
        ]
      },
      "finishReason": "STOP",
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 9,
    "totalTokenCount": 9
  }
}]
    

किसी भी मोड का इस्तेमाल करके, एक बार में एक टर्न करने का उदाहरण

नीचे दिए गए कर्ल का उदाहरण एक मोड़ वाला उदाहरण, लेकिन यह सेट हो जाता है मोड से ANY तक:

"tool_config": {
  "function_calling_config": {
    "mode": "ANY"
  },
}

किसी भी मोड का इस्तेमाल करके सिंगल-टर्न फ़ंक्शन कॉलिंग (अनुरोध)

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": {
      "role": "user",
      "parts": {
        "text": "What movies are showing in North Seattle tonight?"
    }
  },
  "tools": [
    {
      "function_declarations": [
        {
          "name": "find_movies",
          "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "description": {
                "type": "string",
                "description": "Any kind of description including category or genre, title words, attributes, etc."
              }
            },
            "required": [
              "description"
            ]
          }
        },
        {
          "name": "find_theaters",
          "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              }
            },
            "required": [
              "location"
            ]
          }
        },
        {
          "name": "get_showtimes",
          "description": "Find the start times for movies playing in a specific theater",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              },
              "theater": {
                "type": "string",
                "description": "Name of the theater"
              },
              "date": {
                "type": "string",
                "description": "Date for requested showtime"
              }
            },
            "required": [
              "location",
              "movie",
              "theater",
              "date"
            ]
          }
        }
      ]
    }
  ],
  "tool_config": {
    "function_calling_config": {
      "mode": "ANY"
    },
  }
}'
    

जवाब कुछ ऐसा हो सकता है:

कोई भी मोड (रिस्पॉन्स) का इस्तेमाल करके सिंगल-टर्न फ़ंक्शन कॉलिंग

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "find_movies",
              "args": {
                "description": "",
                "location": "North Seattle, WA"
              }
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "promptFeedback": {
    "safetyRatings": [
      {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HARASSMENT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "probability": "NEGLIGIBLE"
      }
    ]
  }
}
    

किसी भी मोड और अनुमति वाले फ़ंक्शन का इस्तेमाल करके एक मोड़ों वाला उदाहरण

यहां दिया गया कर्ल का उदाहरण, सिंगल-टर्न के उदाहरण से मिलता-जुलता है. हालांकि, इसमें mode को ANY पर सेट किया गया है. साथ ही, इसमें अनुमति वाले फ़ंक्शन की सूची शामिल है:

"tool_config": {
  "function_calling_config": {
    "mode": "ANY",
    "allowed_function_names": ["find_theaters", "get_showtimes"]
  },
}

किसी भी मोड और अनुमति वाले फ़ंक्शन (अनुरोध) का इस्तेमाल करके सिंगल-टर्न फ़ंक्शन कॉलिंग

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": {
      "role": "user",
      "parts": {
        "text": "What movies are showing in North Seattle tonight?"
    }
  },
  "tools": [
    {
      "function_declarations": [
        {
          "name": "find_movies",
          "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "description": {
                "type": "string",
                "description": "Any kind of description including category or genre, title words, attributes, etc."
              }
            },
            "required": [
              "description"
            ]
          }
        },
        {
          "name": "find_theaters",
          "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              }
            },
            "required": [
              "location"
            ]
          }
        },
        {
          "name": "get_showtimes",
          "description": "Find the start times for movies playing in a specific theater",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
              },
              "movie": {
                "type": "string",
                "description": "Any movie title"
              },
              "theater": {
                "type": "string",
                "description": "Name of the theater"
              },
              "date": {
                "type": "string",
                "description": "Date for requested showtime"
              }
            },
            "required": [
              "location",
              "movie",
              "theater",
              "date"
            ]
          }
        }
      ]
    }
  ],
  "tool_config": {
    "function_calling_config": {
      "mode": "ANY",
      "allowed_function_names": ["find_theaters", "get_showtimes"]
    },
  }
}'
    

यह मॉडल, find_movies फ़ंक्शन का अनुमान नहीं लगा सकता, क्योंकि यह सूची में शामिल नहीं है इसलिए, यह किसी दूसरे फ़ंक्शन का अनुमान लगाता है. रिस्पॉन्स कुछ ऐसा हो सकता है:

ANY मोड और अनुमति वाले फ़ंक्शन का इस्तेमाल करके, सिंगल-टर्न फ़ंक्शन कॉल करना (रिस्पॉन्स)

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "find_theaters",
              "args": {
                "location": "North Seattle, WA",
                "movie": null
              }
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "promptFeedback": {
    "safetyRatings": [
      {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HARASSMENT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "probability": "NEGLIGIBLE"
      }
    ]
  }
}
    

एक से ज़्यादा बार बातचीत करने के उदाहरण

एक से ज़्यादा टर्न वाले फ़ंक्शन को कॉल करने के लिए, ये काम किए जा सकते हैं:

  1. लैंग्वेज मॉडल को कॉल करके, फ़ंक्शन कॉल का जवाब पाएं. यह पहला मुड़ाव है.
  2. पहले मोड़ से मिले फ़ंक्शन कॉल के जवाब का इस्तेमाल करके, लैंग्वेज मॉडल को कॉल करें और उस फ़ंक्शन को कॉल करने पर आपको मिलने वाला फंक्शन रिस्पॉन्स. यह दूसरा चरण है.

दूसरे चरण का जवाब, पहले चरण में आपकी क्वेरी के जवाब के लिए नतीजों की खास जानकारी देता है या इसमें दूसरा फ़ंक्शन कॉल होता है. इसका इस्तेमाल करके, अपनी क्वेरी के बारे में ज़्यादा जानकारी पाई जा सकती है.

इस विषय में, एक से ज़्यादा बार घुमने वाले कर्ल के दो उदाहरण शामिल हैं:

पिछली क्वेरी के जवाब का इस्तेमाल करें

यहां दिया गया curl सैंपल, जवाब पाने के लिए, एक ही चरण वाले पिछले उदाहरण से मिले फ़ंक्शन और आर्ग्युमेंट को कॉल करता है. तरीका और पैरामीटर एक टर्न वाले उदाहरण से मिले नतीजे, इस JSON में दिए गए हैं.

"functionCall": {
  "name": "find_theaters",
  "args": {
    "movie": "Barbie",
    "location": "Mountain View, CA"
  }
}

मल्टी-टर्न फ़ंक्शन कॉल करने की सुविधा देने वाले यूआरएल का उदाहरण

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": [{
      "role": "user",
      "parts": [{
        "text": "Which theaters in Mountain View show Barbie movie?"
    }]
  }, {
    "role": "model",
    "parts": [{
      "functionCall": {
        "name": "find_theaters",
        "args": {
          "location": "Mountain View, CA",
          "movie": "Barbie"
        }
      }
    }]
  }, {
    "role": "user",
    "parts": [{
      "functionResponse": {
        "name": "find_theaters",
        "response": {
          "name": "find_theaters",
          "content": {
            "movie": "Barbie",
            "theaters": [{
              "name": "AMC Mountain View 16",
              "address": "2000 W El Camino Real, Mountain View, CA 94040"
            }, {
              "name": "Regal Edwards 14",
              "address": "245 Castro St, Mountain View, CA 94040"
            }]
          }
        }
      }
    }]
  }],
  "tools": [{
    "functionDeclarations": [{
      "name": "find_movies",
      "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "description": {
            "type": "STRING",
            "description": "Any kind of description including category or genre, title words, attributes, etc."
          }
        },
        "required": ["description"]
      }
    }, {
      "name": "find_theaters",
      "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "movie": {
            "type": "STRING",
            "description": "Any movie title"
          }
        },
        "required": ["location"]
      }
    }, {
      "name": "get_showtimes",
      "description": "Find the start times for movies playing in a specific theater",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "movie": {
            "type": "STRING",
            "description": "Any movie title"
          },
          "theater": {
            "type": "STRING",
            "description": "Name of the theater"
          },
          "date": {
            "type": "STRING",
            "description": "Date for requested showtime"
          }
        },
        "required": ["location", "movie", "theater", "date"]
      }
    }]
  }]
}'
    

इस कर्ल उदाहरण के लिए प्रतिक्रिया के रूप में find_theaters तरीका. जवाब कुछ ऐसा हो सकता है:

मल्टी-टर्न फ़ंक्शन कॉल करने की सुविधा देने वाले यूआरएल का उदाहरण

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": " OK. Barbie is showing in two theaters in Mountain View, CA: AMC Mountain View 16 and Regal Edwards 14."
          }
        ]
      }
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 9,
    "candidatesTokenCount": 27,
    "totalTokenCount": 36
  }
}
    

मॉडल को कई बार कॉल करना

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

मल्टी-टर्न फ़ंक्शन कॉल करने की सुविधा देने वाले यूआरएल का उदाहरण

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": [{
      "role": "user",
      "parts": [{
        "text": "Which theaters in Mountain View show Barbie movie?"
    }]
  }, {
    "role": "model",
    "parts": [{
      "functionCall": {
        "name": "find_theaters",
        "args": {
          "location": "Mountain View, CA",
          "movie": "Barbie"
        }
      }
    }]
  }, {
    "role": "user",
    "parts": [{
      "functionResponse": {
        "name": "find_theaters",
        "response": {
          "name": "find_theaters",
          "content": {
            "movie": "Barbie",
            "theaters": [{
              "name": "AMC Mountain View 16",
              "address": "2000 W El Camino Real, Mountain View, CA 94040"
            }, {
              "name": "Regal Edwards 14",
              "address": "245 Castro St, Mountain View, CA 94040"
            }]
          }
        }
      }
    }]
  },
  {
    "role": "model",
    "parts": [{
      "text": " OK. Barbie is showing in two theaters in Mountain View, CA: AMC Mountain View 16 and Regal Edwards 14."
    }]
  },{
    "role": "user",
    "parts": [{
      "text": "Can we recommend some comedy movies on show in Mountain View?"
    }]
  }],
  "tools": [{
    "functionDeclarations": [{
      "name": "find_movies",
      "description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "description": {
            "type": "STRING",
            "description": "Any kind of description including category or genre, title words, attributes, etc."
          }
        },
        "required": ["description"]
      }
    }, {
      "name": "find_theaters",
      "description": "find theaters based on location and optionally movie title which is currently playing in theaters",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "movie": {
            "type": "STRING",
            "description": "Any movie title"
          }
        },
        "required": ["location"]
      }
    }, {
      "name": "get_showtimes",
      "description": "Find the start times for movies playing in a specific theater",
      "parameters": {
        "type": "OBJECT",
        "properties": {
          "location": {
            "type": "STRING",
            "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
          },
          "movie": {
            "type": "STRING",
            "description": "Any movie title"
          },
          "theater": {
            "type": "STRING",
            "description": "Name of the theater"
          },
          "date": {
            "type": "STRING",
            "description": "Date for requested showtime"
          }
        },
        "required": ["location", "movie", "theater", "date"]
      }
    }]
  }]
}'
    

मल्टी-टर्न फ़ंक्शन कॉल करने की सुविधा देने वाले यूआरएल का उदाहरण

[{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "find_movies",
              "args": {
                "description": "comedy",
                "location": "Mountain View, CA"
              }
            }
          }
        ]
      },
      "finishReason": "STOP",
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 48,
    "totalTokenCount": 48
  }
}
]
    

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

ज़्यादा सटीक जानकारी पाने के लिए, आपके फ़ंक्शन कॉल की विश्वसनीयता.

उपयोगकर्ता का अनुरोध

सबसे अच्छे नतीजों के लिए, उपयोगकर्ता क्वेरी के शुरू में नीचे दी गई जानकारी जोड़ें:

  • मॉडल के लिए अतिरिक्त जानकारी. उदाहरण के लिए, You are a movie API assistant to help users find movies and showtimes based on their preferences.
  • फ़ंक्शन इस्तेमाल करने के तरीके और समय के बारे में जानकारी या निर्देश. उदाहरण के लिए, Don't make assumptions on showtimes. Always use a future date for showtimes. अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है
  • अगर उपयोगकर्ता की क्वेरी साफ़ तौर पर नहीं दी गई है, तो साफ़ तौर पर सवाल पूछने के लिए निर्देश. इसके लिए उदाहरण, Ask clarifying questions if not enough information is available to complete the request.

सैंपलिंग के पैरामीटर

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

एपीआई को कॉल करना

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