ASG00-0/Saudi-Legal-RAG-System

A Python‑based Retrieval‑Augmented Generation (RAG) system that scrapes Saudi legal statutes, splits them into article‑level chunks, builds a FAISS index with OpenAI embeddings, and answers Arabic legal questions using the ALLaM‑7B‑Instruct model. The pipeline runs on an RTX 5070 Ti (FP16) and provides concise, citation‑backed answers.

Python

0

6 commits

updated Dec 8, 2025

See the code

README

Saudi Legal RAG System

A Python‑based Retrieval‑Augmented Generation (RAG) system that scrapes Saudi legal statutes, splits them into article‑level chunks, builds a FAISS index with OpenAI embeddings, and answers Arabic legal questions using the ALLaM‑7B‑Instruct model. The pipeline runs on an RTX 5070 Ti (FP16) and provides concise, citation‑backed answers.

Features

  • Web scraping and cleaning of Saudi legal statutes.
  • Precise Arabic article splitting with a robust regex.
  • Deduplication and JSONL output of cleaned chunks.
  • FAISS index creation (build_faiss.py).
  • Retrieval of top‑5 relevant chunks.
  • Prompt engineering that treats retrieved chunks as reference material while allowing the model to add extra context.
  • CLI interface.

How it was built

  1. Data collection & cleaning – Legal texts were gathered in the segments/ folder and normalized (whitespace, punctuation) using custom Python utilities.
  2. Chunking – A regular‑expression ((?m)^\s*المادة) splits the statutes into article‑level chunks, each saved as a JSON‑L line by generateJSONL.py.
  3. Embedding & indexing – Each chunk is embedded with OpenAI’s text‑embedding‑3‑large model and stored in a FAISS IndexFlatL2 via build_faiss.py.
  4. LLM integration – The Arabic‑focused model humain‑ai/ALLaM‑7B‑Instruct‑preview is loaded in FP16 (no bitsandbytes) on an RTX 5070 Ti GPU.
  5. RAG pipeline – rag_query.py retrieves the top‑5 most similar chunks, builds a prompt that treats those chunks as reference material, and generates the final answer.

Use Cases

Legal professionals, students, or anyone needing quick access to Saudi statutes can get accurate, citation‑backed answers without manually searching through long documents.

Setup

  1. Clone the repository (or copy the project folder).
  2. Create a Python 3.14 virtual environment and activate it:
    python -m venv venv
    .\venv\Scripts\activate
    
  3. Install required packages:
    pip install -r requirements.txt
    # or manually:
    pip install faiss-cpu openai tqdm transformers torch sentencepiece protobuf
    
  4. Set your OpenAI API key (environment variable):
    $env:OPENAI_API_KEY="sk-..."
    
  5. Download the ALLaM model – the script will fetch humain-ai/ALLaM-7B-Instruct-preview automatically on first run.

Data Preparation

Place your raw statute .txt files in the segments/ directory. Run:

python generateJSONL.py

This creates chunks.jsonl with cleaned article chunks.

Build the Vector Index

python build_faiss.py

Creates faiss_index.bin and metadata.jsonl.

Run a Query

python rag_query.py "ما هي شروط الشهادة في المحكمة؟"

The script retrieves the top‑5 relevant articles, feeds them to the LLM, and prints a concise answer.

Troubleshooting

  • Missing protobuf or sentencepiece: install with pip install protobuf sentencepiece.
  • GPU memory errors: ensure the GPU has enough free VRAM; the model loads in ~14 GB FP16.
  • FAISS index not found: run build_faiss.py before querying.

License

This project is provided for educational purposes. The ALLaM model is subject to its own license on Hugging Face.

Contact

Author: Abdalmalik alGhamdi Email: [email protected]

ASG00-0/Saudi-Legal-RAG-System

A Python‑based Retrieval‑Augmented Generation (RAG) system that scrapes Saudi legal statutes, splits them into article‑level chunks, builds a FAISS index with OpenAI embeddings, and answers Arabic legal questions using the ALLaM‑7B‑Instruct model. The pipeline runs on an RTX 5070 Ti (FP16) and provides concise, citation‑backed answers.

Python

0

6 commits

updated Dec 8, 2025

See the code

README

Saudi Legal RAG System

A Python‑based Retrieval‑Augmented Generation (RAG) system that scrapes Saudi legal statutes, splits them into article‑level chunks, builds a FAISS index with OpenAI embeddings, and answers Arabic legal questions using the ALLaM‑7B‑Instruct model. The pipeline runs on an RTX 5070 Ti (FP16) and provides concise, citation‑backed answers.

Features

  • Web scraping and cleaning of Saudi legal statutes.
  • Precise Arabic article splitting with a robust regex.
  • Deduplication and JSONL output of cleaned chunks.
  • FAISS index creation (build_faiss.py).
  • Retrieval of top‑5 relevant chunks.
  • Prompt engineering that treats retrieved chunks as reference material while allowing the model to add extra context.
  • CLI interface.

How it was built

  1. Data collection & cleaning – Legal texts were gathered in the segments/ folder and normalized (whitespace, punctuation) using custom Python utilities.
  2. Chunking – A regular‑expression ((?m)^\s*المادة) splits the statutes into article‑level chunks, each saved as a JSON‑L line by generateJSONL.py.
  3. Embedding & indexing – Each chunk is embedded with OpenAI’s text‑embedding‑3‑large model and stored in a FAISS IndexFlatL2 via build_faiss.py.
  4. LLM integration – The Arabic‑focused model humain‑ai/ALLaM‑7B‑Instruct‑preview is loaded in FP16 (no bitsandbytes) on an RTX 5070 Ti GPU.
  5. RAG pipeline – rag_query.py retrieves the top‑5 most similar chunks, builds a prompt that treats those chunks as reference material, and generates the final answer.

Use Cases

Legal professionals, students, or anyone needing quick access to Saudi statutes can get accurate, citation‑backed answers without manually searching through long documents.

Setup

  1. Clone the repository (or copy the project folder).
  2. Create a Python 3.14 virtual environment and activate it:
    python -m venv venv
    .\venv\Scripts\activate
    
  3. Install required packages:
    pip install -r requirements.txt
    # or manually:
    pip install faiss-cpu openai tqdm transformers torch sentencepiece protobuf
    
  4. Set your OpenAI API key (environment variable):
    $env:OPENAI_API_KEY="sk-..."
    
  5. Download the ALLaM model – the script will fetch humain-ai/ALLaM-7B-Instruct-preview automatically on first run.

Data Preparation

Place your raw statute .txt files in the segments/ directory. Run:

python generateJSONL.py

This creates chunks.jsonl with cleaned article chunks.

Build the Vector Index

python build_faiss.py

Creates faiss_index.bin and metadata.jsonl.

Run a Query

python rag_query.py "ما هي شروط الشهادة في المحكمة؟"

The script retrieves the top‑5 relevant articles, feeds them to the LLM, and prints a concise answer.

Troubleshooting

  • Missing protobuf or sentencepiece: install with pip install protobuf sentencepiece.
  • GPU memory errors: ensure the GPU has enough free VRAM; the model loads in ~14 GB FP16.
  • FAISS index not found: run build_faiss.py before querying.

License

This project is provided for educational purposes. The ALLaM model is subject to its own license on Hugging Face.

Contact

Author: Abdalmalik alGhamdi Email: [email protected]