Repository navigation
Expand file tree
/
Copy pathtest_loader.py
More file actions
173 lines (142 loc) · 5.2 KB
/
Copy pathtest_loader.py
File metadata and controls
173 lines (142 loc) · 5.2 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
"""
Test Case Loader for User Memory Evaluation Framework
"""
import sys
import json
import subprocess
from pathlib import Path
from typing import Dict, List, Optional, Any
from config import EVAL_FRAMEWORK_PATH
class TestCaseLoader:
"""Load test cases from user-memory-evaluation framework"""
def __init__(self):
self.eval_framework_path = EVAL_FRAMEWORK_PATH
if not self.eval_framework_path.exists():
raise ValueError(f"Evaluation framework not found at {self.eval_framework_path}")
def get_all_test_cases(self) -> List[Dict[str, Any]]:
"""Get all available test cases"""
script = """
import sys
import json
from pathlib import Path
import io
# Suppress rich console output
import rich.console
rich.console.Console = lambda *args, **kwargs: type('FakeConsole', (), {
'print': lambda self, *a, **k: None,
'__getattr__': lambda self, name: lambda *a, **k: None
})()
# Redirect output
old_stdout = sys.stdout
sys.stdout = io.StringIO()
try:
from framework import UserMemoryEvaluationFramework
framework = UserMemoryEvaluationFramework()
test_cases = []
for tc in framework.list_test_cases():
test_cases.append({
'test_id': tc.test_id,
'category': tc.category,
'title': tc.title,
'description': tc.description,
'num_conversations': len(tc.conversation_histories),
'user_question': tc.user_question
})
# Restore stdout for JSON output
sys.stdout = old_stdout
print(json.dumps(test_cases))
except Exception as e:
sys.stdout = old_stdout
print(json.dumps([]))
"""
result = subprocess.run(
[sys.executable, "-c", script],
cwd=self.eval_framework_path,
capture_output=True,
text=True
)
if result.returncode != 0:
print(f"Error getting test cases: {result.stderr}")
return []
try:
return json.loads(result.stdout)
except json.JSONDecodeError:
print(f"Error parsing test cases JSON")
return []
def get_layer3_test_cases(self) -> List[Dict[str, Any]]:
"""Get only Layer 3 test cases (most complex)"""
all_cases = self.get_all_test_cases()
return [tc for tc in all_cases if tc['category'] == 'layer3']
def get_test_case_conversations(self, test_id: str) -> List[Dict[str, Any]]:
"""Get detailed conversation histories for a specific test case"""
script = f"""
import sys
import json
from pathlib import Path
import io
# Redirect stdout to suppress any print statements from framework
old_stdout = sys.stdout
sys.stdout = io.StringIO()
try:
from framework import UserMemoryEvaluationFramework
framework = UserMemoryEvaluationFramework()
tc = framework.get_test_case("{test_id}")
# Restore stdout for our JSON output
sys.stdout = old_stdout
if not tc:
print(json.dumps([]))
else:
conversations = []
for conv in tc.conversation_histories:
conv_data = {{
'conversation_id': conv.conversation_id,
'timestamp': conv.timestamp,
'messages': []
}}
for msg in conv.messages:
msg_data = {{
'role': msg.role.value,
'content': msg.content
}}
# Add metadata if it exists
if hasattr(msg, 'metadata'):
msg_data['metadata'] = msg.metadata
conv_data['messages'].append(msg_data)
conversations.append(conv_data)
print(json.dumps(conversations))
except Exception as e:
import traceback
sys.stdout = old_stdout
sys.stderr.write(traceback.format_exc())
print(json.dumps([]))
"""
result = subprocess.run(
[sys.executable, "-c", script],
cwd=self.eval_framework_path,
capture_output=True,
text=True
)
if result.returncode != 0:
print(f"Error getting conversation histories: {result.stderr}")
return []
try:
return json.loads(result.stdout)
except json.JSONDecodeError as e:
print(f"Error parsing conversation histories JSON: {e}")
if result.stdout:
print(f"stdout (first 500 chars): {result.stdout[:500]}")
if result.stderr:
print(f"stderr (first 500 chars): {result.stderr[:500]}")
return []
def format_conversation_text(self, conversation: Dict[str, Any]) -> str:
"""Format a conversation into readable text"""
lines = []
lines.append(f"Conversation ID: {conversation['conversation_id']}")
lines.append(f"Timestamp: {conversation['timestamp']}")
lines.append("-" * 50)
for msg in conversation['messages']:
role = msg['role'].upper()
content = msg['content']
lines.append(f"{role}: {content}")
lines.append("") # Empty line between messages
return "\n".join(lines)