TensorCat commited on
Commit
605c9c4
·
verified ·
1 Parent(s): 1ce5eff

Improve TensorTalk model card

Browse files
.gitattributes CHANGED
@@ -43,3 +43,4 @@ UM_Handbook/outputs/qwen3_um_handbook_baseline2_rag_a100/trainer_runs/checkpoint
43
  UM_Handbook/outputs/baseline2_rag_harness_agent/lora_adapter/tokenizer.json filter=lfs diff=lfs merge=lfs -text
44
  UM_Handbook/outputs/baseline2_rag_harness_agent/trainer_runs/checkpoint-1100/tokenizer.json filter=lfs diff=lfs merge=lfs -text
45
  UM_Handbook/outputs/baseline2_rag_harness_agent/trainer_runs/checkpoint-1300/tokenizer.json filter=lfs diff=lfs merge=lfs -text
 
 
43
  UM_Handbook/outputs/baseline2_rag_harness_agent/lora_adapter/tokenizer.json filter=lfs diff=lfs merge=lfs -text
44
  UM_Handbook/outputs/baseline2_rag_harness_agent/trainer_runs/checkpoint-1100/tokenizer.json filter=lfs diff=lfs merge=lfs -text
45
  UM_Handbook/outputs/baseline2_rag_harness_agent/trainer_runs/checkpoint-1300/tokenizer.json filter=lfs diff=lfs merge=lfs -text
46
+ UM_Handbook/assets/tensortalk-rag-inspector.png filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -28,1937 +28,158 @@ tags:
28
  - tensor-talk
29
  ---
30
 
31
- # TensorTalk: UM Handbook Qwen3-8B SFT + RAG + Agent + PPO + Harness Engineering
32
 
33
- TensorTalk is a staged LLM engineering project built for **Universiti Malaya Faculty of Computer Science and Information Technology handbook question answering**. The system is designed to answer undergraduate, postgraduate, and general faculty handbook questions using a controlled progression of three experimental stages:
34
 
35
- 1. **Baseline 1 — Closed-book SFT Qwen3-8B**
36
- 2. **Baseline 2 — SFT Qwen3-8B + Metadata-aware RAG + Official Web Agent + Harness Engineering**
37
- 3. **Improved Model — Rule-reward PPO post-training + RAG + Agent + Harness Engineering**
38
 
39
- The project is not just a simple chatbot. It is a controlled comparison of how an LLM system improves when moving from memorized supervised fine-tuning, to retrieval-grounded answering, and finally to rule-reward post-training with a guarded agentic runtime.
40
 
41
- The main idea is:
42
 
43
- > Baseline 1 tests whether a fine-tuned model can answer handbook questions from parameters alone.
44
- > Baseline 2 keeps the same base model family but adds retrieval and harnessed evidence control.
45
- > The Improved Model keeps the RAG + Agent + Harness runtime and further adds PPO post-training to make the model better aligned with the desired answer behavior.
46
 
