Sachi Wagaarachchi
commited on
Commit
·
191c0de
1
Parent(s):
0655268
debug: attention 2
Browse files- src/chat_logic.py +6 -5
- src/utils.py +28 -9
src/chat_logic.py
CHANGED
@@ -10,16 +10,17 @@ class ChatProcessor:
|
|
10 |
self.vector_db = vector_db
|
11 |
self.logger = logging.getLogger(__name__)
|
12 |
|
13 |
-
def process_chat(self, message, history, model_name, temperature=0.7,
|
14 |
-
max_new_tokens=512, top_p=0.9, top_k=50, repetition_penalty=1.2
|
|
|
15 |
"""Process chat input and generate streaming response"""
|
16 |
try:
|
17 |
-
# Format prompt with history
|
18 |
-
prompt = format_prompt(message, history)
|
19 |
-
|
20 |
# Get model pipeline
|
21 |
pipe = self.model_manager.get_pipeline(model_name)
|
22 |
|
|
|
|
|
|
|
23 |
# Set up streamer
|
24 |
streamer = TextIteratorStreamer(
|
25 |
pipe.tokenizer,
|
|
|
10 |
self.vector_db = vector_db
|
11 |
self.logger = logging.getLogger(__name__)
|
12 |
|
13 |
+
def process_chat(self, message, history, model_name, temperature=0.7,
|
14 |
+
max_new_tokens=512, top_p=0.9, top_k=50, repetition_penalty=1.2,
|
15 |
+
system_prompt=""):
|
16 |
"""Process chat input and generate streaming response"""
|
17 |
try:
|
|
|
|
|
|
|
18 |
# Get model pipeline
|
19 |
pipe = self.model_manager.get_pipeline(model_name)
|
20 |
|
21 |
+
# Format prompt with history and tokenizer
|
22 |
+
prompt = format_prompt(message, history, pipe.tokenizer, system_prompt)
|
23 |
+
|
24 |
# Set up streamer
|
25 |
streamer = TextIteratorStreamer(
|
26 |
pipe.tokenizer,
|
src/utils.py
CHANGED
@@ -1,13 +1,32 @@
|
|
1 |
-
def format_prompt(message, history):
|
2 |
-
"""Format message and history into a prompt for Qwen models
|
3 |
-
if not history:
|
4 |
-
return message
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
for user_msg, assistant_msg in history:
|
9 |
-
|
|
|
10 |
|
11 |
# Add current message
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def format_prompt(message, history, tokenizer, system_prompt=""):
|
2 |
+
"""Format message and history into a prompt for Qwen models
|
|
|
|
|
3 |
|
4 |
+
Uses tokenizer.apply_chat_template if available, otherwise falls back to manual formatting.
|
5 |
+
"""
|
6 |
+
# Convert history from tuples to dict format expected by apply_chat_template
|
7 |
+
formatted_history = []
|
8 |
for user_msg, assistant_msg in history:
|
9 |
+
formatted_history.append({"role": "user", "content": user_msg})
|
10 |
+
formatted_history.append({"role": "assistant", "content": assistant_msg})
|
11 |
|
12 |
# Add current message
|
13 |
+
formatted_history.append({"role": "user", "content": message})
|
14 |
+
|
15 |
+
if hasattr(tokenizer, "chat_template") and tokenizer.chat_template:
|
16 |
+
messages = [{"role": "system", "content": system_prompt.strip()}] + formatted_history
|
17 |
+
return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=True)
|
18 |
+
else:
|
19 |
+
# Fallback for base LMs without chat template
|
20 |
+
prompt = ""
|
21 |
+
if system_prompt.strip():
|
22 |
+
prompt = system_prompt.strip() + "\n"
|
23 |
+
|
24 |
+
for msg in formatted_history:
|
25 |
+
if msg['role'] == 'user':
|
26 |
+
prompt += f"<|User|>: {msg['content'].strip()}\n"
|
27 |
+
elif msg['role'] == 'assistant':
|
28 |
+
prompt += f"<|Assistant|>: {msg['content'].strip()}\n"
|
29 |
+
|
30 |
+
if not prompt.strip().endswith("<|Assistant|>:"):
|
31 |
+
prompt += "<|Assistant|>:"
|
32 |
+
return prompt
|