How to Use Canarim-7B-Instruct for Text Generation

Apr 5, 2024 | Educational

The Canarim-7B-Instruct is a powerful 7 billion parameter language model that has been expertly fine-tuned using various publicly available instruction datasets. This guide provides you a simple pathway to harness its capabilities for text generation tasks, particularly in generating human-like text in Portuguese. Let’s dive in!

Step-by-Step Guide to Using Canarim-7B-Instruct

1. Installation Prerequisites

Before you start using the Canarim-7B-Instruct model, ensure you have the necessary libraries installed. Use pip to install the required libraries:

pip install transformers torch

2. Import Libraries

Import the required classes from the libraries:

from transformers import AutoTokenizer, pipeline
import torch

3. Load the Model

You need to load the Canarim-7B-Instruct model and the corresponding tokenizer as follows:

model_id = "dominguesm/Canarim-7B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("text-generation", model=model_id, torch_dtype=torch.float16, device_map="auto")

4. Create a Prompt Function

This function helps format your instructions and inputs for the model:

def make_prompt(instruction, input=None):
    if input:
        return f"Abaixo está uma instrução que descreve uma tarefa, emparelhada com uma entrada que fornece mais contexto. Escreva uma resposta que conclua adequadamente a solicitação.\n### Instruções:{instruction}\n### Entrada:{input}\n### Resposta:"
    else:
        return f"Abaixo está uma instrução que descreve uma tarefa. Escreva uma resposta que conclua adequadamente a solicitação.\n### Instruções:{instruction}\n### Resposta:"

5. Generate Text

Use the following code to generate text based on a specific instruction:

instruction = "Faça um resumo conciso do seguinte texto."
context = "No início da obra, o narrador Bentinho explica como escolheu o título para o seu livro..."
prompt = make_prompt(instruction, context)

sequences = pipe(prompt, do_sample=True, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id, max_length=2048, temperature=0.9, top_p=0.6, repetition_penalty=1.15)

Understanding the Code with an Analogy

Think of using the Canarim-7B-Instruct model as baking a cake. Here’s how each part fits into that analogy:

  • Ingredients: The model and tokenizer are like flour and sugar—they are the essential components needed to start your recipe (in this case, the text generation).
  • Recipe preparation: When you prepare your prompt function, it’s like getting your mixing bowl and measuring cups ready. It organizes the steps so that you can bake successfully.
  • Baking: When you call the pipe function, you’re putting your mixture into the oven, where the magic happens. The model processes your instruction and context and generates a delightful text output—akin to pulling a beautifully baked cake out of the oven!

Troubleshooting Common Issues

If you encounter any issues while working with the model, consider the following troubleshooting ideas:

  • Model not loading: Ensure that you have the right version of PyTorch and Transformers libraries installed.
  • Insufficient memory: With large models like Canarim-7B, ensure your machine has sufficient GPU memory. You may need to reduce torch_dtype or use smaller models if you’re running out of resources.
  • Unexpected outputs: Adjust the parameters in the pipe function such as temperature and top_p for more controlled outputs.

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

Conclusion

And there you have it! With this guide, you can now start using the Canarim-7B-Instruct model effectively for your text generation tasks. The potential of language models like these is tremendous, and we encourage continuous exploration and experimentation.

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