File size: 9,655 Bytes
17131ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import re
from config import Config
from modules.domain_knowledge.vector_store import VectorStore
from sentence_transformers import SentenceTransformer

class RREngine:
    def __init__(self):
        print("[RRE] Initializing Reasoning Engine (Multi-Location Support)...")
        self.vdb = VectorStore(Config.CHROMA_DB_DIR, Config.COLLECTION_NAME)
        self.model = SentenceTransformer(Config.EMBED_MODEL_NAME, device=Config.DEVICE)
        print("[RRE] Ready.")

    def run_pipeline_chatrec(self, state: dict) -> dict:
        query_text = state.get("user_text", "")
        # constraints = state.get("collected_constraints", {})
        
        if not query_text:
            state["candidates"] = []
            state["candidate_count"] = 0
            return state

        # --- STEP 1: RETRIEVAL ---
        formatted_query = f"query: {query_text}"
        query_embedding = self.model.encode([formatted_query], convert_to_numpy=True, normalize_embeddings=True)[0]
        
        # Ambil 150 kandidat
        raw_docs, raw_metas, raw_dists = self.vdb.search(query_embedding, n_results=150)
        
        candidates = []
        for meta in raw_metas:
            full_data = {}
            if "original_json" in meta:
                try: full_data = json.loads(meta["original_json"])
                except: full_data = {}

            if not full_data.get("title"):
                full_data["title"] = meta.get("title", "Unknown Product")
                full_data["price"] = meta.get("price", "0")
                full_data["product_id"] = meta.get("product_id")
                full_data["supplier_detail"] = {
                    "nama_toko": meta.get("supplier") or meta.get("seller", "Unknown Store"),
                    "kota": meta.get("location", ""),
                    "provinsi": ""
                }
                full_data["category_detail"] = {
                    "category": meta.get("category", ""),
                    "sub_category": ""
                }
            candidates.append(full_data)

        state["candidates"] = candidates
        state["candidate_count"] = len(candidates)
        # print(candidates)
        
        return state
    
    def run_pipeline(self, state: dict) -> dict:
        query_text = state.get("relaxed_query", "")
        constraints = state.get("collected_constraints", {})
        
        if not query_text:
            state["candidates"] = []
            state["candidate_count"] = 0
            return state

        # --- STEP 1: RETRIEVAL ---
        formatted_query = f"{query_text}"
        print(formatted_query)
        query_embedding = self.model.encode([formatted_query], convert_to_numpy=True, normalize_embeddings=True)[0]
        
        # Ambil 150 kandidat
        raw_docs, raw_metas, raw_dists = self.vdb.search(query_embedding, n_results=150)
        
        candidates = []
        for meta in raw_metas:
            full_data = {}
            if "original_json" in meta:
                try: full_data = json.loads(meta["original_json"])
                except: full_data = {}

            if not full_data.get("title"):
                full_data["title"] = meta.get("title", "Unknown Product")
                full_data["price"] = meta.get("price", "0")
                full_data["product_id"] = meta.get("product_id")
                full_data["supplier_detail"] = {
                    "nama_toko": meta.get("supplier") or meta.get("seller", "Unknown Store"),
                    "kota": meta.get("location", ""),
                    "provinsi": ""
                }
                full_data["category_detail"] = {
                    "category": meta.get("category", ""),
                    "sub_category": ""
                }
            candidates.append(full_data)

        # --- STEP 2: HARD FILTERING ---
        filtered_candidates = self._apply_hard_filters(candidates, constraints)
        
        # --- STEP 3: ONTOLOGY VALIDATION ---
        validated_candidates = self._validate_ontology(filtered_candidates)
        
        state["candidates"] = validated_candidates
        state["candidate_count"] = len(validated_candidates)

        # print(validated_candidates)
        
        return state

    def _apply_hard_filters(self, items, constraints):
        results = []
        
        # --- 1. HANDLING MULTI-LOCATION (List or String) ---
        raw_loc = constraints.get("location")
        target_locs = [] # Kita standarisasi jadi List of Strings
        
        if isinstance(raw_loc, list):
            target_locs = [str(l).lower().strip() for l in raw_loc if l]
        elif isinstance(raw_loc, str) and raw_loc:
            target_locs = [raw_loc.lower().strip()]
            
        raw_supplier = constraints.get("supplier", "")
        if isinstance(raw_supplier, list):
            raw_supplier = raw_supplier[0] if raw_supplier else ""
        
        target_supplier = raw_supplier.lower().strip() if raw_supplier else ""
        raw_specs = constraints.get("specs", "")
        if isinstance(raw_specs, list):
            # Menggabungkan ['ram 8gb', 'ssd 256'] menjadi "ram 8gb ssd 256"
            target_specs = " ".join(str(s) for s in raw_specs).lower().strip()
        else:
            target_specs = str(raw_specs).lower().strip() if raw_specs else ""
        
        # --- 2. HANDLING PRICE ---
        price_constraint = constraints.get("price")
        target_price_val = 0
        price_operator = None
        
        if isinstance(price_constraint, dict):
            raw_val = price_constraint.get("value")
            target_price_val = int(raw_val) if raw_val is not None else 0
            price_operator = price_constraint.get("operator", "=")

        for item in items:
            keep = True
            
            title = item.get("title", "").lower()
            sup_detail = item.get("supplier_detail", {})
            
            s_kota = str(sup_detail.get('kota', '')).lower()
            s_prov = str(sup_detail.get('provinsi', '')).lower()
            s_toko = str(sup_detail.get('nama_toko', '')).lower()
            s_seller = str(item.get('seller', '')).lower()
            
            item_loc = f"{s_kota} {s_prov}"
            item_supplier_name = f"{s_seller} {s_toko}"
            
            # --- A. FILTER LOCATION (MULTI-MATCH SUPPORT) ---
            if target_locs:
                # Logika: Jika user minta ['banten', 'buleleng'], 
                # maka produk dianggap lolos jika lokasinya mengandung 'banten' ATAU 'buleleng'.
                is_loc_match = False
                
                for t_loc in target_locs:
                    clean_target = t_loc.replace("kab.", "").replace("kota", "").strip()
                    target_tokens = clean_target.split()
                    
                    # Cek 1: Exact substring match (relaxed)
                    if clean_target in item_loc:
                        is_loc_match = True
                        break
                    
                    # Cek 2: Token match (untuk 'jakarta timur' vs 'jakarta')
                    token_match = False
                    for token in target_tokens:
                        if len(token) > 2 and token in item_loc:
                            token_match = True
                            break
                    
                    if token_match:
                        is_loc_match = True
                        break
                
                if not is_loc_match:
                    keep = False

            # --- B. FILTER SUPPLIER ---
            if keep and target_supplier and target_supplier not in item_supplier_name:
                keep = False

            # --- C. FILTER PRICE ---
            if keep and target_price_val > 0:
                item_price_raw = item.get("price", 0)
                item_price_val = self._parse_price(item_price_raw)
                
                if item_price_val > 0:
                    if price_operator == "<":
                        if item_price_val >= target_price_val: keep = False
                    elif price_operator == ">":
                        if item_price_val <= target_price_val: keep = False

            # --- D. FILTER SPECS ---
            if keep and target_specs:
                spec_keywords = target_specs.split()
                title_norm = re.sub(r"[^a-z0-9]", "", title)
                
                for kw in spec_keywords:
                    kw_clean = re.sub(r"[^a-z0-9]", "", kw)
                    if not kw_clean: continue
                    
                    if any(char.isdigit() for char in kw_clean):
                        if kw_clean not in title_norm:
                            keep = False
                            break 

            if keep:
                results.append(item)
                
        return results

    def _parse_price(self, price_input):
        try:
            if price_input is None: return 0
            if isinstance(price_input, (int, float)):
                return int(price_input)
            str_val = str(price_input).strip()
            str_val = re.sub(r"(Rp|IDR|\s)", "", str_val, flags=re.IGNORECASE)
            if "," in str_val: str_val = str_val.split(",")[0]
            str_val = str_val.replace(".", "")
            clean_str = re.sub(r"[^\d]", "", str_val)
            return int(clean_str) if clean_str else 0
        except:
            return 0

    def _validate_ontology(self, items):
        valid_items = []
        for item in items:
            if item.get("title"):
                valid_items.append(item)
        return valid_items