8000 Added ChatGLM chat format (#1059) · abetlen/llama-cpp-python@cf743ec · GitHub
[go: up one dir, main page]

Skip to content

Commit cf743ec

Browse files
xaviviroXavier Vinaixa Rosello
andauthored
Added ChatGLM chat format (#1059)
Co-authored-by: Xavier Vinaixa Rosello <xaviviro@MacBook-Pro-de-Xavier.local>
1 parent eb9c7d4 commit cf743ec

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ def _format_chatml(
172172
ret += role + "\n"
173173
return ret
174174

175+
def _format_chatglm3(
176+
system_message: str, messages: List[Tuple[str, Optional[str]]], sep: str
177+
) -> str:
178+
"""Format the prompt with the chatglm3 style."""
179+
ret = ""
180+
if system_message:
181+
ret += system_message
182+
for role, message in messages:
183+
if message:
184+
ret += role + "\n" + " " + message
185+
else:
186+
ret += role
187+
return ret
188+
175189

176190
@dataclasses.dataclass
177191
class ChatFormatterResponse:
@@ -685,6 +699,22 @@ def format_chatml(
685699
_prompt = _format_chatml(system_message, _messages, _sep)
686700
return ChatFormatterResponse(prompt=_prompt, stop=_sep)
687701

702+
@register_chat_format("chatglm3")
703+
def format_chatglm3(
704+
messages: List[llama_types.ChatCompletionRequestMessage],
705+
**kwargs: Any,
706+
) -> ChatFormatterResponse:
707+
system_template = """<|system|>
708+
{system_message}"""
709+
system_message = _get_system_message(messages)
710+
system_message = system_template.format(system_message=system_message)
711+
_roles = dict(user="<|user|>", assistant="<|assistant|>")
712+
_sep = "</s>"
713+
_messages = _map_roles(messages, _roles)
714+
_messages.append((_roles["assistant"], None))
715+
_prompt = _format_chatglm3(system_message, _messages, _sep)
716+
return ChatFormatterResponse(prompt=_prompt, stop=_sep)
717+
688718

689719
@register_chat_format("openchat")
690720
def format_openchat(

0 commit comments

Comments
 (0)
0