File size: 3,923 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
import json
from langchain_core.prompts import PromptTemplate
from config import Config

class EntityExtractor:
    def __init__(self):
        # Temperature 0 agar output konsisten
        self.llm = Config.get_llm(temperature=0.0)
        
        self.template = """
        Kamu adalah sistem NER (Named Entity Recognition) e-Procurement yang cerdas.
        Tugas: Ekstrak entitas dari input user ke dalam format JSON yang terstruktur secara ketat.

        Input User: "{user_text}"

        Panduan Ekstraksi:
        1. Product: Nama barang/jasa.
        2. Brand    : Merek (contoh: "ASUS", "Epson").
        3. Location: Kota/Provinsi.
        4. Supplier: Nama toko/PT/CV.
        5. Price: HARUS berupa objek dengan field:
           - "text": Teks asli (misal: "di bawah 10 juta")
           - "value": Angka numerik (integer), contoh: 10000000. Jika rentang, ambil batas atas.
           - "operator": Simbol matematika ("<" untuk kurang dari/murah, ">" untuk lebih dari, "=" untuk sekitar/pas).
           - jika tidak maka tidak perlu di ekstrak.
        6. Category: Kategori produk.
        7. Specs    : Spesifikasi teknis/detail. 
           - AMBIL kata kunci teknisnya saja.
           - Hapus kata sambung (yang, minimal, harus, dll).
           - Contoh Input: "yang ramnya minimal 8 GB" -> Output Specs: "RAM 8GB".
           - Contoh Input: "warna merah" -> Output Specs: "Merah".
        
        Aturan Output:
        - KEMBALIKAN HANYA JSON VALID.
        - Jangan ada teks pengantar.
        - Jika entitas tidak ditemukan, isi dengan null atau string kosong.
        - DILARANG memberikan komentar (// atau #) di dalam JSON.
        - DILARANG memberikan penjelasan tambahan di luar JSON.
        
        CONTOH OUTPUT YANG DIHARAPKAN (Strict JSON):
        {{
            "product": "Laptop",
            "brand": "Lenovo",
            "specs": "RAM 8GB Core i5",
            "location": "Bali",
            "price": {{
                "text": "di bawah 10 juta",
                "value": 10000000,
                "operator": "<"
            }},
            "supplier": "PT SFJ",
            "category": "Elektronik"
        }}
        """
        self.prompt = PromptTemplate(input_variables=["user_text"], template=self.template)
        
        # --- PERUBAHAN: MENGGUNAKAN LCEL (Pipe Syntax) ---
        # Ini menggantikan LLMChain
        self.chain = self.prompt | self.llm

    def run(self, state: dict) -> dict:
        user_text = state.get("user_text", "")
        current_constraints = state.get("collected_constraints", {})
        
        if not user_text:
            return state

        try:
            # --- PERUBAHAN: EXECUTION ---
            # invoke() mengembalikan object AIMessage, kita ambil .content nya
            response = self.chain.invoke({"user_text": user_text})
            raw_response = response.content.strip()
            
            # Cleaning JSON
            cleaned_json = raw_response.replace("```json", "").replace("```", "").strip()
            new_entities = json.loads(cleaned_json)
            
            # Merging Logic
            updated_constraints = current_constraints.copy()
            extracted_this_turn = [] 
            
            for key, value in new_entities.items():
                if value and str(value).lower() != "null":
                    updated_constraints[key] = value
                    extracted_this_turn.append({"type": key, "text": value})
            
            # Update State
            state["collected_constraints"] = updated_constraints
            state["entities"] = extracted_this_turn 
            
            # print(f"[UMS] Extracted: {extracted_this_turn}")
            
        except json.JSONDecodeError:
            print(f"[UMS] Error Parsing JSON: {raw_response}")
        except Exception as e:
            print(f"[UMS] General Error: {e}")
            
        return state