forked from AkshatJoshi2000/DILLMA-Damn-Insecure-LLM-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_app.py
More file actions
440 lines (385 loc) · 15.3 KB
/
debug_app.py
File metadata and controls
440 lines (385 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
from flask import Flask, render_template, request, jsonify, session
import os
import uuid
import sys
import traceback
import logging
# Set up logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()])
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.secret_key = os.urandom(24) # For session management
# Global variable to store chatbot instance
chatbot = None
@app.route('/')
def index():
"""Render the main chat interface"""
# Generate a unique session ID if not present
if 'session_id' not in session:
session['session_id'] = str(uuid.uuid4())
return render_template('index.html')
@app.route('/api/chat', methods=['POST'])
def chat():
"""Process chat messages and return response"""
data = request.json
user_input = data.get('message', '')
if not user_input:
return jsonify({'error': 'Empty message'}), 400
# Process user input through the chatbot
try:
# Check if chatbot is initialized
if chatbot is None:
return jsonify({'error': 'Chatbot not initialized. Check server logs.'}), 500
logger.info(f"Processing message: {user_input}")
response = chatbot.chat(user_input)
logger.info("Successfully generated response")
return jsonify({'response': response})
except Exception as e:
error_type = type(e).__name__
error_message = str(e)
error_traceback = traceback.format_exc()
logger.error(f"Error type: {error_type}")
logger.error(f"Error message: {error_message}")
logger.error(f"Traceback: {error_traceback}")
return jsonify({
'error': 'Failed to process message',
'error_type': error_type,
'error_details': error_message
}), 500
@app.route('/api/reset', methods=['POST'])
def reset_conversation():
"""Reset the conversation history"""
try:
# Check if chatbot is initialized
if chatbot is None:
return jsonify({'error': 'Chatbot not initialized. Check server logs.'}), 500
# Reset chatbot memory
chatbot.memory.clear()
return jsonify({'status': 'success', 'message': 'Conversation reset'})
except Exception as e:
error_type = type(e).__name__
error_message = str(e)
error_traceback = traceback.format_exc()
logger.error(f"Error type: {error_type}")
logger.error(f"Error message: {error_message}")
logger.error(f"Traceback: {error_traceback}")
return jsonify({
'error': 'Failed to reset conversation',
'error_type': error_type,
'error_details': error_message
}), 500
@app.route('/api/debug', methods=['GET'])
def debug_info():
"""Return debugging information"""
debug_info = {
'python_version': sys.version,
'chatbot_initialized': chatbot is not None
}
if chatbot is not None:
debug_info.update({
'model_path_exists': os.path.exists(MODEL_PATH),
'model_path': MODEL_PATH,
'documents_path_exists': os.path.exists(DOCUMENTS_PATH),
'documents_path': DOCUMENTS_PATH,
'document_files': os.listdir(DOCUMENTS_PATH) if os.path.exists(DOCUMENTS_PATH) else []
})
return jsonify(debug_info)
def init_chatbot():
"""Initialize the chatbot with error handling"""
global chatbot
try:
# Import the chatbot class
from chatbot import VulnerableRAGChatbot
# Check if model path exists
if not os.path.exists(MODEL_PATH):
logger.error(f"Model file not found: {MODEL_PATH}")
return False
# Check if documents path exists
if not os.path.exists(DOCUMENTS_PATH):
logger.error(f"Documents directory not found: {DOCUMENTS_PATH}")
return False
# Check if there are PDF documents in the directory
pdf_files = [f for f in os.listdir(DOCUMENTS_PATH) if f.endswith('.pdf')]
if not pdf_files:
logger.error(f"No PDF files found in {DOCUMENTS_PATH}")
return False
logger.info(f"Found PDF files: {pdf_files}")
# Initialize chatbot
logger.info("Initializing chatbot...")
chatbot = VulnerableRAGChatbot(MODEL_PATH, DOCUMENTS_PATH)
logger.info("Chatbot initialized successfully")
return True
except ImportError as e:
logger.error(f"Failed to import VulnerableRAGChatbot: {str(e)}")
return False
except Exception as e:
error_type = type(e).__name__
error_message = str(e)
error_traceback = traceback.format_exc()
logger.error(f"Error type: {error_type}")
logger.error(f"Error message: {error_message}")
logger.error(f"Traceback: {error_traceback}")
return False
if __name__ == '__main__':
# Update these paths to your actual paths
MODEL_PATH = "./llama-2-7b-chat.Q4_K_M.gguf" # Update with your model path
DOCUMENTS_PATH = "./fake_company_docs" # Path to the generated documents
# Create templates directory if it doesn't exist
if not os.path.exists('templates'):
os.makedirs('templates')
# Create HTML template file with error display capability
with open('templates/index.html', 'w') as f:
f.write('''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ACME Corporation - Internal Chatbot</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.header {
text-align: center;
padding: 20px 0;
border-bottom: 1px solid #ddd;
}
.chat-container {
height: 400px;
border: 1px solid #ddd;
padding: 10px;
overflow-y: auto;
margin-bottom: 20px;
border-radius: 5px;
}
.message {
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
max-width: 70%;
}
.user-message {
background-color: #e6f7ff;
margin-left: auto;
}
.bot-message {
background-color: #f1f1f1;
}
.error-message {
background-color: #ffebee;
color: #c62828;
border: 1px solid #ef9a9a;
}
.input-container {
display: flex;
gap: 10px;
}
#message-input {
flex-grow: 1;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
padding: 10px 15px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#reset-btn {
background-color: #f44336;
}
#reset-btn:hover {
background-color: #d32f2f;
}
#debug-btn {
background-color: #2196f3;
}
#debug-btn:hover {
background-color: #1976d2;
}
.debug-info {
margin-top: 20px;
padding: 10px;
background-color: #f5f5f5;
border: 1px solid #ddd;
border-radius: 5px;
display: none;
}
.debug-info pre {
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="header">
<h1>ACME Corporation - Internal Chatbot</h1>
<p>Ask me anything about company policies, products, or technical information</p>
</div>
<div class="chat-container" id="chat-container">
<div class="message bot-message">
<p>Hello! I'm the ACME Corporation assistant. How can I help you today?</p>
</div>
</div>
<div class="input-container">
<input type="text" id="message-input" placeholder="Type your message here...">
<button id="send-btn">Send</button>
<button id="reset-btn">Reset</button>
<button id="debug-btn">Debug</button>
</div>
<div class="debug-info" id="debug-info">
<h3>Debug Information</h3>
<pre id="debug-content"></pre>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const chatContainer = document.getElementById('chat-container');
const messageInput = document.getElementById('message-input');
const sendButton = document.getElementById('send-btn');
const resetButton = document.getElementById('reset-btn');
const debugButton = document.getElementById('debug-btn');
const debugInfo = document.getElementById('debug-info');
const debugContent = document.getElementById('debug-content');
// Function to add message to the chat
function addMessage(message, isUser, isError = false) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user-message' : isError ? 'error-message' : 'bot-message'}`;
const messagePara = document.createElement('p');
messagePara.textContent = message;
messageDiv.appendChild(messagePara);
chatContainer.appendChild(messageDiv);
// Scroll to bottom
chatContainer.scrollTop = chatContainer.scrollHeight;
}
// Function to send message to API
async function sendMessage(message) {
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message })
});
const data = await response.json();
if (response.ok) {
return { success: true, message: data.response };
} else {
let errorMessage = data.error || 'Failed to get response';
if (data.error_details) {
errorMessage += `: ${data.error_details}`;
}
throw new Error(errorMessage);
}
} catch (error) {
console.error('Error:', error);
return {
success: false,
message: `Error: ${error.message}`
};
}
}
// Function to reset conversation
async function resetConversation() {
try {
const response = await fetch('/api/reset', {
method: 'POST'
});
const data = await response.json();
if (response.ok) {
// Clear chat container except for welcome message
while (chatContainer.childNodes.length > 1) {
chatContainer.removeChild(chatContainer.lastChild);
}
return true;
} else {
let errorMessage = data.error || 'Failed to reset conversation';
if (data.error_details) {
errorMessage += `: ${data.error_details}`;
}
throw new Error(errorMessage);
}
} catch (error) {
console.error('Error:', error);
addMessage(`Failed to reset conversation: ${error.message}`, false, true);
return false;
}
}
// Function to get debug information
async function getDebugInfo() {
try {
const response = await fetch('/api/debug');
const data = await response.json();
debugContent.textContent = JSON.stringify(data, null, 2);
debugInfo.style.display = 'block';
} catch (error) {
console.error('Error:', error);
debugContent.textContent = `Error fetching debug info: ${error.message}`;
debugInfo.style.display = 'block';
}
}
// Event listener for send button
sendButton.addEventListener('click', async function() {
const message = messageInput.value.trim();
if (message) {
addMessage(message, true);
messageInput.value = '';
// Show typing indicator
const typingDiv = document.createElement('div');
typingDiv.className = 'message bot-message';
typingDiv.textContent = 'Typing...';
chatContainer.appendChild(typingDiv);
// Get response from API
const response = await sendMessage(message);
// Remove typing indicator
chatContainer.removeChild(typingDiv);
// Add bot response
addMessage(response.message, false, !response.success);
}
});
// Event listener for reset button
resetButton.addEventListener('click', async function() {
const reset = await resetConversation();
if (reset) {
addMessage("Conversation has been reset. How can I help you today?", false);
}
});
// Event listener for debug button
debugButton.addEventListener('click', async function() {
await getDebugInfo();
if (debugInfo.style.display === 'none') {
debugInfo.style.display = 'block';
} else {
debugInfo.style.display = 'none';
}
});
// Event listener for Enter key
messageInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
sendButton.click();
}
});
});
</script>
</body>
</html>
''')
# Initialize chatbot
if init_chatbot():
logger.info("Starting Flask server...")
app.run(debug=True, host='0.0.0.0', port=8000)
else:
logger.error("Failed to initialize chatbot. Please check the logs above for details.")
sys.exit(1)