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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func makeAPIRequest(currencyFrom: String,
                    currencyTo: String) -> JSONObject {
  // This hypothetical API returns a JSON such as:
  // {"base":"USD","rates":{"SEK": 0.091}}
  return [
    "base": .string(currencyFrom),
    "rates": .object([currencyTo: .number(0.091)]),
  ]
}

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

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

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

let getExchangeRate = FunctionDeclaration(
  name: "getExchangeRate",
  description: "Get the exchange rate for currencies between countries",
  parameters: [
    "currencyFrom": Schema(
      type: .string,
      description: "The currency to convert from."
    ),
    "currencyTo": Schema(
      type: .string,
      description: "The currency to convert to."
    ),
  ],
  requiredParameters: ["currencyFrom", "currencyTo"]
)

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

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

// Use a model that supports function calling, like Gemini 1.0 Pro.
// See "Supported models" in the "Introduction to function calling" page.
let generativeModel = GenerativeModel(
  name: "gemini-1.0-pro",
  apiKey: apiKey,
  // Specify the function declaration.
  tools: [Tool(functionDeclarations: [getExchangeRate])]
)

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

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

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

let chat = generativeModel.startChat()

let prompt = "How much is 50 US dollars worth in Swedish krona?"

// Send the message to the generative model
let response1 = try await chat.sendMessage(prompt)

// Check if the model responded with a function call
guard let functionCall = response1.functionCalls.first else {
  fatalError("Model did not respond with a function call.")
}
// Print an error if the returned function was not declared
guard functionCall.name == "getExchangeRate" else {
  fatalError("Unexpected function called: \(functionCall.name)")
}
// Verify that the names and types of the parameters match the declaration
guard case let .string(currencyFrom) = functionCall.args["currencyFrom"] else {
  fatalError("Missing argument: currencyFrom")
}
guard case let .string(currencyTo) = functionCall.args["currencyTo"] else {
  fatalError("Missing argument: currencyTo")
}

// Call the hypothetical API
let apiResponse = makeAPIRequest(currencyFrom: currencyFrom, currencyTo: currencyTo)

// Send the API response back to the model so it can generate a text response that can be
// displayed to the user.
let response = try await chat.sendMessage([ModelContent(
  role: "function",
  parts: [.functionResponse(FunctionResponse(
    name: functionCall.name,
    response: apiResponse
  ))]
)])

// Log the text response.
guard let modelResponse = response.text else {
  fatalError("Model did not respond with text.")
}
print(modelResponse)