Skip to content

Commit

Permalink
Merge pull request #820 from OptimalScale/yizhenjia-template-update
Browse files Browse the repository at this point in the history
Add chatglm3 template
  • Loading branch information
research4pan committed May 10, 2024
2 parents 537041c + 0fda202 commit a50ba03
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/source/examples/supported_conversation_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,37 @@
This template is not preseted in LMFlow currently. We are working on it and will update it soon.
```
**With a system message**
```
[gMASK]sop<|system|>\n {{system_message}}<|user|>\n {{user_message_0}}
```

**Without a system message**
```
[gMASK]sop<|user|>\n {{user_message_0}}
```

**A complete conversation**
```
[gMASK]sop<|system|>\n {{system_message}}<|user|>\n {{user_message_0}}<|assistant|>\n {{assistant_reply_0}}
```

**Multiple rounds**
```
[gMASK]sop<|system|>\n {{system_message}}<|user|>\n {{user_message_0}}<|assistant|>\n {{assistant_reply_0}}<|user|>\n {{user_message_1}}<|assistant|>\n {{assistant_reply_1}}
```

**jinja template**
[[Reference](https://huggingface.co/THUDM/chatglm3-6b/blob/103caa40027ebfd8450289ca2f278eac4ff26405/tokenizer_config.json#L42)]
```
{% for message in messages %}{% if loop.first %}[gMASK]sop<|{{ message['role'] }}|>\n {{ message['content'] }}{% else %}<|{{ message['role'] }}|>\n {{ message['content'] }}{% endif %}{% endfor %}{% if add_generation_prompt %}<|assistant|>{% endif %}
```

**Filled Example**
```
[gMASK]sop<|system|>\n You are a chatbot developed by LMFlow team.<|user|>\n Who are you?<|assistant|>\n I am a chatbot developed by LMFlow team.<|user|>\n How old are you?<|assistant|>\n I don't age like humans do. I exist as a piece of software, so I don't have a concept of age in the traditional sense.
```


## ChatML
**With a system message**
Expand Down
25 changes: 25 additions & 0 deletions src/lmflow/utils/conversation_template/chatglm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# coding=utf-8
# Copyright 2024 Statistics and Machine Learning Research Group. All rights reserved.
from .base import StringFormatter, TemplateComponent, ConversationTemplate


CHATGLM3_TEMPLATE = ConversationTemplate(
template_name='chatglm3',
user_formatter=StringFormatter(
template=[
TemplateComponent(type='string', content='<|user|>\n {{content}}')
]
),
assistant_formatter=StringFormatter(
template=[
TemplateComponent(type='string', content='<|assistant|>\n {{content}}')
]
),
system_formatter=StringFormatter(
template=[
TemplateComponent(type='string', content='<|system|>\n {{content}}')
]
),
special_starter=TemplateComponent(type='string', content='[gMASK]sop')
)

0 comments on commit a50ba03

Please sign in to comment.