ogx786 commited on
Commit
7e9ff65
·
verified ·
1 Parent(s): 04c9fcd

Update error

Browse files
Files changed (1) hide show
  1. error +490 -184
error CHANGED
@@ -1,184 +1,490 @@
1
- ============================================================
2
- HBL Assistant Ready
3
- Type exit to quit
4
- ============================================================
5
-
6
- User: who is the CTO of hbl?
7
- Intent: HBL_RELATED
8
-
9
- Retrieved docs....
10
- ================================================================================
11
- Doc 1:
12
- Score: None
13
- /kaggle/input/datasets/muhammadezanraza/hbl-corpus-raw/pdfs/560d9019ef2740d46239095e8ed67f5c.pdf
14
- Corporate Information
15
- Board of Directors
16
- Mr. Sultan Ali Allana
17
- Chairman
18
- Mr. Shaffiq Dharamshi
19
- Director
20
- Mr. Moez Ahamed Jamal
21
- Director
22
- Mr. Salim Raza
23
- Director
24
- Dr. Najeeb Samie
25
- Director
26
- Mr. Khaleel Ahmed
27
- Director
28
- Ms. Saba Kamal
29
- Director
30
- Mr. Muhammad Aurangzeb
31
- President & CEO
32
- Chief Operating Officer
33
- Mr. Sagheer Mufti
34
- Chief Financial Officer
35
-
36
- Mr. Rayomond Kotwal
37
-
38
-
39
- Company Secretary
40
-
41
- Ms. Neelofar Hameed
42
-
43
-
44
- Legal Advisors
45
-
46
-
47
-
48
- Mandviwalla and Zafar
49
-
50
- Legal Consultants and Advocates
51
-
52
- Auditors
53
- KPMG Taseer Hadi & Co.
54
-
55
- Chartered Accountants
56
-
57
-
58
- * The Board was elected in the AGM held on March 26, 2021. The new term of the Board is effective from March 27, 2021 subject to FPT clearance by SBP.
59
- Share Registrar
60
- CDC Share Registrar Services Limited
61
- CDC House, 99 – B, Block ‘B’,
62
-
63
- S.M.
64
- ================================================================================
65
- Doc 2:
66
- Score: None
67
- /kaggle/input/datasets/muhammadezanraza/hbl-corpus-raw/pdfs/6aad91a37e634c47ec9a2316219dd53e.pdf
68
- Board of Directors
69
- Mr. Sultan Ali Allana
70
- Chairman
71
- Mr. Shaffiq Dharamshi
72
- Director
73
- Mr. Moez Ahamed Jamal
74
- Director
75
- Mr. Salim Raza
76
- Director
77
- Dr. Najeeb Samie
78
- Director
79
- Ms. Diane Elizabeth Moore
80
- Director
81
- Mr. Salim Yahya Chinoy
82
- Director
83
- Mr. Muhammad Aurangzeb
84
- President & CEO
85
- Chief Operating Officer
86
- Mr. Sagheer Mufti
87
- Chief Financial Officer
88
- Mr. Rayomond Kotwal
89
- Company Secretary
90
- Ms. Neelofar Hameed
91
- Legal Advisors
92
- Mandviwalla and Zafar
93
- Legal Consultants and Advocates
94
- Auditors
95
- A.F. Ferguson & Co.
96
- Chartered Accountants
97
- Share Registrar
98
- CDC Share Registrar Services Limited
99
- CDC House, 99 – B, Block ‘B’,
100
- S.M.C.H.S., Main Shahra-e-Faisal
101
- Karachi – 74400, Pakistan
102
- Tel: Customer Support Services
103
- (Toll Free) 0800-CDCPL (23275)
104
- Fax: (92-21) 34326053
105
- Email: info@cdcsrsl.com
106
- Website: www.cdcsrsl.com
107
- HBL Corpo
108
- ================================================================================
109
- Doc 3:
110
- Score: None
111
- /kaggle/input/datasets/muhammadezanraza/hbl-corpus-raw/pdfs/b11d15aaa17c3f5142eb68afd877dd6a.pdf
112
- 8. Mr. Rayomond Kotwal
113
- Acting President & CEO
114
- Mr. Rayomond Kotwal joined HBL as
115
- the Chief Financial Officer in 2015
116
- and assumed charge as Acting
117
- President & Chief Executive Officer
118
- on December 1, 2017. He has over 31
119
- years of experience including over 20
120
- years in the banking sector.
121
- He currently serves as the Chairman
122
- of First MicroFinance Bank Limited
123
- and is a Director on the Board of HBL
124
- Asset Management Limited, Habib
125
- Allied Holding Limited UK and
126
- Habibsons Bank UK.
127
- 6. Mr. Agha Sher Shah
128
- Director
129
- Mr. Agha Sher Shah has been on the
130
- Board since 2013. He has over 26
131
- years of experience in the financial
132
- sector.
133
- He is currently Chairman and Chief
134
- Executive of Bandhi Sugar Mills
135
- (Private) Limited. He is also Director
136
- of HBL Asset Management Limited,
137
- Attock Cement Pakistan Limit
138
- ================================================================================
139
- Doc 4:
140
- Score: None
141
- /kaggle/input/datasets/muhammadezanraza/hbl-corpus-raw/pdfs/d57ccbf3a3e1818f13fb29b8526a5bad.pdf
142
- Corporate Information
143
- Board of Directors
144
- Mr. Sultan Ali Allana
145
- Chairman
146
- Mr. Shaffiq Dharamshi
147
- Director
148
- Mr. Moez Ahamed Jamal
149
- Director
150
- Mr. Salim Raza
151
- Director
152
- Dr. Najeeb Samie
153
- Director
154
- Mr. Khaleel Ahmed
155
- Director
156
- Ms. Saba Kamal
157
- Director
158
- Mr. Muhammad Nassir Salim
159
- President & CEO
160
- Chief Financial Officer
161
- Mr. Rayomond H. Kotwal
162
- Company Secretary
163
- Mr. Uzman Naveed Chaudhary
164
- Legal Advisors
165
- Mandviwalla and Zafar
166
- Legal Consultants and Advocates
167
- Auditors
168
- KPMG Taseer Hadi & Co
169
- Chartered Accountants
170
- Share Registrar
171
- CDC Share Registrar Services Limited
172
- CDC House, 99 – B, Block ‘B’,
173
- S.M.C.H.S., Main Shahra-e-Faisal
174
- Karachi – 74400, Pakistan
175
- Tel: Customer Support Services
176
- (Toll Free) 0800-CDCPL (23275)
177
- Fax: (92-21) 34326053
178
- Email: info@cdcsrsl.com
179
- Website: www.cdcsrsl.com
180
- HBL Corporate Secretariat
181
- Ph
182
-
183
- Assistant:
184
- The CTO of Habib Bank Limited is Mr. Uzman Naveed Chaudhary.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:]