File size: 3,534 Bytes
60b4e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import chromadb
from sentence_transformers import SentenceTransformer
from collections import defaultdict

# --- Configuration ---
COLLECTION_NAME = "intent_recognizer"
DB_PATH = "db"
EMBEDDING_MODEL = 'all-mpnet-base-v2'

# --- Tunable Parameters ---
N_RESULTS = 100
CONFIDENCE_THRESHOLD = 18.0


class IntentRecognizer:
    def __init__(self):
        print("--- Initializing Intent Recognizer ---")
        try:
            print(f"Loading embedding model: '{EMBEDDING_MODEL}'...")
            self.model = SentenceTransformer(EMBEDDING_MODEL)
            print(f"Connecting to persistent ChromaDB client at: '{DB_PATH}'...")
            client = chromadb.PersistentClient(path=DB_PATH)
            print(f"Getting collection: '{COLLECTION_NAME}'...")
            self.collection = client.get_collection(name=COLLECTION_NAME)
            print("--- Initialization Complete ---")
        except Exception as e:
            print(f"Error during initialization: {e}")
            self.model = None
            self.collection = None

    def recognize_intent(self, query_text):
        if not self.collection or not self.model:
            return [] # Return an empty list on error

        print(f"\n--- Recognizing Intent for Query: '{query_text}' ---")
        query_embedding = self.model.encode([query_text], show_progress_bar=False)
        results = self.collection.query(
            query_embeddings=query_embedding.tolist(),
            n_results=N_RESULTS,
        )
        
        intent_scores = defaultdict(float)
        retrieved_metadatas = results.get('metadatas', [[]])[0]
        retrieved_distances = results.get('distances', [[]])[0]

        if not retrieved_metadatas:
            return []

        print(f"Retrieved {len(retrieved_metadatas)} results. Calculating scores...")
        for metadata, distance in zip(retrieved_metadatas, retrieved_distances):
            intent = metadata.get('intent')
            if intent:
                similarity_score = 1 - distance
                intent_scores[intent] += similarity_score
        
        if not intent_scores:
            return []

        # --- CHANGE: Sort all intents by score and get the top 2 ---
        sorted_intents = sorted(intent_scores.items(), key=lambda item: item[1], reverse=True)
        
        # Format as a list of dictionaries
        top_predictions = [{"intent": intent, "score": score} for intent, score in sorted_intents[:2]]

        print("\n--- Scoring Summary (Top 2) ---")
        for pred in top_predictions:
            print(f"  - Intent: {pred['intent']:<25} | Total Score: {pred['score']:.4f}")
        
        # --- CHANGE: Apply threshold to the top prediction ---
        # If the best score is too low, return an 'unknown' intent.
        if not top_predictions or top_predictions[0]['score'] < CONFIDENCE_THRESHOLD:
            print(f"\nTop score is below threshold of {CONFIDENCE_THRESHOLD}.")
            return [{"intent": "unknown", "score": top_predictions[0]['score'] if top_predictions else 0.0}]

        print(f"\n=> Top Prediction: '{top_predictions[0]['intent']}' (Confidence: {top_predictions[0]['score']:.4f})")
        return top_predictions

# This part is no longer needed for running the API, but can be kept for testing
if __name__ == '__main__':
    recognizer = IntentRecognizer()
    if recognizer.collection:
        recognizer.recognize_intent("can you reduce the price on the house in islington")