Possibilities injecting custom system into mistral?

#32
by TikaToka - opened

Hello mistralai team, Thank you for sharing amazing work!
I am hoping to use the model for my research, and I have some question.

For llama-2 like models often able to inject custom system prompt, but it looks like there are no information about it.
Is there a way to give custom system prompt to mistral?

I am looking for the same. Please update once you find something.

@sakshat98 @TikaToka

Use this

[INST] System Prompt + Instruction [/INST] Model answer[INST] Follow-up instruction [/INST]

From their official site.

https://docs.mistral.ai/usage/guardrailing

If you want it for gradio. I wrote a formatting function. Remove/add accordingly.

def format_chat_prompt_mistral(message: str, chat_history, instructions: str) -> str:
    if len(chat_history) == 0:
        # If chat_history is empty, return instructions and message
        prompt = f"<s>[INST] {instructions} Hi [/INST] Hello! how can I help you</s>[INST] {message} [/INST]"
        print("sending this prompt\n==============\n",prompt,'\n---------\n')
        return prompt
    else:
        # Initialize chat history text with the first user message and instructions
        user_message, bot_message = chat_history[0]
        chat_history_text = f"<s>[INST] {instructions} {user_message} [/INST] {bot_message}</s>"

        # Use a list comprehension to build the rest of the chat history text
        chat_history_text += "".join(f"[INST] {user_message} [/INST] {bot_message}</s>" for user_message, bot_message in chat_history[1:])

Hi all, the tokenizer.apply_chat_template() method can handle this for you:

messages = [
    {"role": "system", "content": "System message here"},
    {"role": "user", "content:" "User message here"}
]
prompt = tokenizer.apply_chat_template(messages)

You can see the documentation on chat templates for more information.

@Rocketknight1 @pvbhanuteja Thank you for sharing the answer!
But I also want to ask how for huggingface inference api?

Hi all, the tokenizer.apply_chat_template() method can handle this for you:

messages = [
    {"role": "system", "content": "System message here"},
    {"role": "user", "content:" "User message here"}
]
prompt = tokenizer.apply_chat_template(messages)

You can see the documentation on chat templates for more information.

This is incorrect. tokenizer.apply_chat_template will look chat_template key value from here at https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1/blob/main/tokenizer_config.json

The format at chat_template doesn't incline with the actual mistral website. https://docs.mistral.ai/usage/guardrailing I will try to open a PR and make changes to the chat template accordingly.

Sign up or log in to comment