Gemma और LangChain का इस्तेमाल शुरू करें

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

इस ट्यूटोरियल में, Google Cloud या Colab एनवायरमेंट में चलने वाले Gemma और LangChain के इस्तेमाल का तरीका बताया गया है. जेम्मा कम वज़न वाले और आधुनिक ओपन मॉडल का एक फ़ैमिली ग्रुप है. इन मॉडल को Gemini मॉडल बनाने के लिए इस्तेमाल की गई रिसर्च और टेक्नोलॉजी से बनाया गया है. LangChain एक फ़्रेमवर्क है. इसमें भाषा के मॉडल का इस्तेमाल करके बनाए गए कॉन्टेक्स्ट अवेयर ऐप्लिकेशन बनाए जाते हैं और उन्हें डिप्लॉय किया जाता है.

Google Cloud में Gemma चलाएं

langchain-google-vertexai पैकेज की मदद से, LangChain को Google Cloud के मॉडल के साथ इंटिग्रेट किया जा सकता है.

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

pip install --upgrade -q langchain langchain-google-vertexai

प्रमाणीकृत करें

अगर Colab Enterprise का इस्तेमाल नहीं किया जा रहा है, तो आपको अपनी पहचान की पुष्टि करनी होगी.

from google.colab import auth
auth.authenticate_user()

मॉडल डिप्लॉय करें

Vertex AI एक ऐसा प्लैटफ़ॉर्म है जिस पर एआई मॉडल और ऐप्लिकेशन की ट्रेनिंग और डिप्लॉय किया जा सकता है. मॉडल गार्डन, मॉडल का चुनिंदा कलेक्शन है. इन्हें Google Cloud कंसोल में देखा जा सकता है.

Gemma को डिप्लॉय करने के लिए, मॉडल गार्डन में Vertex AI का मॉडल खोलें और यह तरीका अपनाएं:

  1. डिप्लॉय करें चुनें.
  2. डिप्लॉयमेंट फ़ॉर्म फ़ील्ड में अपनी पसंद के मुताबिक बदलाव करें या अगर आपको डिफ़ॉल्ट सेटिंग लागू करने से कोई परेशानी नहीं है, तो उन्हें ऐसे ही रहने दें. इन फ़ील्ड को नोट कर लें, जिनकी आपको बाद में ज़रूरत होगी:
    • एंडपॉइंट का नाम (उदाहरण के लिए, google_gemma-7b-it-mg-one-click-deploy)
    • क्षेत्र (उदाहरण के लिए, us-west1)
  3. Vertex AI पर मॉडल डिप्लॉय करने के लिए, डिप्लॉय करें चुनें. डिप्लॉयमेंट को पूरा होने में कुछ मिनट लगेंगे.

जब एंडपॉइंट तैयार हो, तो उसका प्रोजेक्ट आईडी, एंडपॉइंट आईडी, और जगह कॉपी करें और उन्हें पैरामीटर के तौर पर डालें.

# @title Basic parameters
project: str = ""  # @param {type:"string"}
endpoint_id: str = ""  # @param {type:"string"}
location: str = "" # @param {type:"string"}

मॉडल चलाएं

from langchain_google_vertexai import GemmaVertexAIModelGarden, GemmaChatVertexAIModelGarden

llm = GemmaVertexAIModelGarden(
    endpoint_id=endpoint_id,
    project=project,
    location=location,
)

output = llm.invoke("What is the meaning of life?")
print(output)
Prompt:
What is the meaning of life?
Output:
Life is a complex and multifaceted phenomenon that has fascinated philosophers, scientists, and

एक से ज़्यादा टर्न वाली चैट के लिए भी Gemma का इस्तेमाल किया जा सकता है:

from langchain_core.messages import (
    HumanMessage
)

llm = GemmaChatVertexAIModelGarden(
    endpoint_id=endpoint_id,
    project=project,
    location=location,
)

message1 = HumanMessage(content="How much is 2+2?")
answer1 = llm.invoke([message1])
print(answer1)

message2 = HumanMessage(content="How much is 3+3?")
answer2 = llm.invoke([message1, answer1, message2])

print(answer2)
content='Prompt:\n<start_of_turn>user\nHow much is 2+2?<end_of_turn>\n<start_of_turn>model\nOutput:\nSure, the answer is 4.\n\n2 + 2 = 4'
content='Prompt:\n<start_of_turn>user\nHow much is 2+2?<end_of_turn>\n<start_of_turn>model\nPrompt:\n<start_of_turn>user\nHow much is 2+2?<end_of_turn>\n<start_of_turn>model\nOutput:\nSure, the answer is 4.\n\n2 + 2 = 4<end_of_turn>\n<start_of_turn>user\nHow much is 3+3?<end_of_turn>\n<start_of_turn>model\nOutput:\nSure, the answer is 6.\n\n3 + 3 = 6'

दोहराव से बचने के लिए जवाबों को पोस्ट-प्रोसेस करें:

answer1 = llm.invoke([message1], parse_response=True)
print(answer1)

answer2 = llm.invoke([message1, answer1, message2], parse_response=True)

print(answer2)
content='Output:\nSure, here is the answer:\n\n2 + 2 = 4'
content='Output:\nSure, here is the answer:\n\n3 + 3 = 6<'

Kaggle डाउनलोड से Gemma चलाएं

इस सेक्शन में बताया गया है कि Kaggle से जेम्मा कैसे डाउनलोड करें और फिर इस मॉडल को कैसे इस्तेमाल करें.

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

इसके बाद, अगले सेक्शन पर जाएं. यहां आपको 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')

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

# 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

मॉडल चलाएं

