Skip to content

Commit

Permalink
Add phi-4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
remip2 committed Jan 16, 2025
1 parent 1ffd4a6 commit f7c0194
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions fastchat/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SeparatorStyle(IntEnum):
GEMMA = auto()
CLLM = auto()
DEFAULT = auto()
PHI4 = auto()


IMAGE_PLACEHOLDER_STR = "$$<image>$$"
Expand Down Expand Up @@ -324,6 +325,16 @@ def get_prompt(self) -> str:
else:
ret += role + ":"
return ret
elif self.sep_style == SeparatorStyle.PHI4:
ret = ""
if self.system_message:
ret += system_prompt
for i, (role, message) in enumerate(self.messages):
if message:
ret += f"<|im_start|>{role}<|im_sep|>{message.strip()}<|im_end|>"
else:
ret += f"<|im_start|>{role}<|im_sep|>"
return ret
else:
raise ValueError(f"Invalid style: {self.sep_style}")

Expand Down Expand Up @@ -2298,6 +2309,19 @@ def get_conv_template(name: str) -> Conversation:
)
)

# reference: https://github.com/ggerganov/llama.cpp/pull/11148
register_conv_template(
Conversation(
name="phi-4",
system_template="<|im_start|>system<|im_sep|>{system_message}<|im_end|>",
roles=("user", "assistant"),
sep_style=SeparatorStyle.PHI4,
sep="<|im_sep|>",
stop_str="<|im_end|>",
stop_token_ids=[100257, 100265], # <|endoftext|>: 100257, <|im_end|>": 100265,
)
)


if __name__ == "__main__":
from fastchat.conversation import get_conv_template
Expand Down
11 changes: 11 additions & 0 deletions fastchat/model/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,16 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
return get_conv_template("api_based_default")


class Phi4Adapter(BaseModelAdapter):
"""The model adapter for Phi-4"""

def match(self, model_path: str):
return "phi-4" in model_path.lower()

def get_default_conv_template(self, model_path: str) -> Conversation:
return get_conv_template("phi-4")


# Note: the registration order matters.
# The one registered earlier has a higher matching priority.
register_model_adapter(PeftModelAdapter)
Expand Down Expand Up @@ -2602,6 +2612,7 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
register_model_adapter(Llama31Adapter)
register_model_adapter(GrokAdapter)
register_model_adapter(NoSystemAdapter)
register_model_adapter(Phi4Adapter)

# After all adapters, try the default base adapter.
register_model_adapter(BaseModelAdapter)
9 changes: 9 additions & 0 deletions fastchat/model/model_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ def get_model_info(name: str) -> ModelInfo:
"A capable and cost-effective small language models (SLMs) by Microsoft",
)

register_model_info(
[
"phi-4"
],
"Phi-4",
"https://techcommunity.microsoft.com/blog/aiplatformblog/introducing-phi-4-microsoft%E2%80%99s-newest-small-language-model-specializing-in-comple/4357090",
"A capable and cost-effective small language model (SLMs) by Microsoft",
)

register_model_info(
[
"minicpm-v-2_6",
Expand Down

0 comments on commit f7c0194

Please sign in to comment.