ट्यूटोरियल: Gemini API की मदद से फ़ंक्शन कॉल करना


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

Gemini मॉडल को फ़ंक्शन की जानकारी दी जा सकती है. ये ऐसे फ़ंक्शन हैं जिन्हें ऐप्लिकेशन की भाषा में लिखा जाता है. इसका मतलब है कि ये Google Cloud Functions नहीं हैं. आपकी क्वेरी को हैंडल करने में मॉडल की मदद करने के लिए, मॉडल आपसे किसी फ़ंक्शन को कॉल करने और नतीजा वापस भेजने के लिए कह सकता है.

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

अपना प्रोजेक्ट सेट अप करें

Gemini API को कॉल करने से पहले, आपको अपना प्रोजेक्ट सेट अप करना होगा. इसमें एपीआई पासकोड सेट अप करना, पब्लिशर डिपेंडेंसी में SDK टूल जोड़ना, और मॉडल शुरू करना शामिल है.

फ़ंक्शन कॉल सेट अप करें

इस ट्यूटोरियल के लिए, आपके मॉडल को नीचे दिए गए पैरामीटर के साथ काम करने वाले काल्पनिक मुद्रा एक्सचेंज एपीआई के साथ इंटरैक्ट करना होगा:

पैरामीटर टाइप ज़रूरी है ब्यौरा
currencyDate स्ट्रिंग हां
के लिए एक्सचेंज रेट फ़ेच करने की तारीख (यह हमेशा YYYY-MM-DD फ़ॉर्मैट में होनी चाहिए या कोई समयावधि न होने पर, वैल्यू latest होनी चाहिए)
currencyFrom स्ट्रिंग हां जिस मुद्रा से बदलना है
currencyTo स्ट्रिंग no इसमें बदलने के लिए मुद्रा

एपीआई अनुरोध का उदाहरण

{
  "currencyDate": "2024-04-17",
  "currencyFrom": "USD",
  "currencyTo": "SEK"
}

एपीआई से मिले रिस्पॉन्स का उदाहरण

{
  "base": "USD",
  "date": "2024-04-17",
  "rates": {"SEK": 0.091}
}

पहला चरण: एपीआई अनुरोध करने वाला फ़ंक्शन बनाएं

अगर आपने पहले से ऐसा नहीं किया है, तो एपीआई अनुरोध करने वाला फ़ंक्शन बनाकर शुरुआत करें.

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

Future<Map<String, Object?>> findExchangeRate(
  Map<String, Object?> arguments,
) async =>
    // This hypothetical API returns a JSON such as:
    // {"base":"USD","date":"2024-04-17","rates":{"SEK": 0.091}}
    {
      'date': arguments['currencyDate'],
      'base': arguments['currencyFrom'],
      'rates': <String, Object?>{arguments['currencyTo'] as String: 0.091}
    };

दूसरा चरण: फ़ंक्शन का एलान करना

फ़ंक्शन का एलान करें और जनरेटिव मॉडल को पास करें. यह ट्यूटोरियल का अगला चरण है.

फ़ंक्शन और पैरामीटर की जानकारी में ज़्यादा से ज़्यादा जानकारी शामिल करें. जनरेटिव मॉडल इस जानकारी का इस्तेमाल करके यह तय करता है कि कौनसा फ़ंक्शन चुनना है और फ़ंक्शन कॉल में पैरामीटर के लिए वैल्यू कैसे देनी है.

final exchangeRateTool = FunctionDeclaration(
    'findExchangeRate',
    'Returns the exchange rate between currencies on given date.',
    Schema(SchemaType.object, properties: {
      'currencyDate': Schema(SchemaType.string,
          description: 'A date in YYYY-MM-DD format or '
              'the exact value "latest" if a time period is not specified.'),
      'currencyFrom': Schema(SchemaType.string,
          description: 'The currency code of the currency to convert from, '
              'such as "USD".'),
      'currencyTo': Schema(SchemaType.string,
          description: 'The currency code of the currency to convert to, '
              'such as "USD".')
    }, requiredProperties: [
      'currencyDate',
      'currencyFrom'
    ]));

तीसरा चरण: मॉडल शुरू करने के दौरान फ़ंक्शन के एलान की जानकारी दें

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

final model = GenerativeModel(
  // Use a model that supports function calling, like Gemini 1.0 Pro
  // See "Supported models" in the "Introduction to function calling" page.
  model: 'gemini-1.0-pro',
  apiKey: apiKey,

  // Specify the function declaration.
  tools: [
    Tool(functionDeclarations: [exchangeRateTool])
  ],
);

चौथा चरण: फ़ंक्शन कॉल जनरेट करना

अब मॉडल को तय किए गए फ़ंक्शन के साथ निर्देश दिया जा सकता है.

हमारा सुझाव है कि फ़ंक्शन कॉलिंग का इस्तेमाल करने के लिए, चैट इंटरफ़ेस का इस्तेमाल करें. ऐसा इसलिए, क्योंकि फ़ंक्शन कॉल, चैट के मल्टी-टर्न स्ट्रक्चर में सही तरीके से फ़िट हो जाते हैं.

final chat = model.startChat();
final prompt = 'How much is 50 US dollars worth in Swedish krona?';

// Send the message to the generative model.
var response = await chat.sendMessage(Content.text(prompt));

final functionCalls = response.functionCalls.toList();
// When the model response with a function call, invoke the function.
if (functionCalls.isNotEmpty) {
  final functionCall = functionCalls.first;
  final result = switch (functionCall.name) {
    // Forward arguments to the hypothetical API.
    'findExchangeRate' => await findExchangeRate(functionCall.args),
    // Throw an exception if the model attempted to call a function that was
    // not declared.
    _ => throw UnimplementedError(
        'Function not implemented: ${functionCall.name}')
  };
  // Send the response to the model so that it can use the result to generate
  // text for the user.
  response = await chat
      .sendMessage(Content.functionResponse(functionCall.name, result));
}
// When the model responds with non-null text content, print it.
if (response.text case final text?) {
  print(text);
}