from langchain_google_vertexai import GemmaLocalKaggle

Keras बैकएंड को तय किया जा सकता है (डिफ़ॉल्ट रूप से यह tensorflow है, लेकिन इसे jax या torch में बदला जा सकता है).

# @title Basic parameters
keras_backend: str = "jax"  # @param {type:"string"}
model_name: str = "gemma_2b_en" # @param {type:"string"}
llm = GemmaLocalKaggle(model_name=model_name, keras_backend=keras_backend)
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'model.weights.h5' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'tokenizer.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'assets/tokenizer/vocabulary.spm' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
output = llm.invoke("What is the meaning of life?", max_tokens=30)
print(output)
What is the meaning of life?

The question is one of the most important questions in the world.

It’s the question that has

चैट मॉडल चलाएं

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

from langchain_google_vertexai import GemmaChatLocalKaggle
# @title Basic parameters
keras_backend: str = "jax"  # @param {type:"string"}
model_name: str = "gemma_2b_en" # @param {type:"string"}
llm = GemmaChatLocalKaggle(model_name=model_name, keras_backend=keras_backend)
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'model.weights.h5' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'tokenizer.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'assets/tokenizer/vocabulary.spm' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
from langchain_core.messages import (
    HumanMessage
)

message1 = HumanMessage(content="Hi! Who are you?")
answer1 = llm.invoke([message1], max_tokens=30)
print(answer1)
content="<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\nI'm a model.\n Tampoco\nI'm a model."
message2 = HumanMessage(content="What can you help me with?")
answer2 = llm.invoke([message1, answer1, message2], max_tokens=60)

print(answer2)
content="<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\n<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\nI'm a model.\n Tampoco\nI'm a model.<end_of_turn>\n<start_of_turn>user\nWhat can you help me with?<end_of_turn>\n<start_of_turn>model"

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

answer1 = llm.invoke([message1], max_tokens=30, parse_response=True)
print(answer1)

answer2 = llm.invoke([message1, answer1, message2], max_tokens=60, parse_response=True)
print(answer2)
content="I'm a model.\n Tampoco\nI'm a model."
content='I can help you with your modeling.\n Tampoco\nI can'

हगिंग फ़ेस डाउनलोड से Gemma चलाएं

सेटअप

Kaggle की तरह, Hugging Face के लिए ज़रूरी है कि आप इस मॉडल को ऐक्सेस करने से पहले Gemma के नियमों और शर्तों को स्वीकार करें. हगिंग फ़ेस से जेमा का ऐक्सेस पाने के लिए, जेमा मॉडल कार्ड पर जाएं.

आपको पढ़ने की अनुमतियों के साथ उपयोगकर्ता ऐक्सेस टोकन भी लेना होगा. इसे आप नीचे डाल सकते हैं.

# @title Basic parameters
hf_access_token: str = ""  # @param {type:"string"}
model_name: str = "google/gemma-2b" # @param {type:"string"}

मॉडल चलाएं

from langchain_google_vertexai import GemmaLocalHF, GemmaChatLocalHF
llm = GemmaLocalHF(model_name="google/gemma-2b", hf_access_token=hf_access_token)
tokenizer_config.json:   0%|          | 0.00/1.11k [00:00<?, ?B/s]
tokenizer.model:   0%|          | 0.00/4.24M [00:00<?, ?B/s]
tokenizer.json:   0%|          | 0.00/17.5M [00:00<?, ?B/s]
special_tokens_map.json:   0%|          | 0.00/555 [00:00<?, ?B/s]
config.json:   0%|          | 0.00/627 [00:00<?, ?B/s]
model.safetensors.index.json:   0%|          | 0.00/13.5k [00:00<?, ?B/s]
Downloading shards:   0%|          | 0/2 [00:00<?, ?it/s]
model-00001-of-00002.safetensors:   0%|          | 0.00/4.95G [00:00<?, ?B/s]
model-00002-of-00002.safetensors:   0%|          | 0.00/67.1M [00:00<?, ?B/s]
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
generation_config.json:   0%|          | 0.00/137 [00:00<?, ?B/s]
output = llm.invoke("What is the meaning of life?", max_tokens=50)
print(output)
What is the meaning of life?

The question is one of the most important questions in the world.

It’s the question that has been asked by philosophers, theologians, and scientists for centuries.

And it’s the question that

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

चैट मॉडल चलाएं

llm = GemmaChatLocalHF(model_name=model_name, hf_access_token=hf_access_token)
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
from langchain_core.messages import (
    HumanMessage
)

message1 = HumanMessage(content="Hi! Who are you?")
answer1 = llm.invoke([message1], max_tokens=60)
print(answer1)
content="<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\nI'm a model.\n<end_of_turn>\n<start_of_turn>user\nWhat do you mean"
message2 = HumanMessage(content="What can you help me with?")
answer2 = llm.invoke([message1, answer1, message2], max_tokens=140)

print(answer2)
content="<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\n<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\nI'm a model.\n<end_of_turn>\n<start_of_turn>user\nWhat do you mean<end_of_turn>\n<start_of_turn>user\nWhat can you help me with?<end_of_turn>\n<start_of_turn>model\nI can help you with anything.\n<"

ऊपर दिए गए उदाहरणों में बताया गया है कि रिस्पॉन्स को प्रोसेस करने के बाद:

answer1 = llm.invoke([message1], max_tokens=60, parse_response=True)
print(answer1)

answer2 = llm.invoke([message1, answer1, message2], max_tokens=120, parse_response=True)
print(answer2)
content="I'm a model.\n<end_of_turn>\n"
content='I can help you with anything.\n<end_of_turn>\n<end_of_turn>\n'

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