47
- ---
48
-
49
- # 1. Project Goal
50
-
51
- The goal of this project is to build a reliable and traceable UM Handbook assistant that can answer questions about:
52
-
53
- - Faculty objectives, vision, mission, history, facilities, and academic calendar
54
- - Undergraduate programme details
55
- - Postgraduate programme details
56
- - Candidature requirements
57
- - Grading and academic rules
58
- - Industrial training
59
- - Academic project requirements
60
- - Supervision policy
61
- - Thesis/dissertation requirements
62
- - Academic integrity and plagiarism
63
- - Facilities and labs
64
- - Official UM/FSKTM web information when handbook knowledge is insufficient or time-sensitive
65
-
66
- The project also aims to demonstrate a complete LLM system development path:
67
-
68
- ```text
69
- Closed-book SFT
70
- → RAG-augmented SFT
71
- → Metadata-aware retrieval
72
- → Official-source web agent
73
- → Harness Engineering guardrails
74
- → PPO rule-reward post-training
75
- → Strict artifact verification
76
- → Traceable TensorTalk UI
77
- ```
78
-
79
- ---
80
-
81
- # 2. High-level System Overview
82
-
83
- The final TensorTalk system contains several layers.
84
-
85
- ```text
86
- User Question
87
- ↓
88
- TensorTalk UI
89
- ↓
90
- Planning / Thinking Display Layer
91
- ↓
92
- Local Handbook RAG
93
- ↓
94
- Official UM / FSKTM Web Agent
95
- ↓
96
- Harness Engineering Guardrails
97
- ↓
98
- Evidence Judge / Retry / Fallback
99
- ↓
100
- PPO-trained Qwen3-8B Actor
101
- ↓
102
- Answer Grounding Judge
103
- ↓
104
- Completeness Guard
105
- ↓
106
- Final Answer + Trace Panels
107
- ```
108
-
109
- The final model is not used alone. It is wrapped inside a runtime harness that controls:
110
-
111
- - where the system can search
112
- - which sources it can trust
113
- - whether web evidence is useful
114
- - whether retrieved evidence supports the answer
115
- - whether the model produced fake URLs
116
- - whether the answer leaked internal reasoning
117
- - whether fallback to local handbook RAG is needed
118
- - whether the final answer is grounded enough to show
119
-
120
- This is why the final stage is better described as:
121
-
122
- > **A PPO-aligned RAG agent system with Harness Engineering**, rather than only a fine-tuned model.
123
-
124
- ---
125
-
126
- # 3. Dataset Design
127
-
128
- ## 3.1 Source Domain
129
-
130
- The dataset is built around UM FSKTM undergraduate and postgraduate handbook content. The data is organized into:
131
-
132
- - SFT question-answer dataset
133
- - hidden metadata
134
- - RAG knowledge base
135
- - RAG evaluation dataset
136
- - PPO preference dataset
137
-
138
- The project separates **model-visible training text** from **metadata used for retrieval, evaluation, and analysis**.
139
-
140
- This distinction is important:
141
-
142
- - Baseline 1 intentionally trains on question-answer text without forcing explicit metadata labels into the model-visible answer.
143
- - Baseline 2 uses metadata-aware retrieval to reduce scope confusion.
144
- - Stage 3 PPO uses preference pairs and reward functions to shape answer behavior.
145
-
146
- ---
147
-
148
- ## 3.2 Baseline 1 SFT Dataset
149
-
150
- Baseline 1 uses:
151
-
152
- ```text
153
- SFT_QA_Training_Ready.jsonl
154
- ```
155
-
156
- The notebook validates:
157
-
158
- ```text
159
- Total examples: 1000
160
- Train examples: 800
161
- Validation examples: 100
162
- Test examples: 100
163
- Split ratio: 8:1:1
164
- Duplicate question groups: 0
165
- Duplicate question rows: 0
166
- ```
167
-
168
- Each example follows a supervised chat-style format:
169
-
170
- ```json
171
- {
172
- "prompt": [
173
- {
174
- "role": "system",
175
- "content": "You are an academic assistant for the Faculty of Computer Science and Information Technology, Universiti Malaya..."
176
- },
177
- {
178
- "role": "user",
179
- "content": "What are the faculty objectives?"
180
- }
181
- ],
182
- "completion": [
183
- {
184
- "role": "assistant",
185
- "content": "The faculty objectives are..."
186
- }
187
- ],
188
- "question": "...",
189
- "answer": "..."
190
- }
191
- ```
192
-
193
- This stage teaches the model to imitate handbook-style answers directly.
194
-
195
- ---
196
-
197
- ## 3.3 Baseline 2 RAG Dataset
198
-
199
- Baseline 2 uses the same SFT dataset direction, but adds external retrieval resources:
200
-
201
- ```text
202
- UM_RAG_Knowledge_Base.jsonl
203
- UM_RAG_Evaluation_Dataset.jsonl
204
- SFT_QA_Metadata.jsonl
205
- ```
206
-
207
- The RAG knowledge base contains structured fields such as:
208
-
209
- ```text
210
- kb_id
211
- source_doc
212
- scope_label
213
- section
214
- pages
215
- source_text
216
- retrieval_text
217
- retrieval_keywords
218
- grounded_answer_bank
219
- matched_qa_ids
220
- ```
221
-
222
- The RAG knowledge base loaded in the final Stage 3 runtime contains:
223
-
224
- ```text
225
- Loaded KB rows: 521
226
- ```
227
-
228
- The metadata layer allows the system to distinguish:
229
-
230
- ```text
231
- general
232
- undergraduate
233
- postgraduate
234
- ```
235
-
236
- This is important because many handbook questions look similar but require different answers depending on the student scope.
237
-
238
- ---
239
-
240
- ## 3.4 PPO Preference Dataset
241
-
242
- The Improved Model uses:
243
-
244
- ```text
245
- UM_Handbook_PPO_Preference_Dataset.jsonl
246
- ```
247
-
248
- The final PPO run uses the full dataset:
249
-
250
- ```text
251
- Total PPO preference rows: 1000
252
- Train rows: 900
253
- Validation rows: 100
254
- Train fraction: 0.90
255
- ```
256
-
257
- The PPO dataset is not used like normal SFT data. In SFT, the model directly imitates a reference answer. In PPO, the model generates its own answer, receives a reward, and updates toward higher-reward behavior.
258
-
259
- ---
260
-
261
- # 4. Baseline 1 — Closed-book SFT Qwen3-8B
262
-
263
- ## 4.1 Purpose
264
-
265
- Baseline 1 asks a simple question:
266
-
267
- > Can Qwen3-8B learn UM Handbook question answering from supervised fine-tuning alone?
268
-
269
- This is a **closed-book baseline**. The model does not retrieve handbook evidence during inference. It must answer from what it learned during SFT.
270
-
271
- This is useful as a control baseline because it shows what happens when the model relies mainly on parameter memory.
272
-
273
- ---
274
-
275
- ## 4.2 Model
276
-
277
- Baseline 1 uses:
278
-
279
- ```text
280
- Base model: Qwen/Qwen3-8B
281
- Local path: /scr/user/kevin2002/TensorCat/NLP/UM_Handbook/models/Qwen3-8B
282
- ```
283
-
284
- The notebook detected:
285
-
286
- ```text
287
- Backend: CUDA
288
- GPU: NVIDIA A100-SXM4-80GB
289
- dtype: bfloat16
290
- 4-bit QLoRA: enabled
291
- ```
292
-
293
- ---
294
-
295
- ## 4.3 Training Method
296
-
297
- Baseline 1 uses LoRA / QLoRA supervised fine-tuning.
298
-
299
- LoRA configuration:
300
-
301
- ```text
302
- LoRA rank: 16
303
- LoRA alpha: 32
304
- LoRA dropout: 0.10
305
- Target modules:
306
- - q_proj
307
- - k_proj
308
- - v_proj
309
- - o_proj
310
- - gate_proj
311
- - up_proj
312
- - down_proj
313
- ```
314
-
315
- Training configuration:
316
-
317
- ```text
318
- Epochs: 8
319
- Train split: 800
320
- Validation split: 100
321
- Test split: 100
322
- Per-device train batch size: 2
323
- Per-device eval batch size: 2
324
- Gradient accumulation steps: 8
325
- Learning rate: 1e-4
326
- Packing: False
327
- ```
328
-
329
- ---
330
-
331
- ## 4.4 Baseline 1 Results
332
-
333
- The training completed successfully.
334
-
335
- Training summary:
336
-
337
- ```text
338
- Training steps: 400
339
- Training runtime: ~18.68 minutes for the main train stage
340
- Train loss: 0.4824
341
- Final validation loss: ~0.146
342
- Test loss: ~0.197
343
- Perplexity: ~1.157
344
- ```
345
-
346
- Generation metrics:
347
-
348
- ### Validation
349
-
350
- ```text
351
- Exact match: 0.77
352
- Token F1: 0.9111
353
- ROUGE-1: 0.9122
354
- ROUGE-2: 0.8700
355
- ROUGE-L: 0.8979
356
- SacreBLEU: 81.7240
357
- chrF++: 86.8916
358
- Average prediction words: 36.35
359
- Average reference words: 38.57
360
- ```
361
-
362
- ### Test
363
-
364
- ```text
365
- Exact match: 0.72
366
- Token F1: 0.8869
367
- ROUGE-1: 0.8857
368
- ROUGE-2: 0.8352
369
- ROUGE-L: 0.8677
370
- SacreBLEU: 81.1138
371
- chrF++: 87.7054
372
- Average prediction words: 38.03
373
- Average reference words: 37.03
374
- ```
375
-
376
- ---
377
-
378
- ## 4.5 Baseline 1 Strengths
379
-
380
- Baseline 1 is strong when the question is close to the training distribution. It can reproduce handbook-style answers well and shows high text overlap with the reference answers.
381
-
382
- It is useful because:
383
-
384
- - it establishes the basic Qwen3-8B SFT capability
385
- - it verifies that the dataset format is learnable
386
- - it creates a clean closed-book control model
387
- - it provides a baseline for later RAG and PPO improvements
388
-
389
- ---
390
-
391
- ## 4.6 Baseline 1 Limitations
392
-
393
- Baseline 1 is still limited because it is a closed-book model.
394
-
395
- Main limitations:
396
-
397
- 1. **No retrieval evidence**
398
- It cannot check the handbook at inference time.
399
-
400
- 2. **Potential hallucination**
401
- If the question is out-of-distribution or requires exact source grounding, the model may answer from memory.
402
-
403
- 3. **Scope confusion**
404
- Undergraduate and postgraduate rules may be mixed if the question is ambiguous.
405
-
406
- 4. **No official web update mechanism**
407
- It cannot answer dynamic or latest-information questions reliably.
408
-
409
- 5. **No harness guardrails**
410
- It does not include fake URL detection, evidence judging, WAF handling, or fallback control.
411
-
412
- Baseline 1 is therefore a necessary but incomplete starting point.
413
-
414
- ---
415
-
416
- # 5. Baseline 2 — RAG + SFT + Metadata-aware Retrieval + Harness Agent
417
-
418
- ## 5.1 Purpose
419
-
420
- Baseline 2 asks:
421
-
422
- > What improves if we keep the same Qwen3-8B family but add retrieval-grounded evidence?
423
-
424
- The goal is to reduce hallucination and scope confusion by giving the model relevant handbook evidence at inference time.
425
-
426
- This stage introduces RAG and agentic harness logic while keeping the same broad model family and handbook task.
427
-
428
- ---
429
-
430
- ## 5.2 What RAG Means in This Project
431
-
432
- RAG stands for **Retrieval-Augmented Generation**.
433
-
434
- In simple terms:
435
-
436
- ```text
437
- Instead of asking the model to answer only from memory,
438
- the system first retrieves relevant handbook chunks,
439
- then asks the model to answer using those chunks.
440
- ```
441
-
442
- In this project, RAG is not just keyword search. It uses:
443
-
444
- ```text
445
- Transformer embedding model
446
- + FAISS vector search
447
- + metadata-aware reranking
448
- + scope labels
449
- + top-k evidence blocks
450
- ```
451
-
452
- The Baseline 2 retriever uses:
453
-
454
- ```text
455
- Embedding model: BAAI/bge-base-en-v1.5
456
- Vector index: FAISS
457
- Similarity: inner product after embedding normalization
458
- Top-k retrieval: 3
459
- ```
460
-
461
- ---
462
-
463
- ## 5.3 Metadata-aware Retrieval
464
-
465
- The RAG system uses metadata to control retrieval quality.
466
-
467
- Important metadata fields include:
468
-
469
- ```text
470
- source_doc
471
- scope_label
472
- section
473
- pages
474
- kb_id
475
- knowledge group
476
- retrieval keywords
477
- grounded answer bank
478
- ```
479
-
480
- This allows the retriever to prefer the correct audience scope.
481
-
482
- Example:
483
-
484
- ```text
485
- Question: What are the candidature requirements for Master of Software Engineering?
486
- Expected scope: postgraduate
487
- ```
488
-
489
- The system should retrieve postgraduate chunks, not undergraduate chunks.
490
-
491
- This is one of the main improvements over Baseline 1.
492
-
493
- ---
494
-
495
- ## 5.4 RAG-augmented Training Dataset
496
-
497
- Baseline 2 creates a RAG-augmented dataset where training examples include evidence context.
498
-
499
- The training prompt can contain:
500
-
501
- ```text
502
- User question
503
- + retrieved handbook evidence
504
- + source metadata
505
- + answer instruction
506
- ```
507
-
508
- This teaches the model to answer with evidence-aware context rather than only memorized answers.
509
-
510
- ---
511
-
512
- ## 5.5 Baseline 2 Training Configuration
513
-
514
- Baseline 2 uses Qwen3-8B with LoRA fine-tuning.
515
-
516
- Configuration:
517
-
518
- ```text
519
- Base model: Qwen/Qwen3-8B
520
- Embedding model: BAAI/bge-base-en-v1.5
521
- LoRA rank: 8
522
- LoRA alpha: 16
523
- LoRA dropout: 0.05
524
- Target modules:
525
- - q_proj
526
- - k_proj
527
- - v_proj
528
- - o_proj
529
- - gate_proj
530
- - up_proj
531
- - down_proj
532
- Epochs: 20
533
- Per-device train batch size: 4
534
- Per-device eval batch size: 8
535
- Target global batch size: 8
536
- Learning rate: 8e-5
537
- Max sequence length: 1024
538
- Validation ratio: 0.10
539
- Test ratio: 0.10
540
- Save merged model: False
541
- Runtime model path: base model + LoRA adapter
542
- ```
543
-
544
- The notebook uses a safer non-merged runtime path when merged model export is unavailable or memory-expensive.
545
-
546
- ---
547
-
548
- ## 5.6 Baseline 2 Retrieval Evaluation
549
-
550
- Baseline 2 includes a retrieval evaluation set.
551
-
552
- Retrieval metrics:
553
-
554
- ```json
555
- {
556
- "retrieval_eval_size": 1000,
557
- "top_k": 3,
558
- "hit_at_1_primary": 0.821,
559
- "hit_at_k_primary": 0.954,
560
- "hit_at_k_same_group": 0.991,
561
- "scope_match_at_1": 0.996,
562
- "retriever_type": "dense_embedding + faiss + metadata_rerank",
563
- "embedding_model_name": "BAAI/bge-base-en-v1.5"
564
- }
565
- ```
566
-
567
- Interpretation:
568
-
569
- - `hit_at_1_primary = 0.821` means the top retrieved chunk is exactly the expected primary evidence in 82.1% of cases.
570
- - `hit_at_k_primary = 0.954` means the correct primary evidence appears within top-3 in 95.4% of cases.
571
- - `hit_at_k_same_group = 0.991` means a same-group acceptable evidence appears in top-3 in 99.1% of cases.
572
- - `scope_match_at_1 = 0.996` means the top result almost always matches the correct undergraduate/postgraduate/general scope.
573
-
574
- This confirms that the RAG system is not random retrieval. It is a strong metadata-aware retrieval baseline.
575
-
576
- ---
577
-
578
- ## 5.7 Baseline 2 Generation Evaluation
579
-
580
- Generation evaluation was run on a smaller selected set for runtime practicality.
581
-
582
- Results:
583
-
584
- ```json
585
- {
586
- "generation_eval_size": 20,
587
- "top_k": 3,
588
- "plain_exact_match": 0.0,
589
- "plain_token_f1": 0.3391,
590
- "rag_exact_match": 0.0,
591
- "rag_token_f1": 0.8460,
592
- "rag_minus_plain_exact_match": 0.0,
593
- "rag_minus_plain_token_f1": 0.5069
594
- }
595
- ```
596
-
597
- This shows a large improvement from RAG:
598
-
599
- ```text
600
- Plain token F1: 0.3391
601
- RAG token F1: 0.8460
602
- Improvement: +0.5069
603
- ```
604
-
605
- This is one of the strongest pieces of evidence in the project.
606
-
607
- It shows that retrieval grounding dramatically improves answer quality compared with plain generation.
608
-
609
- ---
610
-
611
- # 6. Agent Layer in Baseline 2 and Improved Model
612
-
613
- ## 6.1 Why an Agent Is Needed
614
-
615
- The handbook is reliable for stable academic rules, but a practical university assistant cannot depend only on static handbook text. Some user questions naturally require **official web discovery**, **source checking**, or **routing decisions**.
616
-
617
- Examples:
618
-
619
- ```text
620
- Who is the current dean?
621
- Where can students find residential college information?
622
- What official page mentions PEKOM?
623
- Where is the official SPeCTRUM page?
624
- What facilities are associated with a specific lab?
625
- ```
626
-
627
- For these cases, TensorTalk uses an official-source web agent. The agent is not designed as an unrestricted autonomous browser. It is deliberately designed as a **constrained agent** because the domain is academic handbook QA, where factual trustworthiness is more important than open-ended exploration.
628
-
629
- In practical terms, the agent layer answers this question:
630
-
631
- > If local handbook RAG is not enough, can the system search official UM/FSKTM sources, reject weak or fake sources, and return evidence safely?
632
-
633
- ---
634
-
635
- ## 6.2 How This Project Relates to LangChain and LangGraph
636
-
637
- TensorTalk does **not** use LangChain, LangGraph, or LangSmith as the runtime framework. The agent and harness were implemented from scratch in the notebook.
638
-
639
- However, the design is conceptually aligned with the official LangChain ecosystem ideas:
640
-
641
- - LangChain describes agents as systems that combine language models with tools so they can reason about tasks, decide which tools to use, and iteratively work toward a result.
642
- - LangGraph describes agent workflows using state, nodes, and edges, where nodes perform computation and edges determine the next transition.
643
- - LangSmith describes evaluation as a workflow involving datasets, evaluators, and experiments to compare application versions.
644
- - LangChain/LangGraph documentation also distinguishes between predetermined workflows and dynamic agents; TensorTalk intentionally uses a hybrid design because the handbook QA task needs both predictable guardrails and dynamic retrieval decisions.
645
-
646
- Therefore, this project is best described as:
647
-
648
- > A from-scratch implementation of a LangChain/LangGraph-inspired agentic RAG harness, not a project built by directly calling LangChain’s prebuilt agent framework.
649
-
650
- This distinction is important. TensorTalk does not simply wrap a LangChain agent. Instead, it manually implements the major control ideas:
651
-
652
- ```text
653
- State tracking
654
- → planning
655
- → retrieval/tool routing
656
- → source filtering
657
- → evidence normalization
658
- → evaluation/judging
659
- → retry/fallback
660
- → final generation
661
- → trace output
662
- ```
663
-
664
- This gives the project more transparency because each part of the agent loop is visible in the notebook and UI trace.
665
-
666
- ---
667
-
668
- ## 6.3 Agent Design Philosophy
669
-
670
- The TensorTalk agent is built around four principles.
671
-
672
- ### 1. Source-constrained autonomy
673
-
674
- The agent can search and fetch information, but only from allowed official sources. It is not free to trust arbitrary search results.
675
-
676
- ### 2. Evidence-first generation
677
-
678
- The model should not directly answer a web-sensitive question before evidence is collected and judged.
679
-
680
- ### 3. Retry and fallback
681
-
682
- If official web evidence is weak, blocked, irrelevant, or unsafe, the system can retry with entity-aware search terms or fall back to local handbook RAG.
683
-
684
- ### 4. Traceable decisions
685
-
686
- The agent does not hide its routing decisions. It records URL candidates, accepted evidence, rejected evidence, grounding decisions, and fallback decisions in trace panels.
687
-
688
- ---
689
-
690
- ## 6.4 Official UM / FSKTM Web Agent
691
-
692
- The web agent is constrained to official UM / FSKTM domains. This is one of the most important safety and reliability choices in the project.
693
-
694
- ### Priority official domains
695
-
696
- ```text
697
- fsktm.um.edu.my
698
- www.um.edu.my
699
- um.edu.my
700
- ```
701
-
702
- ### Auxiliary official UM-related domains
703
-
704
- The project also recognizes selected UM-related service domains when they are relevant to student services, academic systems, library resources, research, career portals, or internal faculty resources:
705
-
706
- ```text
707
- aasd.um.edu.my
708
- maya.um.edu.my
709
- umlib.um.edu.my
710
- umresearch.um.edu.my
711
- jobs.um.edu.my
712
- careerportal.fsktm.um.edu.my
713
- intra.fsktm.um.edu.my
714
- gallery.fsktm.um.edu.my
715
- ```
716
-
717
- The purpose of this whitelist is not to search the whole internet. The purpose is to constrain the agent to sources that are likely to be officially controlled by UM or FSKTM.
718
-
719
- ---
720
-
721
- ## 6.5 Domain Whitelist Design
722
-
723
- The whitelist is used as a **domain guard** before a page can become trusted evidence.
724
-
725
- The system treats URLs in three broad categories:
726
-
727
- | URL type | Handling |
728
- |---|---|
729
- | Official UM/FSKTM URL | Can be considered as candidate evidence |
730
- | UM-related service URL | Can be considered if relevant to the question type |
731
- | Non-official or synthetic URL | Rejected or downgraded |
732
-
733
- The whitelist helps prevent common LLM-agent failure cases:
734
-
735
- ```text
736
- hallucinated programme pages
737
- invented lab pages
738
- fake student service URLs
739
- misrouted search results
740
- random third-party pages
741
- SEO or unrelated pages
742
- ```
743
-
744
- For example, the project specifically tests that the agent should not invent or accept URLs like:
745
-
746
- ```text
747
- programme-ccna-lab-more-detailedly
748
- bachelor-of-computer-science-artificial-intelligence
749
- ```
750
-
751
- when those pages are not the correct evidence for the user’s question.
752
-
753
- ---
754
-
755
- ## 6.6 Web Agent Workflow
756
-
757
- The official-source web agent follows a controlled workflow.
758
-
759
- ```text
760
- User question
761
- ↓
762
- Intent and entity detection
763
- ↓
764
- Official-search query construction
765
- ↓
766
- Candidate URL discovery
767
- ↓
768
- Domain whitelist filtering
769
- ↓
770
- Synthetic/fake URL rejection
771
- ↓
772
- Fetch or static page fallback
773
- ↓
774
- WAF/block detection
775
- ↓
776
- Text extraction and normalization
777
- ↓
778
- Evidence scoring
779
- ↓
780
- Qwen evidence judge
781
- ↓
782
- Accept evidence, retry, or fallback to handbook RAG
783
- ```
784
-
785
- This means the agent is not only a web search function. It is a guarded evidence acquisition pipeline.
786
-
787
- ---
788
-
789
- ## 6.7 Planning Inside the Agent
790
-
791
- Planning is a visible part of the TensorTalk system.
792
-
793
- The planning layer is responsible for deciding:
794
-
795
- ```text
796
- Is this a stable handbook question?
797
- Is this a latest/current official-web question?
798
- Should local RAG be used first?
799
- Should official web discovery be attempted?
800
- Which entity should be searched?
801
- Which scope should be preferred: undergraduate, postgraduate, general, faculty, or university?
802
- What evidence type is expected: handbook chunk, official page, contact page, facility page, announcement, policy page?
803
- ```
804
-
805
- This planning step is aligned with the idea that agentic systems should not directly jump from user question to final answer. They need a control stage that decides which tools and evidence paths are appropriate.
806
-
807
- TensorTalk’s planning is not a free-form hidden chain-of-thought that users must trust blindly. It is operationalized through explicit routing variables, trace objects, search decisions, and UI panels.
808
-
809
- ---
810
-
811
- ## 6.8 Generation Inside the Agent
812
-
813
- Generation is the stage where the Qwen3-8B model produces an answer.
814
-
815
- However, generation is not allowed to operate alone. The answer generator receives controlled context:
816
-
817
- ```text
818
- user question
819
- retrieved local handbook evidence
820
- accepted official web evidence
821
- scope hints
822
- source metadata
823
- harness instructions
824
- answer style constraints
825
- ```
826
-
827
- The generator is expected to:
828
-
829
- ```text
830
- answer directly
831
- avoid unsupported claims
832
- avoid fake URLs
833
- avoid exposing internal reasoning
834
- use local handbook evidence when web evidence is weak
835
- prefer official web evidence only when it is relevant and trusted
836
- ```
837
-
838
- In the final stage, the generator is the PPO-trained Qwen3 actor, but it is still wrapped by the same RAG and Harness Engineering control layer.
839
-
840
- ---
841
-
842
- ## 6.9 Evaluation Inside the Agent
843
-
844
- Evaluation is the other core part of the agent loop. TensorTalk evaluates both evidence and answers.
845
-
846
- ### Evidence evaluation
847
-
848
- The system checks:
849
-
850
- ```text
851
- Is the source official?
852
- Is the URL synthetic or fake?
853
- Is the page blocked by WAF?
854
- Is the evidence relevant to the user question?
855
- Does the evidence mention the right entity?
856
- Does the evidence match the expected scope?
857
- ```
858
-
859
- ### Answer evaluation
860
-
861
- The system checks:
862
-
863
- ```text
864
- Is the final answer grounded in accepted evidence?
865
- Does it answer the user’s actual question?
866
- Does it leak internal thinking?
867
- Does it invent URLs?
868
- Is it too vague?
869
- Is it incomplete enough to require fallback or rewrite?
870
- ```
871
-
872
- This creates a full agentic loop:
873
-
874
- ```text
875
- Planning
876
- → Retrieval / tool use
877
- → Generation
878
- → Evaluation
879
- → Retry or fallback
880
- → Final answer
881
- ```
882
-
883
- ---
884
-
885
- ## 6.10 Why the Agent Is Not Fully Autonomous
886
-
887
- The agent is intentionally not fully autonomous.
888
-
889
- A fully autonomous browsing agent may:
890
-
891
- ```text
892
- search too broadly
893
- trust wrong sources
894
- follow irrelevant pages
895
- invent missing pages
896
- overuse web search
897
- ignore handbook evidence
898
- produce unsupported answers
899
- ```
900
-
901
- TensorTalk instead uses a constrained model:
902
-
903
- ```text
904
- Dynamic when needed
905
- Guarded by default
906
- Official-source-only for web evidence
907
- RAG-first for handbook-stable questions
908
- Fallback-aware when web evidence is weak
909
- Traceable for debugging and demonstration
910
- ```
911
-
912
- This is more appropriate for a university handbook assistant.
913
-
914
- ---
915
-
916
- # 7. Harness Engineering
917
-
918
- ## 7.1 What Harness Engineering Means Here
919
-
920
- Harness Engineering is the external control system around the LLM, RAG, and agent.
921
-
922
- A simple analogy:
923
-
924
- ```text
925
- The LLM/agent is the car.
926
- Harness Engineering is the guardrail, traffic rule, checkpoint, fallback route, dashboard, and driving examiner.
927
- ```
928
-
929
- The model can generate fluent answers, but the harness controls:
930
-
931
- ```text
932
- what it can search
933
- which domains are trusted
934
- which URLs are rejected
935
- which evidence is useful
936
- when to retry
937
- when to fallback
938
- whether the answer is grounded
939
- whether the UI should show warning traces
940
- whether the final response is safe enough to display
941
- ```
942
-
943
- In TensorTalk, Harness Engineering is not just prompt engineering. Prompt engineering tells the model what to do. Harness Engineering builds the surrounding execution system that checks whether the model actually did it correctly.
944
-
945
- ---
946
-
947
- ## 7.2 From Prompt Engineering to Harness Engineering
948
-
949
- Prompt engineering is like telling a driver:
950
-
951
- ```text
952
- Please drive carefully.
953
- ```
954
-
955
- Harness Engineering is like building:
956
-
957
- ```text
958
- lane barriers
959
- speed checks
960
- traffic rules
961
- navigation checkpoints
962
- fallback routes
963
- dashboards
964
- incident logs
965
- ```
966
-
967
- In this project, prompt engineering alone is not enough because the model may still:
968
-
969
- ```text
970
- invent fake URLs
971
- mix undergraduate and postgraduate rules
972
- leak internal reasoning
973
- trust weak web snippets
974
- answer without evidence
975
- overuse web search
976
- ignore local handbook RAG
977
- ```
978
-
979
- The harness prevents or detects these failures through code-level controls, not only prompt instructions.
980
-
981
- ---
982
-
983
- ## 7.3 From-scratch LangChain-style Harness
984
-
985
- TensorTalk’s harness was built manually rather than by importing a prebuilt LangChain/LangGraph agent.
986
-
987
- The implementation follows the same conceptual loop used in many modern agent frameworks:
988
-
989
- ```text
990
- Planning
991
- → Tool / Retrieval Action
992
- → Generation
993
- → Evaluation
994
- → Retry / Fallback
995
- → Finalization
996
- ```
997
-
998
- But each component is implemented explicitly:
999
-
1000
- | Conceptual framework idea | TensorTalk from-scratch implementation |
1001
- |---|---|
1002
- | Agent state | Trace dictionaries, evidence bundles, routing flags, runtime status |
1003
- | Tools | Local RAG retriever, official web search/fetch, URL validator, evidence judge |
1004
- | Nodes | Planning, retrieval, web discovery, evidence filtering, judging, generation, grounding |
1005
- | Edges / transitions | Conditional retry, weak-evidence fallback, RAG fallback, final answer route |
1006
- | Evaluation | Qwen evidence judge, rule checks, answer grounding judge, smoke tests |
1007
- | Observability | Collapsed UI trace panels and printed diagnostic outputs |
1008
-
1009
- This makes the system easier to inspect in an academic notebook because the control logic is visible.
1010
-
1011
- ---
1012
-
1013
- ## 7.4 Planning → Generation → Evaluation Closed Loop
1014
-
1015
- The most important Harness Engineering contribution in TensorTalk is the closed loop:
1016
-
1017
- ```text
1018
- Planning
1019
- ↓
1020
- Generation
1021
- ↓
1022
- Evaluation
1023
- ↓
1024
- Retry / Fallback / Finalization
1025
- ```
1026
-
1027
- ### Planning
1028
-
1029
- The planning layer decides how to handle the query.
1030
-
1031
- It considers:
1032
-
1033
- ```text
1034
- question type
1035
- scope
1036
- entity
1037
- whether local RAG is enough
1038
- whether official web is needed
1039
- whether the query is dynamic/current
1040
- whether the answer should be handbook-grounded or web-grounded
1041
- ```
1042
-
1043
- ### Generation
1044
-
1045
- The generation layer produces an answer using controlled evidence.
1046
-
1047
- It receives:
1048
-
1049
- ```text
1050
- local handbook chunks
1051
- official web evidence
1052
- scope hints
1053
- source metadata
1054
- answer constraints
1055
- ```
1056
-
1057
- ### Evaluation
1058
-
1059
- The evaluation layer checks the result.
1060
-
1061
- It evaluates:
1062
-
1063
- ```text
1064
- source trust
1065
- URL validity
1066
- evidence relevance
1067
- answer grounding
1068
- completeness
1069
- process leakage
1070
- fake URLs
1071
- fallback need
1072
- ```
1073
-
1074
- If evaluation fails, the system can retry, reroute, or fall back.
1075
-
1076
- This is the engineering loop that makes TensorTalk more than a simple RAG chatbot.
1077
-
1078
- ---
1079
-
1080
- ## 7.5 Standardized Harness Core Pipeline
1081
-
1082
- The final standardized TensorTalk Harness Core follows this pipeline:
1083
-
1084
- ```text
1085
- User Question
1086
- ↓
1087
- Planning Layer
1088
- ↓
1089
- Local Handbook RAG
1090
- ↓
1091
- Official Web Discovery
1092
- ↓
1093
- Domain Guard
1094
- ↓
1095
- Fake URL Guard
1096
- ↓
1097
- WAF Detection
1098
- ↓
1099
- Evidence Normalizer
1100
- ↓
1101
- Qwen Evidence Judge
1102
- ↓
1103
- Entity-aware Retry
1104
- ↓
1105
- Weak Evidence Fallback
1106
- ↓
1107
- PPO/SFT Answer Generator
1108
- ↓
1109
- Answer Grounding Judge
1110
- ↓
1111
- Completeness Guard
1112
- ↓
1113
- Final Answer
1114
- ↓
1115
- UI Trace
1116
- ```
1117
-
1118
- This pipeline is intentionally explicit. Each part has a clear job.
1119
-
1120
- ---
1121
-
1122
- ## 7.6 Harness State and Trace Objects
1123
-
1124
- The harness keeps structured trace data so that every answer can be inspected.
1125
-
1126
- Typical trace information includes:
1127
-
1128
- ```text
1129
- retrieved local RAG chunks
1130
- candidate web URLs
1131
- accepted official URLs
1132
- rejected URLs
1133
- web evidence bundle
1134
- harness core route
1135
- evidence judge result
1136
- answer grounding result
1137
- fallback reason
1138
- final answer preview
1139
- ```
1140
-
1141
- This is similar in spirit to observability and tracing in agent platforms, but implemented directly in the notebook and UI.
1142
-
1143
- ---
1144
-
1145
- ## 7.7 Domain Guard
1146
-
1147
- The domain guard checks whether a candidate source belongs to the allowed official domain set.
1148
-
1149
- It protects against:
1150
-
1151
- ```text
1152
- random third-party websites
1153
- unofficial mirrors
1154
- search result noise
1155
- LLM-fabricated domains
1156
- wrong university pages
1157
- ```
1158
-
1159
- It also makes the system explainable. If the agent rejects a page, the trace can show why.
1160
-
1161
- ---
1162
-
1163
- ## 7.8 Fake URL Guard
1164
-
1165
- The fake URL guard is one of the most important parts of the project because raw LLM generations can invent plausible-looking URLs.
1166
-
1167
- Examples of risky synthetic URLs include:
1168
-
1169
- ```text
1170
- https://spectrum.umlms
1171
- http://spectrux.medicum
1172
- programme-ccna-lab-more-detailedly
1173
- https://aasd um edu my/studetn
1174
- ```
1175
-
1176
- The guard checks and rejects URLs that:
1177
-
1178
- ```text
1179
- are malformed
1180
- look fabricated
1181
- contain suspicious path patterns
1182
- do not belong to allowed domains
1183
- are query-fabricated rather than discovered from official search/fetch
1184
- ```
1185
-
1186
- The PPO reward function also penalizes hallucinated URLs, but the harness is still necessary because reward shaping does not guarantee perfect URL behavior.
1187
-
1188
- ---
1189
-
1190
- ## 7.9 WAF Detection
1191
-
1192
- Some official pages can be blocked, partially loaded, or protected by web application firewalls.
1193
-
1194
- The WAF-aware harness detects cases where:
1195
-
1196
- ```text
1197
- the page cannot be fetched normally
1198
- the content is a block page instead of the real page
1199
- the browser click fails
1200
- the official site returns insufficient text
1201
- ```
1202
-
1203
- When this happens, the system avoids treating the blocked page as strong evidence. It can use diagnostics, retry, static fallback, or local RAG fallback.
1204
-
1205
- ---
1206
-
1207
- ## 7.10 Evidence Normalizer
1208
-
1209
- Fetched web pages and handbook chunks may be noisy.
1210
-
1211
- The evidence normalizer attempts to convert them into a consistent evidence structure:
1212
-
1213
- ```text
1214
- title
1215
- url
1216
- source type
1217
- domain
1218
- text snippet
1219
- score
1220
- scope
1221
- entity
1222
- reason
1223
- ```
1224
-
1225
- This makes later judging and UI display easier.
1226
-
1227
- ---
1228
-
1229
- ## 7.11 Qwen Evidence Judge
1230
-
1231
- The Qwen evidence judge is used to decide whether retrieved evidence actually helps answer the user’s question.
1232
-
1233
- It checks:
1234
-
1235
- ```text
1236
- Does the evidence mention the right entity?
1237
- Does it answer the question directly?
1238
- Is it only loosely related?
1239
- Is it a wrong programme/page?
1240
- Is it official but irrelevant?
1241
- ```
1242
-
1243
- This is important because official sources can still be irrelevant. A page can be official and still be the wrong evidence.
1244
-
1245
- ---
1246
-
1247
- ## 7.12 Entity-aware Retry
1248
-
1249
- If the first web discovery result is weak or misrouted, the harness can retry with better query terms.
1250
-
1251
- For example, if a question about PEKOM gets routed toward an AI bachelor programme page, the harness should retry using terms related to:
1252
-
1253
- ```text
1254
- PEKOM
1255
- Persatuan Komputer UM
1256
- student society
1257
- FSKTM student association
1258
- ```
1259
-
1260
- This prevents the agent from accepting the first official-looking but semantically wrong page.
1261
-
1262
- ---
1263
-
1264
- ## 7.13 Weak Evidence Fallback
1265
-
1266
- If the official web evidence is weak, TensorTalk can fall back to local handbook RAG.
1267
-
1268
- This prevents a common agent failure:
1269
-
1270
- ```text
1271
- The system found a web page, so it trusts it even though it does not answer the question.
1272
- ```
1273
-
1274
- Instead, TensorTalk uses:
1275
-
1276
- ```text
1277
- web evidence if strong
1278
- local handbook RAG if web evidence is weak
1279
- hybrid answer if both are useful
1280
- refusal/uncertainty if neither is sufficient
1281
- ```
1282
-
1283
- ---
1284
-
1285
- ## 7.14 Answer Grounding Judge
1286
-
1287
- After answer generation, the answer grounding judge checks whether the final answer is supported by the accepted evidence.
1288
-
1289
- It helps catch cases where:
1290
-
1291
- ```text
1292
- retrieval was correct but generation added unsupported claims
1293
- the model invented a URL
1294
- the model mixed evidence from different scopes
1295
- the answer contains a statement that does not appear in evidence
1296
- ```
1297
-
1298
- This is the evaluation part of the Planning → Generation → Evaluation loop.
1299
-
1300
- ---
1301
-
1302
- ## 7.15 Completeness Guard
1303
-
1304
- The completeness guard checks whether the answer is too short, vague, or incomplete.
1305
-
1306
- It can identify cases where the answer:
1307
-
1308
- ```text
1309
- only repeats the question
1310
- does not include required details
1311
- misses key fields
1312
- does not answer the requested scope
1313
- cuts off early
1314
- ```
1315
-
1316
- Depending on runtime settings, this can trigger a rewrite or fallback.
1317
-
1318
- ---
1319
-
1320
- ## 7.16 Smoke Tests as Harness Unit Checks
1321
-
1322
- The smoke tests are lightweight checks that make sure the harness pipeline still works after model or code changes.
1323
-
1324
- Examples:
1325
-
1326
- ```text
1327
- PEKOM should not be routed to the AI bachelor page.
1328
- Residential college should prefer the student-affairs residential page.
1329
- CCNA Lab should not invent synthetic URLs.
1330
- ```
1331
-
1332
- These tests check:
1333
-
1334
- ```text
1335
- routing
1336
- URL filtering
1337
- official page preference
1338
- fake URL rejection
1339
- answer grounding trace
1340
- harness core route
1341
- ```
1342
-
1343
- They are not a full benchmark. They are fast sanity checks that the system still runs through the expected pipeline.
1344
-
1345
- ---
1346
-
1347
- ## 7.17 Why Harness Engineering Is Central to This Project
1348
-
1349
- The final system does not rely on only one technique.
1350
-
1351
- ```text
1352
- SFT gives domain answer style.
1353
- RAG gives handbook evidence.
1354
- The web agent gives official external evidence.
1355
- PPO improves answer behavior.
1356
- Harness Engineering controls the whole system.
1357
- ```
1358
-
1359
- Without the harness, the system would still be vulnerable to:
1360
-
1361
- ```text
1362
- wrong source selection
1363
- fake URLs
1364
- weak web evidence
1365
- scope confusion
1366
- process leakage
1367
- unsupported final answers
1368
- stale artifact loading
1369
- ```
1370
-
1371
- Therefore, Harness Engineering is the system-level contribution that connects SFT, RAG, Agent, and PPO into one controlled workflow.
1372
-
1373
- ---
1374
-
1375
- # 8. Improved Model — PPO Rule-reward Post-training + RAG + Agent + Harness
1376
-
1377
- ## 8.1 Purpose
1378
-
1379
- The Improved Model asks:
1380
-
1381
- > Can we further improve the model’s behavior after SFT/RAG by using PPO reward-based post-training?
1382
-
1383
- Baseline 2 already improves factual grounding through RAG and Harness Engineering. The Improved Model adds PPO to shape the model’s behavior.
1384
-
1385
- The goal is not to replace RAG. The goal is to make the model more aligned with the desired answer style and safety behavior.
1386
-
1387
- ---
1388
-
1389
- ## 8.2 What PPO Means in This Project
1390
-
1391
- PPO stands for **Proximal Policy Optimization**.
1392
-
1393
- In simple terms:
1394
-
1395
- ```text
1396
- SFT teaches the model by imitation.
1397
- PPO lets the model generate answers, scores them with a reward function, and updates the model toward higher-reward answers.
1398
- ```
1399
-
1400
- In this project:
1401
-
1402
- ```text
1403
- Actor model: Qwen3-8B + LoRA
1404
- Critic/value head: TRL value head model
1405
- Reference model: frozen Qwen3-8B reference
1406
- Reward: rule-based preference reward function
1407
- KL control: used to avoid drifting too far from the reference model
1408
- ```
1409
-
1410
- ---
1411
-
1412
- ## 8.3 Rule-based Reward Function
1413
-
1414
- This project uses a rule-based reward function rather than a separately trained neural reward model.
1415
-
1416
- The reward function evaluates:
1417
-
1418
- ```text
1419
- gold answer similarity
1420
- rejected answer penalty
1421
- evidence overlap
1422
- scope correctness
1423
- hallucinated URL penalty
1424
- vague answer penalty
1425
- process/thinking leakage penalty
1426
- direct answer bonus
1427
- repetition penalty
1428
- degeneration/collapse penalty
1429
- ```
1430
-
1431
- This is why the model card should describe the final stage as:
1432
-
1433
- > Rule-reward PPO post-training
1434
-
1435
- not:
1436
-
1437
- > Full RLHF with a trained reward model
1438
-
1439
- The reward model type recorded in the notebook is:
1440
-
1441
- ```text
1442
- rule_based_preference_reward_function
1443
- uses_separate_neural_reward_model: False
1444
- ```
1445
-
1446
- ---
1447
-
1448
- ## 8.4 PPO Training Configuration
1449
-
1450
- The final PPO run uses:
1451
-
1452
- ```text
1453
- Preference dataset rows: 1000
1454
- Train rows: 900
1455
- Validation rows: 100
1456
- MAX_PPO_ROWS: None
1457
- Train fraction: 0.90
1458
- PPO epochs: 2
1459
- Batch size: 2
1460
- Mini-batch size: 1
1461
- Max new tokens: 72
1462
- Max PPO steps per epoch: None
1463
- Planned steps per epoch: 450
1464
- Total planned steps: 900
1465
- Learning rate: 2e-6
1466
- Target KL: 0.10
1467
- Generation temperature: 0.45
1468
- Top-p: 0.78
1469
- Repetition penalty: 1.3
1470
- No-repeat ngram size: 4
1471
- ```
1472
-
1473
- The run completed successfully:
1474
-
1475
- ```text
1476
- Global PPO steps: 900 / 900
1477
- Elapsed time: 04:47:59
1478
- Degenerate ratio: 0.00%
1479
- ```
1480
-
1481
- ---
1482
-
1483
- ## 8.5 PPO Artifact Verification
1484
-
1485
- The Stage 3 notebook includes strict artifact verification.
1486
-
1487
- This is important because PPO notebooks can easily appear to run while silently saving old or incomplete artifacts.
1488
-
1489
- The strict save cell verifies:
1490
-
1491
- ```text
1492
- training_log exists
1493
- training_log records = 900
1494
- expected steps = 900
1495
- MAX_PPO_ROWS = None
1496
- train rows = 900
1497
- valid rows = 100
1498
- NUM_PPO_EPOCHS = 2
1499
- MAX_PPO_STEPS_PER_EPOCH = None
1500
- parameter hash changed after PPO
1501
- PPO inference full actor exists
1502
- PPO LoRA adapter exists
1503
- non-PPO fallback forbidden
1504
- ```
1505
-
1506
- The final strict save output confirms:
1507
-
1508
- ```text
1509
- Final PPO records saved: 900 / expected 900
1510
- Strict full PPO artifact contract passed.
1511
- ```
1512
-
1513
- The parameter change proof confirms:
1514
-
1515
- ```text
1516
- aggregate_hash_changed: true
1517
- changed_trainable_tensors: 506
1518
- unchanged_trainable_tensors: 0
1519
- ```
1520
-
1521
- This proves that PPO training changed the trainable LoRA/value-head parameters rather than merely running a dry notebook.
1522
-
1523
- ---
1524
-
1525
- ## 8.6 Strict PPO-only Runtime
1526
-
1527
- The final runtime is configured so that the UI must use PPO artifacts only.
1528
-
1529
- The strict PPO gate confirms:
1530
-
1531
- ```text
1532
- PPO records: 900
1533
- PPO full actor usable: True
1534
- PPO LoRA adapter usable: True
1535
- Strict PPO-only UI mode: True
1536
- ```
1537
-
1538
- The runtime loading order is:
1539
-
1540
- ```text
1541
- 1. PPO full inference actor if full weights exist
1542
- 2. Otherwise base Qwen3-8B + PPO LoRA adapter
1543
- 3. Non-PPO fallback is forbidden
1544
- ```
1545
-
1546
- This prevents the final demo from accidentally loading an old Baseline 2 model or a stale 150-step PPO proof artifact.
1547
-
1548
- ---
1549
-
1550
- ## 8.7 PPO Validation
1551
-
1552
- The PPO-only validation evaluation uses a held-out validation sample.
1553
-
1554
- The displayed validation summary is:
1555
-
1556
- ```text
1557
- reward: 0.477789
1558
- gold_overlap: 0.255351
1559
- rejected_overlap: 0.155080
1560
- ```
1561
-
1562
- Interpretation:
1563
-
1564
- - reward is positive
1565
- - gold overlap is higher than rejected overlap
1566
- - the PPO-trained actor tends to move closer to preferred answers than rejected answers
1567
-
1568
- This does not mean the PPO model is perfect. It means the reward-shaped behavior is directionally positive.
1569
-
1570
- ---
1571
-
1572
- ## 8.8 PPO Limitations
1573
-
1574
- The PPO run is successful, but the raw PPO generations still show some imperfections.
1575
-
1576
- Observed issues include:
1577
-
1578
- 1. **Process leakage**
1579
- Some outputs still include phrases like:
1580
- ```text
1581
- Okay, let me try to figure out...
1582
- Wait, I need to check again...
1583
- ```
1584
- The reward function penalizes this, but it is not completely eliminated.
1585
 
