KerasNLP का इस्तेमाल करके Gemma का इस्तेमाल करना शुरू करें

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

इस ट्यूटोरियल में KerasNLP का इस्तेमाल करके Gemma का इस्तेमाल करने का तरीका बताया गया है. जेम्मा, हल्के और आधुनिक आर्ट ओपन मॉडल का एक फ़ैमिली ग्रुप है. इन्हें Gemini मॉडल बनाने के लिए इस्तेमाल की गई रिसर्च और टेक्नोलॉजी से ही बनाया गया है. KerasNLP, नैचुरल लैंग्वेज प्रोसेसिंग (एनएलपी) मॉडल का एक कलेक्शन है. इसे Keras में लागू किया गया है और इन्हें JAX, PyTorch, और TensorFlow पर चलाया जा सकता है.

इस ट्यूटोरियल में, आपको कई प्रॉम्प्ट के टेक्स्ट रिस्पॉन्स जनरेट करने के लिए, Gemma का इस्तेमाल करने का विकल्प मिलेगा. अगर आपने Keras का इस्तेमाल पहले कभी नहीं किया है, तो शुरू करने से पहले Keras का इस्तेमाल शुरू करना पढ़ें. हालांकि, आपको ऐसा करने की ज़रूरत नहीं है. इस ट्यूटोरियल से, आपको Keras के बारे में ज़्यादा जानकारी मिलेगी.

सेटअप

Gemma सेटअप

इस ट्यूटोरियल को पूरा करने के लिए, आपको पहले Gemma सेटअप में जाकर, सेटअप से जुड़े निर्देशों का पालन करना होगा. Gemma सेटअप के निर्देश आपको बताते हैं कि नीचे बताए गए काम कैसे किए जा सकते हैं:

  • kaggle.com पर जाकर, Gemma ऐप्लिकेशन का ऐक्सेस पाएं.
  • Gemma 2B मॉडल को चलाने के लिए, ज़रूरी संसाधनों वाला Colab रनटाइम चुनें.
  • Kaggle उपयोगकर्ता नाम और एपीआई पासकोड जनरेट और कॉन्फ़िगर करें.

जेम्मा सेटअप पूरा करने के बाद, अगले सेक्शन पर जाएं. यहां आपको Colab के एनवायरमेंट के लिए एनवायरमेंट वैरिएबल सेट करने होंगे.

एनवायरमेंट वैरिएबल सेट करना

KAGGLE_USERNAME और KAGGLE_KEY के लिए एनवायरमेंट वैरिएबल सेट करें.

import os
from google.colab import userdata

# Note: `userdata.get` is a Colab API. If you're not using Colab, set the env
# vars as appropriate for your system.
os.environ["KAGGLE_USERNAME"] = userdata.get('KAGGLE_USERNAME')
os.environ["KAGGLE_KEY"] = userdata.get('KAGGLE_KEY')

डिपेंडेंसी इंस्टॉल करें

Keras और KerasNLP इंस्टॉल करें.

# Install Keras 3 last. See https://keras.io/getting_started/ for more details.
pip install -q -U keras-nlp
pip install -q -U keras>=3

कोई बैकएंड चुनें

Keras एक हाई-लेवल, मल्टी-फ़्रेमवर्क डीप लर्निंग एपीआई है. इसे सादगी और इस्तेमाल में आसान बनाने के लिए डिज़ाइन किया गया है. Keras 3 में, आपको बैकएंड: TensorFlow, JAX या PyTorch चुनने की सुविधा मिलती है. इस ट्यूटोरियल के लिए, ये तीनों चीज़ें काम करेंगी.

import os

os.environ["KERAS_BACKEND"] = "jax"  # Or "tensorflow" or "torch".
os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "0.9"

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

Keras और KerasNLP को इंपोर्ट करें.

import keras
import keras_nlp

मॉडल बनाना

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

from_preset तरीके का इस्तेमाल करके मॉडल बनाएं:

gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset("gemma_2b_en")
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...
Attaching 'model.weights.h5' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...
Attaching 'tokenizer.json' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...
Attaching 'assets/tokenizer/vocabulary.spm' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...

