Update app.py
Browse files
app.py
CHANGED
|
@@ -128,7 +128,7 @@ load_model()
|
|
| 128 |
|
| 129 |
# ================= GENERATION =================
|
| 130 |
|
| 131 |
-
SYSTEM_INSTRUCTION = "
|
| 132 |
|
| 133 |
def stream_chat(message, history):
|
| 134 |
"""Streaming chat function - yields updated history as tokens are generated."""
|
|
@@ -141,11 +141,18 @@ def stream_chat(message, history):
|
|
| 141 |
# Build prompt with ChatML format
|
| 142 |
full_prompt = f"<|im_start|>system\n{SYSTEM_INSTRUCTION}<|im_end|>\n"
|
| 143 |
|
| 144 |
-
# Add conversation history
|
| 145 |
for turn in history:
|
|
|
|
|
|
|
| 146 |
if isinstance(turn, dict):
|
| 147 |
role = turn.get("role", "user")
|
| 148 |
content = turn.get("content", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
full_prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
|
| 150 |
|
| 151 |
# Add current user input
|
|
@@ -155,8 +162,16 @@ def stream_chat(message, history):
|
|
| 155 |
idx = tokenizer.encode(full_prompt, return_tensors="pt").to(DEVICE)
|
| 156 |
generated_text = ""
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
# Add user message to history first
|
| 159 |
-
new_history =
|
| 160 |
{"role": "user", "content": message},
|
| 161 |
{"role": "assistant", "content": ""}
|
| 162 |
]
|
|
|
|
| 128 |
|
| 129 |
# ================= GENERATION =================
|
| 130 |
|
| 131 |
+
SYSTEM_INSTRUCTION = "You are OLM North Star, a helpful AI assistant. Follow the user's instructions exactly. Be concise and direct."
|
| 132 |
|
| 133 |
def stream_chat(message, history):
|
| 134 |
"""Streaming chat function - yields updated history as tokens are generated."""
|
|
|
|
| 141 |
# Build prompt with ChatML format
|
| 142 |
full_prompt = f"<|im_start|>system\n{SYSTEM_INSTRUCTION}<|im_end|>\n"
|
| 143 |
|
| 144 |
+
# Add conversation history - handle both dict and ChatMessage objects
|
| 145 |
for turn in history:
|
| 146 |
+
role = ""
|
| 147 |
+
content = ""
|
| 148 |
if isinstance(turn, dict):
|
| 149 |
role = turn.get("role", "user")
|
| 150 |
content = turn.get("content", "")
|
| 151 |
+
else:
|
| 152 |
+
# Gradio ChatMessage objects
|
| 153 |
+
role = getattr(turn, "role", "user")
|
| 154 |
+
content = getattr(turn, "content", "")
|
| 155 |
+
if role and content:
|
| 156 |
full_prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
|
| 157 |
|
| 158 |
# Add current user input
|
|
|
|
| 162 |
idx = tokenizer.encode(full_prompt, return_tensors="pt").to(DEVICE)
|
| 163 |
generated_text = ""
|
| 164 |
|
| 165 |
+
# Build clean history as plain dicts for output
|
| 166 |
+
clean_history = []
|
| 167 |
+
for turn in history:
|
| 168 |
+
if isinstance(turn, dict):
|
| 169 |
+
clean_history.append({"role": turn.get("role", "user"), "content": turn.get("content", "")})
|
| 170 |
+
else:
|
| 171 |
+
clean_history.append({"role": getattr(turn, "role", "user"), "content": getattr(turn, "content", "")})
|
| 172 |
+
|
| 173 |
# Add user message to history first
|
| 174 |
+
new_history = clean_history + [
|
| 175 |
{"role": "user", "content": message},
|
| 176 |
{"role": "assistant", "content": ""}
|
| 177 |
]
|