Gemini API: Python के साथ फ़ंक्शन कॉल करना

ai.google.dev पर देखें Google Colab में चलाएं GitHub पर सोर्स देखें

Gemini मॉडल को फ़ंक्शन की जानकारी दी जा सकती है. आपकी क्वेरी को हैंडल करने में मॉडल की मदद करने के लिए, मॉडल आपसे किसी फ़ंक्शन को कॉल करने और नतीजा वापस भेजने के लिए कह सकता है.

सेटअप

Python SDK टूल इंस्टॉल करें

Gemini API के लिए Python SDK टूल, google-generativeai पैकेज में शामिल होता है. पीआईपी (पिक्चर में पिक्चर) का इस्तेमाल करके डिपेंडेंसी इंस्टॉल करें:

pip install -U -q google-generativeai

पैकेज इंपोर्ट करें

ज़रूरी पैकेज इंपोर्ट करें.

import pathlib
import textwrap
import time

import google.generativeai as genai


from IPython import display
from IPython.display import Markdown

def to_markdown(text):
  text = text.replace('•', '  *')
  return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))

अपनी एपीआई कुंजी सेट अप करें

Gemini API का इस्तेमाल करने से पहले, आपको एपीआई पासकोड हासिल करना होगा. अगर आपके पास पहले से कोई कुंजी नहीं है, तो Google AI Studio में सिर्फ़ एक क्लिक करके कुंजी बनाएं.

एपीआई पासकोड पाएं

Colab में, सीक्रेट मैनेजर में बाईं ओर मौजूद पैनल में " दिलचस्पी" में जाकर कुंजी जोड़ें. इसे API_KEY नाम दें.

एपीआई पासकोड मिलने के बाद, उसे SDK टूल को पास करें. आप इसे दो तरीकों से कर सकते हैं:

  • कुंजी को GOOGLE_API_KEY के एनवायरमेंट वैरिएबल में डालें. SDK टूल इसे वहां से अपने-आप चुन लेगा.
  • कुंजी को genai.configure(api_key=...) पर पास करें
try:
    # Used to securely store your API key
    from google.colab import userdata

    # Or use `os.getenv('API_KEY')` to fetch an environment variable.
    GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')
except ImportError:
    import os
    GOOGLE_API_KEY = os.environ['GOOGLE_API_KEY']

genai.configure(api_key=GOOGLE_API_KEY)

फ़ंक्शन से जुड़ी बुनियादी बातें

genai.GenerativeModel बनाते समय, फ़ंक्शन की सूची को tools आर्ग्युमेंट में पास किया जा सकता है.

def multiply(a:float, b:float):
    """returns a * b."""
    return a*b

model = genai.GenerativeModel(model_name='gemini-1.0-pro',
                              tools=[multiply])

model
genai.GenerativeModel(
    model_name='models/gemini-1.0-pro',
    generation_config={},
    safety_settings={},
    tools=<google.generativeai.types.content_types.FunctionLibrary object at 0x10e73fe90>,
)

फ़ंक्शन कॉलिंग का सुझाया गया तरीका चैट इंटरफ़ेस के ज़रिए है. इसकी मुख्य वजह यह है कि FunctionCalls, चैट के कई मोड़ों पर अच्छी तरह फ़िट होता है.

chat = model.start_chat(enable_automatic_function_calling=True)

अपने-आप फ़ंक्शन कॉलिंग की सुविधा चालू होने पर, chat.send_message आपके फ़ंक्शन को अपने-आप कॉल करता है. ऐसा तब होता है, जब मॉडल ऐसा करने के लिए कहता है.

ऐसा लगता है कि सही जवाब के साथ सिर्फ़ टेक्स्ट जवाब दिया गया है:

response = chat.send_message('I have 57 cats, each owns 44 mittens, how many mittens is that in total?')
response.text
'The total number of mittens is 2508.'
57*44
2508

अगर ChatSession.history में देखा जाए, तो आपको इवेंट का क्रम दिखेगा:

  1. आपने सवाल भेज दिया.
  2. मॉडल ने glm.FunctionCall के साथ जवाब दिया.
  3. genai.ChatSession ने फ़ंक्शन को स्थानीय तौर पर एक्ज़ीक्यूट किया और मॉडल को glm.FunctionResponse वापस भेजा.
  4. इस मॉडल ने अपने जवाब में, फ़ंक्शन आउटपुट का इस्तेमाल किया है.
for content in chat.history:
    part = content.parts[0]
    print(content.role, "->", type(part).to_dict(part))
    print('-'*80)
user -> {'text': 'I have 57 cats, each owns 44 mittens, how many mittens is that in total?'}
--------------------------------------------------------------------------------
model -> {'function_call': {'name': 'multiply', 'args': {'a': 57.0, 'b': 44.0} } }
--------------------------------------------------------------------------------
user -> {'function_response': {'name': 'multiply', 'response': {'result': 2508.0} } }
--------------------------------------------------------------------------------
model -> {'text': 'The total number of mittens is 2508.'}
--------------------------------------------------------------------------------

आम तौर पर, राज्य का डायग्राम यह होता है:

मॉडल कभी भी टेक्स्ट या FunctionCall का जवाब दे सकता है. अगर मॉडल कोई FunctionCall भेजता है, तो उपयोगकर्ता को FunctionResponse से जवाब देना होगा

मॉडल, टेक्स्ट का जवाब देने से पहले कई फ़ंक्शन कॉल का जवाब दे सकता है और फ़ंक्शन कॉल, टेक्स्ट का जवाब देने से पहले आता है.

यह सब अपने-आप मैनेज होता है. अगर आपको ज़्यादा कंट्रोल चाहिए, तो ये काम किए जा सकते हैं:

  • डिफ़ॉल्ट enable_automatic_function_calling=False को छोड़ें और glm.FunctionCall के जवाबों को खुद प्रोसेस करें.
  • आप चाहें, तो GenerativeModel.generate_content का इस्तेमाल करें, जहां आपको चैट का इतिहास भी मैनेज करना होगा.

[ज़रूरी नहीं] कम लेवल का ऐक्सेस

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

AllowedType = (int | float | bool | str | list['AllowedType'] | dict[str, AllowedType]

google.ai.generativelanguage क्लाइंट लाइब्रेरी की मदद से, लो लेवल वाले उन डेटा टाइप का ऐक्सेस दिया जा सकता है जिन पर आपको पूरा कंट्रोल मिलता है.

import google.ai.generativelanguage as glm

सबसे पहले मॉडल के _tools एट्रिब्यूट के बारे में जानें. यहां देखें कि मॉडल को पास किए गए फ़ंक्शन की जानकारी कैसे दी जाती है:

def multiply(a:float, b:float):
    """returns a * b."""
    return a*b

model = genai.GenerativeModel(model_name='gemini-1.0-pro',
                             tools=[multiply])

model._tools.to_proto()
[function_declarations {
   name: "multiply"
   description: "returns a * b."
   parameters {
     type_: OBJECT
     properties {
       key: "b"
       value {
         type_: NUMBER
       }
     }
     properties {
       key: "a"
       value {
         type_: NUMBER
       }
     }
     required: "a"
     required: "b"
   }
 }]

इससे, glm.Tool ऑब्जेक्ट की सूची मिलती है, जिसे एपीआई को भेजा जाएगा. अगर प्रिंट किए गए फ़ॉर्मैट आपको नहीं पता, तो इसकी वजह यह है कि ये Google प्रोटोबफ़ क्लास हैं. हर glm.Tool (इस मामले में 1) में glm.FunctionDeclarations की एक सूची होती है. यह फ़ंक्शन, उसके आर्ग्युमेंट के बारे में बताती है.

यहां glm क्लास का इस्तेमाल करके, एक ही गुणा करने वाले फ़ंक्शन के लिए एलान किया गया है.

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

calculator = glm.Tool(
    function_declarations=[
      glm.FunctionDeclaration(
        name='multiply',
        description="Returns the product of two numbers.",
        parameters=glm.Schema(
            type=glm.Type.OBJECT,
            properties={
                'a':glm.Schema(type=glm.Type.NUMBER),
                'b':glm.Schema(type=glm.Type.NUMBER)
            },
            required=['a','b']
        )
      )
    ])

इसी तरह, इसे JSON के साथ काम करने वाले ऑब्जेक्ट के तौर पर बताया जा सकता है:

calculator = {'function_declarations': [
      {'name': 'multiply',
       'description': 'Returns the product of two numbers.',
       'parameters': {'type_': 'OBJECT',
       'properties': {
         'a': {'type_': 'NUMBER'},
         'b': {'type_': 'NUMBER'} },
       'required': ['a', 'b']} }]}
glm.Tool(calculator)
function_declarations {
  name: "multiply"
  description: "Returns the product of two numbers."
  parameters {
    type_: OBJECT
    properties {
      key: "b"
      value {
        type_: NUMBER
      }
    }
    properties {
      key: "a"
      value {
        type_: NUMBER
      }
    }
    required: "a"
    required: "b"
  }
}

दोनों में से किसी भी तरीके से, आप glm.Tool या टूल की सूची को

model = genai.GenerativeModel('gemini-pro', tools=calculator)
chat = model.start_chat()

response = chat.send_message(
    f"What's 234551 X 325552 ?",
)

ठीक वैसे ही, जब मॉडल कैलकुलेटर के multiply फ़ंक्शन को शुरू करने के लिए glm.FunctionCall का इस्तेमाल करता है:

response.candidates
[index: 0
content {
  parts {
    function_call {
      name: "multiply"
      args {
        fields {
          key: "b"
          value {
            number_value: 325552
          }
        }
        fields {
          key: "a"
          value {
            number_value: 234551
          }
        }
      }
    }
  }
  role: "model"
}
finish_reason: STOP
]

फ़ंक्शन खुद एक्ज़ीक्यूट करें:

fc = response.candidates[0].content.parts[0].function_call
assert fc.name == 'multiply'

result = fc.args['a'] * fc.args['b']
result
76358547152.0

बातचीत जारी रखने के लिए, मॉडल को नतीजा भेजें:

response = chat.send_message(
    glm.Content(
    parts=[glm.Part(
        function_response = glm.FunctionResponse(
          name='multiply',
          response={'result': result}))]))

खास जानकारी

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