This repository was archived by the owner on Apr 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (73 loc) · 2.92 KB
/
Copy pathmain.py
File metadata and controls
97 lines (73 loc) · 2.92 KB
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
import os
import logging
from langchain.globals import set_llm_cache
from utils.logger import setup_logger
from chain.qa_chain import load_data, build_retriever, build_chat_model, build_prompt
from utils.filter_utils import is_relevant_question
from dotenv import load_dotenv
dotenv_path = os.path.join(os.path.dirname(__file__), ".env")
print("Loading .env from:", dotenv_path)
load_dotenv(dotenv_path)
retriever = None
chat_model = None
def chatbot_response(user_input: str) -> str:
global retriever, chat_model
if not is_relevant_question(user_input):
return (
"Maaf, saya hanya bisa membantu pertanyaan seputar layanan WiFi kami, seperti:\n"
"- Harga paket WiFi\n"
"- Cara pasang WiFi\n"
"- Menghubungi teknisi\n"
"- Masalah koneksi atau gangguan\n\n"
"Silakan ajukan pertanyaan sesuai layanan ya 😊"
)
if not retriever or not chat_model:
raise ValueError("Chatbot belum diinisialisasi. Jalankan initialize_chatbot() dulu.")
relevant_docs = retriever.invoke(user_input)
if not relevant_docs:
return (
"Maaf, aku belum menemukan jawaban untuk pertanyaan itu 😔.\n"
"Coba tanyakan dengan kata lain atau cek pertanyaan umum seperti:\n"
"- Berapa harga paket WiFi\n- Cara pasang WiFi\n- Cara menghubungi teknisi"
)
full_prompt = build_prompt(user_input, relevant_docs)
response_text = ""
for chunk in chat_model.stream(full_prompt):
if chunk.content:
response_text += chunk.content
return response_text.strip()
def initialize_chatbot():
global retriever, chat_model
set_llm_cache(None)
setup_logger()
logging.info("Memulai Chatbot WiFi...")
try:
DATA_DIR = os.getenv("DATA_DIR", "./data")
faq_data = load_data(os.path.join(DATA_DIR, "faq_data.json"))
retrieval_data = load_data(os.path.join(DATA_DIR, "retrieval_data.json"))
all_data = faq_data + retrieval_data
retriever = build_retriever(all_data)
chat_model = build_chat_model()
return retriever, chat_model
except Exception as e:
logging.error(f"❌ Error saat inisialisasi chatbot: {e}")
return None, None
def cli_chat():
retriever_, chat_model_ = initialize_chatbot()
if not retriever_ or not chat_model_:
print("❌ Gagal inisialisasi chatbot.")
return
print("🗨️ Chatbot WiFi - Ketik 'exit' untuk keluar.")
while True:
user_input = input("📝 Anda: ")
if user_input.lower() == "exit":
print("👋 Terima kasih telah menggunakan Chatbot WiFi!")
break
try:
response = chatbot_response(user_input)
print("🤖 Bot:", response)
except Exception as e:
logging.error(f"Error saat memproses pertanyaan: {e}")
print("❌ Error:", e)
if __name__ == "__main__":
cli_chat()