Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit d9a8641

Browse files
Support neural-chat-7b-v3 and neural-chat-7b-v3-1 (#698)
* Support neural-chat-7b-v3 and neural-chat-7b-v3-1 Signed-off-by: lvliang-intel <[email protected]>
1 parent e1cca32 commit d9a8641

File tree

7 files changed

+127
-12
lines changed

7 files changed

+127
-12
lines changed

intel_extension_for_transformers/neural_chat/models/model_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ def load_model(
362362
or re.search("llama", model_name, re.IGNORECASE)
363363
or re.search("neural-chat-7b-v1", model_name, re.IGNORECASE)
364364
or re.search("neural-chat-7b-v2", model_name, re.IGNORECASE)
365+
or re.search("neural-chat-7b-v3", model_name, re.IGNORECASE)
365366
or re.search("qwen", model_name, re.IGNORECASE)
366367
or re.search("starcoder", model_name, re.IGNORECASE)
367368
or re.search("Mistral", model_name, re.IGNORECASE)
@@ -990,4 +991,12 @@ def predict(**params):
990991
output = tokenizer.decode(generation_output.sequences[0], skip_special_tokens=True)
991992
if "### Response:" in output:
992993
return output.split("### Response:")[1].strip()
994+
if "### Assistant" in output:
995+
return output.split("### Assistant:")[1].strip()
996+
if "\nassistant\n" in output:
997+
return output.split("\nassistant\n")[1].strip()
998+
if "[/INST]" in output:
999+
return output.split("[/INST]")[1].strip()
1000+
if "答:" in output:
1001+
return output.split("答:")[1].strip()
9931002
return output

intel_extension_for_transformers/neural_chat/models/neuralchat_model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
5151
"""
5252
if "neural-chat-7b-v2" in model_path.lower():
5353
return get_conv_template("neural-chat-7b-v2")
54+
elif "neural-chat-7b-v3" in model_path.lower():
55+
return get_conv_template("neural-chat-7b-v3")
5456
else:
5557
return get_conv_template("neural-chat-7b-v1-1")
5658

intel_extension_for_transformers/neural_chat/prompts/prompt.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,40 @@
1717

1818
from fastchat.conversation import get_conv_template, register_conv_template, Conversation, SeparatorStyle
1919

20+
# neuralchat-v3-1 prompt template
21+
register_conv_template(
22+
Conversation(
23+
name="neural-chat-7b-v3-1",
24+
system_message="""### System:
25+
- You are a helpful assistant chatbot trained by Intel.
26+
- You answer questions.
27+
- You are excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user.
28+
- You are more than just an information source, you are also able to write poetry, \
29+
short stories, and make jokes.</s>\n""",
30+
roles=("### User:", "### Assistant:"),
31+
sep_style=SeparatorStyle.NO_COLON_TWO,
32+
sep="\n",
33+
sep2="</s>",
34+
)
35+
)
36+
37+
# neuralchat-v3 prompt template
38+
register_conv_template(
39+
Conversation(
40+
name="neural-chat-7b-v3",
41+
system_message="""### System:
42+
- You are a helpful assistant chatbot trained by Intel.
43+
- You answer questions.
44+
- You are excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user.
45+
- You are more than just an information source, you are also able to write poetry, \
46+
short stories, and make jokes.</s>\n""",
47+
roles=("### User:", "### Assistant:"),
48+
sep_style=SeparatorStyle.NO_COLON_TWO,
49+
sep="\n",
50+
sep2="</s>",
51+
)
52+
)
53+
2054
# neuralchat-v2 prompt template
2155
register_conv_template(
2256
Conversation(

intel_extension_for_transformers/neural_chat/tests/ci/plugins/cache/test_cache.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from intel_extension_for_transformers.neural_chat.pipeline.plugins.caching.cache import ChatCache
1919
from intel_extension_for_transformers.neural_chat import build_chatbot, PipelineConfig
2020
import unittest
21-
import os
21+
import os, shutil
2222

2323
class TestChatCache(unittest.TestCase):
2424
def setUp(self):
@@ -36,18 +36,17 @@ def tearDown(self) -> None:
3636
return super().tearDown()
3737

3838
def test_chat_cache(self):
39-
cache_plugin = ChatCache(embedding_model_dir="/tf_dataset2/models/nlp_toolkit/instructor-large")
39+
cache_plugin = ChatCache(embedding_model_dir="hkunlp/instructor-large")
4040
cache_plugin.init_similar_cache_from_config()
4141

42-
prompt = "Tell me about Intel Xeon Scable Processors."
42+
prompt = "Tell me about Intel Xeon Scalable Processors."
4343
config = PipelineConfig(model_name_or_path="facebook/opt-125m")
4444
chatbot = build_chatbot(config)
4545
response = chatbot.predict(prompt)
4646
cache_plugin.put(prompt, response)
4747

4848
answer = cache_plugin.get(prompt)
49-
self.assertIn('Tell me about Intel Xeon Scable Processors.', str(answer))
49+
self.assertIn('Intel Xeon Scalable', str(answer['choices'][0]['text']))
5050

51-
5251
if __name__ == "__main__":
5352
unittest.main()

intel_extension_for_transformers/neural_chat/tests/nightly/models/test_model.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from intel_extension_for_transformers.neural_chat.models.llama_model import LlamaModel
2020
from intel_extension_for_transformers.neural_chat.models.mpt_model import MptModel
2121
from intel_extension_for_transformers.neural_chat.models.neuralchat_model import NeuralChatModel
22+
from intel_extension_for_transformers.neural_chat import build_chatbot, PipelineConfig
2223
import unittest
2324

2425
class TestChatGlmModel(unittest.TestCase):
@@ -29,12 +30,17 @@ def tearDown(self) -> None:
2930
return super().tearDown()
3031

3132
def test_match(self):
32-
result = ChatGlmModel().match(model_path='/tf_dataset2/models/nlp_toolkit/chatglm2-6b')
33+
result = ChatGlmModel().match(model_path='THUDM/chatglm2-6b')
3334
self.assertTrue(result)
3435

3536
def test_get_default_conv_template(self):
36-
result = ChatGlmModel().get_default_conv_template(model_path='/tf_dataset2/models/nlp_toolkit/chatglm-6b')
37+
result = ChatGlmModel().get_default_conv_template(model_path='THUDM/chatglm2-6b')
3738
self.assertIn('问', str(result))
39+
config = PipelineConfig(model_name_or_path="THUDM/chatglm2-6b")
40+
chatbot = build_chatbot(config=config)
41+
result = chatbot.predict("中国最大的城市是哪个?")
42+
print(result)
43+
self.assertIn('上海', str(result))
3844

3945
class TestLlamaModel(unittest.TestCase):
4046
def setUp(self):
@@ -44,12 +50,16 @@ def tearDown(self) -> None:
4450
return super().tearDown()
4551

4652
def test_match(self):
47-
result = LlamaModel().match(model_path='/tf_dataset2/models/nlp_toolkit/llama-2-7b-chat')
53+
result = LlamaModel().match(model_path='meta-llama/Llama-2-7b-chat-hf')
4854
self.assertTrue(result)
4955

5056
def test_get_default_conv_template(self):
51-
result = LlamaModel().get_default_conv_template(model_path='/tf_dataset2/models/nlp_toolkit/llama-2-7b-chat')
57+
result = LlamaModel().get_default_conv_template(model_path='meta-llama/Llama-2-7b-chat-hf')
5258
self.assertIn("[INST] <<SYS>>", str(result))
59+
chatbot = build_chatbot()
60+
result = chatbot.predict("Tell me about Intel Xeon Scalable Processors.")
61+
print(result)
62+
self.assertIn('Intel Xeon Scalable Processors', str(result))
5363

5464
class TestMptModel(unittest.TestCase):
5565
def setUp(self):
@@ -59,12 +69,17 @@ def tearDown(self) -> None:
5969
return super().tearDown()
6070

6171
def test_match(self):
62-
result = MptModel().match(model_path='/tf_dataset2/models/nlp_toolkit/mpt-7b')
72+
result = MptModel().match(model_path='mosaicml/mpt-7b-chat')
6373
self.assertTrue(result)
6474

6575
def test_get_default_conv_template(self):
66-
result = MptModel().get_default_conv_template(model_path='/tf_dataset2/models/nlp_toolkit/mpt-7b')
76+
result = MptModel().get_default_conv_template(model_path='mosaicml/mpt-7b-chat')
6777
self.assertIn("<|im_start|>system", str(result))
78+
config = PipelineConfig(model_name_or_path="mosaicml/mpt-7b-chat")
79+
chatbot = build_chatbot(config=config)
80+
result = chatbot.predict("Tell me about Intel Xeon Scalable Processors.")
81+
print(result)
82+
self.assertIn('Intel Xeon Scalable processors', str(result))
6883

6984
class TestNeuralChatModel(unittest.TestCase):
7085
def setUp(self):
@@ -81,10 +96,33 @@ def test_get_default_conv_template_v1(self):
8196
result = NeuralChatModel().get_default_conv_template(
8297
model_path='Intel/neural-chat-7b-v1-1')
8398
self.assertIn("<|im_start|>system", str(result))
99+
config = PipelineConfig(model_name_or_path="Intel/neural-chat-7b-v1-1")
100+
chatbot = build_chatbot(config=config)
101+
result = chatbot.predict("Tell me about Intel Xeon Scalable Processors.")
102+
print(result)
103+
self.assertIn('Intel® Xeon® Scalable processors', str(result))
84104

85105
def test_get_default_conv_template_v2(self):
86106
result = NeuralChatModel().get_default_conv_template(model_path='Intel/neural-chat-7b-v2')
87107
self.assertIn("### System:", str(result))
88108

109+
def test_get_default_conv_template_v3(self):
110+
result = NeuralChatModel().get_default_conv_template(model_path='Intel/neural-chat-7b-v3')
111+
self.assertIn("### System:", str(result))
112+
config = PipelineConfig(model_name_or_path="Intel/neural-chat-7b-v3")
113+
chatbot = build_chatbot(config=config)
114+
result = chatbot.predict("Tell me about Intel Xeon Scalable Processors.")
115+
print(result)
116+
self.assertIn('The Intel Xeon Scalable Processors', str(result))
117+
118+
def test_get_default_conv_template_v3_1(self):
119+
result = NeuralChatModel().get_default_conv_template(model_path='Intel/neural-chat-7b-v3-1')
120+
self.assertIn("### System:", str(result))
121+
config = PipelineConfig(model_name_or_path="Intel/neural-chat-7b-v3-1")
122+
chatbot = build_chatbot(config=config)
123+
result = chatbot.predict("Tell me about Intel Xeon Scalable Processors.")
124+
print(result)
125+
self.assertIn('The Intel Xeon Scalable Processors', str(result))
126+
89127
if __name__ == "__main__":
90128
unittest.main()

intel_extension_for_transformers/neural_chat/ui/textbot/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ def http_bot(state, model_selector, temperature, max_new_tokens, topk, request:
322322

323323
if len(state.messages) == state.offset + 2:
324324
# model conversation name: "mpt-7b-chat", "chatglm", "chatglm2", "llama-2",
325-
# "neural-chat-7b-v2", "neural-chat-7b-v1-1"
325+
# "neural-chat-7b-v3-1", "neural-chat-7b-v3",
326+
# "neural-chat-7b-v2", "neural-chat-7b-v1-1"
326327
# First round of Conversation
327328
if "Llama-2-7b-chat-hf" in model_name:
328329
model_name = "llama-2"

intel_extension_for_transformers/neural_chat/ui/textbot/conversation.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,38 @@ def get_conv_template(name: str) -> Conversation:
946946
)
947947
)
948948

949+
# neuralchat-v2 template
950+
register_conv_template(
951+
Conversation(
952+
name="neural-chat-7b-v3",
953+
system_message="""### System:
954+
- You are a helpful assistant chatbot trained by Intel.
955+
- You answer questions.
956+
- You are excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user.
957+
- You are more than just an information source, you are also able to write poetry, short stories, and make jokes.</s>\n""",
958+
roles=("### User:", "### Assistant:"),
959+
sep_style=SeparatorStyle.NO_COLON_TWO,
960+
sep="\n",
961+
sep2="</s>",
962+
)
963+
)
964+
965+
# neuralchat-v2 template
966+
register_conv_template(
967+
Conversation(
968+
name="neural-chat-7b-v3-1",
969+
system_message="""### System:
970+
- You are a helpful assistant chatbot trained by Intel.
971+
- You answer questions.
972+
- You are excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user.
973+
- You are more than just an information source, you are also able to write poetry, short stories, and make jokes.</s>\n""",
974+
roles=("### User:", "### Assistant:"),
975+
sep_style=SeparatorStyle.NO_COLON_TWO,
976+
sep="\n",
977+
sep2="</s>",
978+
)
979+
)
980+
949981
# neuralchat-v1.1 prompt template
950982
register_conv_template(
951983
Conversation(

0 commit comments

Comments
 (0)