File size: 7,279 Bytes
3109ad4
b760a5b
3109ad4
8ad62dc
3109ad4
851f2ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
---
title: CyberLegalAIendpoint
sdk: docker
app_port: 8000
---
# CyberLegal AI - LangGraph Agent

Advanced cyber-legal assistant powered by LangGraph + LightRAG + GPT-5-Nano for European regulations expertise.

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client API    │────│  LangGraph Agent │────│  LightRAG Serverβ”‚
β”‚   (Port 8000)  β”‚    β”‚   (Orchestration)β”‚    β”‚  (Port 9621)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚ GPT-5-Nano   β”‚
                       β”‚ (Reasoning)   β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸš€ Quick Start

### Using Docker Compose (Recommended)

1. **Environment Setup**
   ```bash
   # Copy and configure environment
   cp .env.example .env
   
   # Edit .env with your API keys
   # OPENAI_API_KEY=your_openai_key
   # LIGHTRAG_API_KEY=your_lightrag_key (optional)
   ```

2. **Deploy**
   ```bash
   docker-compose up -d
   ```

3. **Verify Deployment**
   ```bash
   curl http://localhost:8000/health
   ```

### Using Docker Directly

```bash
# Build the image
docker build -t cyberlegal-ai .

# Run the container
docker run -d \
  --name cyberlegal-ai \
  -p 8000:8000 \
  -e OPENAI_API_KEY=your_key \
  -v $(pwd)/rag_storage:/app/rag_storage \
  cyberlegal-ai
```

## πŸ“‘ API Usage

### Base URL
```
http://localhost:8000
```

### Endpoints

#### Chat with Assistant
```bash
curl -X POST "http://localhost:8000/chat" \
  -H "Content-Type: application/json" \
  -d '{
    "message": "What are the main obligations under GDPR?",
    "role": "client",
    "jurisdiction": "EU",
    "conversationHistory": []
  }'
```

#### Health Check
```bash
curl http://localhost:8000/health
```

#### API Info
```bash
curl http://localhost:8000/
```

## πŸ“ Request Format

```json
{
  "message": "User's legal question",
  "role": "client" | "lawyer",
  "jurisdiction": "EU" | "France" | "Germany" | "Italy" | "Spain" | "Romania" | "Netherlands" | "Belgium",
  "conversationHistory": [
    {"role": "user|assistant", "content": "Previous message"}
  ]
}
```

## πŸ“€ Response Format

```json
{
  "response": "Detailed legal answer with references",
  "confidence": 0.85,
  "processing_time": 2.34,
  "references": ["gdpr_2022_2555.txt", "nis2_2022_2555.txt"],
  "timestamp": "2025-01-15T10:30:00Z",
  "error": null
}
```

## 🧠 Expertise Areas

- **GDPR** (General Data Protection Regulation)
- **NIS2** (Network and Information Systems Directive 2)
- **DORA** (Digital Operational Resilience Act)
- **CRA** (Cyber Resilience Act)
- **eIDAS 2.0** (Electronic Identification, Authentication and Trust Services)
- Romanian Civil Code provisions

## πŸ”„ Workflow

1. **User Query** β†’ API receives request with role/jurisdiction context
2. **LightRAG Retrieval** β†’ Searches legal documents for relevant information
3. **LangGraph Processing** β†’ Orchestrates the workflow through nodes:
   - Query validation
   - LightRAG integration
   - Context enhancement with GPT-5-Nano
   - Response formatting
4. **Enhanced Response** β†’ Returns structured answer with confidence score

## πŸ› οΈ Development

### Local Development

```bash
# Install dependencies
pip install -r requirements.txt

# Start LightRAG server (required)
lightrag-server --host 127.0.0.1 --port 9621

# Start the API
python agent_api.py
```

### Environment Variables

```bash
OPENAI_API_KEY=your_openai_api_key
LIGHTRAG_API_KEY=your_lightrag_api_key
LIGHTRAG_HOST=127.0.0.1
LIGHTRAG_PORT=9621
API_PORT=8000
```

## πŸ“ Project Structure

```
CyberlegalAI/
β”œβ”€β”€ agent_api.py          # FastAPI server
β”œβ”€β”€ langraph_agent.py     # Main LangGraph workflow
β”œβ”€β”€ agent_state.py        # State management
β”œβ”€β”€ prompts.py           # System prompts
β”œβ”€β”€ utils.py             # LightRAG integration
β”œβ”€β”€ requirements.txt      # Python dependencies
β”œβ”€β”€ Dockerfile          # Container configuration
β”œβ”€β”€ docker-compose.yml   # Orchestration
β”œβ”€β”€ rag_storage/        # LightRAG data persistence
└── .env               # Environment variables
```

## πŸ”§ Configuration

### Port Management
- **Port 8000**: API (exposed externally)
- **Port 9621**: LightRAG (internal only, for security)

### Security Features
- LightRAG server not exposed externally
- API key authentication support
- Non-root container execution
- Health checks and monitoring

## πŸ“Š Monitoring

### Health Checks
```bash
# Container health
docker ps

# Service health
curl http://localhost:8000/health

# Logs
docker logs cyberlegal-ai
```

### Performance Metrics
The API returns:
- Processing time per request
- Confidence scores
- Referenced documents
- Error tracking

## 🚨 Error Handling

The API gracefully handles:
- LightRAG server unavailability
- OpenAI API errors
- Invalid request format
- Network timeouts

## πŸ“š API Examples

### Client Role Example
```json
{
  "message": "What should my small business do to comply with GDPR?",
  "role": "client",
  "jurisdiction": "France"
}
```

### Lawyer Role Example
```json
{
  "message": "Analyze the legal implications of NIS2 for financial institutions",
  "role": "lawyer",
  "jurisdiction": "EU"
}
```

### Comparison Query
```json
{
  "message": "Compare incident reporting requirements between NIS2 and DORA",
  "role": "client",
  "jurisdiction": "EU"
}
```

## 🀝 Integration Examples

### Python Client
```python
import requests

response = requests.post("http://localhost:8000/chat", json={
    "message": "What are GDPR penalties?",
    "role": "client",
    "jurisdiction": "EU",
    "conversationHistory": []
})

result = response.json()
print(result["response"])
```

### JavaScript Client
```javascript
const response = await fetch('http://localhost:8000/chat', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    message: 'GDPR requirements',
    role: 'client',
    jurisdiction: 'EU',
    conversationHistory: []
  })
});

const result = await response.json();
console.log(result.response);
```

## πŸ“‹ Troubleshooting

### Common Issues

1. **LightRAG Connection Failed**
   - Verify LightRAG server is running on port 9621
   - Check container logs: `docker logs cyberlegal-ai`

2. **OpenAI API Errors**
   - Verify OPENAI_API_KEY is set correctly
   - Check API key permissions and quota

3. **Slow Responses**
   - Monitor processing time in API response
   - Check LightRAG document indexing

### Debug Mode

Enable debug logging:
```bash
docker-compose logs -f cyberlegal-api
```

## πŸ“œ License

This project provides general legal information and is not a substitute for professional legal advice.

## πŸ”„ Updates

The system automatically:
- Retrieves latest regulatory documents
- Updates knowledge base through LightRAG
- Maintains conversation context
- Provides confidence scoring