GenAI
Updated on
Aug 22, 2024

A Step-by-Step Guide to Using Cohere For AI’s Aya LLM for Multilingual Content Generation

Learn how to use Cohere for AI's Aya 101 LLM model for multilingual content generation and multilingual chatbots. Aya 101 supports 101 languages and is open source.

A Step-by-Step Guide to Using Cohere For AI’s Aya LLM for Multilingual Content Generation
We Help You Engage the Top 1% AI Researchers to Harness the Power of Generative AI for Your Business.

Introduction

As large language models continue to evolve in size and complexity, a recent breakthrough has emerged with the announcement of a multilingual large language model: Cohere for AI’s Aya 101 LLM.

Aya is a multilingual generative language model that follows instructions in 101 languages. Many breakthroughs have been made in instruction-following capabilities with existing open-source models like Alpaca, Vicuna, and Dolly, but primarily for English tasks. Aya LLM is breaking down language barriers with its multilingual capabilities. It has overcome the language barrier by leveraging fine-tuned multilingual instruction datasets, which truly reflect the richness of the world’s languages. This makes large language models more equitable and representative.

In this blog post, we will explore the process of generating multilingual content using Cohere for AI’s Aya LLM with Gradio. To get started, we will follow these steps:

  1. Aya Model Quantization
  2. Query Retrieval with Gradio
  3. Examples in different languages

Multilingual Content Generation with Cohere Aya LLM

To build a multilingual content generation application, first, we will install the dependencies.

% pip install -q accelerate bitsandbytes transformers gradio fastapi

Aya Model Quantization

C4AI Aya 101 is an instruction fine-tuned large language model. It is built by fine-tuning the 13B mT5 model. It is trained on the xP3x (Crosslingual Public Pool of Prompts eXtended) which is an extended version of the xP3 multilingual dataset, the aya_collection dataset which has 51 languages, and synthetically generated data from ShareGPT using Cohere’s Command flagship, which has 6.8 M samples and 93 languages.

Source: Aya Model: An Instruction Finetuned Open-Access Multilingual Language Model

It outperformed mT0 and BLOOMZ on a majority of tasks and also in the number of languages. Aya Model is a vast model, so to reduce its size, we need to quantize the model first.

We will use the BitsAndBytes Module and set the quantization configuration.

from transformers import BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
   load_in_4bit=True,
   bnb_4bit_quant_type="nf4",
   bnb_4bit_compute_dtype="bfloat16",
   bnb_4bit_use_double_quant=True,
)

Then, we’ll load the model with quantization and set the tokenizer with the original model.

from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model = AutoModelForSeq2SeqLM.from_pretrained("CohereForAI/aya-101", quantization_config = bnb_config)
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("CohereForAI/aya-101")

Query Retrieval with Gradio

Now, to retrieve a query with the help of the Cohere Aya Model, we need to create a text-to-text generation pipeline with the help of transformers.

from transformers import pipeline
pipe = pipeline("text2text-generation",
                                    model = model,
                                    tokenizer = tokenizer,
                                    device_map = "auto",
                                    max_length = 512,
                                    early_stopping = True,
                                    num_return_sequences = 1,
                                    no_repeat_ngram_size = 4,)

As the pipeline is ready, now we’ll use Gradio to create a text generation application.

import gradio as gr
import os

def generate(Query):
    Output = pipe(Query)
    return Output[0]['generated_text']

gr.close_all()

with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.ClearButton([msg, chatbot])

    def respond(message, chat_history):
        bot_message = generate(message)
        chat_history.append((message, bot_message))
        return "", chat_history

    msg.submit(respond, [msg, chatbot], [msg, chatbot])

demo.launch(share=True)

Using the local or public URL,  you can ask questions and get answers immediately. The application will look like this:

Examples in different languages

I tried various queries in different languages and the output was amazing.

Hindi

Question 1: Who is the Prime Minister of India?

Answer 1: Mr. Narendra Modi.

Question 2: How many official languages does India have?

Answer 2: India has 22 official languages most of which are spoken in the Indian states.

French

Question 1: List out 10 tourist destinations in France

Answer 1: - Paris - The Seine - Mont Saint Michel - The Eiffel Tower - The City of Versailles - The Cité de l'Elysée - The Gare du Nord - The Palais Garnier - The Louvre - The Notre-Dame de Paris Cathedral - The Exhibition Center - The Halle aux Grains

Question 2: Which French actors are well-known in Hollywood?

Answer 2: Johnny Hallyday, Michel Sardou, Orelsan, Kendji, Lara Fabian, Jennifer, Charles Aznavour, Edith Piaf.

German

Question 1: Name 5 German car brands that sell their cars in India.

Answer 2: 1. The BMW 2. The Audi 3. Porsche 4. The Volkswagen 5. The Mercedes-Benz

Question 2: Where is the largest beer festival in the world celebrated?

Answer 2: In Germany, the largest beer festival is celebrated at Oktoberfest near Nuremberg.

Chinese

Question 1: Where is Qinghai Lake?

Answer 1: Qinghai Lake is located on the northwestern border of China, between plateaus and mountains.

Question 2: Which metal does China export?

Answer 2: China's exports of metals include copper, aluminum, zinc, lead, mercury, arsenic, etc.

Russian

Question 1: 5 Famous Personalities in Russia.

Answer 1: 1. Vladimir Putin 2. Dmitry Medvedev 3.Vladimir Fursenko 4. Igor Sechin 5. Sergei Shoigu

Question 2: Top 5 Tourist Destinations in Russia.

Answer 2: Here is a list of five tourist destinations in Russia: 1. St. Petersburg 2. Sochi 3. Kazan 4. Sochi 5. Vladivostok

Spanish

Question: What is the famous Spanish cuisine?

Answer: Spanish cuisine is known for its traditional dishes such as tapas, pinchos, cocido madrileño, rosquillas, churros, and paella.

Question 2: Top 5 Spanish Actresses in Hollywood

Answer 2: 1. María León 2. Teresa Palmer 3. María Valverde 4. Patricia Arquette 5. Olivia Colman

Conclusion

It was quite interesting to ask questions in 6 different languages, and the answers seemed pretty good. However, the model has the capability of answering questions in 101 languages. Cohere Aya, as an IFT multilingual large language model, has removed language inequality. With 204K original human annotations, Cohere Aya has provided researchers with a groundbreaking foundation to accelerate multilingual AI progress.

References

https://arxiv.org/pdf/2402.07827.pdf

https://cohere.com/research/aya/aya-at-a-glance.pdf

https://cohere.com/research/aya

About Superteams.ai

Superteams.ai connects top AI talent with companies seeking accelerated product and content development. These "Superteamers" offer individual or team-based solutions for projects involving cutting-edge technologies like LLMs, image synthesis, audio or voice synthesis, and other cutting-edge open-source AI solutions. With over 500 AI researchers and developers, Superteams has facilitated diverse projects like 3D e-commerce model generation, advertising creative generation, enterprise-grade RAG pipelines, geospatial applications, and more. Focusing on talent from India and the global South, Superteams offers competitive solutions for companies worldwide. To explore partnership opportunities, please write to founders@superteams.ai or visit this link.

Authors