@@ -178,7 +178,7 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
178178 print ("Query processed successfully" )
179179
180180 # 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 :
182182 formatted_response = "🤔 Let me think about this step by step:\n \n "
183183 print ("\n Chain of Thought Reasoning Steps:" )
184184 print ("-" * 50 )
@@ -195,7 +195,7 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
195195 # Add final answer
196196 print ("\n Final Answer:" )
197197 print ("-" * 50 )
198- final_answer = "\n 🎯 Final Answer:\n " + response [ "answer" ]
198+ final_answer = "\n 🎯 Final Answer:\n " + response . get ( "answer" , "No answer provided" )
199199 formatted_response += final_answer
200200 print (final_answer )
201201
@@ -208,43 +208,45 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
208208 print (sources_text )
209209
210210 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 )
220221
221222 # Add final formatted response to history
222223 history .append ([message , formatted_response ])
223224 else :
224225 # 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 )
226227 print ("\n Standard Response:" )
227228 print ("-" * 50 )
228229 print (formatted_response )
229230
230231 # Add sources if available
231- if response .get ("context" ):
232+ if isinstance ( response , dict ) and response .get ("context" ):
232233 print ("\n Sources Used:" )
233234 print ("-" * 50 )
234235 sources_text = "\n \n 📚 Sources used:\n "
235236 formatted_response += sources_text
236237 print (sources_text )
237238
238239 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 )
248250
249251 history .append ([message , formatted_response ])
250252
0 commit comments