@@ -79,10 +79,17 @@ def process_repo(repo_path: str) -> str:
79
79
def chat (message : str , history : List [List [str ]], agent_type : str , use_cot : bool , language : str , collection : str ) -> List [List [str ]]:
80
80
"""Process chat message using selected agent and collection"""
81
81
try :
82
+ print ("\n " + "=" * 50 )
83
+ print (f"New message received: { message } " )
84
+ print (f"Agent: { agent_type } , CoT: { use_cot } , Language: { language } , Collection: { collection } " )
85
+ print ("=" * 50 + "\n " )
86
+
82
87
# Select appropriate agent
83
88
agent = local_agent if agent_type == "Local (Mistral)" else openai_agent
84
89
if not agent :
85
- return history + [[message , "Agent not available. Please check your configuration." ]]
90
+ response_text = "Agent not available. Please check your configuration."
91
+ print (f"Error: { response_text } " )
92
+ return history + [[message , response_text ]]
86
93
87
94
# Convert language selection to language code
88
95
lang_code = "es" if language == "Spanish" else "en"
@@ -91,21 +98,68 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
91
98
agent .use_cot = use_cot
92
99
agent .language = lang_code
93
100
94
- # Process query based on selected collection
95
- if collection == "PDF Collection" :
96
- context = vector_store .query_pdf_collection (message )
97
- response = agent ._generate_response (message , context ) if context else agent ._generate_general_response (message )
98
- elif collection == "Repository Collection" :
99
- context = vector_store .query_repo_collection (message )
100
- response = agent ._generate_response (message , context ) if context else agent ._generate_general_response (message )
101
- else : # General Knowledge
102
- response = agent ._generate_general_response (message )
101
+ # Process query and get response
102
+ print ("Processing query..." )
103
+ response = agent .process_query (message )
104
+ print ("Query processed successfully" )
105
+
106
+ # Format response with reasoning steps if CoT is enabled
107
+ if use_cot and "reasoning_steps" in response :
108
+ formatted_response = "🤔 Let me think about this step by step:\n \n "
109
+ print ("\n Chain of Thought Reasoning Steps:" )
110
+ print ("-" * 50 )
111
+
112
+ # Add each reasoning step
113
+ for i , step in enumerate (response ["reasoning_steps" ], 1 ):
114
+ step_text = f"Step { i } :\n { step } \n "
115
+ formatted_response += step_text
116
+ print (step_text )
117
+
118
+ # Add final answer
119
+ print ("\n Final Answer:" )
120
+ print ("-" * 50 )
121
+ final_answer = "🎯 Final Answer:\n " + response ["answer" ]
122
+ formatted_response += final_answer
123
+ print (final_answer )
124
+
125
+ # Add sources if available
126
+ if response .get ("context" ):
127
+ print ("\n Sources Used:" )
128
+ print ("-" * 50 )
129
+ sources_text = "\n 📚 Sources used:\n "
130
+ formatted_response += sources_text
131
+ print (sources_text )
132
+
133
+ for ctx in response ["context" ]:
134
+ source = ctx ["metadata" ].get ("source" , "Unknown" )
135
+ if "page_numbers" in ctx ["metadata" ]:
136
+ pages = ctx ["metadata" ].get ("page_numbers" , [])
137
+ source_line = f"- { source } (pages: { pages } )\n "
138
+ else :
139
+ file_path = ctx ["metadata" ].get ("file_path" , "Unknown" )
140
+ source_line = f"- { source } (file: { file_path } )\n "
141
+ formatted_response += source_line
142
+ print (source_line )
143
+ else :
144
+ formatted_response = response ["answer" ]
145
+ print ("\n Standard Response:" )
146
+ print ("-" * 50 )
147
+ print (formatted_response )
148
+
149
+ print ("\n " + "=" * 50 )
150
+ print ("Response complete" )
151
+ print ("=" * 50 + "\n " )
103
152
104
153
# Return updated history with new message pair
105
- history .append ([message , response [ "answer" ] ])
154
+ history .append ([message , formatted_response ])
106
155
return history
107
156
except Exception as e :
108
- history .append ([message , f"Error processing query: { str (e )} " ])
157
+ error_msg = f"Error processing query: { str (e )} "
158
+ print (f"\n Error occurred:" )
159
+ print ("-" * 50 )
160
+ print (error_msg )
161
+ print ("=" * 50 + "\n " )
162
+ history .append ([message , error_msg ])
109
163
return history
110
164
111
165
def create_interface ():
0 commit comments