xu song
commited on
Commit
•
d47c36f
1
Parent(s):
7d0829c
update
Browse files- models/cpp_qwen2.py +13 -29
models/cpp_qwen2.py
CHANGED
@@ -81,18 +81,12 @@ class Qwen2Simulator(Simulator):
|
|
81 |
def tokenize(self, text):
|
82 |
return self.llm.tokenize(text.encode("utf-8"))
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
""
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
# {% endif %}
|
91 |
-
# {{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}
|
92 |
-
# {% endfor %}
|
93 |
-
# {% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}",
|
94 |
-
|
95 |
-
# TODO: 判断是否有结束符,
|
96 |
|
97 |
input_ids = []
|
98 |
for message in history:
|
@@ -101,19 +95,7 @@ class Qwen2Simulator(Simulator):
|
|
101 |
input_ids += self.tokenize(f"<|im_start|>{message['role']}\n") \
|
102 |
+ message["tokens"] \
|
103 |
+ self.tokenize("<|im_end|>\n")
|
104 |
-
input_ids +=
|
105 |
-
if stream:
|
106 |
-
return self._stream_generate(input_ids)
|
107 |
-
else:
|
108 |
-
return self._generate(input_ids)
|
109 |
-
|
110 |
-
def generate_response(self, history, stream=True):
|
111 |
-
input_ids = []
|
112 |
-
for message in history:
|
113 |
-
input_ids += self.tokenize(f"<|im_start|>{message['role']}\n") \
|
114 |
-
+ message["tokens"] \
|
115 |
-
+ self.tokenize("<|im_end|>\n")
|
116 |
-
input_ids += self.tokenize("<|im_start|>assistant\n")
|
117 |
if stream:
|
118 |
return self._stream_generate(input_ids)
|
119 |
else:
|
@@ -151,7 +133,9 @@ if __name__ == "__main__":
|
|
151 |
for generated_text, generated_tokens in bot.generate_query(messages, stream=True):
|
152 |
print(generated_text, generated_tokens)
|
153 |
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
81 |
def tokenize(self, text):
|
82 |
return self.llm.tokenize(text.encode("utf-8"))
|
83 |
|
84 |
+
|
85 |
+
def generate(self, history, stream=True):
|
86 |
+
if history[-1]['role'] in ["user"]:
|
87 |
+
start_tokens = self.tokenize("<|im_start|>assistant\n")
|
88 |
+
elif history[-1]['role'] in ["assistant", "system"]:
|
89 |
+
start_tokens = self.tokenize("<|im_start|>user\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
input_ids = []
|
92 |
for message in history:
|
|
|
95 |
input_ids += self.tokenize(f"<|im_start|>{message['role']}\n") \
|
96 |
+ message["tokens"] \
|
97 |
+ self.tokenize("<|im_end|>\n")
|
98 |
+
input_ids += start_tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
if stream:
|
100 |
return self._stream_generate(input_ids)
|
101 |
else:
|
|
|
133 |
for generated_text, generated_tokens in bot.generate_query(messages, stream=True):
|
134 |
print(generated_text, generated_tokens)
|
135 |
|
136 |
+
for i in range(3):
|
137 |
+
messages.append({"role": "user" if i % 2 == 0 else "assistant", "content": generated_text, "tokens": generated_tokens})
|
138 |
+
print("requesting", messages)
|
139 |
+
for generated_text, all_tokens in bot.generate(messages, stream=True):
|
140 |
+
print(generated_text, all_tokens)
|
141 |
+
|