Spaces:
Sleeping
Sleeping
File size: 13,099 Bytes
79d4fd5 | 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 | # Tests for intent classifier
import pytest
from unittest.mock import Mock, patch, MagicMock
from src.agent.intent_classifier import (
IntentClassifier,
IntentType,
ClassificationResult,
get_intent_classifier,
reset_intent_classifier,
)
class TestIntentType:
# Tests for IntentType enum
def test_intent_types_exist(self):
assert IntentType.DOCUMENT_QUERY is not None
assert IntentType.DATABASE_QUERY is not None
assert IntentType.HYBRID_QUERY is not None
assert IntentType.CONVERSATIONAL is not None
assert IntentType.AMBIGUOUS is not None
def test_intent_type_values(self):
assert IntentType.DOCUMENT_QUERY.value == "document_query"
assert IntentType.DATABASE_QUERY.value == "database_query"
assert IntentType.HYBRID_QUERY.value == "hybrid_query"
assert IntentType.CONVERSATIONAL.value == "conversational"
assert IntentType.AMBIGUOUS.value == "ambiguous"
class TestClassificationResult:
# Tests for ClassificationResult dataclass
def test_classification_result_creation(self):
result = ClassificationResult(
intent=IntentType.DATABASE_QUERY,
confidence=0.95,
reasoning="Test reasoning",
suggested_tables=["users", "orders"],
)
assert result.intent == IntentType.DATABASE_QUERY
assert result.confidence == 0.95
assert result.reasoning == "Test reasoning"
assert result.suggested_tables == ["users", "orders"]
def test_classification_result_default_values(self):
result = ClassificationResult(
intent=IntentType.DOCUMENT_QUERY,
confidence=0.8,
reasoning="Default test",
)
assert result.suggested_tables == []
assert result.requires_clarification is False
def test_classification_result_to_dict(self):
result = ClassificationResult(
intent=IntentType.HYBRID_QUERY,
confidence=0.85,
reasoning="Both sources needed",
suggested_tables=["products"],
)
data = result.to_dict()
assert data["intent"] == "hybrid_query"
assert data["confidence"] == 0.85
assert data["reasoning"] == "Both sources needed"
assert data["suggested_tables"] == ["products"]
def test_is_database_related(self):
db_result = ClassificationResult(IntentType.DATABASE_QUERY, 0.9, "test")
hybrid_result = ClassificationResult(IntentType.HYBRID_QUERY, 0.8, "test")
doc_result = ClassificationResult(IntentType.DOCUMENT_QUERY, 0.9, "test")
assert db_result.is_database_related() is True
assert hybrid_result.is_database_related() is True
assert doc_result.is_database_related() is False
def test_is_document_related(self):
doc_result = ClassificationResult(IntentType.DOCUMENT_QUERY, 0.9, "test")
hybrid_result = ClassificationResult(IntentType.HYBRID_QUERY, 0.8, "test")
db_result = ClassificationResult(IntentType.DATABASE_QUERY, 0.9, "test")
assert doc_result.is_document_related() is True
assert hybrid_result.is_document_related() is True
assert db_result.is_document_related() is False
class TestIntentClassifier:
# Tests for IntentClassifier class
@pytest.fixture
def classifier(self):
with patch("src.agent.intent_classifier.Groq") as mock_groq_class:
mock_client = Mock()
mock_groq_class.return_value = mock_client
classifier = IntentClassifier(api_key="test_key")
classifier._client = mock_client
return classifier
def test_classifier_initialization(self):
classifier = IntentClassifier(api_key="test", confidence_threshold=0.8)
assert classifier._confidence_threshold == 0.8
def test_classifier_with_custom_threshold(self):
classifier = IntentClassifier(
api_key="test",
confidence_threshold=0.5,
)
assert classifier._confidence_threshold == 0.5
def test_classify_database_query(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "DATABASE_QUERY", "confidence": 0.9, "reasoning": "SQL needed", "suggested_tables": ["musteriler"]}'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("Kac tane musterimiz var?")
assert result.intent == IntentType.DATABASE_QUERY
assert result.confidence == 0.9
assert "musteriler" in result.suggested_tables
def test_classify_document_query(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "DOCUMENT_QUERY", "confidence": 0.85, "reasoning": "Document search", "suggested_tables": []}'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("Sirketi kim kurdu?")
assert result.intent == IntentType.DOCUMENT_QUERY
def test_classify_hybrid_query(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "HYBRID_QUERY", "confidence": 0.75, "reasoning": "Both needed", "suggested_tables": ["urunler"]}'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("Urun politikamiz nedir ve kac urunumuz var?")
assert result.intent == IntentType.HYBRID_QUERY
def test_classify_conversational(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "CONVERSATIONAL", "confidence": 0.95, "reasoning": "Greeting", "suggested_tables": []}'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("Merhaba!")
assert result.intent == IntentType.CONVERSATIONAL
def test_classify_with_context(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "DATABASE_QUERY", "confidence": 0.88, "reasoning": "With context", "suggested_tables": []}'))
]
classifier._client.chat.completions.create.return_value = mock_response
chat_history = [{"role": "user", "content": "previous query"}]
result = classifier.classify("Devam et", chat_history)
assert result is not None
classifier._client.chat.completions.create.assert_called_once()
def test_classify_invalid_json_response(self, classifier):
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content="Invalid JSON"))]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("Test query")
# Implementation defaults to DOCUMENT_QUERY on parse failure
assert result.intent == IntentType.DOCUMENT_QUERY
assert result.confidence == 0.5
def test_classify_api_error(self, classifier):
classifier._client.chat.completions.create.side_effect = Exception("API Error")
result = classifier.classify("Test query")
# Implementation defaults to DOCUMENT_QUERY on API error
assert result.intent == IntentType.DOCUMENT_QUERY
assert result.confidence == 0.5
class TestIntentClassifierSingleton:
# Tests for singleton pattern
def setup_method(self):
reset_intent_classifier()
def teardown_method(self):
reset_intent_classifier()
@patch("src.agent.intent_classifier.Groq")
def test_get_intent_classifier_creates_instance(self, mock_groq):
classifier = get_intent_classifier()
assert classifier is not None
assert isinstance(classifier, IntentClassifier)
@patch("src.agent.intent_classifier.Groq")
def test_get_intent_classifier_returns_same_instance(self, mock_groq):
classifier1 = get_intent_classifier()
classifier2 = get_intent_classifier()
assert classifier1 is classifier2
@patch("src.agent.intent_classifier.Groq")
def test_reset_intent_classifier(self, mock_groq):
classifier1 = get_intent_classifier()
reset_intent_classifier()
classifier2 = get_intent_classifier()
assert classifier1 is not classifier2
class TestIntentClassifierEdgeCases:
# Tests for edge cases and confidence thresholds
@pytest.fixture
def classifier(self):
with patch("src.agent.intent_classifier.Groq") as mock_groq_class:
mock_client = Mock()
mock_groq_class.return_value = mock_client
classifier = IntentClassifier(api_key="test_key", confidence_threshold=0.7)
classifier._client = mock_client
return classifier
def test_classify_empty_query(self, classifier):
# Empty query should return AMBIGUOUS with requires_clarification
result = classifier.classify("")
assert result.intent == IntentType.AMBIGUOUS
assert result.requires_clarification is True
assert result.confidence == 1.0
def test_classify_whitespace_only_query(self, classifier):
result = classifier.classify(" ")
assert result.intent == IntentType.AMBIGUOUS
assert result.requires_clarification is True
def test_classify_with_available_tables(self, classifier):
# Verify available_tables is included in prompt
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "DATABASE_QUERY", "confidence": 0.9, "reasoning": "Tables hint", "suggested_tables": ["CreditFileVersion"]}'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify(
"Kac kredi dosyasi var?",
available_tables=["CreditFileVersion", "CreditFileElement", "Contract"],
)
assert result.intent == IntentType.DATABASE_QUERY
# Verify the LLM was called with tables context
call_args = classifier._client.chat.completions.create.call_args
user_msg = call_args[1]["messages"][1]["content"]
assert "CreditFileVersion" in user_msg
def test_low_confidence_sets_requires_clarification(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "DATABASE_QUERY", "confidence": 0.4, "reasoning": "Not sure"}'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("bilgi ver")
assert result.confidence == 0.4
assert result.requires_clarification is True
def test_high_confidence_no_clarification(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "DOCUMENT_QUERY", "confidence": 0.95, "reasoning": "Clear question"}'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("Atlas ERP nedir?")
assert result.confidence == 0.95
assert result.requires_clarification is False
def test_classify_json_with_code_fence(self, classifier):
# Test that JSON wrapped in code fences is parsed correctly
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='```json\n{"intent": "CONVERSATIONAL", "confidence": 0.9, "reasoning": "Greeting"}\n```'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("Merhaba!")
assert result.intent == IntentType.CONVERSATIONAL
def test_classify_batch(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "DOCUMENT_QUERY", "confidence": 0.8, "reasoning": "test"}'))
]
classifier._client.chat.completions.create.return_value = mock_response
results = classifier.classify_batch(["q1", "q2", "q3"])
assert len(results) == 3
assert all(isinstance(r, ClassificationResult) for r in results)
def test_classification_time_recorded(self, classifier):
mock_response = Mock()
mock_response.choices = [
Mock(message=Mock(content='{"intent": "DOCUMENT_QUERY", "confidence": 0.8, "reasoning": "test"}'))
]
classifier._client.chat.completions.create.return_value = mock_response
result = classifier.classify("test query")
assert result.classification_time_ms >= 0
|