Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 305482b

Browse filesBrowse files
committed
Add chatml chat format
1 parent 5ef5280 commit 305482b
Copy full SHA for 305482b

File tree

Expand file treeCollapse file tree

1 file changed

+30
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+30
-0
lines changed

‎llama_cpp/llama_chat_format.py

Copy file name to clipboardExpand all lines: llama_cpp/llama_chat_format.py
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ def _format_add_colon_space_single(
9191
return ret
9292

9393

94+
def _format_chatml(
95+
system_message: str, messages: List[Tuple[str, Optional[str]]], sep: str
96+
) -> str:
97+
"""Format the prompt with the chatml style."""
98+
ret = "" if system_message == "" else system_message + sep + "\n"
99+
for role, message in messages:
100+
if message:
101+
ret += role + "\n" + message + sep + "\n"
102+
else:
103+
ret += role + "\n"
104+
return ret
105+
106+
94107
@dataclasses.dataclass
95108
class ChatFormatterResponse:
96109
prompt: str
@@ -290,3 +303,20 @@ def format_open_orca(
290303
_messages.append((roles[1], None))
291304
_prompt = _format_add_colon_space_single(system_message, _messages, sep)
292305
return ChatFormatterResponse(prompt=_prompt, stop=stop_str)
306+
307+
308+
@register_chat_format("chatml")
309+
def format_chatml(
310+
messages: List[llama_types.ChatCompletionRequestMessage],
311+
**kwargs: Any,
312+
) -> ChatFormatterResponse:
313+
system_template = """<|im_start|>system
314+
{system_message}"""
315+
system_message = _get_system_message(messages)
316+
system_message = system_template.format(system_message=system_message)
317+
_roles = dict(user="<|im_start|>user", assistant="<|im_start|>assistant")
318+
_sep = "<|im_end|>"
319+
_messages = _map_roles(messages, _roles)
320+
_messages.append((_roles["assistant"], None))
321+
_prompt = _format_chatml(system_message, _messages, _sep)
322+
return ChatFormatterResponse(prompt=_prompt)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.