Skip to content

Add Mistral templates that more closely resemble the standard #82

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: master
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
58 changes: 57 additions & 1 deletion src/llama_cpp_agent/messages_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class MessagesFormatterType(Enum):
AUTOCODER = 15
GEMMA_2 = 16
DEEP_SEEK_CODER_2 = 17
MISTRAL_V1 = 18
MISTRAL_V2 = 19
MISTRAL_V3_TEKKEN = 20


@dataclass
class PromptMarkers:
Expand Down Expand Up @@ -178,12 +182,14 @@ def _format_response(
Roles.assistant: PromptMarkers("""### Assistant:\n""", """\n"""),
Roles.tool: PromptMarkers("", ""),
}

gemma_2_prompt_markers = {
Roles.system: PromptMarkers("""""", """\n\n"""),
Roles.user: PromptMarkers("""<start_of_turn>user\n""", """<end_of_turn>\n"""),
Roles.assistant: PromptMarkers("""<start_of_turn>model\n""", """<end_of_turn>\n"""),
Roles.tool: PromptMarkers("", ""),
}

code_ds_prompt_markers = {
Roles.system: PromptMarkers("", """\n\n"""),
Roles.user: PromptMarkers("""@@ Instruction\n""", """\n\n"""),
Expand Down Expand Up @@ -225,25 +231,50 @@ def _format_response(
Roles.assistant: PromptMarkers("""<|assistant|>""", """<|end|>\n"""),
Roles.tool: PromptMarkers("", ""),
}

open_interpreter_chat_prompt_markers = {
Roles.system: PromptMarkers("", "\n\n"),
Roles.user: PromptMarkers("### Instruction:\n", "\n"),
Roles.assistant: PromptMarkers("### Response:\n", "\n"),
Roles.tool: PromptMarkers("", ""),
}

autocoder_chat_prompt_markers = {
Roles.system: PromptMarkers("", "\n"),
Roles.user: PromptMarkers("Human: ", "\n"),
Roles.assistant: PromptMarkers("Assistant: ", "<|EOT|>\n"),
Roles.tool: PromptMarkers("", ""),
}

deep_seek_coder_2_chat_prompt_markers = {
Roles.system: PromptMarkers("""<|begin▁of▁sentence|>""", """\n\n"""),
Roles.user: PromptMarkers("""User: """, """ \n\n"""),
Roles.assistant: PromptMarkers("""Assistant: """, """<|end▁of▁sentence|>"""),
Roles.tool: PromptMarkers("", ""),
}

mistral_v1_markers = {
Roles.system: PromptMarkers(""" [INST]""", """ [/INST]"""),
Roles.user: PromptMarkers(""" [INST]""", """ [/INST]"""),
Roles.assistant: PromptMarkers(""" """, """</s>"""),
Roles.tool: PromptMarkers("", ""),
}

mistral_v2_markers = {
Roles.system: PromptMarkers("""[INST] """, """[/INST]"""),
Roles.user: PromptMarkers("""[INST] """, """[/INST]"""),
Roles.assistant: PromptMarkers(""" """, """</s>"""),
Roles.tool: PromptMarkers("", ""),
}

mistral_v3_tekken_markers = {
Roles.system: PromptMarkers("""[INST]""", """[/INST]"""),
Roles.user: PromptMarkers("""[INST]""", """[/INST]"""),
Roles.assistant: PromptMarkers("""""", """</s>"""),
Roles.tool: PromptMarkers("", ""),
}


"""
### Instruction:
{prompt}
Expand Down Expand Up @@ -381,6 +412,28 @@ def _format_response(
eos_token="<|end▁of▁sentence|>",
)

mistral_v1_formatter = MessagesFormatter(
"",
mistral_v1_markers,
False,
["</s>"],
)

mistral_v2_formatter = MessagesFormatter(
"",
mistral_v2_markers,
False,
["</s>"],
)

mistral_v3_tekken_formatter = MessagesFormatter(
"",
mistral_v3_tekken_markers,
False,
["</s>"],
)


predefined_formatter = {
MessagesFormatterType.MISTRAL: mixtral_formatter,
MessagesFormatterType.CHATML: chatml_formatter,
Expand All @@ -398,7 +451,10 @@ def _format_response(
MessagesFormatterType.OPEN_INTERPRETER: open_interpreter_chat_formatter,
MessagesFormatterType.AUTOCODER: autocoder_chat_formatter,
MessagesFormatterType.GEMMA_2: gemma_2_chat_formatter,
MessagesFormatterType.DEEP_SEEK_CODER_2: deep_seek_coder_2_chat_formatter
MessagesFormatterType.DEEP_SEEK_CODER_2: deep_seek_coder_2_chat_formatter,
MessagesFormatterType.MISTRAL_V1: mistral_v1_formatter,
MessagesFormatterType.MISTRAL_V2: mistral_v2_formatter,
MessagesFormatterType.MISTRAL_V3_TEKKEN: mistral_v3_tekken_formatter
}


Expand Down