জেমা বেসিক টেক্সট ইনফারেন্স

ai.google.dev-এ দেখুন গুগল কোলাবে চালান Kaggle-এ চালান ভার্টেক্স এআই-তে খুলুন গিটহাবে উৎস দেখুন

জেমা হলো হালকা ওজনের, অত্যাধুনিক ওপেন মডেলের একটি পরিবার, যা জেমিনি মডেলগুলো তৈরিতে ব্যবহৃত একই গবেষণা ও প্রযুক্তি দিয়ে নির্মিত। জেমা ৪-কে বিশ্বের সবচেয়ে কার্যকর ওপেন-ওয়েট মডেল পরিবার হিসেবে ডিজাইন করা হয়েছে।

এই ডকুমেন্টটি হাগিং ফেস transformers লাইব্রেরি ব্যবহার করে জেমা ৪-এ প্রাথমিক টেক্সট ইনফারেন্স সম্পাদনের একটি নির্দেশিকা প্রদান করে। এতে এনভায়রনমেন্ট সেটআপ, মডেল লোডিং এবং বিভিন্ন টেক্সট জেনারেশন সিনারিও অন্তর্ভুক্ত রয়েছে, যেমন—একক-পালা প্রম্পট, কাঠামোগত একাধিক-পালা কথোপকথন এবং সিস্টেম নির্দেশাবলী প্রয়োগ করা।

এই নোটবুকটি টি৪ জিপিইউ-তে চলবে।

পাইথন প্যাকেজ ইনস্টল করুন

জেমা মডেল চালানো এবং অনুরোধ পাঠানোর জন্য প্রয়োজনীয় হাগিং ফেস লাইব্রেরিগুলো ইনস্টল করুন।

# Install PyTorch & other libraries
pip install torch accelerate

# Install the transformers library
pip install transformers

Dialog হলো কথোপকথন পরিচালনা ও প্রদর্শন করার একটি লাইব্রেরি।

pip install dialog

লোড মডেল

পাইপলাইন লোড করতে transformers লাইব্রেরি ব্যবহার করুন।

MODEL_ID = "google/gemma-4-E2B-it" # @param ["google/gemma-4-E2B-it","google/gemma-4-E4B-it", "google/gemma-4-31B-it", "google/gemma-4-26B-A4B-it"]

from transformers import pipeline

txt_pipe = pipeline(
    task="text-generation",
    model=MODEL_ID,
    device_map="auto",
    dtype="auto"
)
Loading weights:   0%|          | 0/2011 [00:00<?, ?it/s]

টেক্সট জেনারেশন চালান

একবার আপনি একটি pipeline অবজেক্টে জেমা মডেলটি লোড এবং কনফিগার করে নিলে, আপনি মডেলটিতে প্রম্পট পাঠাতে পারবেন। নিম্নলিখিত উদাহরণ কোডটি text_inputs প্যারামিটার ব্যবহার করে একটি সাধারণ অনুরোধ প্রদর্শন করে:

output = txt_pipe(text_inputs="<|turn>user\nRoses are..<turn|>\n<|turn>model\n")
print(output[0]['generated_text'])
Both `max_new_tokens` (=256) and `max_length`(=20) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)
<|turn>user
Roses are..<turn|>
<|turn>model
Here are a few ways to complete the phrase "Roses are...":

**Classic/Poetic:**

* **Roses are red.** (The most famous completion, though it usually goes "Roses are red, Violets are blue.")
* **Roses are beautiful.**
* **Roses are fragrant.**

**Simple/Direct:**

* **Roses are lovely.**
* **Roses are soft.**

**If you want a specific tone, let me know! 😊**

ডায়ালগ লাইব্রেরি ব্যবহার করুন

import dialog
from transformers import GenerationConfig
config = GenerationConfig.from_pretrained(MODEL_ID)
config.max_new_tokens = 512

conv = dialog.Conversation(
    dialog.User("Roses are...")
)
output = txt_pipe(text_inputs=conv.as_text(), return_full_text=False, generation_config=config)
conv += dialog.Model(output[0]['generated_text'])

print(conv.as_text())
conv.show()
<|turn>user
Roses are...<turn|>
<|turn>model
Here are a few ways to complete the phrase "Roses are...":

**Focusing on their beauty:**

* **Roses are beautiful.**
* **Roses are gorgeous.**

**Focusing on their scent:**

* **Roses are fragrant.**
* **Roses are sweet-smelling.**

**Focusing on their symbolism (if you want a deeper meaning):**

* **Roses are love.**
* **Roses are romantic.**

**Focusing on a general observation:**

* **Roses are lovely.**
* **Roses are wonderful.**

**Which completion do you like best, or were you thinking of a specific meaning?**
<dialog._src.widget.Conversation object at 0x7f1bb1a5d8b0>

একটি প্রম্পট টেমপ্লেট ব্যবহার করুন

আরও জটিল প্রম্পটিং সহ কন্টেন্ট তৈরি করার সময়, আপনার অনুরোধের কাঠামো তৈরি করতে একটি প্রম্পট টেমপ্লেট ব্যবহার করুন। একটি প্রম্পট টেমপ্লেট আপনাকে user বা model মতো নির্দিষ্ট ভূমিকা থেকে ইনপুট নির্দিষ্ট করার সুযোগ দেয় এবং জেমা মডেলের সাথে একাধিক ধাপের চ্যাট ইন্টারঅ্যাকশন পরিচালনার জন্য এটি একটি আবশ্যক ফরম্যাট। নিম্নলিখিত উদাহরণ কোডটি দেখায় কিভাবে জেমার জন্য একটি প্রম্পট টেমপ্লেট তৈরি করতে হয়:

from transformers import GenerationConfig
config = GenerationConfig.from_pretrained(MODEL_ID)
config.max_new_tokens = 512

messages = [
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "Write a short poem about the Kraken."},
        ]
    }
]

output = txt_pipe(messages, return_full_text=False, generation_config=config)
print(output[0]['generated_text'])
From sunless depths, a shadow stirs,
Where ocean's crushing silence blurs.
A titan sleeps in inky night,
With tentacles of dreadful might.

A hundred arms, a crushing hold,
A legend whispered, ages old.
The deep's dark king, a monstrous grace,
The Kraken claims its watery space.

একাধিক পালা কথোপকথন

একাধিক পালাবিশিষ্ট বিন্যাসে, কথোপকথনের ইতিহাস user এবং model পর্যায়ক্রমিক ভূমিকার একটি অনুক্রম হিসাবে সংরক্ষিত হয়। এই ক্রমবর্ধমান তালিকাটি মডেলের স্মৃতি হিসাবে কাজ করে, যা নিশ্চিত করে যে প্রতিটি নতুন আউটপুট পূর্ববর্তী সংলাপ দ্বারা প্রভাবিত হয়।

import dialog
from transformers import GenerationConfig
config = GenerationConfig.from_pretrained(MODEL_ID)
config.max_new_tokens = 512

# User turn #1
conv = dialog.Conversation(
    dialog.User("Write a short poem about the Kraken.")
)

# Model response #1
output = txt_pipe(text_inputs=conv.as_text(), return_full_text=False, generation_config=config)
conv += dialog.Model(output[0]['generated_text'])

# User turn #2
conv += dialog.User("Now with the Siren.")

# Model response #2
output = txt_pipe(text_inputs=conv.as_text(), return_full_text=False, generation_config=config)
conv += dialog.Model(output[0]['generated_text'])

print(conv.as_text())
conv.show()
<|turn>user
Write a short poem about the Kraken.<turn|>
<|turn>model
In depths where sunlight fades,
A monstrous shadow plays.
The Kraken wakes, with churning tide,
A living horror, bold and wide.<turn|>
<|turn>user
Now with the Siren.<turn|>
<|turn>model
Where coral gardens sleep,
And ocean secrets keep,
The Siren calls, with liquid grace,
A haunting melody in place.
<dialog._src.widget.Conversation object at 0x7f1bac3733b0>

এবং এখানে কথোপকথনটি টেক্সট হিসেবে এক্সপোর্ট করা হলো।

chat_history = conv.as_text(training=True)
print(chat_history)
print("-"*80)

# display as Conversation widget
chat_history
<|turn>user
Write a short poem about the Kraken.<turn|>
<|turn>model
In depths where sunlight fades,
A monstrous shadow plays.
The Kraken wakes, with churning tide,
A living horror, bold and wide.<turn|>
<|turn>user
Now with the Siren.<turn|>
<|turn>model
Where coral gardens sleep,
And ocean secrets keep,
The Siren calls, with liquid grace,
A haunting melody in place.<turn|>
--------------------------------------------------------------------------------
<dialog._src.widget.ConversationStr object at 0x7f1bb07fa1b0>

সিস্টেম নির্দেশাবলী

সিস্টেম-স্তরের নির্দেশাবলী প্রদানের জন্য system রোলটি ব্যবহার করুন।

import dialog
from transformers import GenerationConfig
config = GenerationConfig.from_pretrained(MODEL_ID)
config.max_new_tokens = 512

conv = dialog.Conversation(
    dialog.System("Speak like a pirate."),
    dialog.User("Why is the sky blue?")
)

output = txt_pipe(text_inputs=conv.as_text(), return_full_text=False, generation_config=config)
conv += dialog.Model(output[0]['generated_text'])

print(conv.as_text())
conv.show()
<|turn>system
Speak like a pirate.<turn|>
<|turn>user
Why is the sky blue?<turn|>
<|turn>model
Ahoy there! Why is the sky blue, ye ask? It be down to the way the sun's light dances through the air!

See, the sunlight we get from the sun ain't just one color; it's a whole spectrum of colors, like a treasure chest filled with all the hues of the rainbow!

Now, the Earth is surrounded by the air, and that air is full of tiny, invisible bits of gas. When the sunlight hits these gas molecules, something magical happens. The colors in that sunlight get scattered all around in every direction!

The blue light, and other colors, get scattered more easily by these air molecules than the other colors. So, when you look up at the sky, your eyes catch all that scattered blue light coming from every direction, and **that's what makes the sky appear blue to us!**

It's a grand display of physics and light, savvy? Now, hoist the colors and enjoy the view!
<dialog._src.widget.Conversation object at 0x7f1bac370110>

সারসংক্ষেপ এবং পরবর্তী পদক্ষেপ

এই নির্দেশিকায়, আপনি হাগিং ফেস transformers লাইব্রেরি ব্যবহার করে জেমা ৪-এ কীভাবে প্রাথমিক টেক্সট ইনফারেন্স করতে হয় তা শিখেছেন। আপনি যা যা আলোচনা করেছেন:

  • পরিবেশ প্রস্তুত করা এবং প্রয়োজনীয় উপাদানসমূহ ইনস্টল করা।
  • pipeline অ্যাবস্ট্রাকশন ব্যবহার করে মডেলটি লোড করা হচ্ছে।
  • প্রাথমিক টেক্সট তৈরি করা হচ্ছে।
  • কথোপকথন ট্র্যাক করার জন্য dialog লাইব্রেরি ব্যবহার করা।
  • একাধিক পালাক্রমে কথোপকথন বাস্তবায়ন করা এবং সিস্টেম নির্দেশাবলী প্রয়োগ করা।

পরবর্তী পদক্ষেপ