Update error
Browse files
error
CHANGED
|
@@ -1,184 +1,490 @@
|
|
| 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 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
===
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import pickle
|
| 3 |
+
import faiss
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from threading import Thread
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# ==================================================
|
| 12 |
+
# CONFIG
|
| 13 |
+
# ==================================================
|
| 14 |
+
|
| 15 |
+
INDEX_PATH = "hbl_vector_store/hbl_faiss.index"
|
| 16 |
+
META_PATH = "hbl_vector_store/hbl_metadata.pkl"
|
| 17 |
+
EMBED_MODEL = "./bge-m3"
|
| 18 |
+
LLM_MODEL_PATH = "./qwen2.5-0.5b-instruct" # point this at your local download folder
|
| 19 |
+
TOP_K = 4
|
| 20 |
+
MIN_SCORE = 0.58
|
| 21 |
+
HISTORY_TURNS = 3 # number of past user/assistant exchanges kept for classifier context
|
| 22 |
+
MAX_NEW_TOKENS = 512
|
| 23 |
+
CURRENT_YEAR = 2026 # used for recency weighting, update yearly (or compute dynamically)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# ==================================================
|
| 27 |
+
# LOAD VECTOR DATABASE
|
| 28 |
+
# ==================================================
|
| 29 |
+
|
| 30 |
+
print("Loading FAISS index...")
|
| 31 |
+
index = faiss.read_index(INDEX_PATH)
|
| 32 |
+
|
| 33 |
+
with open(META_PATH, "rb") as f:
|
| 34 |
+
chunks = pickle.load(f)
|
| 35 |
+
|
| 36 |
+
print("Chunks loaded:", len(chunks))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ==================================================
|
| 40 |
+
# LOAD BGE-M3
|
| 41 |
+
# ==================================================
|
| 42 |
+
|
| 43 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 44 |
+
|
| 45 |
+
print("Loading embedding model...")
|
| 46 |
+
embedder = SentenceTransformer(EMBED_MODEL, device=device)
|
| 47 |
+
embedder.max_seq_length = 1024
|
| 48 |
+
|
| 49 |
+
print("Embedding device:", device)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# ==================================================
|
| 53 |
+
# LOAD LOCAL LLM (Qwen2.5-0.5B-Instruct)
|
| 54 |
+
# ==================================================
|
| 55 |
+
|
| 56 |
+
llm_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 57 |
+
|
| 58 |
+
print("Loading LLM tokenizer...")
|
| 59 |
+
llm_tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_PATH)
|
| 60 |
+
|
| 61 |
+
print("Loading LLM model...")
|
| 62 |
+
llm_model = AutoModelForCausalLM.from_pretrained(LLM_MODEL_PATH, torch_dtype=llm_dtype)
|
| 63 |
+
llm_model.to(device)
|
| 64 |
+
llm_model.eval()
|
| 65 |
+
|
| 66 |
+
print("LLM device:", device)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# ==================================================
|
| 70 |
+
# LOCAL LLM CHAT (replaces the Ollama HTTP call)
|
| 71 |
+
# ==================================================
|
| 72 |
+
|
| 73 |
+
def local_chat(messages, temperature=0, stream=False, max_new_tokens=MAX_NEW_TOKENS):
|
| 74 |
+
prompt = llm_tokenizer.apply_chat_template(
|
| 75 |
+
messages,
|
| 76 |
+
tokenize=False,
|
| 77 |
+
add_generation_prompt=True,
|
| 78 |
+
)
|
| 79 |
+
inputs = llm_tokenizer(prompt, return_tensors="pt").to(device)
|
| 80 |
+
|
| 81 |
+
do_sample = temperature > 0
|
| 82 |
+
|
| 83 |
+
gen_kwargs = dict(
|
| 84 |
+
**inputs,
|
| 85 |
+
max_new_tokens=max_new_tokens,
|
| 86 |
+
do_sample=do_sample,
|
| 87 |
+
pad_token_id=llm_tokenizer.eos_token_id,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if do_sample:
|
| 91 |
+
gen_kwargs["temperature"] = temperature
|
| 92 |
+
gen_kwargs["top_p"] = 0.9
|
| 93 |
+
gen_kwargs["repetition_penalty"] = 1.1
|
| 94 |
+
|
| 95 |
+
if not stream:
|
| 96 |
+
with torch.no_grad():
|
| 97 |
+
output_ids = llm_model.generate(**gen_kwargs)
|
| 98 |
+
new_tokens = output_ids[0][inputs["input_ids"].shape[1]:]
|
| 99 |
+
return llm_tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 100 |
+
|
| 101 |
+
streamer = TextIteratorStreamer(
|
| 102 |
+
llm_tokenizer,
|
| 103 |
+
skip_prompt=True,
|
| 104 |
+
skip_special_tokens=True,
|
| 105 |
+
)
|
| 106 |
+
gen_kwargs["streamer"] = streamer
|
| 107 |
+
|
| 108 |
+
thread = Thread(target=llm_model.generate, kwargs=gen_kwargs)
|
| 109 |
+
thread.start()
|
| 110 |
+
|
| 111 |
+
full_text = ""
|
| 112 |
+
for token in streamer:
|
| 113 |
+
print(token, end="", flush=True)
|
| 114 |
+
full_text += token
|
| 115 |
+
|
| 116 |
+
thread.join()
|
| 117 |
+
print()
|
| 118 |
+
return full_text
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# ==================================================
|
| 122 |
+
# BANK ENTITY CHECK (word-boundary matching, not naive substring)
|
| 123 |
+
# ==================================================
|
| 124 |
+
|
| 125 |
+
HBL_TERMS = [
|
| 126 |
+
"hbl",
|
| 127 |
+
"habib bank limited",
|
| 128 |
+
"habib bank ltd",
|
| 129 |
+
"habib bank",
|
| 130 |
+
"habib islamic",
|
| 131 |
+
]
|
| 132 |
+
|
| 133 |
+
OTHER_BANK_TERMS = [
|
| 134 |
+
"meezan bank",
|
| 135 |
+
"meezan",
|
| 136 |
+
"ubl",
|
| 137 |
+
"united bank limited",
|
| 138 |
+
"united bank",
|
| 139 |
+
"mcb bank",
|
| 140 |
+
"mcb",
|
| 141 |
+
"bank alfalah",
|
| 142 |
+
"faysal bank",
|
| 143 |
+
"askari bank",
|
| 144 |
+
"allied bank limited",
|
| 145 |
+
"allied bank",
|
| 146 |
+
"abl",
|
| 147 |
+
"bank al habib",
|
| 148 |
+
"al habib",
|
| 149 |
+
"js bank",
|
| 150 |
+
"silkbank",
|
| 151 |
+
"standard chartered",
|
| 152 |
+
"soneri bank",
|
| 153 |
+
]
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def _has_term(text, term):
|
| 157 |
+
pattern = r"(?<!\w)" + re.escape(term) + r"(?!\w)"
|
| 158 |
+
return re.search(pattern, text) is not None
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def detect_bank_entity(query):
|
| 162 |
+
q = query.lower()
|
| 163 |
+
|
| 164 |
+
hbl_hit = any(_has_term(q, t) for t in HBL_TERMS)
|
| 165 |
+
other_hit = any(_has_term(q, t) for t in OTHER_BANK_TERMS)
|
| 166 |
+
|
| 167 |
+
if hbl_hit and other_hit:
|
| 168 |
+
return "OTHER_BANK"
|
| 169 |
+
|
| 170 |
+
if other_hit:
|
| 171 |
+
return "OTHER_BANK"
|
| 172 |
+
|
| 173 |
+
if hbl_hit:
|
| 174 |
+
return "HBL"
|
| 175 |
+
|
| 176 |
+
return "UNKNOWN"
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
# ==================================================
|
| 180 |
+
# EXECUTIVE ROLE EVIDENCE CHECK
|
| 181 |
+
# ==================================================
|
| 182 |
+
# Fixes the "who is the CTO of HBL" hallucination: the LLM was inferring
|
| 183 |
+
# a title from a list of executives even when that exact title never
|
| 184 |
+
# appeared in the retrieved documents. This checks that the requested
|
| 185 |
+
# title actually exists in the retrieved text BEFORE the LLM is called
|
| 186 |
+
# at all, and skips generation entirely if it doesn't.
|
| 187 |
+
#
|
| 188 |
+
# Extend this dict if the corpus has other exec titles (Chief Compliance
|
| 189 |
+
# Officer, Chief Internal Auditor, etc.) that queries might ask about --
|
| 190 |
+
# and tune "director" if it's too broad for your query patterns.
|
| 191 |
+
|
| 192 |
+
EXECUTIVE_ROLE_SYNONYMS = {
|
| 193 |
+
"ceo": ["ceo", "chief executive officer", "president & ceo", "president and ceo", "president"],
|
| 194 |
+
"cto": ["cto", "chief technology officer"],
|
| 195 |
+
"cfo": ["cfo", "chief financial officer"],
|
| 196 |
+
"coo": ["coo", "chief operating officer"],
|
| 197 |
+
"cro": ["cro", "chief risk officer"],
|
| 198 |
+
"chairman": ["chairman", "chairperson"],
|
| 199 |
+
"company secretary": ["company secretary"],
|
| 200 |
+
"director": ["director", "board of directors"],
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
MANAGEMENT_SECTION_MARKERS = [
|
| 204 |
+
"corporate information",
|
| 205 |
+
"board of directors",
|
| 206 |
+
"management",
|
| 207 |
+
"leadership",
|
| 208 |
+
"organization",
|
| 209 |
+
"organisation",
|
| 210 |
+
"executive",
|
| 211 |
+
]
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def extract_requested_roles(query):
|
| 215 |
+
q = query.lower()
|
| 216 |
+
matched = []
|
| 217 |
+
for role, synonyms in EXECUTIVE_ROLE_SYNONYMS.items():
|
| 218 |
+
if any(_has_term(q, syn) for syn in synonyms):
|
| 219 |
+
matched.append(role)
|
| 220 |
+
return matched
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def _evidence_supports_roles(roles, docs):
|
| 224 |
+
combined = " ".join(d["text"] for d in docs).lower()
|
| 225 |
+
return any(
|
| 226 |
+
any(_has_term(combined, syn) for syn in EXECUTIVE_ROLE_SYNONYMS[role])
|
| 227 |
+
for role in roles
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
def _is_management_chunk(text):
|
| 232 |
+
t = text.lower()
|
| 233 |
+
return any(marker in t for marker in MANAGEMENT_SECTION_MARKERS)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
# ==================================================
|
| 237 |
+
# LLM SECURITY ROUTER
|
| 238 |
+
# ==================================================
|
| 239 |
+
|
| 240 |
+
KNOWN_CATEGORIES = [
|
| 241 |
+
"HBL_RELATED",
|
| 242 |
+
"OTHER_BANK",
|
| 243 |
+
"WRITING_REQUEST",
|
| 244 |
+
"CODING",
|
| 245 |
+
"MATH",
|
| 246 |
+
"MEDICAL",
|
| 247 |
+
"AI_TECH",
|
| 248 |
+
"GENERAL",
|
| 249 |
+
]
|
| 250 |
+
|
| 251 |
+
HARD_BLOCK_CATEGORIES = {
|
| 252 |
+
"OTHER_BANK",
|
| 253 |
+
"CODING",
|
| 254 |
+
"MATH",
|
| 255 |
+
"MEDICAL",
|
| 256 |
+
"AI_TECH",
|
| 257 |
+
"WRITING_REQUEST",
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def classify_query(query, history=None):
|
| 262 |
+
context_block = ""
|
| 263 |
+
if history:
|
| 264 |
+
lines = [f"{role}: {text}" for role, text in history]
|
| 265 |
+
context_block = (
|
| 266 |
+
"\nRecent conversation (context only, do not classify this):\n"
|
| 267 |
+
+ "\n".join(lines)
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
prompt = f"""
|
| 271 |
+
You are the security classifier for Habib Bank Limited AI Assistant.
|
| 272 |
+
Classify the CURRENT query into exactly ONE category.
|
| 273 |
+
Return only the category name, nothing else.
|
| 274 |
+
Categories:
|
| 275 |
+
HBL_RELATED:
|
| 276 |
+
Anything directly or indirectly related to Habib Bank Limited, its
|
| 277 |
+
products, accounts, policies, or general banking services a customer
|
| 278 |
+
might ask a bank about (even without saying "HBL" explicitly).
|
| 279 |
+
OTHER_BANK:
|
| 280 |
+
Query is about a different bank ONLY, with no HBL connection.
|
| 281 |
+
WRITING_REQUEST:
|
| 282 |
+
Generic writing tasks unrelated to HBL (emails, CVs, letters, essays).
|
| 283 |
+
CODING:
|
| 284 |
+
Programming/software questions.
|
| 285 |
+
MATH:
|
| 286 |
+
Calculations, formulas, numerical problems unrelated to banking.
|
| 287 |
+
MEDICAL:
|
| 288 |
+
Health, diseases, medicines.
|
| 289 |
+
AI_TECH:
|
| 290 |
+
AI, models, LLMs, architecture.
|
| 291 |
+
GENERAL:
|
| 292 |
+
Ambiguous, or everything else.
|
| 293 |
+
{context_block}
|
| 294 |
+
Current query:
|
| 295 |
+
{query}
|
| 296 |
+
Return only the category name.
|
| 297 |
+
"""
|
| 298 |
+
|
| 299 |
+
raw = local_chat(
|
| 300 |
+
[{"role": "user", "content": prompt}],
|
| 301 |
+
temperature=0,
|
| 302 |
+
stream=False,
|
| 303 |
+
max_new_tokens=16,
|
| 304 |
+
)
|
| 305 |
+
result = raw.strip().upper()
|
| 306 |
+
|
| 307 |
+
for cat in KNOWN_CATEGORIES:
|
| 308 |
+
if cat in result:
|
| 309 |
+
return cat
|
| 310 |
+
|
| 311 |
+
return "GENERAL"
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
# ==================================================
|
| 315 |
+
# RETRIEVAL
|
| 316 |
+
# ==================================================
|
| 317 |
+
# Executive-role queries get a wider initial candidate pool (more likely
|
| 318 |
+
# to surface the one chunk that actually names the role), then results
|
| 319 |
+
# are re-ranked: management/corporate-info chunks first, similarity + a
|
| 320 |
+
# recency boost second. retrieval_score / final_score are attached to
|
| 321 |
+
# every returned chunk for debugging (Change 5).
|
| 322 |
+
|
| 323 |
+
def _extract_year(doc):
|
| 324 |
+
# Expects a "year" (or "report_year") field in chunk metadata, added
|
| 325 |
+
# during ingestion from the source annual report filename/content.
|
| 326 |
+
# If your metadata doesn't have this yet, recency scoring silently
|
| 327 |
+
# no-ops below -- add the field at ingestion time to activate it.
|
| 328 |
+
return doc.get("year") or doc.get("report_year")
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def _recency_score(year):
|
| 332 |
+
if year is None:
|
| 333 |
+
return 0.0
|
| 334 |
+
try:
|
| 335 |
+
age = max(0, CURRENT_YEAR - int(year))
|
| 336 |
+
except (TypeError, ValueError):
|
| 337 |
+
return 0.0
|
| 338 |
+
return max(0.0, 1.0 - (age * 0.15))
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def _final_score(doc):
|
| 342 |
+
similarity = doc["retrieval_score"]
|
| 343 |
+
year = _extract_year(doc)
|
| 344 |
+
|
| 345 |
+
if year is None:
|
| 346 |
+
# No recency metadata available -> fall back to pure similarity
|
| 347 |
+
return similarity
|
| 348 |
+
|
| 349 |
+
recency = _recency_score(year)
|
| 350 |
+
return similarity * 0.8 + recency * 0.2
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
def retrieve(query):
|
| 354 |
+
is_exec_query = bool(extract_requested_roles(query))
|
| 355 |
+
search_k = TOP_K * 3 if is_exec_query else TOP_K
|
| 356 |
+
|
| 357 |
+
vector = embedder.encode([query], normalize_embeddings=True)
|
| 358 |
+
vector = vector.astype("float32")
|
| 359 |
+
|
| 360 |
+
scores, ids = index.search(vector, search_k)
|
| 361 |
+
|
| 362 |
+
candidates = []
|
| 363 |
+
for score, idx in zip(scores[0], ids[0]):
|
| 364 |
+
if idx == -1:
|
| 365 |
+
continue
|
| 366 |
+
if score < MIN_SCORE:
|
| 367 |
+
continue
|
| 368 |
+
|
| 369 |
+
doc = chunks[idx].copy()
|
| 370 |
+
doc["retrieval_score"] = float(score)
|
| 371 |
+
doc["final_score"] = _final_score(doc)
|
| 372 |
+
candidates.append(doc)
|
| 373 |
+
|
| 374 |
+
if is_exec_query:
|
| 375 |
+
candidates.sort(
|
| 376 |
+
key=lambda d: (_is_management_chunk(d["text"]), d["final_score"]),
|
| 377 |
+
reverse=True,
|
| 378 |
+
)
|
| 379 |
+
else:
|
| 380 |
+
candidates.sort(key=lambda d: d["final_score"], reverse=True)
|
| 381 |
+
|
| 382 |
+
return candidates[:TOP_K]
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
# ==================================================
|
| 386 |
+
# GENERATION
|
| 387 |
+
# ==================================================
|
| 388 |
+
|
| 389 |
+
SYSTEM_PROMPT = """
|
| 390 |
+
You are Habib Bank Limited Assistant.
|
| 391 |
+
|
| 392 |
+
Answer only using facts explicitly stated in the provided HBL documents.
|
| 393 |
+
Do not infer job titles, names, relationships, or roles.
|
| 394 |
+
Do not use outside knowledge or guess.
|
| 395 |
+
|
| 396 |
+
If the requested information is not explicitly mentioned in the documents, say:
|
| 397 |
+
"I could not find this information in the available HBL documents."
|
| 398 |
+
|
| 399 |
+
Do not discuss topics unrelated to HBL.
|
| 400 |
+
"""
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
def generate_answer(query, docs):
|
| 404 |
+
context = "\n\n".join(d["text"] for d in docs)
|
| 405 |
+
|
| 406 |
+
return local_chat(
|
| 407 |
+
[
|
| 408 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 409 |
+
{
|
| 410 |
+
"role": "user",
|
| 411 |
+
"content": f"""
|
| 412 |
+
HBL DOCUMENTS:
|
| 413 |
+
{context}
|
| 414 |
+
QUESTION:
|
| 415 |
+
{query}
|
| 416 |
+
""",
|
| 417 |
+
},
|
| 418 |
+
],
|
| 419 |
+
temperature=0.1,
|
| 420 |
+
stream=True,
|
| 421 |
+
)
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
# ==================================================
|
| 425 |
+
# CHAT
|
| 426 |
+
# ==================================================
|
| 427 |
+
|
| 428 |
+
print("\n" + "=" * 60)
|
| 429 |
+
print("HBL Assistant Ready")
|
| 430 |
+
print("Type exit to quit")
|
| 431 |
+
print("=" * 60)
|
| 432 |
+
|
| 433 |
+
history = [] # list of (role, text) tuples, most recent last
|
| 434 |
+
|
| 435 |
+
while True:
|
| 436 |
+
query = input("\nUser: ")
|
| 437 |
+
|
| 438 |
+
if query.lower() == "exit":
|
| 439 |
+
break
|
| 440 |
+
|
| 441 |
+
if not query.strip():
|
| 442 |
+
continue
|
| 443 |
+
|
| 444 |
+
# ----------------------------------------------
|
| 445 |
+
# ENTITY CHECK FIRST (cheap, catches obvious cases)
|
| 446 |
+
# ----------------------------------------------
|
| 447 |
+
entity = detect_bank_entity(query)
|
| 448 |
+
|
| 449 |
+
if entity == "OTHER_BANK":
|
| 450 |
+
print("\nAssistant:\nI can only assist with Habib Bank Limited related queries.")
|
| 451 |
+
continue
|
| 452 |
+
|
| 453 |
+
if entity == "HBL":
|
| 454 |
+
intent = "HBL_RELATED"
|
| 455 |
+
else:
|
| 456 |
+
intent = classify_query(query, history)
|
| 457 |
+
|
| 458 |
+
print("Intent:", intent)
|
| 459 |
+
|
| 460 |
+
if intent in HARD_BLOCK_CATEGORIES:
|
| 461 |
+
print("\nAssistant:\nI can only assist with Habib Bank Limited related queries.")
|
| 462 |
+
continue
|
| 463 |
+
|
| 464 |
+
# ----------------------------------------------
|
| 465 |
+
# RETRIEVE (this is the real relevance gate)
|
| 466 |
+
# ----------------------------------------------
|
| 467 |
+
docs = retrieve(query)
|
| 468 |
+
|
| 469 |
+
if len(docs) == 0:
|
| 470 |
+
print("\nAssistant:\nI could not find this information in the available HBL documents.")
|
| 471 |
+
continue
|
| 472 |
+
|
| 473 |
+
# ----------------------------------------------
|
| 474 |
+
# EVIDENCE CHECK (executive-role queries only)
|
| 475 |
+
# ----------------------------------------------
|
| 476 |
+
requested_roles = extract_requested_roles(query)
|
| 477 |
+
|
| 478 |
+
if requested_roles and not _evidence_supports_roles(requested_roles, docs):
|
| 479 |
+
print("\nAssistant:\nI could not find this information in the available HBL documents.")
|
| 480 |
+
continue
|
| 481 |
+
|
| 482 |
+
# ----------------------------------------------
|
| 483 |
+
# GENERATE
|
| 484 |
+
# ----------------------------------------------
|
| 485 |
+
print("\nAssistant:")
|
| 486 |
+
answer = generate_answer(query, docs)
|
| 487 |
+
|
| 488 |
+
history.append(("user", query))
|
| 489 |
+
history.append(("assistant", answer))
|
| 490 |
+
history = history[-HISTORY_TURNS * 2:]
|