PaLM API: Python की मदद से क्विकस्टार्ट टेक्स्ट भेजें

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

इस notebook में, आपको PaLM API का इस्तेमाल करने का तरीका बताया जाएगा. यह एपीआई, आपको Google के नए बड़े लैंग्वेज मॉडल का ऐक्सेस देता है. यहां आपको PaLM API की टेक्स्ट जनरेट करने वाली सुविधाओं को इस्तेमाल करने के तरीके के बारे में जानकारी मिलेगी.

सेटअप

सबसे पहले, PaLM API Python लाइब्रेरी को डाउनलोड और इंस्टॉल करें.

pip install -q google-generativeai
import pprint
import google.generativeai as palm

कोई API कुंजी पाएं

शुरू करने के लिए, आपको एपीआई पासकोड बनाना होगा.

palm.configure(api_key='YOUR_API_KEY')

टेक्स्ट जनरेट करना

उपलब्ध मॉडल ढूंढने के लिए, palm.list_models फ़ंक्शन का इस्तेमाल करें:

models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
model = models[0].name
print(model)
models/text-bison-001

टेक्स्ट जनरेट करने के लिए, palm.generate_text तरीके का इस्तेमाल करें:

prompt = """
You are an expert at solving word problems.

Solve the following problem:

I have three houses, each with three cats.
each cat owns 4 mittens, and a hat. Each mitten was
knit from 7m of yarn, each hat from 4m.
How much yarn was needed to make all the items?

Think about it step by step, and show your work.
"""

completion = palm.generate_text(
    model=model,
    prompt=prompt,
    temperature=0,
    # The maximum length of the response
    max_output_tokens=800,
)

print(completion.result)
There are 3 houses * 3 cats / house = 9 cats. So, 9 cats * 4 mittens / cat = 36 mittens were made. Also, 9 cats * 1 hat / cat = 9 hats were made. So, 36 mittens * 7m / mitten = 252m of yarn was used for the mittens. Also, 9 hats * 4m / hat = 36m of yarn was used for the hats. In total, 252m + 36m = 288m of yarn was used.
Thus, the answer is 288.

ज़्यादा विकल्प

palm.generate_text फ़ंक्शन में कुछ अन्य आर्ग्युमेंट इस्तेमाल किए जा सकते हैं.

क्रम बंद करें

पहले जनरेट होना बंद करने के लिए, stop_sequences तर्क का इस्तेमाल करें.

उदाहरण के लिए, एलएलएम से अक्सर अंकगणित में गलतियां हो जाती हैं. <calc> टैग में समीकरणों को डालकर, मॉडल को "कैलकुलेटर का इस्तेमाल करने" के लिए कहा जा सकता है.

मॉडल को क्लोज़िंग टैग पर रोकें, ताकि आप प्रॉम्प्ट में बदलाव कर सकें:

calc_prompt = f"""
Please solve the following problem.

{prompt}

----------------

Important: Use the calculator for each step.
Don't do the arithmetic in your head. 

To use the calculator wrap an equation in <calc> tags like this: 

<calc> 3 cats * 2 hats/cat </calc> = 6

----------------

"""

equation=None
while equation is None:
    completion = palm.generate_text(
        model=model,
        prompt=calc_prompt,
        stop_sequences=['</calc>'],
        # The maximum length of the response
        max_output_tokens=800,
    )

    try:
        response, equation = completion.result.split('<calc>', maxsplit=1)
    except Exception:
        continue
print(response)
Chain-of-thought:

There are three houses, and each house has three cats, so there are 3 houses * 3 cats / house = 9 cats. Each cat has 4 mittens, so the cats need 9 cats * 4 mittens / cat = 36 mittens. Each mitten takes 7m of yarn, so 36 mittens * 7m / mitten = 252m of yarn. Each cat has a hat, and each hat takes 4m of yarn, so 9 cats * 4m / cat = 36m of yarn. So, in total, 36m + 252m = 288m of yarn were needed.

The answer should be
print(equation)
9 cats * 4 mittens / cat

यहां से, नतीजे का हिसाब लगाया जा सकता है और जारी रखने के लिए मॉडल के लिए एक नया प्रॉम्प्ट जोड़ा जा सकता है. काम को पूरी तरह से लागू करने के लिए, टेक्स्ट कैलकुलेटर का उदाहरण देखें.

उम्मीदवार

आम तौर पर, एलएलएम के बने टेक्स्ट में कुछ हद तक रैंडमनेस होता है. (एलएलएम प्राइमर में इसके बारे में ज़्यादा पढ़ें). इसका मतलब है कि एक ही इनपुट का इस्तेमाल करके, एपीआई को एक से ज़्यादा बार कॉल करने पर, आपको अलग-अलग रिस्पॉन्स मिल सकते हैं. वैकल्पिक मॉडल रिस्पॉन्स पाने के लिए, इस सुविधा का इस्तेमाल अपने फ़ायदे के लिए किया जा सकता है.

temperature आर्ग्युमेंट, रिस्पॉन्स के वैरियंस को कंट्रोल करता है. palm.Model ऑब्जेक्ट, temperature और दूसरे पैरामीटर के लिए डिफ़ॉल्ट वैल्यू देता है.

models[0]
Model(name='models/text-bison-001', base_model_id='', version='001', display_name='Text Bison', description='Model targeted for text generation.', input_token_limit=8196, output_token_limit=1024, supported_generation_methods=['generateText'], temperature=0.7, top_p=0.95, top_k=40)

candidate_count आर्ग्युमेंट, मिलने वाले रिस्पॉन्स की संख्या को कंट्रोल करता है:

completion = palm.generate_text(
    model=model,
    prompt=prompt,
    # The number of candidates to return
    candidate_count=8,
    # Set the temperature to 1.0 for more variety of responses.
    temperature=1.0,
    max_output_tokens=800,
)

print(completion.result)
In each house there are 3 cats * 4 mittens / cat = 12 mittens. In total there are 3 houses * 12 mittens / house = 36 mittens. In total there are 36 mittens * 7m / mitten = 252m of yarn for the mittens. In total there are 3 houses * 3 cats / house * 1 hat / cat = 9 hats. In total there are 9 hats * 4m / hat = 36m of yarn for the hats. In total there are 36m yarn for the hats + 252m yarn for the mittens = 288m of yarn.
The answer: 288.

जब एक से ज़्यादा उम्मीदवारों के लिए अनुरोध किया जाता है, तब भी Completion.result एट्रिब्यूट में सिर्फ़ पहला विकल्प शामिल होता है. Completion.candidates एट्रिब्यूट में ये सभी चीज़ें शामिल होती हैं:

import pprint
pprint.pprint(completion.candidates)
[{'output': 'In each house there are 3 cats * 4 mittens / cat = 12 mittens. In '
            'total there are 3 houses * 12 mittens / house = 36 mittens. In '
            'total there are 36 mittens * 7m / mitten = 252m of yarn for the '
            'mittens. In total there are 3 houses * 3 cats / house * 1 hat / '
            'cat = 9 hats. In total there are 9 hats * 4m / hat = 36m of yarn '
            'for the hats. In total there are 36m yarn for the hats + 252m '
            'yarn for the mittens = 288m of yarn.\n'
            'The answer: 288.',
  'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>}]},
 {'output': 'Each house has 3 cats, so each house needs 3 * 4 = 12 mittens. '
            "With three houses, that's 3 * 12 = 36 mittens. And each house "
            'needs 3 * 1 = 3 hats. So in total, we need 3 hats + 36 mittens = '
            '39 items. Each mitten needs 7 meters of yarn, so 39 mittens need '
            '39 * 7 = 273 meters of yarn. Each hat needs 4 meters of yarn, and '
            "we need 3 hats, so that's 4 * 3 = 12 meters of yarn. So in total, "
            'we needed 12 + 273 = 285 meters of yarn.\n'
            'Thus, the answer is 285.',
  'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>}]},
 {'output': 'There are 3 houses * 3 cats / house = 9 cats. There are 9 cats * '
            '4 mittens / cat = 36 mittens. There are 9 cats * 1 hat / cat = 9 '
            'hats. The total amount of yarn for the mittens is 36 mittens * 7m '
            '/ mitten = 252m. The total amount of yarn for the hats is 9 hats '
            '* 4m / hat = 36m. The total amount of yarn is 252m + 36m = 288m.\n'
            'Thus, the answer is 288.',
  'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>}]},
 {'output': 'There are 3 houses * 3 cats / house = 9 cats. Each cat has 4 '
            'mittens + 1 hat = 5 items. So the total number of items is 9 cats '
            '* 5 items / cat = 45 items. Thus, 45 items * 7m / item = 315m of '
            'yarn was needed.\n'
            'Thus, the answer is 315.',
  'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>}]},
 {'output': 'Chain-of-thought:\n'
            'There are 3 houses * 3 cats / house = 9 cats.\n'
            'The cats need 9 cats * 4 mittens / cat = 36 mittens.\n'
            'The cats need 9 cats * 1 hat / cat = 9 hats.\n'
            'The mittens need 36 mittens * 7m / mitten = 252m of yarn.\n'
            'The hats need 9 hats * 4m / hat = 36m of yarn.\n'
            'Therefore, the total amount of yarn needed is 252m + 36m = 288m.\n'
            '\n'
            'The answer should be 288',
  'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>}]},
 {'output': 'First find the total number of cats: 3 houses * 3 cats / house = '
            '9 cats. Then multiply that number by the number of mittens per '
            'cat to find the total number of mittens: 9 cats * 4 mittens / cat '
            '= 36 mittens. Then multiply that number by the number of meters '
            'of yarn per mitten to find the total amount of yarn used for '
            'mittens: 36 mittens * 7 meters / mitten = 252 meters. Then do the '
            'same thing for hats: 9 cats * 1 hat / cat = 9 hats. Then multiply '
            'that number by the number of meters of yarn per hat to find the '
            'total amount of yarn used for hats: 9 hats * 4 meters / hat = 36 '
            'meters. Then add the amount of yarn used for mittens and hats to '
            'find the total amount of yarn used: 36 meters + 252 meters = 288 '
            'meters.\n'
            'Thus, the answer is 288.',
  'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>}]},
 {'output': 'The total number of cats is 3 houses * 3 cats / house = 9 cats. '
            'So, the total number of mittens is 9 cats * 4 mittens / cat = 36 '
            'mittens. The total number of hats is 9 cats * 1 hat / cat = 9 '
            'hats. The total length of yarn needed to make the mittens is 36 '
            'mittens * 7 m / mitten = 252 m. The total length of yarn needed '
            'to make the hats is 9 hats * 4 m / hat = 36 m. So, the total '
            'length of yarn needed is 252 m + 36 m = 288 m.\n'
            '\n'
            'The answer: 288',
  'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>}]},
 {'output': 'There are 3 houses with 3 cats each, so 3 * 3 = 9 cats. Each cat '
            'has 4 mittens and a hat, so 9 cats * 4 mittens / cat + 9 cats * 1 '
            'hat / cat = 36 mittens and 9 hats. Each mitten takes 7m of yarn '
            'and each hat takes 4m of yarn, so the total yarn needed is 36 '
            'mittens * 7m / mitten + 9 hats * 4m / hat = 252m + 36m = 288m.\n'
            'The answer: 288.',
  'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>},
                     {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>,
                      'probability': <HarmProbability.NEGLIGIBLE: 1>}]}]

इसलिए, आपको इस समस्या का जवाब पता है. इसलिए, सॉल्व रेट की जांच करना आसान है:

import numpy as np
np.mean(['288' in c['output'] for c in completion.candidates])
0.75