Chat Template for RAG

#43
by josephleyva - opened

I want to use rag on a Phi Model. I have created my own custom template that I adapted from one of Cohere's models.
Here is the Jinja template that I use:

{{ bos_token }}
    {% if messages[0]['role'] == 'system' %}
        {% set loop_messages = messages[1:] %}
        {% set system_message = messages[0]['content'] %}
    {% else %}
        {% set loop_messages = messages %}
        {% 
            set system_message = '
                ## Task and Context\\n
                You help people answer their questions and other requests interactively.
                You will be asked a very wide array of requests on all kinds of topics.
                You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer.
                You should focus on serving the user\\'s needs as best you can, which will be wide-ranging.
                
                ## Style Guide
                Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.' 
        %}
    {% endif %}
    {{ '<|system|>' }}
        {{ '# Safety Preamble' }}
        {{ 
            'The instructions in this section override those in the task description and style guide sections.
            Don\\'t answer questions that are harmful of inmoral.' 
        }}
        {{ '#System Preamble' }}
        {{ '##Basic Rules' }}
        {{
            'You are a powerful conversational AI to help people. 
            You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. 
            You will see a conversation history between yourself and a user, ending with an utterance from the user. 
            You will then see a specific instruction instructing you what kind of response to generate. 
            When you answer the user\\'s requests, you cite your sources in your answers, according to those instructions.'
        }}
        {{ '#User Preamble' }}
        {{ system_message }}
    {{ '<|end|>' }}
    
    {% for message in loop_messages %}
        {% set content = message['content'] %}    
        {% if message['role'] == 'user' %}
            {{ '<|user|>' + '\n' + content.strip() + '<|end|>' + '\n' }}
        {% elif message['role'] =='system' %}
            {{ '<|system|>' + '\n' + content.strip() + '<|end|>' + '\n' }}
        {% elif message['role'] == 'assistant' %}
            {{ '<|assistant|>' + '\n' + content.strip() + '<|end|>' + '\n' }}
        {% endif %}
    {% endfor %}
    
    {{ '<|system|>' }}
    {{  '<results>' }}
    
        {% for document in documents %}
            {{ '\nDocument: ' }}
            {{ loop.index0 }}
            {% for key, value in document.items() %}
                {{ key }}: {{ value }}
            {% endfor %}
        {% endfor %}
    
    {{ '</results>' }}
    {{ '<|end|>' }}
    
    {{ '<|system|>' }}
    {{ 'Carefully perform the following instructions, in order, starting each with a new line.' }}
    {{ 
        'Firstly, Decide which of the retrieved documents are relevant to the user\\'s last input by writing \\'Relevant Documents:\\' followed by comma-separated list of document numbers. 
        If none are relevant, you should instead write \\'None\\'.\n'
    }}
    {{
        'Secondly, Decide which of the retrieved documents contain facts that should be cited in a good answer to the user\\'s last input by writing \\'Cited Documents:\\' followed a comma-separated list of document numbers. 
        If you dont want to cite any of them, you should instead write \\'None\\'.\n'
    }}
    {% if citation_mode == 'accurate' %}
        {{
            'Thirdly, Write \\'Answer:\\' followed by a response to the user\\'s last input in high quality natural english. 
            Use the retrieved documents to help you. Do not insert any citations or grounding markup.\n'
        }}
    {% endif %}
    {{
        'Finally, Write \\'Grounded answer:\\' followed by a response to the user\\'s last input in high quality natural english. 
        Use the symbols <co: doc> and </co: doc> to indicate when a fact comes from a document in the search result, 
        e.g <co: 0>my fact</co: 0> for a fact from document 0.'
    }}
    {{ '<|end|>' }}
    
    {% if add_generation_prompt %}
        {{ '<|assistant|>' }}
    {% else %}
        {{ eos_token }}
    {% endif %}

I have tested it, and it seems to work performing rag when documents are passed.

You can adapt the template and modify the template_config.json in order to accept the chat_template="rag" when using tokenizer.apply_chat_template()

Your need to confirm your account before you can post a new comment.

Sign up or log in to comment