How to Use the Jais Family of Bilingual Models: A Comprehensive Guide

Aug 6, 2024 | Educational

The Jais family of models represents a significant leap in bilingual Natural Language Processing (NLP) with its fine-tuned capabilities in both Arabic and English. If you’re looking to leverage these models for research, commercial use, or experimentation, this guide will walk you through the essentials and potential use cases.

Understanding the Jais Family Models

The Jais family includes two categories of models:

  • Pre-trained from scratch: These models start with foundational training and include variants like jais-family-* .
  • Pre-trained adaptively from Llama-2: These models adapt the Llama-2 architecture for improved bilingual performance, labeled as jais-adapted-* .

With 20 models of varying sizes (from 590M to 70B parameters) and trained on over 1.6T tokens in Arabic, English, and code data, these models can significantly accelerate research in Arabic NLP.

Getting Started with the Model

Let’s walk through how to implement the jais-family-2p7b-chat model in Python using the Hugging Face Transformers library. Think of this like baking a cake;

  • You will need some ingredients (libraries and model paths),
  • A recipe (code to run), and
  • A baking tray (your computational environment) to mix everything without burning it!

# Step 1: Import Libraries
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# Step 2: Define Model Path
model_path = "inceptionai/jais-family-2p7b-chat"

# Step 3: Get the Device
device = "cuda" if torch.cuda.is_available() else "cpu"

# Step 4: Load the Tokenizer and Model
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto", trust_remote_code=True)

# Step 5: Define a Function to Get Model Responses
def get_response(text, tokenizer=tokenizer, model=model):
    input_ids = tokenizer(text, return_tensors="pt").input_ids
    inputs = input_ids.to(device)
    generate_ids = model.generate(
        inputs,
        top_p=0.9,
        temperature=0.3,
        max_length=2048,
        min_length=input_ids.shape[-1] + 4,
        repetition_penalty=1.2,
        do_sample=True,
    )
    response = tokenizer.batch_decode(generate_ids, skip_special_tokens=True)[0]
    return response.split("### Response :")[-1]

# Step 6: Test the Model
question_arabic = "ما هي عاصمة الامارات؟"
question_english = "What is the capital of UAE?"

text_arabic = f"### Instruction:اسمك 'جيس'... {question_arabic}\n### Response :"
text_english = f"### Instruction:Your name is 'Jais'... {question_english}\n### Response :"

print(get_response(text_arabic))
print(get_response(text_english))

Common Troubleshooting Tips

While the Jais models are robust, there may be hiccups along the way. Here are some common issues and solutions:

  • Issue: Model not loading due to memory constraints.
    Solution: Reduce model parameter size or ensure proper GPU setup.
  • Issue: Inconsistent outputs when generating text.
    Solution: Check temperature settings in the model.generate() function to maintain coherence in responses.
  • Issue: Error in tokenizer processing.
    Solution: Ensure the correct tokenizer is loaded with the respective model.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox