-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphase4_test.py
More file actions
221 lines (172 loc) · 6.49 KB
/
phase4_test.py
File metadata and controls
221 lines (172 loc) · 6.49 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
#!/usr/bin/env python3
"""
Phase 4 Test: Local AI Model + MCP Routing.
This script demonstrates:
1. Loading KCA University past exam questions
2. Filtering questions by unit
3. Sending questions to MCPClient in exam mode
4. Displaying questions and answers
This is a proof-of-concept for the MCP routing system.
"""
import sys
import os
from typing import List, Dict
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from scripts.load_data import load_questions
from mcp.client import MCPClient
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def test_phase4():
"""
Main Phase 4 test function.
Workflow:
1. Load KCA questions from deduplicated dataset
2. Filter questions by unit (BBIT106)
3. Initialize MCP client
4. Send questions to MCP in exam mode
5. Display results
"""
print("=" * 80)
print("PHASE 4 TEST: Local AI Model + MCP Routing")
print("=" * 80)
# Step 1: Load questions
print("\n[1/5] Loading KCA University past exam questions...")
try:
loader = load_questions()
all_questions = loader.get_all_questions()
print(f"✓ Loaded {len(all_questions)} questions")
except Exception as e:
print(f"❌ Failed to load questions: {e}")
return False
# Step 2: Filter by unit
print("\n[2/5] Filtering questions by unit (BBIT106)...")
unit_questions = loader.filter_by_unit("BBIT106")
if not unit_questions:
print("❌ No questions found for BBIT106")
print(f"Available units: {loader.get_unique_units()}")
return False
print(f"✓ Found {len(unit_questions)} BBIT106 questions")
# Limit to first 2 questions for testing
test_questions = unit_questions[:2]
print(f" Using first {len(test_questions)} questions for testing")
# Step 3: Initialize MCP client
print("\n[3/5] Initializing MCP Client...")
client = MCPClient(default_mode="exam")
# Check connection
if not client.check_connection():
print("❌ Cannot connect to Ollama server")
print(" Make sure Ollama is running: ollama serve")
return False
print("✓ Connected to Ollama server")
print(f"✓ Available modes: {', '.join(client.get_available_modes())}")
# Step 4: Send questions to MCP
print("\n[4/5] Sending questions to MCP Client (EXAM mode)...")
print("-" * 80)
results = []
for i, question_obj in enumerate(test_questions, 1):
question_text = question_obj.get("question", "")
question_number = question_obj.get("question_number", i)
year = question_obj.get("year", "Unknown")
print(f"\n📝 Question {i}/{len(test_questions)} (Q{question_number}, {year})")
print(f" Unit: {question_obj.get('unit')}")
print(f" Preview: {question_text[:100]}...")
# Send to MCP
print(f" Generating answer in EXAM mode...")
answer = client.answer_question(question_text, mode="exam")
if answer:
print(f" ✓ Answer generated ({len(answer)} chars)")
results.append({
"question_number": question_number,
"year": year,
"question": question_text,
"answer": answer,
"mode": "exam"
})
else:
print(f" ❌ Failed to generate answer")
results.append({
"question_number": question_number,
"year": year,
"question": question_text,
"answer": None,
"mode": "exam"
})
# Step 5: Display results
print("\n[5/5] Displaying Results...")
print("=" * 80)
for i, result in enumerate(results, 1):
print(f"\n{'='*80}")
print(f"RESULT {i}/{len(results)}")
print(f"{'='*80}")
print(f"\n📌 Question Details:")
print(f" Number: Q{result['question_number']}")
print(f" Year: {result['year']}")
print(f" Mode: {result['mode'].upper()}")
print(f"\n❓ Question:")
print(f" {result['question'][:200]}...")
if result['answer']:
print(f"\n✅ Answer:")
print(f" {result['answer'][:300]}...")
if len(result['answer']) > 300:
print(f" [... {len(result['answer']) - 300} more characters ...]")
else:
print(f"\n❌ No answer generated")
print("\n" + "=" * 80)
print("✓ PHASE 4 TEST COMPLETE")
print("=" * 80)
return True
def test_multiple_modes():
"""
Test MCP client with different answer modes.
This demonstrates how the same question can be answered
in different modes (exam, local, global, mixed).
"""
print("\n" + "=" * 80)
print("BONUS: Testing Multiple Answer Modes")
print("=" * 80)
# Load a single question
loader = load_questions()
questions = loader.filter_by_unit("BBIT106")
if not questions:
print("❌ No questions found")
return False
question_obj = questions[0]
question_text = question_obj.get("question", "")
print(f"\n📝 Test Question (Q{question_obj.get('question_number')}):")
print(f" {question_text[:150]}...")
# Initialize client
client = MCPClient()
if not client.check_connection():
print("❌ Cannot connect to Ollama")
return False
# Test each mode
modes = ["exam", "local", "global", "mixed"]
for mode in modes:
print(f"\n{'─'*80}")
print(f"Testing {mode.upper()} mode...")
print(f"{'─'*80}")
answer = client.answer_question(question_text, mode=mode)
if answer:
print(f"✓ {mode.upper()} mode answer:")
print(f" {answer[:200]}...")
if len(answer) > 200:
print(f" [... {len(answer) - 200} more characters ...]")
else:
print(f"❌ Failed to generate {mode} mode answer")
return True
if __name__ == "__main__":
# Run Phase 4 test
success = test_phase4()
if success:
# Optionally test multiple modes
try:
test_multiple_modes()
except Exception as e:
logger.warning(f"Multiple modes test failed: {e}")
sys.exit(0 if success else 1)