forked from dusty-nv/jetson-containers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplates.py
70 lines (58 loc) · 2.57 KB
/
templates.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
from .utils import AttributeDict
ChatTemplates = {
# https://huggingface.co/blog/llama2#how-to-prompt-llama-2
'llama-2': {
'system_prompt': "Answer the questions.",
'system': '<s>[INST] <<SYS>>\n${MESSAGE}\n<</SYS>>\n\n',
'first': '${MESSAGE} [/INST]',
'user': '<s>[INST] ${MESSAGE} [/INST]',
'bot': ' ${MESSAGE}' # llama-2 output already ends in </s>
},
# https://github.com/lm-sys/FastChat/blob/main/docs/vicuna_weights_version.md#prompt-template
'vicuna-v0': {
'system_prompt': "A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.",
'system': '${MESSAGE}\n\n',
'user': '### Human: ${MESSAGE}\n',
'bot': '### Assistant: ${MESSAGE}\n',
},
'vicuna-v1': {
'system_prompt': "A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.",
'system': '${MESSAGE}\n\n',
'user': 'USER: ${MESSAGE}\n',
'bot': 'ASSISTANT: ${MESSAGE}</s>\n', # TODO: does output already end in </s> ?
},
}
ChatTemplates['llava-v0'] = ChatTemplates['vicuna-v0']
ChatTemplates['llava-v1'] = ChatTemplates['vicuna-v1']
ChatTemplates['llava-llama-2'] = ChatTemplates['llama-2'].copy()
ChatTemplates['llava-llama-2'].update({
'system_prompt': "You are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language."
})
for key in ChatTemplates:
ChatTemplates[key] = AttributeDict(name=key, **ChatTemplates[key])
def ChatTemplate(model):
"""
Attempt to automatically determine the chat template from the model name/type.
Either returns one of the ChatTemplate dicts from above, or None if undetermined.
"""
if not isinstance(model, str):
model = model.config.name.lower()
if 'llama-2' in model:
if 'llava' in model:
chat_template = 'llava-llama-2'
else:
chat_template = 'llama-2'
elif 'vicuna' in model:
if 'v1' in model:
chat_template = 'vicuna-v1'
else:
chat_template = 'vicuna-v0'
elif 'llava' in model:
if 'v1' in model:
chat_template = 'llava-v1'
else:
chat_template = 'llava-v0'
else:
return None
return AttributeDict(ChatTemplates[chat_template]) # return a copy