Spaces:
Sleeping
Sleeping
File size: 3,858 Bytes
401b16c | 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 | #!/usr/bin/env python3
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
from intent_classifier import IntentClassifier, IntentType
def test_intent_classification():
print("π§ͺ Testing OpenAI Intent Classification")
print("="*60)
print("Note: Make sure to set OPENAI_API_KEY environment variable")
print("="*60)
classifier = IntentClassifier()
# Test cases with expected intents
test_cases = [
# Transaction intents
("Add a purchase of 20 USB drives from TechMart at β¬5 each", IntentType.TRANSACTION),
("Sold 10 laptops to John Smith at β¬800 each", IntentType.TRANSACTION),
("Purchase 5 office chairs from Office Supplies Co at β¬150 per chair", IntentType.TRANSACTION),
("We bought 100 pens from Staples for $2 each", IntentType.TRANSACTION),
# Query intents
("How many USB drives did we purchase?", IntentType.QUERY),
("What's the total value of all purchases?", IntentType.QUERY),
("Show me all sales to John Smith", IntentType.QUERY),
("List recent transactions", IntentType.QUERY),
("What's our total spending on electronics?", IntentType.QUERY),
# Semantic search intents
("Show me similar purchases to this one", IntentType.SEMANTIC_SEARCH),
("Find events related to supplier meetings", IntentType.SEMANTIC_SEARCH),
("What's similar to our last laptop purchase?", IntentType.SEMANTIC_SEARCH),
("Show me related transactions", IntentType.SEMANTIC_SEARCH),
# General info intents
("Meeting with new supplier scheduled for next week", IntentType.GENERAL_INFO),
("Remember to check inventory levels before next order", IntentType.GENERAL_INFO),
("The conference call went well today", IntentType.GENERAL_INFO),
("Don't forget to update the quarterly report", IntentType.GENERAL_INFO),
# Edge cases
("Hello", IntentType.GENERAL_INFO),
("What's the weather like?", IntentType.GENERAL_INFO),
("Can you help me?", IntentType.GENERAL_INFO),
]
correct_predictions = 0
total_predictions = len(test_cases)
for i, (message, expected_intent) in enumerate(test_cases, 1):
print(f"\nπ Test {i}: {message}")
print("-" * 60)
result = classifier.classify_intent(message)
print(f"Expected: {expected_intent.value}")
print(f"Predicted: {result.intent.value}")
print(f"Confidence: {result.confidence:.2f}")
print(f"Reasoning: {result.reasoning}")
if result.entities_hint:
print(f"Entities: {result.entities_hint}")
is_correct = result.intent == expected_intent
if is_correct:
print("β
CORRECT")
correct_predictions += 1
else:
print("β INCORRECT")
print()
# Summary
accuracy = correct_predictions / total_predictions
print("="*60)
print(f"π Results Summary:")
print(f"Correct predictions: {correct_predictions}/{total_predictions}")
print(f"Accuracy: {accuracy:.2%}")
print("="*60)
if accuracy >= 0.8:
print("π Excellent accuracy! Intent classification is working well.")
elif accuracy >= 0.6:
print("π Good accuracy. Consider refining prompts for better results.")
else:
print("β οΈ Low accuracy. Review and improve the classification prompts.")
if __name__ == "__main__":
if not os.getenv('OPENAI_API_KEY'):
print("β Error: OPENAI_API_KEY environment variable not set")
print("Please set your OpenAI API key:")
print("export OPENAI_API_KEY='your-key-here'")
sys.exit(1)
test_intent_classification() |