llmproccrs / modules /user_modeling /extractor.py
aitek230telu's picture
Upload 40 files
17131ee verified
Raw History Blame Contribute Delete
3.92 kB
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