# lingyoumax/qwen4law architecture

> Architecture diagram of the GitHub repository lingyoumax/qwen4law, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lingyoumax/qwen4law
- Repository: https://github.com/lingyoumax/qwen4law

## Overview

##### Project Type and Purpose
- This is an open-source, research-oriented legal Q&A system built on RAG (Retrieval-Augmented Generation).
- It orchestrates a pipeline for:
- ingesting and preprocessing Chinese legal documents
- sampling a representative subset via embedding clustering
- generating datasets for three tasks (retrieval, reranking, answer generation)
- fine-tuning three model families (Embedding, Reranker, LLM) with various algorithms (Freeze, QLoRA, PPO, DPO, etc.)
- evaluating each component (token lengths, retrieval accuracy, margin, loss curves).

##### File Structure Analysis
- Top-level scripts group naturally into:
- Data Ingestion & Preprocessing: getLaws.py, processlaws.py
- Sampling & Dataset Generation: selectLaws.py, getRetrieverDataset.py, processRetrieverDataset.py, getLLMDataset_SFT.py
- Token Length Evaluation: evalEmbeddingTokenLength.py, evalRerankerTokenLength.py, evalLLMTokenLength.py
- Model Training: trainEmbeddingModel_*.py, trainRerankerModel_QLoRA.py, trainLLM_*.py
- Model Evaluation: evalEmbeddingModel_*.py, evalRerankerModel_based.py, evalLLM_*.py
- Utilities & Config: settings.py, tools.py
- Experiment Selection & Analysis: evalLawsSelected.py
- README.md and Figs/ for documentation and plots.

##### Key Insights from README
- RAG architecture with a Retriever → Reranker → LLM chain
- Embedding and Reranker models use dual-encoder architectures; LLM uses single-autoregressive
- Fine-tuning techniques compared: Freeze vs. QLoRA vs. full-RL (PPO, DPO, GRPO, IPO, RRHF)
- Heavy reliance on Qwen3 models and Alibaba Cloud API for generation.

##### Main Components
a. Data Layer
- Source: National Laws Database (crawler or manual download)
- Preprocessing: processlaws.py → clean text, remove headers/footers, segment by structure
- Sampling: selectLaws.py (use Qwen3-Embedding to sample 10k diverse chunks)
b. Dataset Generation
- Retriever Dataset: getRetrieverDataset.py + processRetrieverDataset.py (query, +/– docs)
- Reranker Dataset: reuse embedding dataset (query, doc pairs)
- LLM Dataset: getLLMDataset_SFT.py (query, doc, answer triplets)
c. Training & Evaluation Layer
- Embedding Model: trainEmbeddingModel_Freeze.py / _QLoRA.py → evalEmbeddingModel_*.py
- Reranker Model: trainRerankerModel_QLoRA.py → evalRerankerModel_based.py
- LLM Model: trainLLM_SFT.py, trainLLM_PPO.py, etc. → evalLLM_*.py
d. Inference / RAG Service
- Retriever: dual-encoder embeddings + nearest-neighbor search (vector store)
- Reranker: pairwise regressor refines top-k candidates
- LLM: final answer generator
- External: Qwen3 API, Alibaba Cloud for batch generation
e. Supporting Utilities
- settings.py for hyperparameters
- tools.py for common functions (tokenization wrapper, clustering, logging)

##### Relationships & Data Flow
- Raw Laws → Preprocessing → Chunked Corpus
- Chunked Corpus → Embedding Model (vectorize) → Sampling → Embedding Dataset
- Embedding Dataset → Retriever Training → Retriever Model
- Embedding Dataset → Reranker Training → Reranker Model
- At Query Time: Query → Retriever → top-k docs → Reranker → best doc → LLM → Answer

