# aymane-ait-arab/moroccan-legal-chatbot architecture

> Architecture diagram of the GitHub repository aymane-ait-arab/moroccan-legal-chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aymane-ait-arab/moroccan-legal-chatbot
- Repository: https://github.com/aymane-ait-arab/moroccan-legal-chatbot

## Overview

This repository is a bilingual Moroccan-law chatbot built around a legal-document preparation and retrieval-augmented answering workflow. Official PDFs are extracted to text, parsed into articles, indexed, then intended to support guarded retrieval and cited answers; the Streamlit chat and evaluation dashboard are user-facing surfaces. The sampled chat is still a placeholder and does not call the RAG pipeline, and the main app imports page paths that do not match the tree’s app/views paths. Accordingly, UI-to-RAG wiring is not asserted; the RAG stages follow the README and sampled pipeline code.

Main entry points: [`src/rag/pipeline.py`](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/rag/pipeline.py) (RAG pipeline), [`src/embedding/indexer.py`](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/embedding/indexer.py) (Vector indexer), [`data/processed/txt`](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/tree/main/data/processed/txt) (Extracted text), [`data/processed`](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/tree/main/data/processed) (Processed law articles)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingestion["Legal corpus preparation"]
  node_extract["PDF text extraction<br/>[extract_text.py]"]
  node_parse_laws["Law article parser<br/>[parse_laws.py]"]
  node_generic_parser["Regex article parser<br/>[parser.py]"]
end

subgraph group_retrieval["Retrieval and answering"]
  node_indexer["Vector indexer<br/>[indexer.py]"]
  node_embedding_model{{"BGE-M3 model"}}
  node_guardrails["Scope detector<br/>[detector.py]"]
  node_rag["RAG pipeline<br/>[pipeline.py]"]
  node_llm{{"Gemini LLM"}}
  node_groq{{"Groq LLM option"}}
end

subgraph group_interface["Chat and evaluation UI"]
  node_app["Streamlit entry<br/>[main.py]"]
  node_chat["Bilingual chat view<br/>[chat.py]"]
  node_dashboard["Metrics dashboard<br/>[dashboard.py]"]
end

subgraph group_sources["Legal data"]
  node_pdfs["Official law PDFs"]
  node_rawtext["Extracted text"]
  node_articles["Processed law articles"]
  node_vectorstore[("ChromaDB collection")]
end

node_user(("Legal questioner"))

node_pdfs -->|"extracts text"| node_extract
node_extract -->|"writes text"| node_rawtext
node_rawtext -->|"reads text"| node_parse_laws
node_parse_laws -->|"writes articles"| node_articles
node_rawtext -.->|"parses text"| node_generic_parser
node_generic_parser -->|"saves JSON"| node_articles
node_articles -->|"loads articles"| node_indexer
node_indexer -->|"embeds articles"| node_embedding_model
node_indexer -->|"upserts vectors"| node_vectorstore
node_user -->|"asks question"| node_chat
node_user -->|"submits query"| node_guardrails
node_guardrails -->|"allows in-scope"| node_rag
node_rag -->|"embeds query"| node_embedding_model
node_rag -->|"queries articles"| node_vectorstore
node_rag -->|"generates answer"| node_llm
node_rag -.->|"may generate"| node_groq
node_app -.->|"dispatches chat"| node_chat
node_app -.->|"dispatches dashboard"| node_dashboard

click node_pdfs "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/tree/main/data/raw"
click node_extract "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/parsing/extract_text.py"
click node_rawtext "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/tree/main/data/processed/txt"
click node_parse_laws "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/parsing/parse_laws.py"
click node_generic_parser "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/parsing/parser.py"
click node_articles "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/tree/main/data/processed"
click node_indexer "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/embedding/indexer.py"
click node_guardrails "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/guardrails/detector.py"
click node_rag "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/rag/pipeline.py"
click node_app "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/app/main.py"
click node_chat "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/app/views/chat.py"
click node_dashboard "https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/app/views/dashboard.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_extract,node_parse_laws,node_generic_parser toneBlue
class node_indexer,node_embedding_model,node_guardrails,node_rag,node_llm,node_groq toneAmber
class node_app,node_chat,node_dashboard toneMint
class node_pdfs,node_rawtext,node_articles,node_vectorstore toneRose
class node_user toneIndigo
```

## Components

### Legal corpus preparation

- [PDF text extraction](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/parsing/extract_text.py): `src/parsing/extract_text.py`
- [Law article parser](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/parsing/parse_laws.py): `src/parsing/parse_laws.py`
- [Regex article parser](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/parsing/parser.py): `src/parsing/parser.py`

### Retrieval and answering

- [Vector indexer](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/embedding/indexer.py): `src/embedding/indexer.py`
- **BGE-M3 model**
- [Scope detector](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/guardrails/detector.py): `src/guardrails/detector.py`
- [RAG pipeline](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/src/rag/pipeline.py): `src/rag/pipeline.py`
- **Gemini LLM**
- **Groq LLM option**

### Chat and evaluation UI

- [Streamlit entry](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/app/main.py): `app/main.py`
- [Bilingual chat view](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/app/views/chat.py): `app/views/chat.py`
- [Metrics dashboard](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/blob/main/app/views/dashboard.py): `app/views/dashboard.py`

### Legal data

- [Official law PDFs](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/tree/main/data/raw): `data/raw`
- [Extracted text](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/tree/main/data/processed/txt): `data/processed/txt`
- [Processed law articles](https://github.com/aymane-ait-arab/moroccan-legal-chatbot/tree/main/data/processed): `data/processed`
- **ChromaDB collection**

### Other components

- **Legal questioner**

## Connections

- Official law PDFs → PDF text extraction: extracts text (evidence: `src/parsing/extract_text.py`)
- PDF text extraction → Extracted text: writes text (evidence: `src/parsing/extract_text.py`)
- Extracted text → Law article parser: reads text (evidence: `src/parsing/parse_laws.py`)
- Law article parser → Processed law articles: writes articles (evidence: `src/parsing/parse_laws.py`)
- Extracted text → Regex article parser: parses text
- Regex article parser → Processed law articles: saves JSON (evidence: `src/parsing/parser.py`)
- Processed law articles → Vector indexer: loads articles (evidence: `src/embedding/indexer.py`)
- Vector indexer → BGE-M3 model: embeds articles (evidence: `src/embedding/indexer.py`)
- Vector indexer → ChromaDB collection: upserts vectors (evidence: `src/embedding/indexer.py`)
- Legal questioner → Bilingual chat view: asks question (evidence: `app/views/chat.py`)
- Legal questioner → Scope detector: submits query (evidence: `README.md`)
- Scope detector → RAG pipeline: allows in-scope (evidence: `README.md`)
- RAG pipeline → BGE-M3 model: embeds query (evidence: `src/rag/pipeline.py`)
- RAG pipeline → ChromaDB collection: queries articles (evidence: `src/rag/pipeline.py`)
- RAG pipeline → Gemini LLM: generates answer (evidence: `src/rag/pipeline.py`)
- RAG pipeline → Groq LLM option: may generate (evidence: `README.md`)
- Streamlit entry → Bilingual chat view: dispatches chat
- Streamlit entry → Metrics dashboard: dispatches dashboard
