Download modules/reasoning_engine/engine.py from recommender-system/llmproccrs: direct link, hf CLI and curl.
- Browser
- Download file 9.66 kB
-
https://huggingface.co/recommender-system/llmproccrs/resolve/main/modules/reasoning_engine/engine.py
- Command line
-
hf download hf://recommender-system/llmproccrs/modules/reasoning_engine/engine.py
-
curl -L -o engine.py https://huggingface.co/recommender-system/llmproccrs/resolve/main/modules/reasoning_engine/engine.py
9.66 kB
| 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 |