Mistral Instruct model repeats Instruction

#101
by shriML - opened

The instruct model repeats entire instructions given to it before generating the response. Is there a way to prevent this and generate only the required response?

from transformers import AutoModelForCausalLM, AutoTokenizer, MistralForCausalLM
model_path = ""
base_model = AutoModelForCausalLM.from_pretrained(untrained_model_path,torch_dtype=torch.float16)
tokenizer = AutoTokenizer.from_pretrained(model_path)
base_model.to(device)
messages = [
{"role": "user", "content": "What is your favourite condiment?"},
{"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
{"role": "user", "content": "Do you have mayonnaise recipes?"}
]

encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
model_inputs = encodeds.to(device)
generated_ids = base_model.generate(model_inputs,pad_token_id=tokenizer.eos_token_id, max_new_tokens=1000, do_sample=True)
decoded = tokenizer.batch_decode(generated_ids)
print(decoded[0])

Output:

[INST] What is your favourite condiment? [/INST]Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen! [INST] Do you have mayonnaise recipes? [/INST] Of course! I have a few different recipes for mayonnaise that you might enjoy. Here are a few options:

  1. Classic mayonnaise:

Ingredients:

  • 2 large egg yolks
  • 2 tablespoons cold water
  • 1/2 teaspoon salt
  • 1/4 teaspoon freshly ground black pepper
  • 1/3 cup vegetable oil
  • 1/3 cup olive oil
  • 2 tablespoons chopped fresh parsley
  • 1 tablespoon champagne vinegar

Using Python I replace the string

    # remove prompt in answer
    answer = answer.replace(prompt,"")

Thanks, got it. But I want the model to not generate the prompt.

Mistral AI_ org
edited Mar 12

Thanks, got it. But I want the model to not generate the prompt.

It's not actually generating it, it's just answering you with the entire text fully completed. That's usually what an LLM is designed for.

Got it

Sign up or log in to comment