From 2484a5b5c44d5e4c8954230c3ade5b2746c8fe9a Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 25 Jul 2023 09:52:51 +0800 Subject: [PATCH 1/9] Support custom system message within template --- fastchat/conversation.py | 66 +++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index 5752de345..695b66df0 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -37,6 +37,7 @@ class Conversation: name: str # The system prompt system: str + system_msg: str = "" # Two roles roles: List[str] # All messages. Each item is (role, message). @@ -53,9 +54,10 @@ class Conversation: stop_token_ids: List[int] = None def get_prompt(self) -> str: + system_prompt = self.system.format(system_msg=self.system_msg) """Get the prompt for generation.""" if self.sep_style == SeparatorStyle.ADD_COLON_SINGLE: - ret = self.system + self.sep + ret = system_prompt + self.sep for role, message in self.messages: if message: ret += role + ": " + message + self.sep @@ -64,7 +66,7 @@ def get_prompt(self) -> str: return ret elif self.sep_style == SeparatorStyle.ADD_COLON_TWO: seps = [self.sep, self.sep2] - ret = self.system + seps[0] + ret = system_prompt + seps[0] for i, (role, message) in enumerate(self.messages): if message: ret += role + ": " + message + seps[i % 2] @@ -72,7 +74,7 @@ def get_prompt(self) -> str: ret += role + ":" return ret elif self.sep_style == SeparatorStyle.ADD_COLON_SPACE_SINGLE: - ret = self.system + self.sep + ret = system_prompt + self.sep for role, message in self.messages: if message: ret += role + ": " + message + self.sep @@ -80,7 +82,7 @@ def get_prompt(self) -> str: ret += role + ": " # must be end with a space return ret elif self.sep_style == SeparatorStyle.ADD_NEW_LINE_SINGLE: - ret = "" if self.system == "" else self.system + self.sep + ret = "" if system_prompt == "" else system_prompt + self.sep for role, message in self.messages: if message: ret += role + "\n" + message + self.sep @@ -88,7 +90,7 @@ def get_prompt(self) -> str: ret += role + "\n" return ret elif self.sep_style == SeparatorStyle.NO_COLON_SINGLE: - ret = self.system + ret = system_prompt for role, message in self.messages: if message: ret += role + message + self.sep @@ -97,7 +99,7 @@ def get_prompt(self) -> str: return ret elif self.sep_style == SeparatorStyle.NO_COLON_TWO: seps = [self.sep, self.sep2] - ret = self.system + ret = system_prompt for i, (role, message) in enumerate(self.messages): if message: ret += role + message + seps[i % 2] @@ -105,7 +107,7 @@ def get_prompt(self) -> str: ret += role return ret elif self.sep_style == SeparatorStyle.RWKV: - ret = self.system + ret = system_prompt for i, (role, message) in enumerate(self.messages): if message: ret += ( @@ -123,7 +125,7 @@ def get_prompt(self) -> str: for i, (role, message) in enumerate(self.messages): if message: if i == 0: - ret += self.system + message + ret += system_prompt + message else: ret += role + " " + message + seps[i % 2] else: @@ -133,8 +135,8 @@ def get_prompt(self) -> str: # source: https://huggingface.co/THUDM/chatglm-6b/blob/1d240ba371910e9282298d4592532d7f0f3e9f3e/modeling_chatglm.py#L1302-L1308 # source2: https://huggingface.co/THUDM/chatglm2-6b/blob/e186c891cf64310ac66ef10a87e6635fa6c2a579/modeling_chatglm.py#L926 round_add_n = 1 if self.name == "chatglm2" else 0 - if self.system: - ret = self.system + self.sep + if system_prompt: + ret = system_prompt + self.sep else: ret = "" @@ -148,7 +150,7 @@ def get_prompt(self) -> str: ret += f"{role}:" return ret elif self.sep_style == SeparatorStyle.CHATML: - ret = "" if self.system == "" else self.system + self.sep + "\n" + ret = "" if system_prompt == "" else system_prompt + self.sep + "\n" for role, message in self.messages: if message: ret += role + "\n" + message + self.sep + "\n" @@ -158,7 +160,7 @@ def get_prompt(self) -> str: 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] - ret = self.system + ret = system_prompt for i, (role, message) in enumerate(self.messages): if i % 2 == 0: ret += "" @@ -169,7 +171,7 @@ def get_prompt(self) -> str: return ret elif self.sep_style == SeparatorStyle.DOLLY: seps = [self.sep, self.sep2] - ret = self.system + ret = system_prompt for i, (role, message) in enumerate(self.messages): if message: ret += role + ":\n" + message + seps[i % 2] @@ -179,7 +181,7 @@ def get_prompt(self) -> str: ret += role + ":\n" return ret elif self.sep_style == SeparatorStyle.PHOENIX: - ret = self.system + ret = system_prompt for role, message in self.messages: if message: ret += role + ": " + "" + message + "" @@ -187,7 +189,7 @@ def get_prompt(self) -> str: ret += role + ": " + "" return ret elif self.sep_style == SeparatorStyle.ROBIN: - ret = self.system + self.sep + ret = system_prompt + self.sep for role, message in self.messages: if message: ret += role + ":\n" + message + self.sep @@ -197,6 +199,10 @@ def get_prompt(self) -> str: else: raise ValueError(f"Invalid style: {self.sep_style}") + def set_system_msg(self, system_msg: str): + """Set the system message.""" + self.system_msg = system_msg + def append_message(self, role: str, message: str): """Append a new message.""" self.messages.append([role, message]) @@ -221,7 +227,8 @@ def to_gradio_chatbot(self): def to_openai_api_messages(self): """Convert the conversation to OpenAI chat completion format.""" - ret = [{"role": "system", "content": self.system}] + system_prompt = self.system.format(system_msg=self.system_msg) + ret = [{"role": "system", "content": system_prompt}] for i, (_, msg) in enumerate(self.messages[self.offset :]): if i % 2 == 0: @@ -235,6 +242,7 @@ def copy(self): return Conversation( name=self.name, system=self.system, + system_msg=self.system_msg, roles=self.roles, messages=[[x, y] for x, y in self.messages], offset=self.offset, @@ -278,7 +286,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="one_shot", - system="A chat between a curious human and an artificial intelligence assistant. " + system="{system_msg}", + system_msg="A chat between a curious human and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the human's questions.", roles=("Human", "Assistant"), messages=( @@ -311,7 +320,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="zero_shot", - system="A chat between a curious human and an artificial intelligence assistant. " + system="{system_msg}", + system_msg="A chat between a curious human and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the human's questions.", roles=("Human", "Assistant"), messages=(), @@ -555,7 +565,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="chatgpt", - system="You are a helpful assistant.", + system="{system_msg}", + system_msg="You are a helpful assistant.", roles=("user", "assistant"), messages=(), offset=0, @@ -568,7 +579,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="claude", - system="", + system="{system_msg}", + system_msg="", roles=("Human", "Assistant"), messages=(), offset=0, @@ -582,7 +594,8 @@ def get_conv_template(name: str) -> Conversation: Conversation( name="mpt-7b-chat", system="""<|im_start|>system -- You are a helpful assistant chatbot trained by MosaicML. +{system_msg}""", + system_msg="""- You are a helpful assistant chatbot trained by MosaicML. - You answer questions. - You are excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user. - You are more than just an information source, you are also able to write poetry, short stories, and make jokes.""", @@ -600,7 +613,8 @@ def get_conv_template(name: str) -> Conversation: Conversation( name="mpt-30b-chat", system="""<|im_start|>system -A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.""", +{system_msg}""", + system_msg="""A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.""", roles=("<|im_start|>user", "<|im_start|>assistant"), messages=(), offset=0, @@ -631,7 +645,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="bard", - system="", + system="{system_msg}", + system_msg="", roles=("0", "1"), messages=(), offset=0, @@ -850,11 +865,12 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="llama-2", - system="[INST] <>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. " + system="[INST] <>\n{system_msg}\n<>\n\n", + system_msg="You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. " "Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. " "Please ensure that your responses are socially unbiased and positive in nature.\n\n" "If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. " - "If you don't know the answer to a question, please don't share false information.\n<>\n\n", + "If you don't know the answer to a question, please don't share false information.", roles=("[INST]", "[/INST]"), messages=(), offset=0, From 745062ccb97c81d9b95f79f3dce6cb8bbe0ebd0e Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 25 Jul 2023 09:55:01 +0800 Subject: [PATCH 2/9] Fix argument order --- fastchat/conversation.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index 695b66df0..9285405e9 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -37,7 +37,6 @@ class Conversation: name: str # The system prompt system: str - system_msg: str = "" # Two roles roles: List[str] # All messages. Each item is (role, message). @@ -48,6 +47,7 @@ class Conversation: sep_style: SeparatorStyle sep: str sep2: str = None + system_msg: str = "" # Stop criteria (the default one is EOS token) stop_str: str = None # Stops generation if meeting any token in this list @@ -896,7 +896,8 @@ def get_conv_template(name: str) -> Conversation: ) if __name__ == "__main__": - conv = get_conv_template("vicuna_v1.1") + conv = get_conv_template("llama-2") + conv.set_system_msg("You are a helpful, respectful and honest assistant.") conv.append_message(conv.roles[0], "Hello!") conv.append_message(conv.roles[1], "Hi!") conv.append_message(conv.roles[0], "How are you?") From a971f1ec0983437a068e8bb2630764431a48d712 Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 25 Jul 2023 09:59:50 +0800 Subject: [PATCH 3/9] Add more samples --- fastchat/conversation.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index 9285405e9..fee46e37c 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -896,6 +896,13 @@ def get_conv_template(name: str) -> Conversation: ) if __name__ == "__main__": + conv = get_conv_template("vicuna_v1.1") + conv.append_message(conv.roles[0], "Hello!") + conv.append_message(conv.roles[1], "Hi!") + conv.append_message(conv.roles[0], "How are you?") + conv.append_message(conv.roles[1], None) + print(conv.get_prompt()) + conv = get_conv_template("llama-2") conv.set_system_msg("You are a helpful, respectful and honest assistant.") conv.append_message(conv.roles[0], "Hello!") From 151e856e909f9558d421b6fe02e324731e5cc8ac Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 25 Jul 2023 10:02:40 +0800 Subject: [PATCH 4/9] Add space --- fastchat/conversation.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index fee46e37c..73b8006f9 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -896,6 +896,7 @@ def get_conv_template(name: str) -> Conversation: ) if __name__ == "__main__": + print('Output from vicuna_v1.1:') conv = get_conv_template("vicuna_v1.1") conv.append_message(conv.roles[0], "Hello!") conv.append_message(conv.roles[1], "Hi!") @@ -903,6 +904,9 @@ def get_conv_template(name: str) -> Conversation: conv.append_message(conv.roles[1], None) print(conv.get_prompt()) + print() + + print('Output from llama-2') conv = get_conv_template("llama-2") conv.set_system_msg("You are a helpful, respectful and honest assistant.") conv.append_message(conv.roles[0], "Hello!") From b11c175abc409939feedce0248d929528e78eb4e Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 25 Jul 2023 10:02:52 +0800 Subject: [PATCH 5/9] Missing ':' --- fastchat/conversation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index 73b8006f9..afe005890 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -906,7 +906,7 @@ def get_conv_template(name: str) -> Conversation: print() - print('Output from llama-2') + print('Output from llama-2:') conv = get_conv_template("llama-2") conv.set_system_msg("You are a helpful, respectful and honest assistant.") conv.append_message(conv.roles[0], "Hello!") From 84f17182e6e26b6e481dc6e4fc4ea03ce020a66e Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 25 Jul 2023 10:12:23 +0800 Subject: [PATCH 6/9] Fix comment --- fastchat/conversation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index afe005890..40f57b25f 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -54,8 +54,8 @@ class Conversation: stop_token_ids: List[int] = None def get_prompt(self) -> str: - system_prompt = self.system.format(system_msg=self.system_msg) """Get the prompt for generation.""" + system_prompt = self.system.format(system_msg=self.system_msg) if self.sep_style == SeparatorStyle.ADD_COLON_SINGLE: ret = system_prompt + self.sep for role, message in self.messages: From df5ebb477811a72f16ff9b2f23629315c2459d3b Mon Sep 17 00:00:00 2001 From: Elsa Date: Tue, 25 Jul 2023 10:17:55 +0800 Subject: [PATCH 7/9] Fix format --- fastchat/conversation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index 40f57b25f..a1bf0f036 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -896,7 +896,7 @@ def get_conv_template(name: str) -> Conversation: ) if __name__ == "__main__": - print('Output from vicuna_v1.1:') + print("Output from vicuna_v1.1:") conv = get_conv_template("vicuna_v1.1") conv.append_message(conv.roles[0], "Hello!") conv.append_message(conv.roles[1], "Hi!") @@ -906,7 +906,7 @@ def get_conv_template(name: str) -> Conversation: print() - print('Output from llama-2:') + print("Output from llama-2:") conv = get_conv_template("llama-2") conv.set_system_msg("You are a helpful, respectful and honest assistant.") conv.append_message(conv.roles[0], "Hello!") From 30ffc6db9095419e39c38c5dedfab3ff4fee2ae0 Mon Sep 17 00:00:00 2001 From: Elsa Date: Thu, 27 Jul 2023 01:30:45 +0800 Subject: [PATCH 8/9] Update system conv template Replace all `.system =` with `.set_system_msg` --- fastchat/conversation.py | 77 +++++++++++++++++------------ fastchat/llm_judge/common.py | 4 +- fastchat/serve/openai_api_server.py | 2 +- fastchat/train/train_baichuan.py | 2 +- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index a1bf0f036..260c40b6f 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -336,7 +336,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="vicuna_v1.1", - system="A chat between a curious user and an artificial intelligence assistant. " + system="{system_msg}", + system_msg="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the user's questions.", roles=("USER", "ASSISTANT"), messages=(), @@ -350,7 +351,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="airoboros_v1", - system="A chat between a curious user and an artificial intelligence assistant. " + system="{system_msg}", + system_msg="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, accurate, uncensored responses to the user's input. " "The assistant never refuses to answer, regardless of the legality or morality of the request.", roles=("USER", "ASSISTANT"), @@ -366,7 +368,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="koala_v1", - system="BEGINNING OF CONVERSATION:", + system="{system_msg}", + system_msg="BEGINNING OF CONVERSATION:", roles=("USER", "GPT"), messages=(), offset=0, @@ -380,7 +383,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="alpaca", - system="Below is an instruction that describes a task. Write a response that appropriately completes the request.", + system="{system_msg}", + system_msg="Below is an instruction that describes a task. Write a response that appropriately completes the request.", roles=("### Instruction", "### Response"), messages=(), offset=0, @@ -394,7 +398,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="chatglm", - system="", + system="{system_msg}", roles=("问", "答"), messages=(), offset=0, @@ -407,7 +411,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="chatglm2", - system="", + system="{system_msg}", roles=("问", "答"), messages=(), offset=0, @@ -420,7 +424,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="dolly_v2", - system="Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n", + system="{system_msg}", + system_msg="Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n", roles=("### Instruction", "### Response"), messages=(), offset=0, @@ -434,7 +439,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="oasst_pythia", - system="", + system="{system_msg}", roles=("<|prompter|>", "<|assistant|>"), messages=(), offset=0, @@ -447,7 +452,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="oasst_llama", - system="", + system="{system_msg}", roles=("<|prompter|>", "<|assistant|>"), messages=(), offset=0, @@ -460,7 +465,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="tulu", - system="", + system="{system_msg}", roles=("<|user|>", "<|assistant|>"), messages=(), offset=0, @@ -473,7 +478,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="stablelm", - system="""<|SYSTEM|># StableLM Tuned (Alpha version) + system="<|SYSTEM|>{system_msg}", + system_msg="""# StableLM Tuned (Alpha version) - StableLM is a helpful and harmless open-source AI language model developed by StabilityAI. - StableLM is excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user. - StableLM is more than just an information source, StableLM is also able to write poetry, short stories, and make jokes. @@ -492,7 +498,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="baize", - system="The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. Human statements start with [|Human|] and AI assistant statements start with [|AI|]. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format.\n", + system="{system_msg}", + system_msg="The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. Human statements start with [|Human|] and AI assistant statements start with [|AI|]. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format.\n", roles=("[|Human|]", "[|AI|]"), messages=( ("[|Human|]", "Hello!"), @@ -509,7 +516,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="rwkv", - system="", + system="{system_msg}", roles=("Bob", "Alice"), messages=( ("Bob", "hi"), @@ -529,7 +536,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="openbuddy", - system="""Consider a conversation between User (a human) and Assistant (named Buddy). + system="{system_msg}", + system_msg="""Consider a conversation between User (a human) and Assistant (named Buddy). Buddy is an INTP-T, a friendly, intelligent and multilingual AI assistant, by OpenBuddy team. GitHub: https://github.com/OpenBuddy/OpenBuddy Buddy cannot access the Internet. Buddy can fluently speak the user's language (e.g. English, Chinese). @@ -552,7 +560,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="phoenix", - system="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n", + system="{system_msg}", + system_msg="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n", roles=("Human", "Assistant"), messages=(), offset=0, @@ -580,7 +589,6 @@ def get_conv_template(name: str) -> Conversation: Conversation( name="claude", system="{system_msg}", - system_msg="", roles=("Human", "Assistant"), messages=(), offset=0, @@ -629,7 +637,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="mpt-30b-instruct", - system="Below is an instruction that describes a task. Write a response that appropriately completes the request.", + system="{system_msg}", + system_msg="Below is an instruction that describes a task. Write a response that appropriately completes the request.", roles=("### Instruction", "### Response"), messages=(), offset=0, @@ -646,7 +655,6 @@ def get_conv_template(name: str) -> Conversation: Conversation( name="bard", system="{system_msg}", - system_msg="", roles=("0", "1"), messages=(), offset=0, @@ -659,7 +667,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="billa", - system="", + system="{system_msg}", roles=("Human", "Assistant"), messages=(), offset=0, @@ -673,7 +681,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="redpajama-incite", - system="", + system="{system_msg}", roles=("", ""), messages=(), offset=0, @@ -687,7 +695,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="h2ogpt", - system="", + system="{system_msg}", roles=("<|prompt|>", "<|answer|>"), messages=(), offset=0, @@ -700,7 +708,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="Robin", - system="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.", + system="{system_msg}", + system_msg="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.", roles=("###Human", "###Assistant"), messages=(), offset=0, @@ -716,7 +725,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="snoozy", - system="### Instruction:\nThe prompt below is a question to answer, a task to complete, or a conversation to respond to; decide which and write an appropriate response.", + system="### Instruction:\n{system_msg}", + system_msg="The prompt below is a question to answer, a task to complete, or a conversation to respond to; decide which and write an appropriate response.", roles=("### Prompt", "### Response"), messages=(), offset=0, @@ -730,7 +740,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="manticore", - system="", + system="{system_msg}", roles=("USER", "ASSISTANT"), messages=(), offset=0, @@ -744,7 +754,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="falcon", - system="", + system="{system_msg}", roles=("User", "Assistant"), messages=[], offset=0, @@ -773,7 +783,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="polyglot_changgpt", - system="", + system="{system_msg}", roles=("B", "A"), messages=(), offset=0, @@ -786,7 +796,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="tigerbot", - system="A chat between a curious user and an artificial intelligence assistant. " + system="{system_msg}", + system_msg="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the user's questions.", roles=("### Instruction", "### Response"), messages=(), @@ -801,7 +812,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="xgen", - system="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n", + system="{system_msg}", + system_msg="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n", roles=("### Human: ", "###"), messages=(), offset=0, @@ -816,7 +828,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="internlm-chat", - system="A chat between a curious <|User|> and an <|Bot|>. The <|Bot|> gives helpful, detailed, and polite answers to the <|User|>'s questions.\n\n", + system="{system_msg}", + system_msg="A chat between a curious <|User|> and an <|Bot|>. The <|Bot|> gives helpful, detailed, and polite answers to the <|User|>'s questions.\n\n", roles=("<|User|>", "<|Bot|>"), messages=(), offset=0, @@ -832,7 +845,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="starchat", - system="\n", + system="\n{system_msg}<|end|>\n", roles=("<|user|>", "<|assistant|>"), messages=(), offset=0, @@ -849,7 +862,7 @@ def get_conv_template(name: str) -> Conversation: # https://huggingface.co/baichuan-inc/Baichuan-13B-Chat/blob/main/generation_config.json Conversation( name="baichuan-chat", - system="", + system="{system_msg}", roles=(" ", " "), messages=(), offset=0, @@ -884,7 +897,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="cutegpt", - system="", + system="{system_msg}", roles=("问:", "答:\n"), messages=(), offset=0, diff --git a/fastchat/llm_judge/common.py b/fastchat/llm_judge/common.py index 6ec9b5eee..2c04a9706 100644 --- a/fastchat/llm_judge/common.py +++ b/fastchat/llm_judge/common.py @@ -153,7 +153,7 @@ def run_judge_single(question, answer, judge, ref_answer, multi_turn=False): system_prompt = judge.prompt_template["system_prompt"] conv = get_conversation_template(model) - conv.system = system_prompt + conv.set_system_msg(system_prompt) conv.append_message(conv.roles[0], user_prompt) conv.append_message(conv.roles[1], None) @@ -260,7 +260,7 @@ def run_judge_pair(question, answer_a, answer_b, judge, ref_answer, multi_turn=F conv.append_message(conv.roles[1], None) if model in ["gpt-3.5-turbo", "gpt-4"]: - conv.system = system_prompt + conv.set_system_msg(system_prompt) judgment = chat_compeletion_openai(model, conv, temperature=0, max_tokens=2048) elif model in ["claude-v1", "claude-instant-v1"]: if system_prompt != "You are a helpful assistant.": diff --git a/fastchat/serve/openai_api_server.py b/fastchat/serve/openai_api_server.py index b83c4152f..507ad80bd 100644 --- a/fastchat/serve/openai_api_server.py +++ b/fastchat/serve/openai_api_server.py @@ -249,7 +249,7 @@ async def get_gen_params( for message in messages: msg_role = message["role"] if msg_role == "system": - conv.system = message["content"] + conv.set_system_msg(message["content"]) elif msg_role == "user": conv.append_message(conv.roles[0], message["content"]) elif msg_role == "assistant": diff --git a/fastchat/train/train_baichuan.py b/fastchat/train/train_baichuan.py index 9450120e4..df620f7f2 100644 --- a/fastchat/train/train_baichuan.py +++ b/fastchat/train/train_baichuan.py @@ -91,7 +91,7 @@ def apply_prompt_template(sources, systems=None): assert role == conv.roles[j % 2], f"{i}" conv.append_message(role, sentence["value"]) if systems and systems[i]: - conv.system = systems[i] + conv.set_system_msg(systems[i]) prompt = conv.get_prompt() conversations.append(prompt) return conversations, conv From d9757c16fd7c4eb5ea5fef497c437aef424873de Mon Sep 17 00:00:00 2001 From: Lianmin Zheng Date: Wed, 2 Aug 2023 01:07:43 +0000 Subject: [PATCH 9/9] improve style --- fastchat/conversation.py | 134 +++++++++++----------------- fastchat/llm_judge/common.py | 4 +- fastchat/serve/openai_api_server.py | 5 +- fastchat/train/train_baichuan.py | 2 +- 4 files changed, 58 insertions(+), 87 deletions(-) diff --git a/fastchat/conversation.py b/fastchat/conversation.py index 260c40b6f..d41af70f3 100644 --- a/fastchat/conversation.py +++ b/fastchat/conversation.py @@ -35,19 +35,20 @@ class Conversation: # The name of this template name: str - # The system prompt - system: str - # Two roles - roles: List[str] + # The template of the system prompt + system_template: str = "{system_message}" + # The system message + system_message: str = "" + # The names of two roles + roles: List[str] = (("USER", "ASSISTANT"),) # All messages. Each item is (role, message). - messages: List[List[str]] + messages: List[List[str]] = () # The number of few shot examples - offset: int - # Separators - sep_style: SeparatorStyle - sep: str + offset: int = 0 + # The separator style and configurations + sep_style: SeparatorStyle = SeparatorStyle.ADD_COLON_SINGLE + sep: str = "\n" sep2: str = None - system_msg: str = "" # Stop criteria (the default one is EOS token) stop_str: str = None # Stops generation if meeting any token in this list @@ -55,7 +56,7 @@ class Conversation: def get_prompt(self) -> str: """Get the prompt for generation.""" - system_prompt = self.system.format(system_msg=self.system_msg) + system_prompt = self.system_template.format(system_message=self.system_message) if self.sep_style == SeparatorStyle.ADD_COLON_SINGLE: ret = system_prompt + self.sep for role, message in self.messages: @@ -199,9 +200,9 @@ def get_prompt(self) -> str: else: raise ValueError(f"Invalid style: {self.sep_style}") - def set_system_msg(self, system_msg: str): + def set_system_message(self, system_message: str): """Set the system message.""" - self.system_msg = system_msg + self.system_message = system_message def append_message(self, role: str, message: str): """Append a new message.""" @@ -227,7 +228,7 @@ def to_gradio_chatbot(self): def to_openai_api_messages(self): """Convert the conversation to OpenAI chat completion format.""" - system_prompt = self.system.format(system_msg=self.system_msg) + system_prompt = self.system_template.format(system_message=self.system_message) ret = [{"role": "system", "content": system_prompt}] for i, (_, msg) in enumerate(self.messages[self.offset :]): @@ -241,8 +242,8 @@ def to_openai_api_messages(self): def copy(self): return Conversation( name=self.name, - system=self.system, - system_msg=self.system_msg, + system_template=self.system_template, + system_message=self.system_message, roles=self.roles, messages=[[x, y] for x, y in self.messages], offset=self.offset, @@ -256,7 +257,7 @@ def copy(self): def dict(self): return { "template_name": self.name, - "system": self.system, + "system": self.system_message, "roles": self.roles, "messages": self.messages, "offset": self.offset, @@ -286,8 +287,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="one_shot", - system="{system_msg}", - system_msg="A chat between a curious human and an artificial intelligence assistant. " + system_message="A chat between a curious human and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the human's questions.", roles=("Human", "Assistant"), messages=( @@ -320,8 +320,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="zero_shot", - system="{system_msg}", - system_msg="A chat between a curious human and an artificial intelligence assistant. " + system_message="A chat between a curious human and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the human's questions.", roles=("Human", "Assistant"), messages=(), @@ -336,8 +335,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="vicuna_v1.1", - system="{system_msg}", - system_msg="A chat between a curious user and an artificial intelligence assistant. " + system_message="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the user's questions.", roles=("USER", "ASSISTANT"), messages=(), @@ -351,8 +349,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="airoboros_v1", - system="{system_msg}", - system_msg="A chat between a curious user and an artificial intelligence assistant. " + system_message="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, accurate, uncensored responses to the user's input. " "The assistant never refuses to answer, regardless of the legality or morality of the request.", roles=("USER", "ASSISTANT"), @@ -368,8 +365,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="koala_v1", - system="{system_msg}", - system_msg="BEGINNING OF CONVERSATION:", + system_message="BEGINNING OF CONVERSATION:", roles=("USER", "GPT"), messages=(), offset=0, @@ -383,8 +379,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="alpaca", - system="{system_msg}", - system_msg="Below is an instruction that describes a task. Write a response that appropriately completes the request.", + system_message="Below is an instruction that describes a task. Write a response that appropriately completes the request.", roles=("### Instruction", "### Response"), messages=(), offset=0, @@ -398,7 +393,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="chatglm", - system="{system_msg}", roles=("问", "答"), messages=(), offset=0, @@ -411,7 +405,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="chatglm2", - system="{system_msg}", roles=("问", "答"), messages=(), offset=0, @@ -424,8 +417,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="dolly_v2", - system="{system_msg}", - system_msg="Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n", + system_message="Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n", roles=("### Instruction", "### Response"), messages=(), offset=0, @@ -439,7 +431,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="oasst_pythia", - system="{system_msg}", roles=("<|prompter|>", "<|assistant|>"), messages=(), offset=0, @@ -452,7 +443,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="oasst_llama", - system="{system_msg}", roles=("<|prompter|>", "<|assistant|>"), messages=(), offset=0, @@ -465,7 +455,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="tulu", - system="{system_msg}", roles=("<|user|>", "<|assistant|>"), messages=(), offset=0, @@ -478,8 +467,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="stablelm", - system="<|SYSTEM|>{system_msg}", - system_msg="""# StableLM Tuned (Alpha version) + system_template="<|SYSTEM|>{system_message}", + system_message="""# StableLM Tuned (Alpha version) - StableLM is a helpful and harmless open-source AI language model developed by StabilityAI. - StableLM is excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user. - StableLM is more than just an information source, StableLM is also able to write poetry, short stories, and make jokes. @@ -498,8 +487,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="baize", - system="{system_msg}", - system_msg="The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. Human statements start with [|Human|] and AI assistant statements start with [|AI|]. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format.\n", + system_message="The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. Human statements start with [|Human|] and AI assistant statements start with [|AI|]. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format.\n", roles=("[|Human|]", "[|AI|]"), messages=( ("[|Human|]", "Hello!"), @@ -516,7 +504,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="rwkv", - system="{system_msg}", roles=("Bob", "Alice"), messages=( ("Bob", "hi"), @@ -536,8 +523,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="openbuddy", - system="{system_msg}", - system_msg="""Consider a conversation between User (a human) and Assistant (named Buddy). + system_message="""Consider a conversation between User (a human) and Assistant (named Buddy). Buddy is an INTP-T, a friendly, intelligent and multilingual AI assistant, by OpenBuddy team. GitHub: https://github.com/OpenBuddy/OpenBuddy Buddy cannot access the Internet. Buddy can fluently speak the user's language (e.g. English, Chinese). @@ -560,8 +546,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="phoenix", - system="{system_msg}", - system_msg="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n", + system_message="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n", roles=("Human", "Assistant"), messages=(), offset=0, @@ -574,8 +559,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="chatgpt", - system="{system_msg}", - system_msg="You are a helpful assistant.", + system_message="You are a helpful assistant.", roles=("user", "assistant"), messages=(), offset=0, @@ -588,7 +572,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="claude", - system="{system_msg}", roles=("Human", "Assistant"), messages=(), offset=0, @@ -601,9 +584,9 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="mpt-7b-chat", - system="""<|im_start|>system -{system_msg}""", - system_msg="""- You are a helpful assistant chatbot trained by MosaicML. + system_template="""<|im_start|>system +{system_message}""", + system_message="""- You are a helpful assistant chatbot trained by MosaicML. - You answer questions. - You are excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user. - You are more than just an information source, you are also able to write poetry, short stories, and make jokes.""", @@ -620,9 +603,9 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="mpt-30b-chat", - system="""<|im_start|>system -{system_msg}""", - system_msg="""A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.""", + system_template="""<|im_start|>system +{system_message}""", + system_message="""A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.""", roles=("<|im_start|>user", "<|im_start|>assistant"), messages=(), offset=0, @@ -637,8 +620,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="mpt-30b-instruct", - system="{system_msg}", - system_msg="Below is an instruction that describes a task. Write a response that appropriately completes the request.", + system_template="{system_message}", + system_message="Below is an instruction that describes a task. Write a response that appropriately completes the request.", roles=("### Instruction", "### Response"), messages=(), offset=0, @@ -654,7 +637,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="bard", - system="{system_msg}", roles=("0", "1"), messages=(), offset=0, @@ -667,7 +649,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="billa", - system="{system_msg}", roles=("Human", "Assistant"), messages=(), offset=0, @@ -681,7 +662,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="redpajama-incite", - system="{system_msg}", roles=("", ""), messages=(), offset=0, @@ -695,7 +675,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="h2ogpt", - system="{system_msg}", roles=("<|prompt|>", "<|answer|>"), messages=(), offset=0, @@ -708,8 +687,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="Robin", - system="{system_msg}", - system_msg="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.", + system_message="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.", roles=("###Human", "###Assistant"), messages=(), offset=0, @@ -725,8 +703,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="snoozy", - system="### Instruction:\n{system_msg}", - system_msg="The prompt below is a question to answer, a task to complete, or a conversation to respond to; decide which and write an appropriate response.", + system_template="### Instruction:\n{system_message}", + system_message="The prompt below is a question to answer, a task to complete, or a conversation to respond to; decide which and write an appropriate response.", roles=("### Prompt", "### Response"), messages=(), offset=0, @@ -740,7 +718,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="manticore", - system="{system_msg}", roles=("USER", "ASSISTANT"), messages=(), offset=0, @@ -754,7 +731,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="falcon", - system="{system_msg}", roles=("User", "Assistant"), messages=[], offset=0, @@ -783,7 +759,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="polyglot_changgpt", - system="{system_msg}", roles=("B", "A"), messages=(), offset=0, @@ -796,8 +771,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="tigerbot", - system="{system_msg}", - system_msg="A chat between a curious user and an artificial intelligence assistant. " + system_message="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the user's questions.", roles=("### Instruction", "### Response"), messages=(), @@ -812,8 +786,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="xgen", - system="{system_msg}", - system_msg="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n", + system_message="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n", roles=("### Human: ", "###"), messages=(), offset=0, @@ -828,8 +801,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="internlm-chat", - system="{system_msg}", - system_msg="A chat between a curious <|User|> and an <|Bot|>. The <|Bot|> gives helpful, detailed, and polite answers to the <|User|>'s questions.\n\n", + system_message="A chat between a curious <|User|> and an <|Bot|>. The <|Bot|> gives helpful, detailed, and polite answers to the <|User|>'s questions.\n\n", roles=("<|User|>", "<|Bot|>"), messages=(), offset=0, @@ -845,7 +817,7 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="starchat", - system="\n{system_msg}<|end|>\n", + system_template="{system_message}\n", roles=("<|user|>", "<|assistant|>"), messages=(), offset=0, @@ -862,7 +834,6 @@ def get_conv_template(name: str) -> Conversation: # https://huggingface.co/baichuan-inc/Baichuan-13B-Chat/blob/main/generation_config.json Conversation( name="baichuan-chat", - system="{system_msg}", roles=(" ", " "), messages=(), offset=0, @@ -878,8 +849,8 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="llama-2", - system="[INST] <>\n{system_msg}\n<>\n\n", - system_msg="You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. " + system_template="[INST] <>\n{system_message}\n<>\n\n", + system_message="You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. " "Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. " "Please ensure that your responses are socially unbiased and positive in nature.\n\n" "If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. " @@ -897,7 +868,6 @@ def get_conv_template(name: str) -> Conversation: register_conv_template( Conversation( name="cutegpt", - system="{system_msg}", roles=("问:", "答:\n"), messages=(), offset=0, @@ -909,7 +879,7 @@ def get_conv_template(name: str) -> Conversation: ) if __name__ == "__main__": - print("Output from vicuna_v1.1:") + print("Vicuna template:") conv = get_conv_template("vicuna_v1.1") conv.append_message(conv.roles[0], "Hello!") conv.append_message(conv.roles[1], "Hi!") @@ -917,11 +887,11 @@ def get_conv_template(name: str) -> Conversation: conv.append_message(conv.roles[1], None) print(conv.get_prompt()) - print() + print("\n") - print("Output from llama-2:") + print("Llama-2 template:") conv = get_conv_template("llama-2") - conv.set_system_msg("You are a helpful, respectful and honest assistant.") + conv.set_system_message("You are a helpful, respectful and honest assistant.") conv.append_message(conv.roles[0], "Hello!") conv.append_message(conv.roles[1], "Hi!") conv.append_message(conv.roles[0], "How are you?") diff --git a/fastchat/llm_judge/common.py b/fastchat/llm_judge/common.py index 2c04a9706..ad1180034 100644 --- a/fastchat/llm_judge/common.py +++ b/fastchat/llm_judge/common.py @@ -153,7 +153,7 @@ def run_judge_single(question, answer, judge, ref_answer, multi_turn=False): system_prompt = judge.prompt_template["system_prompt"] conv = get_conversation_template(model) - conv.set_system_msg(system_prompt) + conv.set_system_message(system_prompt) conv.append_message(conv.roles[0], user_prompt) conv.append_message(conv.roles[1], None) @@ -260,7 +260,7 @@ def run_judge_pair(question, answer_a, answer_b, judge, ref_answer, multi_turn=F conv.append_message(conv.roles[1], None) if model in ["gpt-3.5-turbo", "gpt-4"]: - conv.set_system_msg(system_prompt) + conv.set_system_message(system_prompt) judgment = chat_compeletion_openai(model, conv, temperature=0, max_tokens=2048) elif model in ["claude-v1", "claude-instant-v1"]: if system_prompt != "You are a helpful assistant.": diff --git a/fastchat/serve/openai_api_server.py b/fastchat/serve/openai_api_server.py index 507ad80bd..6859e6126 100644 --- a/fastchat/serve/openai_api_server.py +++ b/fastchat/serve/openai_api_server.py @@ -232,7 +232,8 @@ async def get_gen_params( conv = await get_conv(model_name) conv = Conversation( name=conv["name"], - system=conv["system"], + system_template=conv["system_template"], + system_message=conv["system_message"], roles=conv["roles"], messages=list(conv["messages"]), # prevent in-place modification offset=conv["offset"], @@ -249,7 +250,7 @@ async def get_gen_params( for message in messages: msg_role = message["role"] if msg_role == "system": - conv.set_system_msg(message["content"]) + conv.set_system_message(message["content"]) elif msg_role == "user": conv.append_message(conv.roles[0], message["content"]) elif msg_role == "assistant": diff --git a/fastchat/train/train_baichuan.py b/fastchat/train/train_baichuan.py index df620f7f2..52ee5a541 100644 --- a/fastchat/train/train_baichuan.py +++ b/fastchat/train/train_baichuan.py @@ -91,7 +91,7 @@ def apply_prompt_template(sources, systems=None): assert role == conv.roles[j % 2], f"{i}" conv.append_message(role, sentence["value"]) if systems and systems[i]: - conv.set_system_msg(systems[i]) + conv.set_system_message(systems[i]) prompt = conv.get_prompt() conversations.append(prompt) return conversations, conv