इस notebook में, PaLM के लिए Colab के मैजिक कमांड की जानकारी दी गई है. मैजिक की मदद से, Colab notebook में प्रॉम्प्ट डेवलप करना, टेस्ट करना, तुलना करना, और उनका आकलन करना आसान हो जाता है.
ai.google.dev पर देखें | Google Colab में चलाएं | GitHub पर सोर्स देखें |
सेटअप
मैजिक को इंस्टॉल और टेस्ट करने के लिए, यहां दिया गया तरीका अपनाएं.
PaLM का मैजिक इंस्टॉल करना
Colab या अन्य IPython एनवायरमेंट में, PaLM के मैजिक कमांड का इस्तेमाल करने के लिए, आपको पहले google-generativeai
Python पैकेज को डाउनलोड और इंस्टॉल करना होगा.
%pip install -q google-generativeai
PaLM का जादू लोड हो रहा है
इसके बाद, %load_ext
मैजिक का इस्तेमाल करके %%palm
मैजिक को लोड करें:
%load_ext google.generativeai.notebook
इंस्टॉलेशन की जांच करना
यह जांचने के लिए कि मैजिक कमांड सही तरीके से इंस्टॉल किए गए हैं या नहीं, %%palm --help
चलाएं. ध्यान रखें कि अगर आपके पास PaLM API पासकोड नहीं है, तो आपको एक PaLM API पासकोड की भी ज़रूरत होगी (अगला चरण देखें).
%%palm --help
usage: palm [-h] {run,compile,compare,eval} ... A system for interacting with LLMs. positional arguments: {run,compile,compare,eval} options: -h, --help show this help message and exit
PaLM API कुंजी लोड की जा रही है
PaLM API का इस्तेमाल करने के लिए, आपको एपीआई पासकोड बनाना होगा. (आपको यह चरण सिर्फ़ एक बार करना होगा.)
नोटबुक में एपीआई पासकोड सेट करें
नीचे दिया गया सेल चलाकर अपनी एपीआई कुंजी सेट करें.
%env GOOGLE_API_KEY=YOUR PALM KEY
PaLM के मैजिक कमांड: run
, compile
, compare
, और evaluate
PaLM का इस्तेमाल करके, चार अलग-अलग निर्देश दिए जा सकते हैं:
run
compile
compare
evaluate
निर्देश: palm run
run
निर्देश, सेल के कॉन्टेंट को मॉडल को भेजता है.
प्रॉम्प्ट चलाना बहुत आम बात है. इसलिए, कोई निर्देश न देने पर PaLM, run
कमांड को डिफ़ॉल्ट रूप से लागू कर देता है. उदाहरण के लिए, अगली दो सेल एक जैसी हैं.
%%palm run
The opposite of hot is
%%palm
The opposite of hot is
आउटपुट को समझना
Prompt
कॉलम में, मॉडल को भेजा गया टेक्स्ट दिखता है और text_result
कॉलम में नतीजा दिखता है. इस गाइड को पढ़ने के बाद, अन्य कॉलम भी दिखने लगेंगे.
प्रॉम्प्ट टेंप्लेट
यह ज़रूरी नहीं है कि प्रॉम्प्ट में कोई स्ट्रिंग न हो. {curly braces}
का इस्तेमाल करके, टेंप्लेट प्लेसहोल्डर का इस्तेमाल करके प्रॉम्प्ट में वैल्यू इंजेक्ट की जा सकती है.
english_words = {
# Each value here (hot, cold) will be substituted in for {word} in the prompt
'word': ['hot', 'cold']
}
%%palm --inputs english_words
The opposite of {word} is
आउटपुट को समझना
Input Num
कॉलम, सूची(सूचियों) में इनपुट शब्द के इंडेक्स को ट्रैक करता है. इन
उदाहरणों में, 0
का Input Num
, 'hot'
है और 1
, 'cold'
है.
इनपुट के कई सेट तय करना
एक बार में इनपुट के कई सेट भी तय किए जा सकते हैं.
extreme_temperatures = {
'word': ['hot', 'cold']
}
minor_temperatures = {
'word': ['warm', 'chilly']
}
%%palm --inputs extreme_temperatures minor_temperatures
The opposite of {word} is
Google Sheets से डेटा पढ़ना
PaLM की मदद से, Google Sheets में मौजूद कॉन्टेंट को भी पढ़ा और लिखा जा सकता है. Sheets का डेटा ऐक्सेस करने के लिए, आपको लॉग इन करना होगा. इस सेक्शन में, Sheets का डेटा पढ़ने पर फ़ोकस किया जाता है. बाद के सेक्शन में बताया गया है कि Google शीट पर आउटपुट कैसे लिखा जा सकता है.
लॉग इन करें और Sheets को ऐक्सेस करने की अनुमति दें
PaLM मैजिक के साथ इस्तेमाल करने के लिए, स्प्रेडशीट को फ़ॉर्मैट करना
किसी Google शीट का आईडी या यूआरएल --sheets_input_names
फ़्लैग पर पास करें, ताकि उसे टेंप्लेट डेटा के तौर पर लोड किया जा सके.
प्रॉम्प्ट टेंप्लेट में डेटा का इस्तेमाल करने के लिए, अपनी स्प्रेडशीट में इस फ़ॉर्मैट का इस्तेमाल करें:
- शीट की पहली लाइन में वैरिएबल के नाम (अपने प्रॉम्प्ट टेंप्लेट के) डालें.
- नीचे की पंक्तियों में, हर वैरिएबल की जगह डेटा डालें.
उदाहरण के लिए, अगर आपके प्रॉम्प्ट टेंप्लेट में name
और temperament
जैसे दो वैरिएबल शामिल हैं, तो आपको स्प्रेडशीट इस तरह लिखना होगी:
नाम | स्वभाव |
---|---|
मिलो | बेशर्म |
बहुत बड़ा | तनाव से दूर |
सुब्रा | शर्म |
%%palm --sheets_input_names https://docs.google.com/spreadsheets/d/1UHfpkmBqIX5RjeJcGXOevIEhMmEoKlf5f9teqwQyHqc/edit
Create a single sentence description of a monkey's personality. The monkey's name is {name} and it has a {temperament} temperament.
खुद आज़माकर देखें!
अपने डेटा का इस्तेमाल करके इसे आज़माने के लिए, एक नई शीट बनाएं और आईडी को --sheets_input_names
को पास करें. आईडी और यूआरएल के साथ-साथ, टाइटल के हिसाब से भी शीट खोजी जा सकती हैं. जैसे, %%palm --sheets_input_names "Animal adjectives"
.
Python इनपुट के साथ Sheets इनपुट को जोड़ना
शीट में इनपुट को --inputs
के साथ भी जोड़ा जा सकता है:
new_monkeys = {
'name': ['Hackerella'],
'temperament': ['clever'],
}
%%palm --inputs new_monkeys --sheets_input_names 1UHfpkmBqIX5RjeJcGXOevIEhMmEoKlf5f9teqwQyHqc 1UHfpkmBqIX5RjeJcGXOevIEhMmEoKlf5f9teqwQyHqc
Create a single sentence description of a monkey's personality. The monkey's name is {name} and it has a {temperament} temperament.
निर्देश: palm eval
किसी प्रॉम्प्ट के आउटपुट की तुलना, पहले से मौजूद सटीक डेटा से करने के लिए, %%palm eval
का इस्तेमाल करें.
test_data = {
"word": ["dog", "cat", "house"]
}
ground_truth = ["chien", "chat", "maison"]
%%palm eval --inputs test_data --ground_truth ground_truth
English: Hello
French: Bonjour
English: {word}
French:
पोस्ट प्रोसेसिंग मॉडल के आउटपुट
तथ्यों की जांच करने के लिए, आपको मॉडल आउटपुट को प्रोसेस करना पड़ सकता है.
पोस्ट-प्रोसेस फ़ंक्शन की मदद से, ऐसा फ़ंक्शन तय किया जा सकता है जो मॉडल आउटपुट को प्रोसेस करता है. eval
कमांड के मामले में, बराबरी की आखिरी जांच में सिर्फ़ नतीजे के कॉलम का इस्तेमाल किया जाता है.
प्रोसेस के बाद के नतीजों के लिए कोई फ़ंक्शन तय करने के लिए, post_process_replace_fn
डेकोरेटर का इस्तेमाल करें:
from google.generativeai.notebook import magics
# Define a function to extract only the first response.
@magics.post_process_replace_fn
def extract_and_normalize(input):
first_line, *unused = input.split('English:')
return first_line.strip().lower()
ऊपर बताया गया extract_and_normalize
फ़ंक्शन, मॉडल से आउटपुट लेगा और दोहराए गए किसी भी भाषा के जोड़े को ट्रिम करेगा. इस तरह, वह सिर्फ़ पहला जवाब छोड़ देगा. पोस्ट-प्रोसेसिंग के बारे में ज़्यादा जानने के लिए, पोस्ट-प्रोसेसिंग सेक्शन देखें.
%%palm eval --inputs test_data --ground_truth ground_truth | extract_and_normalize
English: Hello
French: Bonjour
English: {word}
French:
निर्देश: palm compile
प्लेसहोल्डर वाले प्रॉम्प्ट को Python से कॉल किए जा सकने वाले फ़ंक्शन में बदलने के लिए, %%palm compile
कमांड का इस्तेमाल करें.
सभी फ़्लैग और पोस्ट-प्रोसेसिंग को फ़ंक्शन में " कंपाइल" किया जाता है और उन्हें शुरू करने पर इनका इस्तेमाल किया जाएगा.
इस उदाहरण में, पहले के extract_and_normalize
पोस्ट-प्रोसेसिंग फ़ंक्शन का इस्तेमाल करके, translate_en_to_fr
नाम का फ़ंक्शन बनाया गया है.
%%palm compile translate_en_to_fr | extract_and_normalize
English: Hello
French: Bonjour
English: {word}
French:
'Saved function to Python variable: translate_en_to_fr'
en_words = ['cat', 'dog']
translate_en_to_fr({'word': en_words})
आउटपुट फ़ॉर्मैट
डिफ़ॉल्ट रूप से, "कंपाइल किया गया" फ़ंक्शन अपने आउटपुट को एक ऑब्जेक्ट के तौर पर दिखाता है, जो Pandas DataFrame
के तौर पर दिखेगा. हालांकि, आपके पास नतीजे वाले ऑब्जेक्ट को .as_dict()
या .as_dataframe()
की मदद से DataFrame
या डिक्शनरी में बदलने का विकल्प है.
ज़्यादा जानकारी के लिए, --outputs
फ़्लैग देखें.
results = translate_en_to_fr({'word': en_words}).as_dict()
fr_words = results['text_result']
for en, fr in zip(en_words, fr_words):
print(f'{fr} is French for {en}')
chat is French for cat chien is French for dog
निर्देश: palm compare
%%palm compare
, कंपाइल किए गए प्रॉम्प्ट चलाकर एक टेबल बनाता है. इसमें तुलना के नतीजे साथ-साथ दिखते हैं, ताकि आप अंतर देख सकें.
%%palm compile few_shot_prompt
English: Hello
French: Bonjour
English: {word}
French:
'Saved function to Python variable: few_shot_prompt'
%%palm compile zero_shot_prompt
{word} translated to French is:
'Saved function to Python variable: zero_shot_prompt'
words = {
"word": ["dog", "cat", "house"]
}
%%palm compare few_shot_prompt zero_shot_prompt --inputs words
पसंद के मुताबिक तुलना के फ़ंक्शन
डिफ़ॉल्ट रूप से, compare
सिर्फ़ यह जांच करता है कि नतीजे में दिखाए गए डेटा के बराबर है या नहीं. हालांकि, --compare_fn
फ़्लैग के साथ एक या उससे ज़्यादा कस्टम फ़ंक्शन तय किए जा सकते हैं:.
def average_word_length(lhs, rhs):
"""Count the average number of words used across prompts."""
return (len(lhs.split(' ')) + len(rhs.split(' '))) / 2
def shortest_answer(lhs, rhs):
"""Label the prompt that generated the shortest output."""
if len(lhs) < len(rhs):
return 'first'
elif len(lhs) > len(rhs):
return 'second'
else:
return 'same'
%%palm compare few_shot_prompt zero_shot_prompt --inputs words --compare_fn average_word_length shortest_answer
दूसरे निर्देश
सहायता
--help
फ़्लैग काम करने वाले उन निर्देशों को दिखाता है जिन्हें सीधे %%palm
को भेजा जा सकता है
हर निर्देश के बारे में पूरी जानकारी देने वाला दस्तावेज़ देखने के लिए, --help
जोड़ें. उदाहरण के लिए,
%%palm run --help
usage: palm run [-h] [--model_type {echo,text}] [--temperature TEMPERATURE] [--model MODEL] [--candidate_count CANDIDATE_COUNT] [--unique] [--inputs INPUTS [INPUTS ...]] [--sheets_input_names SHEETS_INPUT_NAMES [SHEETS_INPUT_NAMES ...]] [--outputs OUTPUTS [OUTPUTS ...]] [--sheets_output_names SHEETS_OUTPUT_NAMES [SHEETS_OUTPUT_NAMES ...]] options: -h, --help show this help message and exit --model_type {echo,text}, -mt {echo,text} The type of model to use. --temperature TEMPERATURE, -t TEMPERATURE Controls the randomness of the output. Must be positive. Typical values are in the range: [0.0, 1.0]. Higher values produce a more random and varied response. A temperature of zero will be deterministic. --model MODEL, -m MODEL The name of the model to use. If not provided, a default model will be used. --candidate_count CANDIDATE_COUNT, -cc CANDIDATE_COUNT The number of candidates to produce. --unique Whether to dedupe candidates returned by the model. --inputs INPUTS [INPUTS ...], -i INPUTS [INPUTS ...] Optional names of Python variables containing inputs to use to instantiate a prompt. The variable must be either: a dictionary {'key1': ['val1', 'val2'] ...}, or an instance of LLMFnInputsSource such as SheetsInput. --sheets_input_names SHEETS_INPUT_NAMES [SHEETS_INPUT_NAMES ...], -si SHEETS_INPUT_NAMES [SHEETS_INPUT_NAMES ...] Optional names of Google Sheets to read inputs from. This is equivalent to using --inputs with the names of variables that are instances of SheetsInputs, just more convenient to use. --outputs OUTPUTS [OUTPUTS ...], -o OUTPUTS [OUTPUTS ...] Optional names of Python variables to output to. If the Python variable has not already been defined, it will be created. If the variable is defined and is an instance of LLMFnOutputsSink, the outputs will be written through the sink's write_outputs() method. --sheets_output_names SHEETS_OUTPUT_NAMES [SHEETS_OUTPUT_NAMES ...], -so SHEETS_OUTPUT_NAMES [SHEETS_OUTPUT_NAMES ...] Optional names of Google Sheets to write inputs to. This is equivalent to using --outputs with the names of variables that are instances of SheetsOutputs, just more convenient to use.
मॉडल
PaLM मॉडल के जिस वैरिएंट का आपको इस्तेमाल करना है उसकी जानकारी देने के लिए, --model
फ़्लैग का इस्तेमाल करें.
काम करने वाले मॉडल वापस पाने के लिए, list_models()
तरीका देखें. PaLM मैजिक का इस्तेमाल, generateText
तरीके के साथ काम करने वाले किसी भी मॉडल के साथ किया जा सकता है.
%%palm run --model models/text-bison-001
My favourite color is
मॉडल पैरामीटर
--candidate_count
और --temperature
जैसे मॉडल पैरामीटर भी कॉन्फ़िगर किए जा सकते हैं.
%%palm run --model models/text-bison-001 --temperature 0.5
My favourite color is
डीबग करना: इको मॉडल
एक echo
मॉडल भी उपलब्ध है, जिसके लिए आपको प्रॉम्प्ट इको होगा. यह न तो कोई एपीआई कॉल करता है और न ही आपके कोटा का इस्तेमाल करता है. इसलिए, यह आउटपुट या पोस्ट-प्रोसेसिंग को टेस्ट करने का तेज़ और आसान तरीका हो सकता है.
%%palm --model_type echo
A duck's quack does not echo.
आउटपुट को Python में एक्सपोर्ट करना
टेबल में आउटपुट दिखाने के अलावा, PaLM मैजिक, मॉडल आउटपुट को Python वैरिएबल में सेव भी कर सकता है. इससे, उनमें और बदलाव किया जा सकता है या अपने नतीजे एक्सपोर्ट किए जा सकते हैं.
इस उदाहरण में, आउटपुट को Python वैरिएबल में सेव किया गया है: fave_colors
%%palm --outputs fave_colors
The best colors to wear in spring-time are
आउटपुट वैरिएबल ऐसे कस्टम ऑब्जेक्ट होते हैं जो डिफ़ॉल्ट रूप से, Pandas DataFrame
के तौर पर दिखेंगे. as_dict()
या as_pandas_dataframe()
को कॉल करके, उन्हें Python डिक्शनरी या डेटाफ़्रेम में डाला जा सकता है.
from pprint import pprint
pprint(fave_colors.as_dict())
{'Input Num': [0], 'Prompt': ['The best colors to wear in spring-time are'], 'Prompt Num': [0], 'Result Num': [0], 'text_result': ['* Pastels: These soft, muted colors are perfect for the ' 'springtime, as they are fresh and airy. Some popular pastel ' 'colors include baby blue, mint green, and pale pink.\n' '* Brights: If you want to make a statement, bright colors ' 'are a great option for spring. Some popular bright colors ' 'include fuchsia, cobalt blue, and yellow.\n' '* Neutrals: Neutral colors are always a good choice, as they ' 'can be easily dressed up or down. Some popular neutrals ' 'include beige, gray, and white.\n' '\n' 'When choosing colors to wear in the spring, it is important ' 'to consider the occasion and your personal style. For ' 'example, if you are attending a formal event, you may want ' 'to choose a more muted color palette, such as pastels or ' 'neutrals. If you are going for a more casual look, you may ' 'want to choose brighter colors, such as brights or pastels.']}
Google Sheets में लिखें
--sheets_output_names
का इस्तेमाल करके, आउटपुट को वापस Google Sheets में सेव किया जा सकता है. यह ज़रूरी है कि आपने लॉग इन किया हो और आपके पास निजी शीट ऐक्सेस करने के लिए ज़रूरी अनुमतियां हों.
इसे आज़माने के लिए, एक नई शीट बनाएं और उसे Translation results
नाम दें. इनपुट फ़्लैग की तरह, --sheets_output_names
फ़्लैग भी टेक्स्ट नाम की जगह शीट का यूआरएल या आईडी स्वीकार करता है.
%%palm --inputs english_words --sheets_output_names "Translation results"
English: Hello
French: Bonjour
English: {word}
French:
नतीजे एक नए टैब में सेव किए जाते हैं. साथ ही, इनमें वही डेटा होता है जो आपको यहां Colab में दिखता था.
कई सुझाव जनरेट करना
अगर आपको एक प्रॉम्प्ट के लिए एक से ज़्यादा आउटपुट जनरेट करना है, तो मॉडल में --candidate_count
पास किया जा सकता है. यह डिफ़ॉल्ट रूप से 1 पर सेट होता है, जिससे सिर्फ़ सबसे ऊपर का नतीजा मिलता है.
कभी-कभी सभी उम्मीदवारों के लिए मॉडल एक ही नतीजे जनरेट करेगा. इन्हें --unique
फ़्लैग के साथ फ़िल्टर किया जा सकता है, जो कैंडिडेट बैच से नतीजों की डुप्लीकेट कॉपी हटा देता है. हालांकि, एक से ज़्यादा प्रॉम्प्ट में ऐसा नहीं किया जा सकता.
%%palm run --temperature 1.0 --candidate_count 8 --unique
In a single word, my favourite color is
Result Num
कॉलम में, एक ही प्रॉम्प्ट से जनरेट किए गए कई उम्मीदवारों के बीच अंतर किया जा सकता है.
पोस्ट-प्रोसेसिंग मॉडल आउटपुट
संभावित आउटपुट और स्ट्रक्चर की बड़ी रेंज की वजह से, मॉडल के आउटपुट को आपके समस्या वाले डोमेन के हिसाब से ढालना मुश्किल हो सकता है. PaLM मैजिक, पोस्ट-प्रोसेसिंग के विकल्प देता है. इनकी मदद से, Python कोड का इस्तेमाल करके मॉडल आउटपुट में बदलाव किया जा सकता है या उसे प्रोसेस किया जा सकता है.
प्रोसेस होने के बाद वाले फ़ंक्शन, आउटपुट में नया कॉलम जोड़ सकते हैं या text_result
कॉलम में बदलाव कर सकते हैं. text_result
कॉलम आखिरी कॉलम है. आखिरी आउटपुट तय करने के लिए, eval
और compare
कमांड इसका इस्तेमाल करते हैं.
पोस्ट-प्रोसेसिंग में इस्तेमाल करने के लिए, कुछ फ़ंक्शन सैंपल के तौर पर यहां दिए गए हैं. पहला कॉलम एक नया कॉलम जोड़ता है और दूसरा, post_process_replace_fn
डेकोरेटर का इस्तेमाल करके नतीजे के कॉलम को अपडेट करता है.
import re
from google.generativeai.notebook import magics
# Add a new column.
def word_count(result):
return len(result.split(' '))
# Modify the text_result column
@magics.post_process_replace_fn
def extract_first_sentence(result):
"""Extracts the first word from the raw result."""
first, *_ = re.split(r'\.\s*', result)
return first
इन फ़ंक्शन का इस्तेमाल करने के लिए, इस तरह से पाइप (|
) ऑपरेटर का इस्तेमाल करके उन्हें %%palm
कमांड में जोड़ें.
%%palm run | word_count | extract_first_sentence
The happiest thing I can imagine is
यहां ऑर्डर की जानकारी ज़रूरी है. word_count
को शुरू करने पर, शब्दों की संख्या कैलकुलेट करने के लिए, ओरिजनल मॉडल आउटपुट का इस्तेमाल किया जाता है. अगर इन्हें आपस में बदला जाता है, तो शब्दों की संख्या, निकाले गए पहले वाक्य में मौजूद शब्दों की संख्या होगी.
इसके बारे में और पढ़ें
- एलएलएम के बारे में ज़्यादा जानने के लिए, एलएलएम के कॉन्सेप्ट की गाइड पढ़ें.
- PaLM के साथ काम करने का ज़्यादा से ज़्यादा फ़ायदा पाने के लिए, प्रॉम्प्ट से जुड़े दिशा-निर्देश देखें. इससे आपको प्रॉम्प्ट तैयार करने के बारे में ज़्यादा जानकारी मिलेगी.
- अलग-अलग प्रॉम्प्ट को प्रोटोटाइप करने और प्रयोग करने के लिए, Google AI Studio पर जाएं. साथ ही, ज़्यादा जानकारी के लिए, Google AI Studio का क्विकस्टार्ट लेख पढ़ें.