1586
- 2. **Occasional hallucinated URLs**
1587
- Some raw generations may still invent URLs. The harness fake URL guard is therefore still necessary.
1588
 
1589
- 3. **OCR-style text artifacts**
1590
- Some source chunks contain spacing or OCR issues, and the model may reproduce them.
1591
 
1592
- 4. **KL can be high**
1593
- Some PPO logs show high `objective/kl`, meaning the PPO actor can drift noticeably from the reference model. However, the run completed with:
1594
- ```text
1595
- degenerate_ratio = 0.00%
1596
- ```
1597
- and no detected repetition collapse.
1598
 
1599
- 5. **RAG/Harness remains necessary**
1600
- PPO improves model behavior, but it does not replace retrieval grounding or guardrails.
1601
 
1602
- ---
1603
-
1604
- # 9. TensorTalk UI
1605
-
1606
- The project includes a WhatsApp-style Jupyter HTML UI called **TensorTalk**.
1607
-
1608
- The UI supports:
1609
-
1610
- - chat-style interface
1611
- - TensorCat avatar
1612
- - RAG on/off control
1613
- - web agent on/off control
1614
- - collapsed trace panels
1615
- - retrieved evidence display
1616
- - web evidence display
1617
- - planning/thinking display layer
1618
- - harness decision trace
1619
- - answer grounding information
1620
- - strict PPO artifact loading
1621
- - new chat reset behavior
1622
-
1623
- The UI is part of the engineering contribution because it makes the harness process visible rather than hidden.
1624
-
1625
- ---
1626
-
1627
- # 10. Smoke Tests
1628
-
1629
- ## 10.1 What Smoke Test Means Here
1630
-
1631
- A smoke test is a lightweight system sanity check.
1632
-
1633
- It is not a full evaluation. It is a quick check that the main pipeline still works.
1634
-
1635
- In this project, smoke tests check whether:
1636
-
1637
- ```text
1638
- PPO model loads
1639
- RAG retrieves evidence
1640
- web agent searches official sources
1641
- fake URL guard blocks synthetic links
1642
- answer grounding returns a result
1643
- trace structure is produced
1644
- fallback behavior still works
1645
- ```
1646
-
1647
- ---
1648
-
1649
- ## 10.2 Example Smoke Tests
1650
-
1651
- The notebook defines smoke tests such as:
1652
-
1653
- ```text
1654
- 1. PEKOM should not be routed to AI bachelor page
1655
- 2. Residential college should prefer student-affairs residential page
1656
- 3. CCNA Lab should not invent synthetic URLs
1657
- ```
1658
-
1659
- These are not random examples. They are chosen to test known fragile parts of the pipeline:
1660
-
1661
- - entity routing
1662
- - official URL preference
1663
- - fake URL rejection
1664
- - web/RAG trace structure
1665
-
1666
- ---
1667
-
1668
- # 11. Control Variable Design
1669
-
1670
- The project uses a control-variable style comparison.
1671
-
1672
- The base task remains the same:
1673
-
1674
- ```text
1675
- UM FSKTM Handbook QA
1676
- ```
1677
-
1678
- The base model family remains the same:
1679
-
1680
- ```text
1681
- Qwen3-8B
1682
- ```
1683
-
1684
- The dataset domain remains the same:
1685
-
1686
- ```text
1687
- Undergraduate + postgraduate + general UM Handbook knowledge
1688
- ```
1689
-
1690
- What changes is the system layer:
1691
-
1692
- ```text
1693
- Baseline 1: SFT only
1694
- Baseline 2: SFT + RAG + Harness Agent
1695
- Improved: SFT/RAG/Harness + PPO post-training
1696
- ```
1697
-
1698
- This allows the project to compare which improvements come from:
1699
-
1700
- - parameter learning
1701
- - retrieval grounding
1702
- - metadata-aware scope control
1703
- - official web augmentation
1704
- - harness guardrails
1705
- - PPO reward shaping
1706
-
1707
- This is more rigorous than simply building three unrelated systems.
1708
-
1709
- ---
1710
-
1711
- # 12. Stage-by-stage Comparison Table
1712
-
1713
- | Dimension | Baseline 1: Closed-book SFT | Baseline 2: RAG + SFT + Agent/Harness | Improved Model: PPO + RAG + Agent/Harness |
1714
- |---|---|---|---|
1715
- | Main research question | Can the model memorize and reproduce handbook QA from SFT? | Does retrieval-grounded evidence improve handbook QA? | Can rule-reward PPO further align answer behavior while keeping RAG/Harness control? |
1716
- | Base model | Qwen3-8B | Qwen3-8B | Qwen3-8B |
1717
- | Main training method | Supervised fine-tuning | RAG-augmented supervised fine-tuning | Rule-reward PPO post-training |
1718
- | Dataset used | 1000 SFT QA rows | SFT QA + metadata + RAG KB + RAG eval | 1000 PPO preference rows |
1719
- | Train/validation/test | 800 / 100 / 100 | 8:1:1 RAG-augmented split | 900 train / 100 validation |
1720
- | Retrieval | No | Yes | Yes |
1721
- | Retrieval type | None | Dense embedding + FAISS + metadata-aware rerank | Same RAG runtime reused |
1722
- | Embedding model | None | BAAI/bge-base-en-v1.5 | RAG runtime inherited from Baseline 2 |
1723
- | Top-k evidence | None | Top-3 | Top-3 / runtime-dependent |
1724
- | Metadata awareness | Hidden metadata only, not used at inference | Yes, scope/source/section aware | Yes, used by RAG/Harness runtime |
1725
- | Scope control | Weak; model may confuse UG/PG if prompt is ambiguous | Stronger due to metadata-aware retrieval | Stronger due to RAG + PPO reward + harness |
1726
- | Web agent | No | Yes | Yes |
1727
- | Official domain control | No | Yes, UM/FSKTM official domain whitelist | Yes, same official-source guardrails |
1728
- | Fake URL guard | No | Yes | Yes |
1729
- | WAF handling | No | Yes | Yes |
1730
- | Evidence judge | No | Yes, Qwen evidence judge | Yes |
1731
- | Retry/fallback policy | No | Yes | Yes |
1732
- | Answer grounding judge | No | Yes | Yes |
1733
- | Completeness guard | No | Yes | Yes |
1734
- | UI trace | Basic chat UI | Harness trace panels | Strict PPO + Harness trace panels |
1735
- | LoRA rank | 16 | 8 | PPO actor based on LoRA actor/value setup |
1736
- | Training epochs | 8 SFT epochs | 20 SFT epochs | 2 PPO epochs |
1737
- | Main output artifact | LoRA adapter + merged model + `.pt` export | LoRA adapter, optional non-merged runtime | PPO full inference actor + PPO LoRA adapter + manifest |
1738
- | Artifact strictness | Standard save | Adapter/runtime path checks | Manifest, training log count, parameter hash proof, strict gate |
1739
- | Key metric | Test token F1 ≈ 0.8869 | RAG token F1 ≈ 0.846 on selected eval; retrieval Hit@3 ≈ 0.954 | PPO validation reward ≈ 0.4778; gold overlap > rejected overlap |
1740
- | Strongest contribution | Clean SFT baseline | Evidence-grounded QA and metadata-aware retrieval | Full PPO post-training with strict artifact verification and harnessed runtime |
1741
- | Main weakness | Closed-book hallucination risk | More complex runtime, depends on retriever quality | PPO raw outputs still need Harness/RAG due to possible process leakage and fake URLs |
1742
- | Control variable role | Establishes parameter-only baseline | Adds retrieval and harness while keeping same domain/model family | Adds PPO reward shaping while preserving RAG/Harness pipeline |
1743
-
1744
- ---
1745
-
1746
- # 13. Technical Comparison of the Three Stages
1747
-
1748
- ## 13.1 Content-level Difference
1749
-
1750
- | Content Aspect | Baseline 1 | Baseline 2 | Improved Model |
1751
- |---|---|---|---|
1752
- | Stable handbook facts | Learned into model parameters | Retrieved from handbook KB | Retrieved and answered by PPO-aligned actor |
1753
- | Latest or official web info | Not supported | Supported through official web agent | Supported through same official web agent |
1754
- | UG vs PG distinction | Learned implicitly | Controlled by metadata retrieval | Controlled by metadata retrieval + reward/harness |
1755
- | Evidence visibility | Not shown | Evidence shown in RAG trace | Evidence shown in PPO/Harness trace |
1756
- | Hallucination control | Mostly prompt-based | Retrieval + grounding | Retrieval + grounding + reward penalties |
1757
- | Fake URL control | Not available | Harness URL guard | Harness URL guard + PPO penalty signal |
1758
-
1759
- ---
1760
-
1761
- ## 13.2 Engineering-level Difference
1762
-
1763
- | Engineering Aspect | Baseline 1 | Baseline 2 | Improved Model |
1764
- |---|---|---|---|
1765
- | Notebook purpose | Train and evaluate closed-book SFT model | Build RAG-augmented model and harnessed agent runtime | Train PPO actor and attach it to final harness runtime |
1766
- | Runtime complexity | Low | High | Highest |
1767
- | Debug trace | Basic | Detailed RAG/Web/Harness trace | Detailed PPO/RAG/Web/Harness trace |
1768
- | Failure handling | Minimal | Fallback and guardrail logic | Strict PPO-only fallback prevention plus harness fallback |
1769
- | Artifact verification | Basic output save | Adapter/merged path checks | Manifest, training log count, parameter hash proof, strict gate |
1770
- | Risk of stale artifact use | Moderate | Moderate | Actively guarded against |
1771
- | Demo readiness | Good for simple QA | Strong for grounded QA | Strongest for final controlled system demo |
1772
-
1773
- ---
1774
-
1775
- # 14. Why the Improved Model Does Not Replace RAG
1776
 
