How to Get Started with the Jais Family of Bilingual Language Models

Aug 5, 2024 | Educational

The Jais family of models is a groundbreaking suite of bilingual large language models (LLMs) designed to bolster the capabilities of Arabic and English NLP. With strong performance in Arabic and additional strengths in English, these models open up new avenues for research and application. If you’re eager to dive into this world, this guide will help you navigate the process smoothly.

Understanding the Jais Family Models

The Jais family offers two main variants:

  • Pre-trained from scratch: Models labeled as jais-family-*.
  • Adaptively pre-trained from Llama-2: Models labeled as jais-adapted-*.

Currently, there are a total of 20 models available across eight sizes, ranging from 590M to 70B parameters, trained on up to 1.6 trillion tokens of diverse Arabic, English, and coding data. All pre-trained models come fine-tuned for dialogues in both languages.

How to Use the Jais Language Models

To utilize these models in your projects, you’ll need to follow this straightforward script:


# Import necessary libraries
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# Specify the model path
model_path = 'inceptionai/jais-adapted-13b'

# Detect device for acceleration
device = 'cuda' if torch.cuda.is_available() else 'cpu'

# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path, device_map='auto', trust_remote_code=True)

# Set pad token if not already defined
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

# Define function to generate responses
def get_response(text, tokenizer=tokenizer, model=model):
    tokenized = tokenizer(text, return_tensors='pt')
    input_ids, attention_mask = tokenized.input_ids.to(device), tokenized.attention_mask.to(device)
    input_len = input_ids.shape[-1]
    generate_ids = model.generate(
        input_ids,
        attention_mask=attention_mask,
        top_p=0.9,
        temperature=0.3,
        max_length=2048,
        min_length=input_len + 4,
        repetition_penalty=1.2,
        do_sample=True,
        pad_token_id=tokenizer.pad_token_id
    )
    response = tokenizer.batch_decode(
        generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
    )
    return response

# Example usages
text = "عاصمة دولة الإمارات العربية المتحدة هو"
print(get_response(text))

text = "The capital of UAE is"
print(get_response(text))

Analogy for Code Explanation

Imagine you’re setting up a smart assistant in your home. First, you need to get the assistant (the model) ready by providing it with a home (the model path). Then, you tell your assistant where to operate best—on a desk (CPU) or a high-tech gadget (GPU). Next, you fill it with knowledge (loading the tokenizer and model), ensuring it understands various languages and commands. Finally, when friends ask questions (user inputs), your assistant processes the information and provides thoughtful answers based on what it knows!

Troubleshooting

As you journey through working with the Jais family models, you may encounter various challenges. Here are some troubleshooting tips:

  • Issue: Model not loading due to trust issues.
  • Solution: Ensure you have set trust_remote_code=True while loading the model.
  • Issue: Responses are not being generated.
  • Solution: Check that your device setup (CPU/GPU) is correct and that your input text is properly formatted.
  • Issue: Model provides irrelevant output.
  • Solution: Adjust the top_p and temperature parameters in the model.generate() function for better results based on your requirements.

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

Conclusion

By following this guide, you should feel confident in navigating the Jais family of models and utilizing them for your Arabic and English NLP needs. From academic research to commercial applications, the potential is vast, and we at fxis.ai are committed to continuing support and innovation in this field.

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