NyayaLM is a fine-tuned large language model specialized in Nepal law, built on top of Qwen3-1.7B. It is designed to answer legal questions accurately and professionally with reference to the Nepal Civil Code and nine major Nepali statutes.
"Nyaya" (न्याय) means justice in Nepali and Sanskrit.
The model was trained on a combined dataset of two custom legal corpora:
1. 9-Law RAG Dataset (9 law_rag_train.jsonl)
A Retrieval-Augmented Generation (RAG)-style dataset covering nine major Nepali laws, including question-answer pairs grounded in statutory text. This dataset teaches the model to cite and reason from specific legal provisions.
A curated instruction-following dataset based on the Nepal Civil Code (Muluki Dewani Sanhita, 2074 B.S.), containing structured legal Q&A without additional input context — training the model for direct legal reasoning.
Dataset Split
Split
Proportion
Purpose
Train
80%
Model training
Validation
10%
Evaluation during training
Test
10%
Final held-out evaluation
All records were formatted using the Qwen3-Instruct ChatML template with the following system prompt:
You are NyayaLM, a legal assistant specializing in Nepal law.
Answer legal questions accurately based on Nepal Civil Code and other Nepal laws.
⚙️ Training Configuration
Hyperparameter
Value
Optimizer
AdamW 8-bit
Learning Rate
2e-5
LR Scheduler
Cosine
Warmup Ratio
0.08 (~51 warmup steps)
Epochs
1
Batch Size (per device)
4
Gradient Accumulation Steps
4 (effective batch = 16)
Weight Decay
0.001
Max Grad Norm
1.0
Precision
FP16
Eval Strategy
Every 40 steps
Save Strategy
Every 80 steps
Best Model Selection
Lowest eval_loss
Seed
3407
Framework
Unsloth + TRL SFTTrainer
Training used train_on_responses_only — only the assistant turns were used to compute the loss, ignoring user instruction tokens. This improves instruction-following quality.
📈 Training Curves
Training converged smoothly over ~500 steps. Both training and validation loss decreased steadily with no signs of overfitting.
Training Loss: Started at ~1.6, converged to ~0.53
Validation Loss: Started at ~0.86, converged to ~0.60
📊 Evaluation Results
Evaluated on the held-out test set (10%) using 10 sampled examples with the following inference settings:
Note on BERTScore (0.9167): The high BERTScore reflects strong semantic similarity between generated and reference answers — the model captures the legal meaning well even when surface phrasing varies. ROUGE scores are lower by nature since legal answers can be paraphrased in multiple correct ways.
🚀 Usage
Basic Inference
python
1from unsloth import FastLanguageModel
2import torch
34model, tokenizer = FastLanguageModel.from_pretrained(5 model_name ="chhatramani/nyayalm1.7B_civil9law",6 max_seq_length =2048,7 load_in_4bit =True,8)9model = FastLanguageModel.for_inference(model)1011SYSTEM_PROMPT =(12"You are NyayaLM, a legal assistant specializing in Nepal law. "13"Answer legal questions accurately based on Nepal Civil Code and other Nepal laws."14)1516messages =[17{"role":"system","content": SYSTEM_PROMPT},18{"role":"user","content":"What are the grounds for divorce under Nepal Civil Code?"}19]2021input_text = tokenizer.apply_chat_template(22 messages,23 tokenize=False,24 add_generation_prompt=True,25 enable_thinking=False,26)2728inputs = tokenizer(input_text, return_tensors="pt").to("cuda")2930with torch.no_grad():31 outputs = model.generate(32**inputs,33 max_new_tokens=800,34 temperature=0.4,35 top_p=0.9,36 repetition_penalty=1.1,37)3839response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)40print(response)
Using with Transformers (standard)
python
1from transformers import AutoModelForCausalLM, AutoTokenizer
23tokenizer = AutoTokenizer.from_pretrained("chhatramani/nyayalm1.7B_civil9law")4model = AutoModelForCausalLM.from_pretrained(5"chhatramani/nyayalm1.7B_civil9law",6 torch_dtype="auto",7 device_map="auto"8)910messages =[11{"role":"system","content":"You are NyayaLM, a legal assistant specializing in Nepal law."},12{"role":"user","content":"Explain the inheritance rights of a daughter under Nepal Civil Code."}13]1415text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)16inputs = tokenizer(text, return_tensors="pt").to(model.device)17outputs = model.generate(**inputs, max_new_tokens=512)18print(tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True))
🏛️ Covered Legal Domains
NyayaLM is trained to assist with questions related to (but not limited to):
9 Major Nepal Laws covered in the RAG dataset (civil, criminal, procedural statutes)
Legal reasoning, statutory interpretation, and rights-based queries under Nepali law
⚠️ Limitations & Disclaimer
This model is for informational and research purposes only. It is not a substitute for professional legal advice.
Evaluations were conducted on a small test sample (n=10); performance may vary on diverse real-world queries.
The model may occasionally hallucinate legal provisions. Always verify answers against official legal texts.
Laws change — this model reflects training data up to its creation date and may not capture recent amendments.
The model performs best on English-language legal queries related to Nepal law.
📋 Citation
If you use NyayaLM in your research or projects, please cite:
bibtex
1@misc{nyayalm2025,
2 author = {chhatramani},
3 title = {NyayaLM: A Legal Language Model for Nepal Law},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/chhatramani/nyayalm1.7B_civil9law}},
7}