Rulga commited on
Commit
721bbf2
·
1 Parent(s): 8100019

Refactor history formatting in respond function for improved clarity and error handling

Browse files
Files changed (2) hide show
  1. app.py +32 -70
  2. 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
- # Create a properly formatted history for Gradio
155
- formatted_history = []
156
-
157
- # Include existing history (ensure proper format)
158
- if history:
159
- for item in history:
160
- if isinstance(item, list) and len(item) == 2:
161
- user_item, assistant_item = item
162
-
163
- # Ensure user message has correct format
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
- # Save history if response is complete
182
- if is_complete or response: # add response check as fallback
183
- messages.append({"role": "assistant", "content": response})
184
- try:
185
- from src.knowledge_base.dataset import DatasetManager
186
- from config.settings import HF_TOKEN
187
-
188
- dataset = DatasetManager(token=HF_TOKEN)
189
- success, msg = dataset.save_chat_history(conversation_id, messages)
190
- print(f"Chat history save attempt: {success}, Message: {msg}")
191
- if not success:
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
- print(f"Error generating response: {str(e)}")
200
- # Format error response in the way Gradio chatbot expects
201
- formatted_history = []
202
-
203
- # Copy existing history (ensure proper format)
204
- if history:
205
- for item in history:
206
- if isinstance(item, list) and len(item) == 2:
207
- user_item, assistant_item = item
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