Skip to content
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

add chatglm3 conv template support in conversation.py #2622

Merged
merged 7 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions fastchat/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SeparatorStyle(IntEnum):
PHOENIX = auto()
ROBIN = auto()
FALCON_CHAT = auto()
CHATGLM3 = auto()


@dataclasses.dataclass
Expand Down Expand Up @@ -163,6 +164,16 @@ def get_prompt(self) -> str:
else:
ret += role + "\n"
return ret
elif self.sep_style == SeparatorStyle.CHATGLM3:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ret = ""
if self.system_message:
ret += system_prompt
for role, message in self.messages:
if message:
ret += role + "\n" + " " + message
else:
ret += role
return ret
elif self.sep_style == SeparatorStyle.CHATINTERN:
# source: https://huggingface.co/internlm/internlm-chat-7b-8k/blob/bd546fa984b4b0b86958f56bf37f94aa75ab8831/modeling_internlm.py#L771
seps = [self.sep, self.sep2]
Expand Down Expand Up @@ -448,6 +459,21 @@ def get_conv_template(name: str) -> Conversation:
)
)

# ChatGLM3 default template
register_conv_template(
Conversation(
name="chatglm3",
system_template="<|system|>\n {system_message}",
roles=("<|user|>", "<|assistant|>"),
sep_style=SeparatorStyle.CHATGLM3,
stop_token_ids=[
64795,
64797,
2,
], # "<|user|>", "<|observation|>", "</s>"
)
)

# CodeGeex(2) Template
register_conv_template(
Conversation(
Expand Down
16 changes: 13 additions & 3 deletions fastchat/model/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,17 @@ def match(self, model_path: str):

def load_model(self, model_path: str, from_pretrained_kwargs: dict):
revision = from_pretrained_kwargs.get("revision", "main")
tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=True, revision=revision
)
if "chatglm3" in model_path.lower():
tokenizer = AutoTokenizer.from_pretrained(
model_path,
encode_special_tokens=True,
trust_remote_code=True,
revision=revision,
)
else:
tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=True, revision=revision
)
model = AutoModel.from_pretrained(
model_path, trust_remote_code=True, **from_pretrained_kwargs
)
Expand All @@ -766,6 +774,8 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
model_path = model_path.lower()
if "chatglm2" in model_path.lower():
return get_conv_template("chatglm2")
if "chatglm3" in model_path.lower():
return get_conv_template("chatglm3")
return get_conv_template("chatglm")


Expand Down