-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
371 lines (293 loc) · 13.3 KB
/
main.py
File metadata and controls
371 lines (293 loc) · 13.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
"""
Main Entry Point - Telco Troubleshooting Agentic Challenge
Phase 3 Evaluation: Autonomous agent execution with time constraints
"""
import os
import sys
import json
import time
import argparse
import logging
from pathlib import Path
# Add agent directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'agent'))
from agent.llm_engine import get_llm_engine
from agent.react_loop import get_react_agent
from agent.trace_logger import get_trace_logger
from agent.tools import get_tool_executor
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class TelcoAgent:
"""
Main agent class for the Telco Troubleshooting Challenge
"""
def __init__(self, server_url: str = "http://localhost:8000"):
self.server_url = server_url
self.llm_engine = None
self.react_agent = None
self.trace_logger = None
self.tool_executor = None
# Performance tracking
self.start_time = None
self.max_execution_time = 300 # 5 minutes for Phase 3
def initialize(self) -> bool:
"""
Initialize all components
"""
try:
logger.info("Initializing Telco Agent...")
# Initialize LLM engine
logger.info("Loading LLM engine...")
self.llm_engine = get_llm_engine()
# Initialize tool executor with server URL
logger.info(f"Initializing tool executor for {self.server_url}")
self.tool_executor = get_tool_executor()
self.tool_executor.server_url = self.server_url
# Initialize ReAct agent
logger.info("Initializing ReAct agent...")
self.react_agent = get_react_agent()
# Initialize trace logger
logger.info("Initializing trace logger...")
self.trace_logger = get_trace_logger()
logger.info("Telco Agent initialized successfully")
return True
except Exception as e:
logger.error(f"Failed to initialize agent: {e}")
return False
def load_scenario(self, scenario_file: str) -> Dict:
"""
Load scenario from file
"""
try:
with open(scenario_file, 'r') as f:
scenario = json.load(f)
logger.info(f"Loaded scenario from {scenario_file}")
return scenario
except Exception as e:
logger.error(f"Failed to load scenario: {e}")
return {}
def run_scenario(self, scenario: Dict) -> Dict:
"""
Run a single scenario
"""
logger.info("Running scenario...")
self.start_time = time.time()
# Log conversation start
self.trace_logger.log_conversation_start(scenario)
# Run the ReAct loop
try:
result = self.react_agent.run_react_loop(scenario)
# Check time constraint
execution_time = time.time() - self.start_time
if execution_time > self.max_execution_time:
logger.warning(f"Scenario exceeded time limit: {execution_time:.2f}s > {self.max_execution_time}s")
result["time_exceeded"] = True
else:
logger.info(f"Scenario completed in {execution_time:.2f}s")
# Log conversation end
self.trace_logger.log_conversation_end(result)
return result
except Exception as e:
logger.error(f"Scenario execution failed: {e}")
error_result = {
"error": str(e),
"execution_time": time.time() - self.start_time,
"success": False
}
self.trace_logger.log_conversation_end(error_result)
return error_result
def run_batch_scenarios(self, scenarios_file: str) -> Dict:
"""
Run multiple scenarios from a file
"""
try:
with open(scenarios_file, 'r') as f:
scenarios = json.load(f)
logger.info(f"Running {len(scenarios)} scenarios...")
results = []
total_start_time = time.time()
for i, scenario in enumerate(scenarios):
logger.info(f"Running scenario {i+1}/{len(scenarios)}")
result = self.run_scenario(scenario)
result["scenario_index"] = i
results.append(result)
# Check if we're approaching time limit
elapsed = time.time() - total_start_time
remaining = self.max_execution_time - elapsed
if remaining < 60: # Less than 1 minute remaining
logger.warning(f"Approaching time limit, stopping early. Remaining: {remaining:.2f}s")
break
total_time = time.time() - total_start_time
logger.info(f"Batch completed in {total_time:.2f}s")
return {
"total_scenarios": len(results),
"total_execution_time": total_time,
"results": results,
"trace_summary": self.trace_logger.get_trace_summary()
}
except Exception as e:
logger.error(f"Batch execution failed: {e}")
return {"error": str(e)}
def generate_submission_file(self, results: Dict, output_file: str = "result.csv"):
"""
Generate submission file from results with robust formatting
"""
try:
import pandas as pd
# Convert results to submission format
submission_data = []
for result in results.get("results", []):
scenario = result.get("scenario", {})
solution = result.get("final_state", {}).get("solution", {})
# Extract and format Track A answer
track_a_answer = solution.get("track_a_answer", "")
if not track_a_answer:
# Fallback: derive from conversation history
track_a_answer = self._extract_track_a_from_history(result)
# Ensure Track A format (C1|C2|C3)
track_a_answer = self._format_track_a(track_a_answer)
# Extract and format Track B answer
track_b_answer = solution.get("track_b_answer", "")
if not track_b_answer:
# Fallback: derive from conversation history
track_b_answer = self._extract_track_b_from_history(result)
# Ensure Track B is string (can be empty)
track_b_answer = str(track_b_answer) if track_b_answer else ""
submission_data.append({
"ID": scenario.get("id", f"scenario_{result.get('scenario_index', 'unknown')}"),
"Track A": track_a_answer,
"Track B": track_b_answer
})
# Create DataFrame with proper column order
df = pd.DataFrame(submission_data, columns=["ID", "Track A", "Track B"])
# Remove any NaN values
df = df.fillna("")
# Validate format before saving
from utils.format_validator import get_format_validator
validator = get_format_validator()
# Save to temporary file first
temp_file = output_file.replace('.csv', '_temp.csv')
df.to_csv(temp_file, index=False)
# Validate the format
is_valid, validation_result = validator.validate_csv_format(temp_file)
if not is_valid:
logger.error(f"CSV validation failed: {validation_result}")
return False
# Move to final location
import os
os.rename(temp_file, output_file)
logger.info(f"Submission file saved and validated: {output_file}")
logger.info(f"Total entries: {len(df)}")
logger.info(f"Track A non-empty: {(df['Track A'] != '').sum()}")
logger.info(f"Track B non-empty: {(df['Track B'] != '').sum()}")
return True
except Exception as e:
logger.error(f"Failed to generate submission file: {e}")
return False
def _extract_track_a_from_history(self, result: Dict) -> str:
"""Extract Track A answer from conversation history"""
history = result.get("conversation_history", [])
# Look for cell selections in conversation
for entry in reversed(history):
thought = entry.get("thought", "")
action = entry.get("action", "")
# Look for cell patterns like "C1", "C2", etc.
import re
cells = re.findall(r'C\d+', thought)
if cells:
return "|".join(sorted(set(cells)))
return ""
def _extract_track_b_from_history(self, result: Dict) -> str:
"""Extract Track B answer from conversation history"""
history = result.get("conversation_history", [])
# Look for diagnosis or solution in conversation
for entry in reversed(history):
thought = entry.get("thought", "")
observation = entry.get("observation", {})
# Look for diagnosis patterns
if "diagnosis" in thought.lower() or "root cause" in thought.lower():
return thought[:200] # First 200 chars
return ""
def _format_track_a(self, track_a: str) -> str:
"""Format Track A answer to ensure proper format"""
if not track_a:
return ""
# Extract cell references
import re
cells = re.findall(r'C\d+', str(track_a))
# Remove duplicates and sort
unique_cells = sorted(set(cells))
# Join with pipe
return "|".join(unique_cells) if unique_cells else ""
def cleanup(self):
"""
Clean up resources
"""
try:
if self.llm_engine:
self.llm_engine.cleanup()
logger.info("Agent cleanup completed")
except Exception as e:
logger.error(f"Cleanup failed: {e}")
def main():
"""
Main entry point for Phase 3 evaluation
"""
parser = argparse.ArgumentParser(description="Telco Troubleshooting Agent")
parser.add_argument("--server", default="http://localhost:8000", help="Server URL")
parser.add_argument("--scenario", help="Single scenario file")
parser.add_argument("--scenarios", help="Batch scenarios file")
parser.add_argument("--output", default="result.csv", help="Output submission file")
parser.add_argument("--validate", action="store_true", help="Validate trace file only")
args = parser.parse_args()
# Initialize agent
agent = TelcoAgent(server_url=args.server)
if not agent.initialize():
sys.exit(1)
try:
if args.validate:
# Validate trace file
is_valid = agent.trace_logger.validate_trace_file()
print(f"Trace file validation: {'PASS' if is_valid else 'FAIL'}")
return
if args.scenarios:
# Run batch scenarios
results = agent.run_batch_scenarios(args.scenarios)
# Generate submission file
agent.generate_submission_file(results, args.output)
# Print summary
print(f"\nEXECUTION SUMMARY")
print(f"Total scenarios: {results.get('total_scenarios', 0)}")
print(f"Total time: {results.get('total_execution_time', 0):.2f}s")
print(f"Trace entries: {results.get('trace_summary', {}).get('total_entries', 0)}")
print(f"Success rate: {results.get('trace_summary', {}).get('success_rate', 0):.1f}%")
elif args.scenario:
# Run single scenario
scenario = agent.load_scenario(args.scenario)
if scenario:
result = agent.run_scenario(scenario)
print(f"Scenario completed: {result.get('final_state', {}).get('completed', False)}")
print(f"Execution time: {result.get('execution_time', 0):.2f}s")
else:
# Interactive mode for testing
logger.info("Running in interactive mode...")
test_scenario = {
"id": "test_001",
"description": "Network connectivity issue",
"symptoms": ["ping fails", "high latency"],
"environment": "test"
}
result = agent.run_scenario(test_scenario)
print(f"Test completed: {result.get('final_state', {}).get('completed', False)}")
except KeyboardInterrupt:
logger.info("Interrupted by user")
except Exception as e:
logger.error(f"Execution failed: {e}")
finally:
agent.cleanup()
if __name__ == "__main__":
main()