File size: 7,853 Bytes
89c85d4
 
 
 
 
 
 
 
 
9391abd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---

title: ViLexAgent
emoji: ⚖️
colorFrom: blue
colorTo: indigo
sdk: docker
pinned: false
---


# ⚖️ ViLexAgent

> **A Multi-Agent RAG System for Vietnamese Legal Q&A**  
> Supports Vietnamese labor law, food safety regulations, and international trade agreements (EVFTA, CPTPP).

---

## Overview

ViLexAgent is an agentic Retrieval-Augmented Generation (RAG) system that answers complex legal questions about Vietnamese law and its compliance with international trade agreements. The system decomposes user queries into sub-questions, retrieves relevant legal documents from a vector database, cross-references domestic law with international standards, and synthesizes a cited, legally-grounded answer.

**Key capabilities:**
- Query decomposition into domain-specific sub-questions (labor law, food safety)
- Hybrid retrieval from domestic Vietnamese legal documents and international treaty clauses (EVFTA, CPTPP)
- Automatic cross-reference analysis with alignment scoring (`aligned` / `conflict` / `gap` / `no_international`)
- Expired document detection with explicit warnings
- Step-by-step reasoning trace visible in the UI

---

## Architecture

```

User Query



┌─────────────────────┐

│   Query Decomposer  │  → Sub-questions + domain tags (labor / food_safety)

└─────────────────────┘


    ├──────────────────────────────────┐

    ▼                                  ▼

┌──────────────────┐        ┌──────────────────────────┐

│ Domestic         │        │ International             │

│ Retriever        │        │ Retriever                 │

│ (Qdrant + Jina)  │        │ (Qdrant + Jina)           │

└──────────────────┘        └──────────────────────────┘

    │                                  │

    └──────────────┬───────────────────┘


        ┌─────────────────────┐

        │   Cross-Reference   │  → alignment: aligned / conflict / gap

        └─────────────────────┘



        ┌─────────────────────┐

        │     Synthesizer     │  → Final answer with citations

        └─────────────────────┘

```

**Tech Stack:**

| Layer | Technology |
|---|---|
| UI | Chainlit 2.11 |
| Agent Orchestration | LangGraph 1.1 |
| Vector Database | Qdrant |
| Embedding Model | Jina Embeddings v5 Small (4-bit, CUDA) |
| LLM Backend | LiteLLM (OpenAI-compatible) |
| OCR / PDF Parsing | PaddleOCR, PyMuPDF |
| Vietnamese NLP | Underthesea |
| Experiment Tracking | MLflow |
| Evaluation | RAGAS |
| Runtime | Python 3.13, Poetry |

---

## Project Structure

```

vilexagent/

├── app/

│   └── vilexagent_ui.py       # Chainlit UI entry point

├── src/

│   ├── agents/

│   │   ├── state.py               # AgentState (LangGraph TypedDict)

│   │   ├── query_decomposer.py    # Query decomposition node

│   │   ├── domestic_retriever.py  # Vietnamese law retrieval node

│   │   ├── international_retriever.py  # EVFTA/CPTPP retrieval node

│   │   ├── cross_reference.py     # Cross-reference analysis node

│   │   ├── synthesizer.py         # Answer synthesis node

│   │   └── graph.py               # LangGraph pipeline definition

│   ├── ingestion/                 # Document ingestion pipeline

│   ├── retrieval/                 # Retrieval utilities

│   └── utils/

│       ├── llm.py                 # LLM client (LiteLLM wrapper)

│       ├── model_loader.py        # Singleton embedding model loader

│       ├── logger.py              # Loguru logger

│       └── json_utils.py          # Robust JSON extractor

├── evaluation/                    # RAGAS benchmark suite

├── docker/                        # Docker/Qdrant setup

├── tests/

├── pyproject.toml

└── chainlit.md

```

---

## Installation

### Prerequisites

- Python 3.13
- [Poetry](https://python-poetry.org/docs/#installation)
- CUDA-capable GPU (recommended: 4GB+ VRAM)
- [Qdrant](https://qdrant.tech/documentation/quick-start/) running locally on port `6333`
- An OpenAI-compatible LLM API endpoint on port `3001`

### Steps

**1. Clone the repository**
```bash

git clone https://github.com/dnAnh1523/vilexagent.git

cd vilexagent

```

**2. Install dependencies**
```bash

poetry install

```

**3. Set up environment variables**

Create a `.env` file in the project root:
```env

LLM_BASE_URL=http://localhost:3001/v1

LLM_API_KEY=your_api_key_here

LLM_MODEL=your_model_name

```

**4. Start Qdrant**
```bash

docker compose -f docker/docker-compose.yml up -d

```

**5. Ingest documents**

Place your Vietnamese legal PDFs and international treaty documents in the `data/` directory, then run the ingestion pipeline (see `src/ingestion/`).

**6. Run the app**
```bash

poetry run chainlit run app/vilexagent_ui.py --port 8000

```

Open `http://localhost:8000` in your browser.

---

## Usage

Once running, you can ask questions in Vietnamese about:

- **Labor law** — probationary periods, wrongful termination compensation, minimum wage, overtime
- **Food safety** — export conditions, hygiene standards, quarantine requirements
- **International compliance** — whether Vietnamese law meets EVFTA/CPTPP standards

The UI displays a step-by-step reasoning trace (collapsible) showing which documents were retrieved, cross-reference results, and alignment scores before the final answer.

**Example questions:**
```

Thời gian thử việc tối đa theo pháp luật lao động Việt Nam là bao lâu?



Nếu người sử dụng lao động đơn phương chấm dứt hợp đồng trái pháp luật

thì phải bồi thường những gì cho người lao động?



Việt Nam có đáp ứng các tiêu chuẩn lao động của CPTPP về tự do hiệp hội không?

```

---

## Configuration

Key settings in `chainlit.md` (UI welcome screen) and `.chainlit/config.toml`:

| Setting | Location | Description |
|---|---|---|
| `cot` | `config.toml [UI]` | Chain-of-thought display: `"full"` shows all steps |
| `session_timeout` | `config.toml [project]` | Session retention in seconds |
| `LLM_BASE_URL` | `.env` | OpenAI-compatible LLM endpoint |
| `LLM_MODEL` | `.env` | Model name passed to LiteLLM |

---

## Evaluation

The system includes a RAGAS-based evaluation suite in `evaluation/` with a benchmark of labeled legal Q&A pairs across three difficulty types:

- **Type A** — Single-domain factual queries (domestic law only)
- **Type B** — Multi-aspect domestic queries requiring reasoning across clauses
- **Type C** — Cross-reference queries requiring domestic + international alignment

Run evaluation:
```bash

poetry run python evaluation/run_evaluation.py

```

Results are tracked with MLflow. Start the MLflow UI:
```bash

poetry run mlflow ui

```

---

## License

This project is for educational and portfolio purposes.

---

*Built with LangGraph · Qdrant · Chainlit · Jina Embeddings*