Skip to content

Add the Command R chat format #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion llama_cpp/llama_chat_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,24 @@ def format_gemma(
_prompt = _format_no_colon_single(system_message="", messages=_messages, sep=_sep)
return ChatFormatterResponse(prompt=_prompt, stop=_sep)

# Chat format for Cohere's Command R models (close to chatml but with different tokens).
# See https://docs.cohere.com/docs/prompting-command-r
@register_chat_format("command-r")
def format_commandr(
messages: List[llama_types.ChatCompletionRequestMessage],
**kwargs: Any,
) -> ChatFormatterResponse:
system_template = "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{system_message}"
system_message = _get_system_message(messages)
system_message = system_template.format(system_message=system_message)
_roles = dict(user="<|START_OF_TURN_TOKEN|><|USER_TOKEN|>", assistant="<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>")
_sep = "<|END_OF_TURN_TOKEN|>"
_messages = _map_roles(messages, _roles)
_messages.append((_roles["assistant"], None))
_prompt = _format_chatml(system_message, _messages, _sep)
return ChatFormatterResponse(prompt=_prompt, stop=_sep)


# Tricky chat formats that require custom chat handlers


Expand Down Expand Up @@ -2755,4 +2772,4 @@ def chatml_function_calling(
},
}

raise ValueError("Automatic streaming tool choice is not supported")
raise ValueError("Automatic streaming tool choice is not supported")