1777
- A key design decision is that PPO does not replace RAG.
1778
 
1779
- PPO improves the model’s tendency to:
 
 
 
 
 
 
 
 
 
1780
 
1781
- - answer directly
1782
- - avoid rejected-style answers
1783
- - avoid vague answers
1784
- - avoid process leakage
1785
- - avoid fake URLs
1786
- - avoid repetition collapse
1787
- - use evidence-like wording more appropriately
1788
 
1789
- But PPO does not guarantee factual correctness by itself.
1790
 
1791
- Therefore, the final system still needs:
 
 
 
 
 
 
 
1792
 
1793
- ```text
1794
- RAG for evidence
1795
- Web Agent for official/latest information
1796
- Harness for source control
1797
- Grounding judge for answer verification
1798
- Fallback for weak evidence
1799
- ```
1800
-
1801
- This is the correct division of responsibility:
1802
-
1803
- ```text
1804
- SFT: teaches domain answer style
1805
- RAG: supplies factual evidence
1806
- Agent: finds official external evidence
1807
- Harness: controls trust, routing, fallback, and trace
1808
- PPO: improves answer behavior according to reward preferences
1809
- ```
1810
-
1811
- ---
1812
-
1813
- # 15. Known Limitations
1814
-
1815
- This project is a strong applied LLM system prototype, but it has limitations.
1816
-
1817
- ## 15.1 Not a full human-feedback RLHF system
1818
-
1819
- The PPO stage uses a rule-based reward function. It does not train a separate neural reward model from human preference labels.
1820
-
1821
- Correct description:
1822
-
1823
- ```text
1824
- Rule-reward PPO post-training
1825
- ```
1826
-
1827
- Not:
1828
-
1829
- ```text
1830
- Full RLHF with learned reward model
1831
- ```
1832
-
1833
- ---
1834
-
1835
- ## 15.2 Raw PPO generations can still be imperfect
1836
-
1837
- Observed raw PPO generations may include:
1838
-
1839
- - process leakage
1840
- - occasional hallucinated URLs
1841
- - OCR-like token spacing
1842
- - incomplete course titles
1843
- - noisy source-text reproduction
1844
-
1845
- The final Harness runtime is therefore necessary.
1846
-
1847
- ---
1848
-
1849
- ## 15.3 Web search is constrained
1850
-
1851
- The web agent is intentionally limited to official UM/FSKTM sources. It may refuse or fallback when official evidence is weak.
1852
-
1853
- This is a feature, not a bug, because the system prioritizes trustworthiness over open-ended browsing.
1854
-
1855
- ---
1856
-
1857
- ## 15.4 RAG depends on knowledge base quality
1858
-
1859
- If the RAG KB contains OCR noise or incomplete chunks, the model may inherit that noise. Future work should improve source cleaning and chunk normalization.
1860
-
1861
- ---
1862
-
1863
- ## 15.5 Notebook-based prototype
1864
-
1865
- The project is implemented as notebooks. A production version should separate modules into:
1866
-
1867
- ```text
1868
- data/
1869
- retrieval/
1870
- agent/
1871
- harness/
1872
- training/
1873
- evaluation/
1874
- ui/
1875
- tests/
1876
- ```
1877
-
1878
- ---
1879
-
1880
- # 16. Recommended Usage
1881
-
1882
- This project is intended for research, coursework, and demonstration purposes.
1883
-
1884
- It is not an official Universiti Malaya system.
1885
-
1886
- For official academic decisions, students should always refer to the official handbook, faculty office, or UM/FSKTM official websites.
1887
 
