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

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

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

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

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

  • प्रकार्य का नाम
  • OpenAPI के साथ काम करने वाले स्कीमा फ़ॉर्मैट में, फ़ंक्शन पैरामीटर. एक चुनिंदा सबसेट का इस्तेमाल किया जा सकता है. कर्ल का इस्तेमाल करते समय, स्कीमा को JSON का इस्तेमाल करके तय किया जाता है.
  • फ़ंक्शन का ब्यौरा (ज़रूरी नहीं). सबसे अच्छे नतीजों के लिए, हमारा सुझाव है कि आप इसमें ब्यौरा शामिल करें.

इस दस्तावेज़ में कर्ल के उदाहरण शामिल हैं, जो GenerativeModel क्लास और उसके तरीकों की मदद से REST कॉल करते हैं.

इस्तेमाल किए जा सकने वाले मॉडल

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

  • gemini-1.0-pro
  • gemini-1.0-pro-001
  • gemini-1.5-pro-latest

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

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

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

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

यहां अनुरोध का उदाहरण दिया गया है, जो मोड को 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"
      }
    ]
  }
}
    

किसी भी मोड और मंज़ूर किए गए फ़ंक्शन का इस्तेमाल करके सिंगल-टर्न का उदाहरण

यहां दिया गया कर्ल उदाहरण, सिंगल-टर्न के उदाहरण से मिलता-जुलता है. हालांकि, यह मोड को 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 फ़ंक्शन का अनुमान नहीं लगा सकता, क्योंकि यह इस्तेमाल किए जा सकने वाले फ़ंक्शन की सूची में शामिल नहीं है. इसलिए, यह किसी दूसरे फ़ंक्शन का अनुमान लगाता है. ये रिस्पॉन्स इनके जैसे हो सकते हैं:

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

{
  "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. पहले मोड़ से फ़ंक्शन कॉल रिस्पॉन्स और उस फ़ंक्शन को कॉल करने पर आपको मिलने वाले फ़ंक्शन रिस्पॉन्स का इस्तेमाल करके, लैंग्वेज मॉडल को कॉल करें. यह दूसरा मोड़ है.

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

इस विषय में मल्टी-टर्न कर्ल के दो उदाहरण शामिल हैं:

कर्ल का उदाहरण, जो पिछले मोड़ के जवाब का इस्तेमाल करता है

नीचे दिया गया कर्ल सैंपल, रिस्पॉन्स पाने के लिए एक टर्न वाले पिछले उदाहरण से मिले फ़ंक्शन और आर्ग्युमेंट को कॉल करता है. सिंगल-टर्न वाले उदाहरण में दिए गए तरीके और पैरामीटर, इस 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": "function",
    "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 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": "function",
    "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
  }
}
]
    

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

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

फ़ंक्शन के मुख्य फ़ील्ड

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

function_declarations (कलेक्शन):

  • इसमें एक या उससे ज़्यादा ऑब्जेक्ट होते हैं. हर ऑब्जेक्ट का फ़ंक्शन अलग होता है.

हर function_declarations ऑब्जेक्ट में:

  • name (स्ट्रिंग): एपीआई कॉल में फ़ंक्शन के लिए यूनीक आइडेंटिफ़ायर.
    • सबसे सही तरीका: साफ़ और जानकारी देने वाले ऐसे नामों का इस्तेमाल करें जिनमें स्पेस, पीरियड (.) या डैश (-) वर्ण न हो. इसके बजाय, अंडरस्कोर (_) वर्णों या ऊंट के केस का इस्तेमाल करें.
  • description (स्ट्रिंग): फ़ंक्शन के मकसद और क्षमताओं के बारे में पूरी जानकारी.
    • सबसे सही तरीका: फ़ंक्शन के ब्यौरे में पूरी, साफ़, और सटीक जानकारी दें. ज़रूरत पड़ने पर उदाहरण भी दें. जैसे, find theaters के बजाय, find theaters based on location and optionally movie title that is currently playing in theaters. का इस्तेमाल करें. ऐसे ब्यौरे में बहुत ज़्यादा जानकारी देने से बचें.
  • parameters (ऑब्जेक्ट): फ़ंक्शन के लिए ज़रूरी इनपुट डेटा तय करता है.
    • type (स्ट्रिंग): इससे पूरे डेटा टाइप के बारे में पता चलता है (उदाहरण के लिए, object).
    • properties (ऑब्जेक्ट):
      • यह अलग-अलग पैरामीटर की जानकारी देता है. हर पैरामीटर में:
        • type (स्ट्रिंग): पैरामीटर का डेटा टाइप (उदाहरण के लिए, string, integer, boolean).
          • सबसे सही तरीका: मॉडल गलत जानकारी को कम करने के लिए, ज़्यादा सटीक टाइप किए गए पैरामीटर का इस्तेमाल करें. उदाहरण के लिए, अगर पैरामीटर वैल्यू किसी सीमित सेट से हैं, तो ब्यौरे में वैल्यू को शामिल करने के बजाय, enum फ़ील्ड का इस्तेमाल करें (जैसे, "type": "enum", "values": ["now_playing", "upcoming"]). अगर पैरामीटर की वैल्यू हमेशा पूर्णांक होती है, तो टाइप को number के बजाय integer पर सेट करें.
        • description (स्ट्रिंग): इससे पैरामीटर के मकसद और सही फ़ॉर्मैट के बारे में साफ़ तौर पर पता चलता है.
          • सबसे सही तरीका: सटीक उदाहरण और सीमाएं दें. उदाहरण के लिए, the location to search के बजाय, The city and state, e.g. San Francisco, CA or a zip code e.g. 95616 का इस्तेमाल करें.
    • required (कलेक्शन):
      • स्ट्रिंग का कलेक्शन, जिसमें उन पैरामीटर के नाम शामिल हैं जो फ़ंक्शन के काम करने के लिए ज़रूरी हैं.

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

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

  • मॉडल के लिए अतिरिक्त जानकारी. उदाहरण के लिए, 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 या किसी दूसरी कम वैल्यू का इस्तेमाल करें. यह मॉडल को ज़्यादा भरोसेमंद नतीजे जनरेट करने और गलत जानकारी को कम करने का निर्देश देता है.

एपीआई शुरू करने की प्रक्रिया

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