from_preset, पहले से सेट आर्किटेक्चर और वेट से मॉडल को इंस्टैंशिएट करता है. ऊपर दिए गए कोड में, "gemma_2b_en" स्ट्रिंग प्रीसेट आर्किटेक्चर के बारे में बताती है: दो अरब पैरामीटर वाला जेम्मा मॉडल.

मॉडल के बारे में ज़्यादा जानकारी पाने के लिए summary का इस्तेमाल करें:

gemma_lm.summary()

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

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

अब कुछ टेक्स्ट जनरेट करने का समय है! इस मॉडल में, generate वाला ऐसा तरीका है जो प्रॉम्प्ट के आधार पर टेक्स्ट जनरेट करता है. वैकल्पिक max_length आर्ग्युमेंट, जनरेट किए गए क्रम की ज़्यादा से ज़्यादा लंबाई बताता है.

"What is the meaning of life?" प्रॉम्प्ट की मदद से, इस सुविधा को आज़माएं.

gemma_lm.generate("What is the meaning of life?", max_length=64)
'What is the meaning of life?\n\nThe question is one of the most important questions in the world.\n\nIt’s the question that has been asked by philosophers, theologians, and scientists for centuries.\n\nAnd it’s the question that has been asked by people who are looking for answers to their own lives'

किसी दूसरे प्रॉम्प्ट का इस्तेमाल करके, generate को फिर से कॉल करने की कोशिश करें.

gemma_lm.generate("How does the brain work?", max_length=64)
'How does the brain work?\n\nThe brain is the most complex organ in the human body. It is responsible for controlling all of the body’s functions, including breathing, heart rate, digestion, and more. The brain is also responsible for thinking, feeling, and making decisions.\n\nThe brain is made up'

JAX या TensorFlow के बैकएंड का इस्तेमाल करने पर, आपको दिखेगा कि दूसरा generate कॉल तुरंत आ जाता है. इसकी वजह यह है कि किसी खास बैच साइज़ और max_length के लिए, generate को किए जाने वाले हर कॉल को XLA की मदद से इकट्ठा किया जाता है. पहली बार दौड़ना महंगा है, लेकिन बाद में चलाना ज़्यादा तेज़ होता है.

इनपुट के तौर पर सूची का इस्तेमाल करके भी ऐसे प्रॉम्प्ट दिए जा सकते हैं जो बैच में दिए गए हैं:

gemma_lm.generate(
    ["What is the meaning of life?",
     "How does the brain work?"],
    max_length=64)
['What is the meaning of life?\n\nThe question is one of the most important questions in the world.\n\nIt’s the question that has been asked by philosophers, theologians, and scientists for centuries.\n\nAnd it’s the question that has been asked by people who are looking for answers to their own lives',
 'How does the brain work?\n\nThe brain is the most complex organ in the human body. It is responsible for controlling all of the body’s functions, including breathing, heart rate, digestion, and more. The brain is also responsible for thinking, feeling, and making decisions.\n\nThe brain is made up']

ज़रूरी नहीं: कोई दूसरा सैंपलर आज़माएं

compile() पर sampler आर्ग्युमेंट सेट करके, GemmaCausalLM के लिए जनरेट करने की रणनीति को कंट्रोल किया जा सकता है. डिफ़ॉल्ट रूप से, "greedy" सैंपलिंग का इस्तेमाल किया जाएगा.

एक्सपेरिमेंट के तौर पर, कोई "top_k" रणनीति सेट करें:

gemma_lm.compile(sampler="top_k")
gemma_lm.generate("What is the meaning of life?", max_length=64)
'What is the meaning of life? That was a question I asked myself as I was driving home from work one night in 2012. I was driving through the city of San Bernardino, and all I could think was, “What the heck am I doing?”\n\nMy life was completely different. I'

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

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

आगे क्या करना है

इस ट्यूटोरियल में, आपने KerasNLP और Gemma का इस्तेमाल करके टेक्स्ट जनरेट करने का तरीका सीखा. आगे की जानकारी के लिए यहां कुछ सुझाव दिए गए हैं: