File size: 1,258 Bytes
09321b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def qwen_chatml_prompt_preprocessor(messages):
    prompt = ''
    for message in messages:
        if message['role'] == 'assistant' and message['content'] == '':
            prompt += '<|im_start|>assistant\n'
        else:
            prompt = prompt + '<|im_start|>{role}\n{content}<|im_end|>\n'.format(
                role=message['role'],
                content=message['content'].lstrip('\n').rstrip())

    # in the case of the assistant message is not in the last one, such as function result
    if messages[-1]['role'] == 'assistant':
        last_assistant_message_list = messages[-1]['content'].split('\n')
        if last_assistant_message_list[-1] == '':
            last_assistant_message_list = last_assistant_message_list[:-1]
        if len(last_assistant_message_list) == 0:
            return prompt
        else:
            item_length = len('<|im_end|>\n')
            prompt = prompt[:-item_length]

    return prompt


def plate_preprocessor(messages):
    return qwen_chatml_prompt_preprocessor(messages)


def build_raw_prompt(model):
    if isinstance(model, str) or hasattr(model, '__name__'):
        if model.startswith('qwen'):
            return qwen_chatml_prompt_preprocessor
    else:
        return plate_preprocessor