Download main.py from recommender-system/llmproccrs: direct link, hf CLI and curl.
- Browser
- Download file 4.48 kB
-
https://huggingface.co/recommender-system/llmproccrs/resolve/main/main.py
- Command line
-
hf download hf://recommender-system/llmproccrs/main.py
-
curl -L -o main.py https://huggingface.co/recommender-system/llmproccrs/resolve/main/main.py
4.48 kB
| import sys | |
| import os | |
| import traceback | |
| from langchain_core.prompts import PromptTemplate | |
| # --- IMPORT MODULES --- | |
| from config import Config | |
| from modules.dialog_manager.state import ChatState | |
| from modules.dialog_manager.workflow import build_graph | |
| from modules.dialog_manager.intent import IntentDetector | |
| def handle_chitchat(llm, user_text): | |
| """ | |
| Menangani percakapan ringan/basa-basi (Non-Transactional). | |
| """ | |
| template = """ | |
| Kamu adalah M2Proc, asisten pintar e-Procurement. | |
| User menyapa: "{text}" | |
| Tugas: | |
| 1. Jawab dengan ramah, sopan, dan profesional. | |
| 2. Tawarkan bantuan untuk mencari Barang, Jasa, atau Supplier. | |
| Jawab singkat saja. | |
| """ | |
| prompt = PromptTemplate(input_variables=["text"], template=template) | |
| chain = prompt | llm | |
| try: | |
| response = chain.invoke({"text": user_text}) | |
| return response.content.strip() | |
| except Exception as e: | |
| return "Halo! Ada yang bisa saya bantu untuk pencarian produk?" | |
| def main(): | |
| print("\n" + "="*60) | |
| print(" 🤖 M2Proc - Conversational Recommender System ") | |
| print(" Status: Production Mode (Real Data) ") | |
| print("="*60) | |
| # 1. INITIALIZATION | |
| print("⏳ Sedang memuat modul dan database... (Mohon tunggu)") | |
| try: | |
| # Load Graph (DMS + RRE + UMS) | |
| app_graph = build_graph() | |
| # Load Helper Components | |
| intent_detector = IntentDetector() | |
| # Load LLM khusus untuk basa-basi (temperature agak tinggi biar luwes) | |
| llm_chitchat = Config.get_llm(temperature=0.6) | |
| print("✅ Sistem Siap! Silakan ketik 'exit' untuk berhenti.\n") | |
| except Exception as e: | |
| print(f"❌ Gagal memuat sistem: {e}") | |
| traceback.print_exc() | |
| return | |
| # 2. STATE INITIALIZATION | |
| # State ini akan terus dibawa selama percakapan berlangsung | |
| current_state = ChatState( | |
| user_text="", | |
| histories=[], # Log percakapan | |
| intent="", | |
| topic_status="same", | |
| entities=[], | |
| collected_constraints={}, # Memori pencarian (Product, Price, dll) | |
| relaxed_query="", | |
| candidates=[], | |
| candidate_count=0, | |
| final_response="", | |
| done=False | |
| ) | |
| # 3. MAIN CONVERSATION LOOP | |
| while True: | |
| try: | |
| # Input User | |
| user_input = input("\nUser : ").strip() | |
| # Cek Exit | |
| if user_input.lower() in ["exit", "quit", "stop", "keluar"]: | |
| print("Bot : Terima kasih telah menggunakan M2Proc. Sampai jumpa!") | |
| break | |
| if not user_input: | |
| continue | |
| # Update State dengan Input Baru | |
| current_state["user_text"] = user_input | |
| # --- STEP A: DETEKSI INTENT --- | |
| intent = intent_detector.detect(current_state) | |
| current_state["intent"] = intent | |
| if intent == "basa_basi": | |
| # --- JALUR 1: BASA-BASI --- | |
| resp = handle_chitchat(llm_chitchat, user_input) | |
| print(f"Bot : {resp}") | |
| # Update History (Manual untuk jalur ini) | |
| current_state["histories"].append(f"User: {user_input}") | |
| current_state["histories"].append(f"Bot: {resp}") | |
| else: | |
| # --- JALUR 2: SEARCH / TRANSAKSI --- | |
| # Masukkan input user ke history dulu agar Graph punya konteks lengkap | |
| current_state["histories"].append(f"User: {user_input}") | |
| # JALANKAN GRAPH (Topic -> Extract -> Relax -> RRE -> Generator) | |
| current_state = app_graph.invoke(current_state) | |
| # Ambil respon dari node terakhir (Generator) | |
| bot_resp = current_state.get("final_response", "Maaf, sistem tidak dapat memproses permintaan.") | |
| print(f"Bot : {bot_resp}") | |
| # Note: History Bot sudah otomatis di-append oleh node generator di dalam graph | |
| except KeyboardInterrupt: | |
| print("\n\nBot : Force Stop. Bye!") | |
| break | |
| except Exception as e: | |
| print(f"\n❌ Terjadi Kesalahan Runtime: {e}") | |
| traceback.print_exc() | |
| # Reset user text agar tidak looping error | |
| current_state["user_text"] = "" | |
| if __name__ == "__main__": | |
| main() |