Spaces:
Sleeping
Sleeping
File size: 10,497 Bytes
bfcc872 cf9b3dc bfcc872 cf9b3dc bfcc872 | 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 | """
query_logger.py — Logs user queries and AI responses for RLHF analysis
Automatically logs every interaction to CSV and JSONL formats.
Export to RLHF training format with export_logs.py script.
"""
from __future__ import annotations
import csv
import json
import logging
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
logger = logging.getLogger(__name__)
class QueryLogger:
"""Logs user queries and AI responses to JSON and CSV for RLHF analysis."""
def __init__(self, log_dir: str = "logs"):
"""
Initialize query logger.
Args:
log_dir: Directory to store log files (default: "logs/")
"""
self.log_dir = Path(log_dir)
self.log_dir.mkdir(parents=True, exist_ok=True)
# Get today's date for log file naming
today = datetime.now().strftime("%Y%m%d")
self.jsonl_path = self.log_dir / f"queries_{today}.jsonl"
self.csv_path = self.log_dir / f"queries_{today}.csv"
# Initialize CSV file with headers if it doesn't exist
if not self.csv_path.exists():
self._init_csv()
def _init_csv(self):
"""Initialize CSV file with headers."""
headers = [
'timestamp',
'user_query',
'ai_response',
'tools_called',
'response_time_ms',
'success',
'rating',
'feedback',
'model',
'tokens_used'
]
with open(self.csv_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=headers)
writer.writeheader()
def log_interaction(
self,
user_query: str,
ai_response: str,
*,
tools_called: Optional[List[str]] = None,
response_time_ms: Optional[int] = None,
success: bool = True,
rating: Optional[int] = None,
feedback: Optional[str] = None,
model: Optional[str] = None,
tokens_used: Optional[int] = None,
metadata: Optional[Dict[str, Any]] = None
):
"""
Log a user query and AI response.
Args:
user_query: The user's input query
ai_response: The AI's response
tools_called: List of tool names that were called
response_time_ms: Response time in milliseconds
success: Whether the interaction was successful
rating: Optional 1-5 rating (for RLHF)
feedback: Optional text feedback (for RLHF)
model: Model name used (e.g., "gpt-4-mini")
tokens_used: Number of tokens consumed
metadata: Additional metadata to log
"""
timestamp = datetime.utcnow().isoformat()
# Prepare log entry
log_entry = {
'timestamp': timestamp,
'user_query': user_query,
'ai_response': ai_response,
'tools_called': tools_called or [],
'response_time_ms': response_time_ms,
'success': success,
'rating': rating,
'feedback': feedback,
'model': model,
'tokens_used': tokens_used,
'metadata': metadata or {}
}
# Write to JSONL (one JSON object per line)
try:
with open(self.jsonl_path, 'a', encoding='utf-8') as f:
f.write(json.dumps(log_entry) + '\n')
except Exception as e:
logger.warning(f"Failed to write to JSONL log: {e}")
# Write to CSV (Excel-compatible)
try:
with open(self.csv_path, 'a', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=[
'timestamp', 'user_query', 'ai_response', 'tools_called',
'response_time_ms', 'success', 'rating', 'feedback',
'model', 'tokens_used'
])
writer.writerow({
'timestamp': timestamp,
'user_query': user_query,
'ai_response': ai_response,
'tools_called': ','.join(tools_called or []),
'response_time_ms': response_time_ms,
'success': success,
'rating': rating or '',
'feedback': feedback or '',
'model': model or '',
'tokens_used': tokens_used or ''
})
except Exception as e:
logger.warning(f"Failed to write to CSV log: {e}")
logger.debug(f"Logged interaction: {user_query[:50]}...")
def get_stats(self) -> Dict[str, Any]:
"""
Get statistics about logged queries.
Returns:
Dictionary with statistics (total queries, success rate, avg response time, etc.)
"""
try:
with open(self.csv_path, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
rows = list(reader)
if not rows:
return {
'total_queries': 0,
'successful_queries': 0,
'failed_queries': 0,
'avg_response_time_ms': 0,
'avg_rating': 0,
'rated_queries': 0,
'tool_usage': {}
}
total = len(rows)
successful = sum(1 for r in rows if r.get('success') == 'True')
failed = total - successful
# Calculate average response time
response_times = [
int(r['response_time_ms'])
for r in rows
if r.get('response_time_ms') and r['response_time_ms'].isdigit()
]
avg_response_time = sum(response_times) / len(response_times) if response_times else 0
# Calculate average rating
ratings = [
int(r['rating'])
for r in rows
if r.get('rating') and r['rating'].isdigit()
]
avg_rating = sum(ratings) / len(ratings) if ratings else 0
rated_queries = len(ratings)
# Count tool usage
tool_usage = {}
for row in rows:
tools = row.get('tools_called', '').split(',')
for tool in tools:
tool = tool.strip()
if tool:
tool_usage[tool] = tool_usage.get(tool, 0) + 1
return {
'total_queries': total,
'successful_queries': successful,
'failed_queries': failed,
'avg_response_time_ms': avg_response_time,
'avg_rating': avg_rating,
'rated_queries': rated_queries,
'tool_usage': tool_usage
}
except Exception as e:
logger.warning(f"Failed to calculate stats: {e}")
return {
'total_queries': 0,
'successful_queries': 0,
'failed_queries': 0,
'avg_response_time_ms': 0,
'avg_rating': 0,
'rated_queries': 0,
'tool_usage': {}
}
def export_for_rlhf(self, output_path: Optional[Path] = None) -> Path:
"""
Export logs in RLHF training format.
Format:
[
{
"prompt": "user query",
"completion": "ai response",
"rating": 5,
"feedback": "Great!",
"tools_used": ["list_grants"],
"timestamp": "2025-10-23T10:15:30"
},
...
]
Args:
output_path: Optional custom output path
Returns:
Path to exported file
"""
if output_path is None:
today = datetime.now().strftime("%Y%m%d")
output_path = self.log_dir / f"rlhf_data_{today}.json"
rlhf_data = []
try:
# Read from JSONL
with open(self.jsonl_path, 'r', encoding='utf-8') as f:
for line in f:
entry = json.loads(line)
# Only include successful interactions
if not entry.get('success', False):
continue
rlhf_entry = {
'prompt': entry['user_query'],
'completion': entry['ai_response'],
'rating': entry.get('rating'),
'feedback': entry.get('feedback'),
'tools_used': entry.get('tools_called', []),
'timestamp': entry['timestamp'],
'response_time_ms': entry.get('response_time_ms'),
'model': entry.get('model')
}
rlhf_data.append(rlhf_entry)
# Write RLHF format
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(rlhf_data, f, indent=2, ensure_ascii=False)
logger.info(f"Exported {len(rlhf_data)} interactions to {output_path}")
return output_path
except Exception as e:
logger.error(f"Failed to export RLHF data: {e}")
raise
# Global singleton instance
_query_logger: Optional[QueryLogger] = None
def get_query_logger(log_dir: str = "logs") -> QueryLogger:
"""
Get or create the global query logger instance.
Args:
log_dir: Directory to store log files
Returns:
QueryLogger instance
"""
global _query_logger
if _query_logger is None:
_query_logger = QueryLogger(log_dir=log_dir)
return _query_logger
# Quick self-test
if __name__ == "__main__":
logger = get_query_logger(log_dir="_out/test_logs")
# Log a test interaction
logger.log_interaction(
user_query="What grants are available for batteries?",
ai_response="Here are the battery-related grants: 1. Battery Innovation Grant...",
tools_called=["list_grants"],
response_time_ms=1500,
success=True,
rating=5,
feedback="Very helpful!",
model="gpt-4-mini"
)
# Get stats
stats = logger.get_stats()
print("Statistics:", json.dumps(stats, indent=2))
# Export for RLHF
output = logger.export_for_rlhf()
print(f"Exported to: {output}")
|