1888
- ---
1889
-
1890
- # 17. Suggested Inference Flow
1891
 
1892
- For final demonstration, use the Improved Model runtime:
 
 
 
 
 
 
 
 
 
 
1893
 
1894
- ```text
1895
- 1. Load PPO full inference actor if available.
1896
- 2. If unavailable, load base Qwen3-8B + PPO LoRA adapter.
1897
- 3. Initialize local handbook RAG.
1898
- 4. Enable official UM/FSKTM web agent if the question may need external/latest information.
1899
- 5. Run through TensorTalkHarnessCore.
1900
- 6. Display answer with evidence trace.
1901
- ```
1902
 
1903
- Strict runtime requirement:
1904
 
1905
  ```text
1906
- Non-PPO fallback is forbidden in the final Improved Model demo.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1907
  ```
1908
 
1909
- ---
1910
-
1911
- # 18. Relation to LangChain / LangGraph / LangSmith Concepts
1912
-
1913
- This project does not claim to be a LangChain implementation. Instead, it uses a from-scratch notebook implementation that follows similar engineering ideas.
1914
-
1915
- Official LangChain ecosystem references that influenced the design include:
1916
 
1917
- - [LangChain Agents documentation](https://docs.langchain.com/oss/javascript/langchain/agents): agents combine language models with tools and can iteratively work toward a goal.
1918
- - [LangGraph Overview](https://docs.langchain.com/oss/python/langgraph/overview): LangGraph focuses on durable execution, streaming, human-in-the-loop, persistence, and orchestration for agent workflows.
1919
- - [LangGraph Graph API](https://docs.langchain.com/oss/python/langgraph/graph-api): agent workflows can be modeled through state, nodes, and edges.
1920
- - [LangGraph Workflows and Agents](https://docs.langchain.com/oss/python/langgraph/workflows-agents): workflows use predetermined code paths, while agents are more dynamic in tool usage and process control.
1921
- - [LangSmith Evaluation](https://docs.langchain.com/langsmith/evaluation): evaluation can be structured around datasets, evaluators, and experiments.
1922
- - [LangSmith Evaluation Types](https://docs.langchain.com/langsmith/evaluation-types): evaluation may include benchmarking, unit tests, regression tests, LLM-as-judge evaluators, code evaluators, and online monitoring.
1923
- - [LangSmith Application-specific Evaluation Approaches](https://docs.langchain.com/langsmith/evaluation-approaches): autonomous agents are commonly discussed in terms of tool calling, memory, and planning.
1924
 
1925
- TensorTalk maps these ideas into a custom system:
 
 
 
1926
 
1927
- | LangChain ecosystem idea | TensorTalk implementation |
1928
- |---|---|
1929
- | Agent uses model + tools | Qwen3 model + local RAG + official web search + URL validator + evidence judge |
1930
- | State | Trace dictionaries, evidence bundles, routing flags, model/backend status |
1931
- | Nodes | Planning, retrieval, web discovery, filtering, judging, generation, grounding, completeness checking |
1932
- | Edges | Conditional retry, official-web route, local-RAG fallback, weak-evidence fallback, final-answer route |
1933
- | Planning | Query classification, scope detection, entity-aware routing, web/RAG decision |
1934
- | Generation | SFT/PPO Qwen3 actor generates with accepted evidence |
1935
- | Evaluation | Evidence judge, answer grounding judge, completeness guard, fake URL checks, smoke tests |
1936
- | Observability | TensorTalk collapsed trace panels and diagnostic logs |
1937
- | Regression/smoke testing | PEKOM route test, residential-college URL test, CCNA synthetic URL test |
1938
 
1939
- This is why the project can be described as:
 
 
 
 
1940
 
1941
- > A from-scratch LangChain/LangGraph-inspired RAG agent harness for UM Handbook QA, with a Planning → Generation → Evaluation control loop.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1942
 
1943
- ---
1944
-
1945
- # 19. Summary
1946
-
1947
- TensorTalk demonstrates a staged LLM system development workflow:
1948
-
1949
- ```text
1950
- Baseline 1:
1951
- Qwen3-8B learns handbook QA through closed-book SFT.
1952
-
1953
- Baseline 2:
1954
- The system adds RAG, dense retrieval, metadata-aware reranking, official web search, and Harness Engineering.
1955
-
1956
- Improved Model:
1957
- The system adds full 1000-row rule-reward PPO post-training, strict artifact verification, and a PPO-only final harness runtime.
1958
- ```
1959
 
1960
- The most important contribution is not only that the model can answer handbook questions, but that the system is controlled, evidence-aware, source-constrained, traceable, and evaluated through a clear baseline progression.
 
 
 
 
 
 
 
 
1961
 
1962
- The final system should be understood as:
1963
 
1964
- > **A Qwen3-8B based UM Handbook RAG Agent, improved with rule-reward PPO and controlled by Harness Engineering.**
 
28
  - tensor-talk
29
  ---
30
 
31
+ # TensorTalk
32
 
33
+ **TensorTalk** is a Universiti Malaya Faculty of Computer Science and Information Technology handbook QA project built around **Qwen3-8B**, supervised fine-tuning, metadata-aware RAG, an official-source web helper, and a guarded harness for traceable answers.
34
 
35
+ The repository is best read as a complete LLM engineering project, not only as a standalone checkpoint. It contains the handbook datasets, SFT/RAG/PPO notebooks, the Baseline 2 LoRA adapter artifacts, retrieval and generation evaluation outputs, and the TensorTalk UI demonstration.
 
 
36
 
37
+ ## Project Screenshot
38
 
39
+ ![TensorTalk RAG Inspector](UM_Handbook/assets/tensortalk-rag-inspector.png)
40
 
41
+ The UI shows the intended runtime behavior: a user asks a UM/FSKTM handbook question, TensorTalk answers with RAG and official-web support enabled, and the interface exposes trace panels for planning, retrieved evidence, selected web evidence, grounding checks, and candidate URLs.
 
 
42
 
43
+ ## What This Project Does
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ TensorTalk answers handbook-style questions about UM FSKTM academic rules, student guidance, programme details, facilities, dress-code guidance, industrial training, supervision policy, postgraduate requirements, and other faculty handbook topics.
 
46
 
47
+ The project compares three stages:
 
48
 
49
+ 1. **Baseline 1: Closed-book SFT Qwen3-8B**
50
+ Fine-tunes Qwen3-8B on handbook question-answer pairs and tests how much the model can answer from parameters alone.
 
 
 
 
51
 
52
+ 2. **Baseline 2: SFT + metadata-aware RAG + agent harness**
53
+ Adds dense retrieval over handbook chunks, metadata reranking, official-source web assistance, and guardrail checks before showing the final answer.
54
 
55
+ 3. **Improved stage: PPO rule-reward post-training + RAG + agent harness**
56
+ Experiments with rule-based reward shaping so responses are more grounded, concise, and aligned with the desired handbook-answer style.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ ## System Design
59
 
60
+ | Layer | Purpose |
61
+ | --- | --- |
62
+ | Qwen3-8B base model | General language model foundation. |
63
+ | LoRA / QLoRA SFT | Adapts the model to UM FSKTM handbook QA style. |
64
+ | Handbook knowledge base | Structured chunks from the undergraduate, postgraduate, and general handbook sources. |
65
+ | Dense retrieval + FAISS | Retrieves candidate evidence using `BAAI/bge-base-en-v1.5` embeddings. |
66
+ | Metadata-aware reranker | Uses scope, section, subsection, and keywords to reduce wrong-context answers. |
67
+ | Official web helper | Searches constrained official UM/FSKTM-related sources when local handbook evidence is not enough. |
68
+ | Harness engineering | Runs source guards, fake-URL guards, evidence checks, grounding checks, retry logic, and fallback rules. |
69
+ | TensorTalk UI | Shows answers together with traceable RAG, web, and harness evidence panels. |
70
 
71
+ ## Data Assets
 
 
 
 
 
 
72
 
73
+ The repository includes the core artifacts used to build and evaluate the assistant:
74
 
75
+ | Artifact | Path | Size / Role |
76
+ | --- | --- | --- |
77
+ | SFT QA dataset | `UM_Handbook/Dataset/SFT_Dataset/SFT_QA_Training_Ready.jsonl` | 1,000 question-answer rows. |
78
+ | SFT metadata | `UM_Handbook/Dataset/SFT_Dataset/SFT_QA_Metadata.jsonl` | 1,000 rows with scope and source metadata. |
79
+ | RAG knowledge base | `UM_Handbook/Dataset/RAG/UM_RAG_Knowledge_Base.jsonl` | 521 retrieval chunks. |
80
+ | RAG evaluation set | `UM_Handbook/Dataset/RAG/UM_RAG_Evaluation_Dataset.jsonl` | 1,000 retrieval-evaluation rows. |
81
+ | Source chunk report | `UM_Handbook/Dataset/Source Chunk Dataset/Source_Chunks_Dataset_report.json` | Chunk distribution and preprocessing notes. |
82
+ | Baseline 2 LoRA adapter | `UM_Handbook/outputs/baseline2_rag_harness_agent/lora_adapter/` | PEFT LoRA adapter and tokenizer assets. |
83
 
84
+ The 521 handbook chunks are split into 58 general, 250 postgraduate, and 213 undergraduate chunks. Low-information cover pages and divider pages are filtered before retrieval.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
+ ## Evaluation Snapshot
 
 
87
 
88
+ | Component | Result |
89
+ | --- | --- |
90
+ | Dataset split | 800 train / 100 validation / 100 test, seed 42. |
91
+ | Baseline 2 train loss | 0.2748. |
92
+ | Retrieval eval size | 1,000 questions. |
93
+ | Hit@1 primary chunk | 82.1%. |
94
+ | Hit@3 primary chunk | 95.4%. |
95
+ | Hit@3 same knowledge group | 99.1%. |
96
+ | Scope match at rank 1 | 99.6%. |
97
+ | Plain generation token-F1 | 0.3391 on the sampled generation evaluation. |
98
+ | RAG generation token-F1 | 0.8460 on the same sampled evaluation. |
99
 
100
+ The retrieval results show why the project moved beyond closed-book SFT. The model can speak in the right academic tone after fine-tuning, but RAG and harness checks make the answers more evidence-grounded and easier to audit.
 
 
 
 
 
 
 
101
 
102
+ ## Repository Map
103
 
104
  ```text
105
+ UM_Handbook/
106
+ Baseline_1_SFT_QWEN3_UM_Handbook_.ipynb
107
+ Baseline_2_RAG_SFT_QWEN3_UM_Handbook_A100_intelligent_harness_agent.ipynb
108
+ Improved_Model_PPO_QWEN3_UM_Handbook_RAG_Agent_Harness.ipynb
109
+ UM_Handbook_Markdown_Preprocess.py
110
+ UM_Source_Chunk_Dataset_Builder.py
111
+ UM_SFT_QA_Dataset_Builder_from_Index.py
112
+ Dataset/
113
+ SFT_Dataset/
114
+ RAG/
115
+ Source Chunk Dataset/
116
+ outputs/
117
+ baseline2_rag_harness_agent/
118
+ lora_adapter/
119
+ retrieval_eval/
120
+ generation_eval/
121
+ rag_augmented_dataset/
122
  ```
123
 
124
+ ## Loading the Baseline 2 Adapter
 
 
 
 
 
 
125
 
126
+ The Baseline 2 adapter is stored in a subfolder of this repository. A typical PEFT loading flow is:
 
 
 
 
 
 
127
 
128
+ ```python
129
+ import torch
130
+ from peft import PeftModel
131
+ from transformers import AutoModelForCausalLM, AutoTokenizer
132
 
133
+ base_model_id = "Qwen/Qwen3-8B"
134
+ adapter_repo = "TensorCat/TensorTalk"
135
+ adapter_subfolder = "UM_Handbook/outputs/baseline2_rag_harness_agent/lora_adapter"
 
 
 
 
 
 
 
 
136
 
137
+ tokenizer = AutoTokenizer.from_pretrained(
138
+ adapter_repo,
139
+ subfolder=adapter_subfolder,
140
+ trust_remote_code=True,
141
+ )
142
 
143
+ base_model = AutoModelForCausalLM.from_pretrained(
144
+ base_model_id,
145
+ torch_dtype=torch.bfloat16,
146
+ device_map="auto",
147
+ trust_remote_code=True,
148
+ )
149
+
150
+ model = PeftModel.from_pretrained(
151
+ base_model,
152
+ adapter_repo,
153
+ subfolder=adapter_subfolder,
154
+ )
155
+ model.eval()
156
+ ```
157
+
158
+ For the full TensorTalk behavior shown in the screenshot, use the adapter together with the RAG knowledge base, FAISS retriever, official-source web helper, and harness checks from the notebooks. The model weights alone do not include the live retrieval index or web-agent runtime.
159
+
160
+ ## Intended Use
161
+
162
+ TensorTalk is intended for research, education, and demonstration of:
163
+
164
+ - handbook question answering for UM FSKTM content;
165
+ - RAG-grounded answer generation;
166
+ - metadata-aware retrieval and reranking;
167
+ - controlled agent behavior over official web sources;
168
+ - harness engineering for evidence checks, fake URL detection, retries, and fallback;
169
+ - comparing closed-book SFT against retrieval-grounded and reward-shaped systems.
170
 
171
+ ## Out-of-Scope Use
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
 
173
+ Do not use TensorTalk as an official university policy authority, legal adviser, disciplinary decision system, or fully autonomous student-support system. University policies can change, and final answers should be checked against the latest official UM/FSKTM documents when used for real administrative decisions.
174
+
175
+ ## Limitations
176
+
177
+ - The strongest behavior comes from the full runtime pipeline, not from the adapter by itself.
178
+ - RAG quality depends on the handbook chunks, retrieval metadata, and official-source availability.
179
+ - The web helper is intentionally constrained to trusted domains; it is not a general web search assistant.
180
+ - PPO in this project is rule-reward post-training, not a large-scale human-feedback RLHF pipeline.
181
+ - Some notebook paths reflect the original training environment and may need local path adjustment before rerunning.
182
 
183
+ ## License
184
 
185
+ This project is released under the Apache 2.0 license.
UM_Handbook/assets/tensortalk-rag-inspector.png ADDED

Git LFS Details

  • SHA256: 3aba6623a02e130048d6d2c43e0d24acfe667f31fc4b38cc9ee531dbabad4966
  • Pointer size: 131 Bytes
  • Size of remote file: 518 kB