@@ -178,7 +178,7 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
178
178
print ("Query processed successfully" )
179
179
180
180
# Format response with reasoning steps if CoT is enabled
181
- if use_cot and "reasoning_steps" in response :
181
+ if use_cot and isinstance ( response , dict ) and "reasoning_steps" in response :
182
182
formatted_response = "🤔 Let me think about this step by step:\n \n "
183
183
print ("\n Chain of Thought Reasoning Steps:" )
184
184
print ("-" * 50 )
@@ -195,7 +195,7 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
195
195
# Add final answer
196
196
print ("\n Final Answer:" )
197
197
print ("-" * 50 )
198
- final_answer = "\n 🎯 Final Answer:\n " + response [ "answer" ]
198
+ final_answer = "\n 🎯 Final Answer:\n " + response . get ( "answer" , "No answer provided" )
199
199
formatted_response += final_answer
200
200
print (final_answer )
201
201
@@ -208,43 +208,45 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
208
208
print (sources_text )
209
209
210
210
for ctx in response ["context" ]:
211
- source = ctx ["metadata" ].get ("source" , "Unknown" )
212
- if "page_numbers" in ctx ["metadata" ]:
213
- pages = ctx ["metadata" ].get ("page_numbers" , [])
214
- source_line = f"- { source } (pages: { pages } )\n "
215
- else :
216
- file_path = ctx ["metadata" ].get ("file_path" , "Unknown" )
217
- source_line = f"- { source } (file: { file_path } )\n "
218
- formatted_response += source_line
219
- print (source_line )
211
+ if isinstance (ctx , dict ) and "metadata" in ctx :
212
+ source = ctx ["metadata" ].get ("source" , "Unknown" )
213
+ if "page_numbers" in ctx ["metadata" ]:
214
+ pages = ctx ["metadata" ].get ("page_numbers" , [])
215
+ source_line = f"- { source } (pages: { pages } )\n "
216
+ else :
217
+ file_path = ctx ["metadata" ].get ("file_path" , "Unknown" )
218
+ source_line = f"- { source } (file: { file_path } )\n "
219
+ formatted_response += source_line
220
+ print (source_line )
220
221
221
222
# Add final formatted response to history
222
223
history .append ([message , formatted_response ])
223
224
else :
224
225
# For standard response (no CoT)
225
- formatted_response = response [ "answer" ]
226
+ formatted_response = response . get ( "answer" , "No answer provided" ) if isinstance ( response , dict ) else str ( response )
226
227
print ("\n Standard Response:" )
227
228
print ("-" * 50 )
228
229
print (formatted_response )
229
230
230
231
# Add sources if available
231
- if response .get ("context" ):
232
+ if isinstance ( response , dict ) and response .get ("context" ):
232
233
print ("\n Sources Used:" )
233
234
print ("-" * 50 )
234
235
sources_text = "\n \n 📚 Sources used:\n "
235
236
formatted_response += sources_text
236
237
print (sources_text )
237
238
238
239
for ctx in response ["context" ]:
239
- source = ctx ["metadata" ].get ("source" , "Unknown" )
240
- if "page_numbers" in ctx ["metadata" ]:
241
- pages = ctx ["metadata" ].get ("page_numbers" , [])
242
- source_line = f"- { source } (pages: { pages } )\n "
243
- else :
244
- file_path = ctx ["metadata" ].get ("file_path" , "Unknown" )
245
- source_line = f"- { source } (file: { file_path } )\n "
246
- formatted_response += source_line
247
- print (source_line )
240
+ if isinstance (ctx , dict ) and "metadata" in ctx :
241
+ source = ctx ["metadata" ].get ("source" , "Unknown" )
242
+ if "page_numbers" in ctx ["metadata" ]:
243
+ pages = ctx ["metadata" ].get ("page_numbers" , [])
244
+ source_line = f"- { source } (pages: { pages } )\n "
245
+ else :
246
+ file_path = ctx ["metadata" ].get ("file_path" , "Unknown" )
247
+ source_line = f"- { source } (file: { file_path } )\n "
248
+ formatted_response += source_line
249
+ print (source_line )
248
250
249
251
history .append ([message , formatted_response ])
250
252
0 commit comments