Spaces:
Running
Running
fix response format
Browse files
app.py
CHANGED
|
@@ -257,10 +257,16 @@ def generate_response(persona_key: str, message: str, history: list, max_tokens:
|
|
| 257 |
messages.append({"role": "system", "content": system_prompt})
|
| 258 |
|
| 259 |
# Add conversation history (last 5 exchanges to avoid too long context)
|
| 260 |
-
#
|
| 261 |
-
for
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
# Add current message
|
| 266 |
messages.append({"role": "user", "content": message})
|
|
@@ -315,8 +321,9 @@ def generate_response(persona_key: str, message: str, history: list, max_tokens:
|
|
| 315 |
response = " ".join(response.split())
|
| 316 |
response = response.strip()
|
| 317 |
|
| 318 |
-
# Update history with
|
| 319 |
-
history.append(
|
|
|
|
| 320 |
|
| 321 |
return history, ""
|
| 322 |
|
|
@@ -328,7 +335,7 @@ def generate_response(persona_key: str, message: str, history: list, max_tokens:
|
|
| 328 |
|
| 329 |
def clear_chat():
|
| 330 |
"""Clear the chat history."""
|
| 331 |
-
return [], "" # Empty list for
|
| 332 |
|
| 333 |
|
| 334 |
# Create Gradio interface
|
|
|
|
| 257 |
messages.append({"role": "system", "content": system_prompt})
|
| 258 |
|
| 259 |
# Add conversation history (last 5 exchanges to avoid too long context)
|
| 260 |
+
# Handle both tuple format [user_msg, assistant_msg] and messages format
|
| 261 |
+
for item in history[-5:]:
|
| 262 |
+
if isinstance(item, dict) and "role" in item:
|
| 263 |
+
# Messages format
|
| 264 |
+
messages.append(item)
|
| 265 |
+
else:
|
| 266 |
+
# Tuple format [user_msg, assistant_msg]
|
| 267 |
+
user_msg, assistant_msg = item
|
| 268 |
+
messages.append({"role": "user", "content": user_msg})
|
| 269 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 270 |
|
| 271 |
# Add current message
|
| 272 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 321 |
response = " ".join(response.split())
|
| 322 |
response = response.strip()
|
| 323 |
|
| 324 |
+
# Update history with messages format (Gradio expects this format)
|
| 325 |
+
history.append({"role": "user", "content": message})
|
| 326 |
+
history.append({"role": "assistant", "content": response})
|
| 327 |
|
| 328 |
return history, ""
|
| 329 |
|
|
|
|
| 335 |
|
| 336 |
def clear_chat():
|
| 337 |
"""Clear the chat history."""
|
| 338 |
+
return [], "" # Empty list for messages format
|
| 339 |
|
| 340 |
|
| 341 |
# Create Gradio interface
|