How to Utilize the Jais Family of Bilingual Models

Aug 7, 2024 | Educational

The Jais family of models are powerful bilingual large language models designed specifically for English and Arabic. They provide a robust framework for various natural language processing tasks, making it easier for researchers and developers to create applications catering to Arabic-speaking users. In this guide, we will take you through the steps required to implement the Jais models and troubleshoot common issues.

Understanding the Jais Family Model Variants

The Jais models come in two primary variants:

  • Pre-trained from Scratch: These models, denoted as jais-family-*, have been developed from the ground up.
  • Adaptively Pre-trained from Llama-2: These models, denoted as jais-adapted-*, leverage the capabilities of the Llama-2 architecture for enhanced performance in bilingual contexts.

The series boasts a total of 20 models spanning sizes from 590M to a staggering 70B parameters, trained on a diverse corpus of up to 1.6 trillion tokens encompassing text data in multiple languages.

Getting Started with Jais Models

To use the Jais models effectively, follow these steps:

  • Install the required libraries, specifically transformers for model loading and tokenization.
  • Choose the appropriate model size based on your computational resources and application needs.
  • Implement the model loading process as demonstrated in the sample code below.

Sample Code for Model Implementation

The following code snippet outlines how to load a model and generate text responses in both Arabic and English:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_path = "inceptionai/jais-family-6p7b"
device = "cuda" if torch.cuda.is_available() else "cpu"

tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto", trust_remote_code=True)

def get_response(text, tokenizer=tokenizer, model=model):
    input_ids = tokenizer(text, return_tensors="pt").input_ids
    inputs = input_ids.to(device)
    input_len = inputs.shape[-1]
    generate_ids = model.generate(
        inputs,
        top_p=0.9,
        temperature=0.3,
        max_length=2048,
        min_length=input_len + 4,
        repetition_penalty=1.2,
        do_sample=True,
    )
    response = tokenizer.batch_decode(
        generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
    )[0]
    return response

text = "عاصمة دولة الإمارات العربية المتحدة ه"
print(get_response(text))
text = "The capital of UAE is"
print(get_response(text))

An Analogy to Understand the Model’s Working

Think of the Jais models as a bilingual dictionary that not only translates words but also constructs meaningful sentences in both Arabic and English. When you input a phrase, just like referencing a dictionary, the model analyzes the context and generates an appropriate response that seamlessly incorporates cultural nuances and language intricacies. The training phase is akin to teaching a student multiple languages, ensuring that they understand not just vocabulary, but also the essence and emotion behind the words used.

Troubleshooting Common Issues

If you encounter any issues while implementing the Jais family models, consider the following troubleshooting tips:

  • Error in model loading: Ensure you have the correct model path and that trust_remote_code=True is enabled.
  • Out of memory errors: If using large models like the 30B or 70B variants, verify that your system has sufficient GPU memory. Consider using smaller models if necessary.
  • No response or garbled text: Check your input data for any encoding issues or unsupported characters. Also, ensure your inputs are appropriately tokenized.

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

Conclusion

The Jais family of language models is groundbreaking in its ability to cater to Arabic and English speakers. By utilizing these models, developers can create nuanced applications that respect linguistic diversity while leveraging advanced AI capabilities.

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