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


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

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

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

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

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

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

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

पैरामीटर टाइप ज़रूरी है ब्यौरा
currencyFrom स्ट्रिंग हां जिस मुद्रा से बदलना है
currencyTo स्ट्रिंग हां इसमें बदलने के लिए मुद्रा

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

{
  "currencyFrom": "USD",
  "currencyTo": "SEK"
}

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

{
  "base": "USD",
  "rates": {"SEK": 0.091}
}

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

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

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

async function makeApiRequest(currencyFrom, currencyTo) {
  // This hypothetical API returns a JSON such as:
  // {"base":"USD","rates":{"SEK": 0.091}}
  return {
    base: currencyFrom,
    rates: { [currencyTo]: 0.091 },
  };
}

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

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

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

// Function declaration, to pass to the model.
const getExchangeRateFunctionDeclaration = {
  name: "getExchangeRate",
  parameters: {
    type: "OBJECT",
    description: "Get the exchange rate for currencies between countries",
    properties: {
      currencyFrom: {
        type: "STRING",
        description: "The currency to convert from.",
      },
      currencyTo: {
        type: "STRING",
        description: "The currency to convert to.",
      },
    },
    required: ["currencyTo", "currencyFrom"],
  },
};

// Executable function code. Put it in a map keyed by the function name
// so that you can call it once you get the name string from the model.
const functions = {
  getExchangeRate: ({ currencyFrom, currencyTo }) => {
    return makeApiRequest( currencyFrom, currencyTo)
  }
};

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

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

const { GoogleGenerativeAI } = require("@google/generative-ai");

// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);

// ...

const generativeModel = genAI.getGenerativeModel({
  // 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",

  // Specify the function declaration.
  tools: {
    functionDeclarations: [getExchangeRateFunctionDeclaration],
  },
});

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

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

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

const chat = generativeModel.startChat();
const prompt = "How much is 50 US dollars worth in Swedish krona?";

// Send the message to the model.
const result = await chat.sendMessage(prompt);

// For simplicity, this uses the first function call found.
const call = result.response.functionCalls()[0];

if (call) {
  // Call the executable function named in the function call
  // with the arguments specified in the function call and
  // let it call the hypothetical API.
  const apiResponse = await functions[call.name](call.args);

  // Send the API response back to the model so it can generate
  // a text response that can be displayed to the user.
  const result2 = await chat.sendMessage([{functionResponse: {
    name: 'getExchangeRate',
    response: apiResponse
  }}]);

  // Log the text response.
  console.log(result2.response.text());
}