-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharc_conversation_logger.py
More file actions
346 lines (286 loc) · 13 KB
/
arc_conversation_logger.py
File metadata and controls
346 lines (286 loc) · 13 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
#!/usr/bin/env python3
"""
Conversation Logger for ARC AGI AI Assistant
Logs all AI interactions for analysis and debugging
"""
import json
import os
from datetime import datetime
from typing import Dict, Any, Optional
import logging
from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler
from pathlib import Path
class ConversationLogger:
"""Manages logging of all AI conversations"""
def __init__(self, log_dir: str = "logs/conversations"):
"""Initialize the conversation logger"""
self.log_dir = Path(log_dir)
self.log_dir.mkdir(parents=True, exist_ok=True)
# Setup different loggers for different purposes
self.setup_loggers()
# Session tracking
self.session_id = datetime.now().strftime("%Y%m%d_%H%M%S")
self.conversation_count = 0
def setup_loggers(self):
"""Setup various loggers for different types of logs"""
# Main conversation logger (human-readable)
self.conversation_logger = self._create_logger(
'conversation',
self.log_dir / 'conversations.log',
max_bytes=10*1024*1024, # 10MB
backup_count=10
)
# Structured JSON logger for analysis
self.json_logger = self._create_logger(
'json_conversation',
self.log_dir / 'conversations.json',
max_bytes=50*1024*1024, # 50MB
backup_count=5,
formatter=None # No formatting for JSON
)
# Function calls logger
self.function_logger = self._create_logger(
'functions',
self.log_dir / 'function_calls.log',
max_bytes=5*1024*1024, # 5MB
backup_count=5
)
# Verification attempts logger
self.verification_logger = self._create_logger(
'verification',
self.log_dir / 'verification_attempts.log',
max_bytes=5*1024*1024, # 5MB
backup_count=5
)
# Daily conversation logger
self.daily_logger = self._create_daily_logger(
'daily',
self.log_dir / 'daily'
)
def _create_logger(self, name: str, filename: Path, max_bytes: int = 10*1024*1024,
backup_count: int = 5, formatter=None) -> logging.Logger:
"""Create a rotating file logger"""
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
# Remove existing handlers
logger.handlers.clear()
# Create rotating file handler
handler = RotatingFileHandler(
filename,
maxBytes=max_bytes,
backupCount=backup_count,
encoding='utf-8'
)
# Set formatter
if formatter is None:
formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def _create_daily_logger(self, name: str, log_dir: Path) -> logging.Logger:
"""Create a daily rotating logger"""
log_dir.mkdir(parents=True, exist_ok=True)
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
# Remove existing handlers
logger.handlers.clear()
# Create timed rotating handler (daily)
handler = TimedRotatingFileHandler(
log_dir / 'conversation.log',
when='midnight',
interval=1,
backupCount=30, # Keep 30 days
encoding='utf-8'
)
handler.suffix = "%Y%m%d"
formatter = logging.Formatter(
'%(asctime)s - [%(levelname)s] - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def log_conversation(self, user_message: str, ai_response: Dict[str, Any],
puzzle_id: Optional[str] = None,
output_grid: Optional[list] = None,
metadata: Optional[Dict] = None):
"""Log a complete conversation exchange"""
self.conversation_count += 1
timestamp = datetime.now().isoformat()
# Create conversation record
conversation = {
"timestamp": timestamp,
"session_id": self.session_id,
"conversation_number": self.conversation_count,
"puzzle_id": puzzle_id,
"user_message": user_message,
"ai_response": ai_response,
"output_grid_provided": output_grid is not None,
"metadata": metadata or {}
}
# Log to human-readable format
self.conversation_logger.info(
f"[Session {self.session_id}] Conversation #{self.conversation_count}\n"
f" Puzzle: {puzzle_id or 'None'}\n"
f" User: {user_message}\n"
f" AI: {ai_response.get('message', 'No message')}\n"
f" Type: {ai_response.get('type', 'Unknown')}"
)
# Log to daily log
self.daily_logger.info(
f"[{puzzle_id or 'NO_PUZZLE'}] User: {user_message[:100]}... | "
f"AI: {ai_response.get('message', '')[:100]}..."
)
# Log to JSON for analysis
self.json_logger.info(json.dumps(conversation))
# If there was a function call, log it separately
if ai_response.get('function_call'):
self.log_function_call(
function_name=ai_response['function_call'].get('name'),
parameters=ai_response['function_call'].get('parameters'),
result=ai_response.get('function_result'),
puzzle_id=puzzle_id
)
# If this was a verification attempt, log it
if 'verification' in ai_response or 'submission_result' in ai_response:
self.log_verification_attempt(
puzzle_id=puzzle_id,
result=ai_response.get('verification') or ai_response.get('submission_result'),
message=user_message
)
def log_function_call(self, function_name: str, parameters: Dict,
result: Any, puzzle_id: Optional[str] = None):
"""Log AI function calls"""
timestamp = datetime.now().isoformat()
self.function_logger.info(
f"[{timestamp}] Function: {function_name}\n"
f" Puzzle: {puzzle_id or 'None'}\n"
f" Parameters: {json.dumps(parameters, indent=2)}\n"
f" Result: {json.dumps(result, default=str, indent=2)[:500]}"
)
def log_verification_attempt(self, puzzle_id: str, result: Dict, message: str):
"""Log verification attempts"""
timestamp = datetime.now().isoformat()
self.verification_logger.info(
f"[{timestamp}] Verification for {puzzle_id}\n"
f" Message: {message}\n"
f" Correct: {result.get('correct', False)}\n"
f" Accuracy: {result.get('accuracy', 0)*100:.1f}%\n"
f" Attempts Remaining: {result.get('attempts_remaining', 'N/A')}\n"
f" Feedback: {result.get('feedback', 'None')}"
)
def log_heuristic_application(self, heuristic_id: str, heuristic_name: str,
puzzle_id: str, success: bool, confidence: float):
"""Log heuristic applications"""
timestamp = datetime.now().isoformat()
log_entry = {
"timestamp": timestamp,
"puzzle_id": puzzle_id,
"heuristic_id": heuristic_id,
"heuristic_name": heuristic_name,
"success": success,
"confidence": confidence
}
# Log to function logger as heuristics are a type of function
self.function_logger.info(
f"[{timestamp}] Heuristic Applied: {heuristic_name} ({heuristic_id})\n"
f" Puzzle: {puzzle_id}\n"
f" Success: {success}\n"
f" Confidence: {confidence:.2f}"
)
def log_error(self, error_message: str, error_type: str = "general",
puzzle_id: Optional[str] = None, context: Optional[Dict] = None):
"""Log errors that occur during conversations"""
timestamp = datetime.now().isoformat()
error_log = {
"timestamp": timestamp,
"session_id": self.session_id,
"error_type": error_type,
"error_message": error_message,
"puzzle_id": puzzle_id,
"context": context or {}
}
self.conversation_logger.error(
f"[ERROR] {error_type}: {error_message}\n"
f" Puzzle: {puzzle_id or 'None'}\n"
f" Context: {json.dumps(context or {}, indent=2)}"
)
# Also log to daily
self.daily_logger.error(f"[{error_type}] {error_message}")
def get_conversation_stats(self) -> Dict:
"""Get statistics about conversations"""
stats = {
"session_id": self.session_id,
"total_conversations": self.conversation_count,
"session_start": self.session_id,
"log_directory": str(self.log_dir)
}
# Count log files
try:
stats["log_files"] = {
"conversation_logs": len(list(self.log_dir.glob("conversations.log*"))),
"json_logs": len(list(self.log_dir.glob("conversations.json*"))),
"function_logs": len(list(self.log_dir.glob("function_calls.log*"))),
"verification_logs": len(list(self.log_dir.glob("verification_attempts.log*"))),
"daily_logs": len(list((self.log_dir / "daily").glob("*.log*")))
}
except:
stats["log_files"] = "Unable to count"
return stats
def search_conversations(self, query: str, max_results: int = 10) -> list:
"""Search through conversation logs"""
results = []
try:
# Search in the current conversation log
with open(self.log_dir / 'conversations.log', 'r', encoding='utf-8') as f:
lines = f.readlines()
current_conv = []
for line in lines:
if "Conversation #" in line:
if current_conv and query.lower() in ''.join(current_conv).lower():
results.append(''.join(current_conv))
if len(results) >= max_results:
break
current_conv = [line]
else:
current_conv.append(line)
# Check last conversation
if current_conv and query.lower() in ''.join(current_conv).lower():
results.append(''.join(current_conv))
except Exception as e:
return [f"Error searching conversations: {e}"]
return results[:max_results]
def export_session_logs(self, export_path: Optional[str] = None) -> str:
"""Export current session logs to a single file"""
if not export_path:
export_path = self.log_dir / f"session_{self.session_id}_export.log"
export_path = Path(export_path)
try:
with open(export_path, 'w', encoding='utf-8') as export_file:
export_file.write(f"=== Session Export: {self.session_id} ===\n")
export_file.write(f"Total Conversations: {self.conversation_count}\n\n")
# Read and append conversation log
if (self.log_dir / 'conversations.log').exists():
with open(self.log_dir / 'conversations.log', 'r', encoding='utf-8') as f:
export_file.write("=== Conversations ===\n")
export_file.write(f.read())
export_file.write("\n\n")
# Read and append function calls
if (self.log_dir / 'function_calls.log').exists():
with open(self.log_dir / 'function_calls.log', 'r', encoding='utf-8') as f:
export_file.write("=== Function Calls ===\n")
export_file.write(f.read())
export_file.write("\n\n")
# Read and append verification attempts
if (self.log_dir / 'verification_attempts.log').exists():
with open(self.log_dir / 'verification_attempts.log', 'r', encoding='utf-8') as f:
export_file.write("=== Verification Attempts ===\n")
export_file.write(f.read())
return str(export_path)
except Exception as e:
return f"Error exporting logs: {e}"
# Global logger instance
conversation_logger = ConversationLogger()