Skip to content
TheChatArchiveThe Chat Archive
Mistralmistral-7b-instruct· Mistral AIHugging Face Arena Human Preference 55K

Convert from yaml to python. here is an example of a correct conversion. [code] this…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-2-7b-chat’s answer.
  1. User

    Convert from yaml to python. here is an example of a correct conversion.

    instruction_template: |-
      {%- set ns = namespace(found=false) -%}
      {%- for message in messages -%}
          {%- if message['role'] == 'system' -%}
              {%- set ns.found = true -%}
          {%- endif -%}
      {%- endfor -%}
      {%- if not ns.found -%}
          {{- '' + '' + '' -}}
      {%- endif %}
      {%- for message in messages %}
          {%- if message['role'] == 'system' -%}
              {{- '<prefix>' + message['content'] + '</prefix>' -}}
          {%- else -%}
              {%- if message['role'] == 'user' -%}
                  {{-'<human>' + message['content'] + ''-}}
              {%- else -%}
                  {{-'<bot>' + message['content'] + '' -}}
              {%- endif -%}
          {%- endif -%}
      {%- endfor -%}
      {%- if add_generation_prompt -%}
          {{-'<bot>'-}}
      {%- endif -%}
    

    this yaml becomes

    class TagPromptStyle(AbstractPromptStyle):
        """Transforms the sequence of messages into a prompt that should look like:
        ```text
        <human>: user message here
        
        <bot>: assistant (model) response here.
        ```
        """
    
        def _messages_to_prompt(self, messages: Sequence[ChatMessage]) -> str:
            """Format message to prompt with `<human>: MSG` style."""
            prompt = ""
            for message in messages:
                role = message.role
                content = message.content or ""
                message_from_user = f"<human>: {content.strip()}"
                message_from_user += "\n"
                prompt += message_from_user
            # we are missing the last <bot>: tag that will trigger a completion
            prompt += "<bot>: "
            return prompt
    
        def _completion_to_prompt(self, completion: str) -> str:
            return self._messages_to_prompt(
                [ChatMessage(content=completion, role=MessageRole.USER)]
            )
    

    Now use what you have learned in the example to convert this yaml to python:

    instruction_template: |-
      {%- set ns = namespace(found=false) -%}
      {%- for message in messages -%}
          {%- if message['role'] == 'system' -%}
              {%- set ns.found = true -%}
          {%- endif -%}
      {%- endfor -%}
      {%- if not ns.found -%}
          {{- '<|im_start|>system\n' + '' + '<|im_end|>\n' -}}
      {%- endif %}
      {%- for message in messages %}
          {%- if message['role'] == 'system' -%}
              {{- '<|im_start|>system\n' + message['content'] + '<|im_end|>\n' -}}
          {%- else -%}
              {%- if message['role'] == 'user' -%}
                  {{-'<|im_start|>user\n' + message['content'] + '<|im_end|>\n'-}}
              {%- else -%}
                  {{-'<|im_start|>assistant\n' + message['content'] + '<|im_end|>\n' -}}
              {%- endif -%}
          {%- endif -%}
      {%- endfor -%}
      {%- if add_generation_prompt -%}
          {{-'<|im_start|>assistant\n'-}}
      {%- endif -%}
    
  2. Mistral Assistant

    Here's the equivalent code in Python:

    import os
    import json
    from typing import Union
    
    class InstructionTemplate:
        """
        Convert the given template yaml to a function that can be called with messages.
        """
        def __init__(self, template: str):
            self.template = template
            self.variables = {}
    
        def _replace_variables(self, content: str) -> str:
            """
            Substitute variables in the given string.
            """
            for key, value in self.variables.items():
                content = content.replace(f"${key}", value)
            return content
    
        def _split_template(self) -> list:
            """
            Split the template into chunks based on the newline character.
            """
            return [line.strip() for line in os.linesep.split(self.template)]
    
        def _parse_template(self) -> dict:
            """
            Parse the template and extract the role-based content.
            """
            lines = self._split_template()
            variables = {}
            for line in lines:
                if line.startswith("{%"):
                    key, value = line.split("=", 1)
                    variables[key.strip()] = value.strip()
                elif line.startswith("{%-"):
                    key, value = line.split("=", 1)
                    variables[key.strip()] = value.strip()
                    variables["found"] = True
            return variables
    
        def generate_prompt(self, messages: Sequence[ChatMessage]) -> str:
            """
            Generate the prompt based on the given messages.
            """
            variables = self._parse_template()
            if variables["found"]:
                messages = [message for message in messages if message.role == "system"]
            for message in messages:
                content = message.content or ""
                if message.role == "system":
                    content = variables["system"]
                    content = self._replace_variables(content)
                elif message.role == "user":
                    content = variables["user"]
                    content = self._replace_variables(content)
                else:
                    content = variables["assistant"]
                    content = self._replace_variables(content)
                print(f"<|im_start|>{message.role}\n{content}\n<|im_end|>")
            return
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.