##### Architectural Patterns & Design Principles
- Modular pipeline: each stage is a standalone script or module
- Separation of concerns: data ingestion, sampling, dataset gen, training, evaluation
- RAG pattern: decoupled retrieval and generation
- Compare fine-tuning strategies via unified interfaces (train vs. eval scripts)
- Parameterize via settings.py to allow easy experiment reproduction

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data & Preprocessing Layer
    subgraph "Data & Preprocessing"
        direction TB
        RawData["National Laws Database"]:::data
        GL["getLaws.py"]:::data
        PL["processlaws.py"]:::data
        Chunked["Chunked Corpus"]:::data
    end
    RawData --> GL --> PL --> Chunked

    %% Dataset Generation Layer
    subgraph "Dataset Generation"
        direction TB
        selectLaws["selectLaws.py"]:::dataset
        embedData["Embedding Dataset"]:::dataset
        getRet["getRetrieverDataset.py"]:::dataset
        procRet["processRetrieverDataset.py"]:::dataset
        retData["Retriever Dataset"]:::dataset
        rerankData["Reranker Dataset"]:::dataset
        getLLM["getLLMDataset_SFT.py"]:::dataset
        llmData["LLM Dataset"]:::dataset
    end
    Chunked --> selectLaws --> embedData
    embedData --> getRet --> procRet --> retData
    embedData --> rerankData
    embedData --> getLLM --> llmData

    %% Training & Evaluation Layer
    subgraph "Training & Evaluation"
        direction TB
        subgraph "Embedding Model"
            direction TB
            trEmbF["trainEmbeddingModel_Freeze.py"]:::train
            trEmbQ["trainEmbeddingModel_QLoRA.py"]:::train
            evEmbF["evalEmbeddingModel_Freeze.py"]:::train
            evEmbQ["evalEmbeddingModel_QLoRA.py"]:::train
            evEmbB["evalEmbeddingModel_based.py"]:::train
        end
        subgraph "Reranker Model"
            direction TB
            trRer["trainRerankerModel_QLoRA.py"]:::train
            evRer["evalRerankerModel_based.py"]:::train
        end
        subgraph "LLM Model"
            direction TB
            trLLMSFT["trainLLM_SFT.py"]:::train
            trLLMPPO["trainLLM_PPO.py"]:::train
            trLLMDPO["trainLLM_DPO.py"]:::train
            trLLMGRPO["trainLLM_GRPO.py"]:::train
            trLLMIPO["trainLLM_IPO.py"]:::train
            trLLMRRHF["trainLLM_RRHF.py"]:::train
            evLLMSFT["evalLLM_SFT.py"]:::train
            evLLMB["evalLLM_based.py"]:::train
        end
    end
    embedData --> trEmbF --> evEmbF
    trEmbF --> evEmbQ
    trEmbF --> evEmbB
    embedData --> trEmbQ --> evEmbF
    embedData --> trRer --> evRer
    llmData --> trLLMSFT --> evLLMSFT
    llmData --> trLLMPPO --> evLLMB
    llmData --> trLLMDPO --> evLLMB
    llmData --> trLLMGRPO --> evLLMB
    llmData --> trLLMIPO --> evLLMB
    llmData --> trLLMRRHF --> evLLMB

    %% Inference & Analysis Layer
    subgraph "Inference & RAG Service"
        direction TB
        Query["User Query"]:::infra
        Retriever["Retriever Service"]:::infra
        Reranker["Reranker Service"]:::infra
        LLM["LLM Service"]:::infra
        VectorStore["Vector Store"]:::data
        Qwen3["Qwen3 API"]:::external
        Alibaba["Alibaba Cloud"]:::external
        evalSel["evalLawsSelected.py"]:::infra
    end
    Query --> Retriever --> VectorStore
    Retriever --> Reranker --> LLM --> Answer["Answer"]:::infra
    Retriever -.-> Qwen3
    LLM -.-> Alibaba
    evalSel

    %% Utilities & Documentation
    settings["settings.py"]:::utils
    tools["tools.py"]:::utils
    README["README.md"]:::doc
    Figs["Figs/"]:::doc

    %% Click Events
    click GL "https://github.com/lingyoumax/qwen4law/blob/main/getLaws.py"
    click PL "https://github.com/lingyoumax/qwen4law/blob/main/processlaws.py"
    click selectLaws "https://github.com/lingyoumax/qwen4law/blob/main/selectLaws.py"
    click getRet "https://github.com/lingyoumax/qwen4law/blob/main/getRetrieverDataset.py"
    click procRet "https://github.com/lingyoumax/qwen4law/blob/main/processRetrieverDataset.py"
    click getLLM "https://github.com/lingyoumax/qwen4law/blob/main/getLLMDataset_SFT.py"
    click trEmbF "https://github.com/lingyoumax/qwen4law/blob/main/trainEmbeddingModel_Freeze.py"
    click trEmbQ "https://github.com/lingyoumax/qwen4law/blob/main/trainEmbeddingModel_QLoRA.py"
    click evEmbF "https://github.com/lingyoumax/qwen4law/blob/main/evalEmbeddingModel_Freeze.py"
    click evEmbQ "https://github.com/lingyoumax/qwen4law/blob/main/evalEmbeddingModel_QLoRA.py"
    click evEmbB "https://github.com/lingyoumax/qwen4law/blob/main/evalEmbeddingModel_based.py"
    click trRer "https://github.com/lingyoumax/qwen4law/blob/main/trainRerankerModel_QLoRA.py"
    click evRer "https://github.com/lingyoumax/qwen4law/blob/main/evalRerankerModel_based.py"
    click trLLMSFT "https://github.com/lingyoumax/qwen4law/blob/main/trainLLM_SFT.py"
    click trLLMPPO "https://github.com/lingyoumax/qwen4law/blob/main/trainLLM_PPO.py"
    click trLLMDPO "https://github.com/lingyoumax/qwen4law/blob/main/trainLLM_DPO.py"
    click trLLMGRPO "https://github.com/lingyoumax/qwen4law/blob/main/trainLLM_GRPO.py"
    click trLLMIPO "https://github.com/lingyoumax/qwen4law/blob/main/trainLLM_IPO.py"
    click trLLMRRHF "https://github.com/lingyoumax/qwen4law/blob/main/trainLLM_RRHF.py"
    click evLLMSFT "https://github.com/lingyoumax/qwen4law/blob/main/evalLLM_SFT.py"
    click evLLMB "https://github.com/lingyoumax/qwen4law/blob/main/evalLLM_based.py"
    click evalSel "https://github.com/lingyoumax/qwen4law/blob/main/evalLawsSelected.py"
    click settings "https://github.com/lingyoumax/qwen4law/blob/main/settings.py"
    click tools "https://github.com/lingyoumax/qwen4law/blob/main/tools.py"
    click README "https://github.com/lingyoumax/qwen4law/blob/main/README.md"
    click Figs "https://github.com/lingyoumax/qwen4law/tree/main/Figs/"

    %% Styles
    classDef data fill:#D0E6FF,stroke:#005B96,color:#003F63
    classDef dataset fill:#DFF2BF,stroke:#4F8A10,color:#4F8A10
    classDef train fill:#FFE4B5,stroke:#FF8C00,color:#8B4513
    classDef infra fill:#E2D5F9,stroke:#6A0DAD,color:#4B0082
    classDef external fill:#F0F0F0,stroke:#A9A9A9,color:#696969
    classDef utils fill:#F5F5F5,stroke:#A0A0A0,color:#404040
    classDef doc fill:#FFFACD,stroke:#DAA520,color:#8B4513
```
