Skip to content

Commit

Permalink
add phi 3.5 template
Browse files Browse the repository at this point in the history
  • Loading branch information
amakropoulos committed Sep 26, 2024
1 parent 75eaf19 commit 0c1c407
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
27 changes: 26 additions & 1 deletion Runtime/LLMChatTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static ChatTemplate()
new LLama3ChatTemplate(),
new LLama2ChatTemplate(),
new LLama2Template(),
new Phi3_5Template(),
new Phi3Template(),
new Phi2Template(),
new VicunaTemplate(),
Expand Down Expand Up @@ -448,7 +449,7 @@ public override string[] GetStop(string playerName, string AIName)

/// @ingroup template
/// <summary>
/// Class implementing the Zephyr template
/// Class implementing the Phi-3 template
/// </summary>
public class Phi3Template : ChatTemplate
{
Expand Down Expand Up @@ -488,6 +489,30 @@ public override string[] GetStop(string playerName, string AIName)
}
}

/// @ingroup template
/// <summary>
/// Class implementing the Phi-3.5 template
/// </summary>
public class Phi3_5Template : ChatTemplate
{
public override string GetName() { return "phi-3.5"; }
public override string GetDescription() { return "phi-3.5"; }
public override string[] GetNameMatches() { return new string[] {"phi-3.5"}; }
public override string[] GetChatTemplateMatches() { return new string[] {"{% for message in messages %}{% if message['role'] == 'system' and message['content'] %}{{'<|system|>\n' + message['content'] + '<|end|>\n'}}{% elif message['role'] == 'user' %}{{'<|user|>\n' + message['content'] + '<|end|>\n'}}{% elif message['role'] == 'assistant' %}{{'<|assistant|>\n' + message['content'] + '<|end|>\n'}}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>\n' }}{% else %}{{ eos_token }}{% endif %}"};}

protected override string PlayerPrefix(string playerName) { return $"<|user|>\n"; }
protected override string AIPrefix(string AIName) { return $"<|assistant|>\n"; }
protected override string RequestSuffix() { return "<|end|>\n"; }
protected override string PairSuffix() { return "<|end|>\n"; }
protected override string SystemPrefix() { return "<|system|>\n"; }
protected override string SystemSuffix() { return "<|end|>\n"; }

public override string[] GetStop(string playerName, string AIName)
{
return AddStopNewlines(new string[] { "<|end|>", "<|user|>", "<|assistant|>" });
}
}

/// @ingroup template
/// <summary>
/// Class implementing the Zephyr template
Expand Down
2 changes: 1 addition & 1 deletion Runtime/LLMUnitySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class LLMUnitySetup
("OpenHermes 2.5 7B", "https://huggingface.co/TheBloke/OpenHermes-2.5-Mistral-7B-GGUF/resolve/main/openhermes-2.5-mistral-7b.Q4_K_M.gguf?download=true", null),
}},
{"Small models", new(string, string, string)[]
{
{
("Llama 3.2 3B", "https://huggingface.co/hugging-quants/Llama-3.2-3B-Instruct-Q4_K_M-GGUF/resolve/main/llama-3.2-3b-instruct-q4_k_m.gguf", null),
("Phi 3.5 4B", "https://huggingface.co/bartowski/Phi-3.5-mini-instruct-GGUF/resolve/main/Phi-3.5-mini-instruct-Q4_K_M.gguf", null),
}},
Expand Down
9 changes: 9 additions & 0 deletions Tests/Runtime/TestLLMChatTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ public void TestPhi3()
);
}

[Test]
public void TestPhi3_5()
{
Assert.AreEqual(
new Phi3_5Template().ComputePrompt(messages, "user", "assistant"),
"<|system|>\nyou are a bot<|end|>\n<|user|>\nHello, how are you?<|end|>\n<|assistant|>\nI'm doing great. How can I help you today?<|end|>\n<|user|>\nI'd like to show off how chat templating works!<|end|>\n<|assistant|>\nchat template is awesome<|end|>\n<|user|>\ndo you think so?<|end|>\n<|assistant|>\n"
);
}

[Test]
public void TestZephyr()
{
Expand Down

0 comments on commit 0c1c407

Please sign in to comment.