Spaces:
Running
Running
Refactor history formatting in respond function for improved clarity and error handling
Browse files- app.py +32 -70
- config/constants.py +3 -5
app.py
CHANGED
|
@@ -150,79 +150,41 @@ def respond(
|
|
| 150 |
token = chunk.choices[0].delta.content
|
| 151 |
if token:
|
| 152 |
response += token
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
if not isinstance(user_item, dict) or "role" not in user_item or "content" not in user_item:
|
| 165 |
-
user_item = {"role": "user", "content": str(user_item) if not isinstance(user_item, dict) else user_item.get("content", "")}
|
| 166 |
-
|
| 167 |
-
# Ensure assistant message has correct format
|
| 168 |
-
if not isinstance(assistant_item, dict) or "role" not in assistant_item or "content" not in assistant_item:
|
| 169 |
-
assistant_item = {"role": "assistant", "content": str(assistant_item) if not isinstance(assistant_item, dict) else assistant_item.get("content", "")}
|
| 170 |
-
|
| 171 |
-
formatted_history.append([user_item, assistant_item])
|
| 172 |
-
|
| 173 |
-
# Add the new message pair
|
| 174 |
-
formatted_history.append([
|
| 175 |
-
{"role": "user", "content": message},
|
| 176 |
-
{"role": "assistant", "content": response}
|
| 177 |
-
])
|
| 178 |
-
|
| 179 |
-
yield formatted_history, conversation_id
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
print(f"Failed to save chat history: {msg}")
|
| 193 |
-
except Exception as e:
|
| 194 |
-
import traceback
|
| 195 |
-
print(f"Exception while saving chat history: {str(e)}")
|
| 196 |
-
print(traceback.format_exc())
|
| 197 |
|
| 198 |
except Exception as e:
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
# Ensure user message has correct format
|
| 210 |
-
if not isinstance(user_item, dict) or "role" not in user_item or "content" not in user_item:
|
| 211 |
-
user_item = {"role": "user", "content": str(user_item) if not isinstance(user_item, dict) else user_item.get("content", "")}
|
| 212 |
-
|
| 213 |
-
# Ensure assistant message has correct format
|
| 214 |
-
if not isinstance(assistant_item, dict) or "role" not in assistant_item or "content" not in assistant_item:
|
| 215 |
-
assistant_item = {"role": "assistant", "content": str(assistant_item) if not isinstance(assistant_item, dict) else assistant_item.get("content", "")}
|
| 216 |
-
|
| 217 |
-
formatted_history.append([user_item, assistant_item])
|
| 218 |
-
|
| 219 |
-
# Add error message
|
| 220 |
-
formatted_history.append([
|
| 221 |
-
{"role": "user", "content": message},
|
| 222 |
-
{"role": "assistant", "content": f"An error occurred while generating the response: {str(e)}"}
|
| 223 |
-
])
|
| 224 |
-
|
| 225 |
-
yield formatted_history, conversation_id
|
| 226 |
|
| 227 |
|
| 228 |
def update_kb():
|
|
|
|
| 150 |
token = chunk.choices[0].delta.content
|
| 151 |
if token:
|
| 152 |
response += token
|
| 153 |
+
# Create proper history format for Gradio
|
| 154 |
+
current_history = history.copy() if history else []
|
| 155 |
+
current_history.append({
|
| 156 |
+
"role": "user",
|
| 157 |
+
"content": message
|
| 158 |
+
})
|
| 159 |
+
current_history.append({
|
| 160 |
+
"role": "assistant",
|
| 161 |
+
"content": response
|
| 162 |
+
})
|
| 163 |
+
yield current_history, conversation_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
if is_complete or response:
|
| 166 |
+
final_history = history.copy() if history else []
|
| 167 |
+
final_history.append({
|
| 168 |
+
"role": "user",
|
| 169 |
+
"content": message
|
| 170 |
+
})
|
| 171 |
+
final_history.append({
|
| 172 |
+
"role": "assistant",
|
| 173 |
+
"content": response
|
| 174 |
+
})
|
| 175 |
+
yield final_history, conversation_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
except Exception as e:
|
| 178 |
+
error_history = history.copy() if history else []
|
| 179 |
+
error_history.append({
|
| 180 |
+
"role": "user",
|
| 181 |
+
"content": message
|
| 182 |
+
})
|
| 183 |
+
error_history.append({
|
| 184 |
+
"role": "assistant",
|
| 185 |
+
"content": f"An error occurred: {str(e)}"
|
| 186 |
+
})
|
| 187 |
+
yield error_history, conversation_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
|
| 190 |
def update_kb():
|
config/constants.py
CHANGED
|
@@ -11,11 +11,9 @@ URLS = [
|
|
| 11 |
"https://status.law/international-prosecution-protection",
|
| 12 |
"https://status.law/interpol-red-notice-removal",
|
| 13 |
"https://status.law/practice-areas",
|
| 14 |
-
"https://status.law/reputation-protection"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# "https://status.law/faq"
|
| 18 |
-
#]
|
| 19 |
|
| 20 |
# Text chunking settings
|
| 21 |
CHUNK_SIZE = 500
|
|
|
|
| 11 |
"https://status.law/international-prosecution-protection",
|
| 12 |
"https://status.law/interpol-red-notice-removal",
|
| 13 |
"https://status.law/practice-areas",
|
| 14 |
+
"https://status.law/reputation-protection",
|
| 15 |
+
"https://status.law/faq"
|
| 16 |
+
]
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Text chunking settings
|
| 19 |
CHUNK_SIZE = 500
|