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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# AI Contract Risk Analyzer API Documentation

This document details the REST API endpoints for the AI Contract Risk Analyzer service.

**Base URL:** `http://<your-host>:<your-port>/api/v1` (e.g., `http://localhost:8000/api/v1`)

## Table of Contents

*   [Health Check](#health-check)
*   [Get Service Status](#get-service-status)
*   [Get Contract Categories](#get-contract-categories)
*   [Analyze Contract from File](#analyze-contract-from-file)
*   [Analyze Contract from Text](#analyze-contract-from-text)
*   [Generate PDF Report](#generate-pdf-report)
*   [Validate Contract File](#validate-contract-file)
*   [Validate Contract Text](#validate-contract-text)

---

## Health Check

Checks the basic health and availability of the API service.

### Endpoint

`GET /api/v1/health`

### Request

No body required.

### Response

**Status Code:** `200 OK`

**Content-Type:** `application/json`

```json
{
  "status": "healthy",
  "version": "1.0.0",
  "timestamp": "2025-11-17T15:00:00.123456",
  "models_loaded": 5,
  "services_loaded": 6,
  "memory_usage_mb": 2048.5
}
```

---

## Get Service Status

Retrieves detailed status information about the loaded models and services.

### Endpoint

`GET /api/v1/status`

### Request

No body required.

### Response

**Status Code:** `200 OK`

**Content-Type:** `application/json`

```json
{
  "services": {
    "classifier": "loaded",
    "clause_extractor": "loaded",
    "risk_analyzer": "loaded",
    "llm_interpreter": "loaded",
    "negotiation_engine": "loaded",
    "summary_generator": "loaded",
    "term_analyzer": "loaded",
    "protection_checker": "loaded"
  },
  "models": {
    "legal-bert": {
      "name": "legal-bert",
      "type": "LEGAL_BERT",
      "status": "LOADED",
      "loaded_at": "2025-11-17T14:55:00.123456",
      "memory_size_mb": 400.0,
      "access_count": 10,
      "last_accessed": "2025-11-17T15:00:00.123456"
    },
    "embedding": {
      "name": "embedding",
      "type": "EMBEDDING",
      "status": "LOADED",
      "loaded_at": "2025-11-17T14:55:00.123456",
      "memory_size_mb": 100.0,
      "access_count": 8,
      "last_accessed": "2025-11-17T14:59:59.123456"
    }
  },
  "memory_usage_mb": 2048.5,
  "total_services_loaded": 8,
  "total_models_loaded": 5
}
```

---

## Get Contract Categories

Retrieves a list of contract categories that the classifier can identify.

### Endpoint

`GET /api/v1/categories`

### Request

No body required.

### Response

**Status Code:** `200 OK`

**Content-Type:** `application/json`

```json
{
  "categories": [
    "employment",
    "consulting",
    "nda",
    "software",
    "service",
    "partnership",
    "lease",
    "purchase",
    "general"
  ]
}
```

---

## Analyze Contract from File

Uploads a contract file (PDF, DOCX, TXT) for analysis.

### Endpoint

`POST /api/v1/analyze/file`

### Request

**Content-Type:** `multipart/form-data`

**Form Data:**

- `file`: **(Required)** The contract file to analyze (PDF, DOCX, TXT).
- `max_clauses`: **(Optional, Integer)** Maximum number of clauses to analyze (default: `50`, min: `5`, max: `30`).
- `interpret_clauses`: **(Optional, Boolean)** Whether to generate LLM interpretations for clauses (default: `true`).
- `generate_negotiation_points`: **(Optional, Boolean)** Whether to generate negotiation points (default: `true`).
- `compare_to_market`: **(Optional, Boolean)** Whether to perform market comparison (default: `false`, currently disabled).

### Response

**Status Code:** `200 OK`

**Content-Type:** `application/json`

```json
{
  "analysis_id": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
  "timestamp": "2025-11-17T15:01:00.123456",
  "classification": {
    "category": "employment",
    "subcategory": "executive",
    "confidence": 0.95,
    "reasoning": ["Keywords like 'executive', 'compensation', 'non-compete' found"],
    "detected_keywords": ["employment", "executive", "non-compete", "compensation"]
  },
  "clauses": [
    {
      "text": "Employee agrees to a 24-month non-compete...",
      "reference": "Clause 9.5",
      "category": "restrictive_covenants",
      "confidence": 0.98,
      "start_pos": 1200,
      "end_pos": 1350,
      "extraction_method": "semantic",
      "risk_indicators": ["non-compete", "24 months", "entire industry"],
      "risk_score": 90
    }
  ],
  "risk_analysis": {
    "overall_score": 85,
    "risk_level": "CRITICAL",
    "category_scores": {
      "restrictive_covenants": 95,
      "penalties_liability": 90,
      "compensation_benefits": 80
    },
    "risk_factors": ["Restrictive Covenants", "Penalties & Liability"],
    "detailed_findings": {
      "restrictive_covenants": [
        "Non-compete clause (Clause 9.5) is overly broad and long.",
        "Non-solicitation clause (Clause 17.6) has excessive duration."
      ]
    },
    "benchmark_comparison": {
      "overall": "✗ Significantly above market risk levels",
      "high_risk_areas": ["Restrictive Covenants", "Penalties & Liability"]
    },
    "risk_breakdown": [
      {
        "category": "Restrictive Covenants",
        "score": 95,
        "summary": "The agreement contains exceptionally broad and long-lasting non-compete...",
        "findings": ["Non-compete clause (Clause 9.5) is overly broad and long."]
      }
    ],
    "contract_type": "employment",
    "unfavorable_terms": [],
    "missing_protections": []
  },
  "unfavorable_terms": [
    {
      "term": "Risk Factor: entire industry",
      "category": "restrictive_covenants",
      "severity": "critical",
      "explanation": "Non-compete restricts the Employee from applying to any company in the same 'Industry'...",
      "risk_score": 90,
      "clause_reference": "Clause 9.5",
      "suggested_fix": "Negotiate to have this clause removed entirely...",
      "contract_type": "EMPLOYMENT",
      "specific_text": "entire industry",
      "benchmark_info": null,
      "legal_basis": "Reasonableness standard for restrictive covenants"
    }
  ],
  "missing_protections": [
    {
      "protection": "For Cause Definition",
      "importance": "critical",
      "risk_score": 25,
      "explanation": "Without a clear 'for cause' definition, termination grounds remain ambiguous...",
      "recommendation": "Add clear 'For Cause' definition including...",
      "categories": ["termination_rights"],
      "contract_type": "EMPLOYMENT",
      "suggested_language": "\"For Cause\" means: (a) gross negligence...",
      "legal_basis": "Employment protection statutes...",
      "affected_clauses": ["Clause 17.1"]
    }
  ],
  "clause_interpretations": [
    {
      "clause_reference": "Clause 9.5",
      "original_text": "Employee agrees to a 24-month non-compete...",
      "plain_english_summary": "You cannot work for or apply to any company in the same industry for 24 months after leaving.",
      "key_points": [
        "Duration: 24 months",
        "Scope: Entire industry",
        "Applies to: Applying for jobs too"
      ],
      "potential_risks": [
        "Severely limits future job opportunities.",
        "Scope is likely unenforceable."
      ],
      "favorability": "unfavorable",
      "confidence": 0.85,
      "risk_score": 90,
      "negotiation_priority": "high",
      "suggested_improvements": [
        "Reduce duration to 6-12 months.",
        "Narrow scope to direct competitors only."
      ]
    }
  ],
  "negotiation_points": [
    {
      "priority": 1,
      "category": "restrictive_covenants",
      "issue": "Extremely broad non-compete clause",
      "current_language": "Employee agrees to a 24-month non-compete...",
      "proposed_language": "Limit non-compete to 6 months and direct competitors only.",
      "rationale": "The current clause is overly broad and likely unenforceable.",
      "tactic": "limitation",
      "fallback_position": "If 6 months is not accepted, propose 12 months.",
      "estimated_difficulty": "medium",
      "legal_basis": "Reasonableness standard for restrictive covenants",
      "business_impact": "Severely restricts the Employee's ability to find future employment.",
      "counterparty_concerns": "They may argue this is necessary to protect trade secrets.",
      "timing_suggestion": "Address this early in negotiations.",
      "bargaining_chips": [
        "Offer to sign a stronger confidentiality agreement.",
        "Agree to a shorter notice period for termination."
      ]
    }
  ],
  "market_comparisons": [],
  "executive_summary": "This employment agreement is heavily skewed in favor of the Employer...",
  "metadata": {
    "text_length": 15000,
    "word_count": 2500,
    "num_clauses": 20,
    "contract_type": "EMPLOYMENT",
    "actual_category": "employment",
    "options": {
      "max_clauses": 50,
      "interpret_clauses": true,
      "generate_negotiation_points": true,
      "compare_to_market": false
    }
  },
  "pdf_available": true
}
```

### Error Response

**Status Code:** `400 Bad Request` or `500 Internal Server Error`

**Content-Type:** `application/json`

```json
{
  "error": "Analysis failed",
  "detail": "Contract text too short. Minimum 300 characters required.",
  "timestamp": "2025-11-17T15:01:01.123456"
}
```

---

## Analyze Contract from Text

Analyzes a contract provided as plain text.

### Endpoint

`POST /api/v1/analyze/text`

### Request

**Content-Type:** `application/x-www-form-urlencoded`

**Form Data:**

- `contract_text`: **(Required, String)** The full text of the contract.
- `max_clauses`: **(Optional, Integer)** Maximum number of clauses to analyze (default: `15`, min: `5`, max: `30`).
- `interpret_clauses`: **(Optional, Boolean)** Whether to generate LLM interpretations for clauses (default: `true`).
- `generate_negotiation_points`: **(Optional, Boolean)** Whether to generate negotiation points (default: `true`).
- `compare_to_market`: **(Optional, Boolean)** Whether to perform market comparison (default: `false`, currently disabled).

### Response

Same as the response for [Analyze Contract from File](#analyze-contract-from-file).

### Error Response

Same as the error response for [Analyze Contract from File](#analyze-contract-from-file).

---

## Generate PDF Report

Generates a downloadable PDF report based on the analysis result provided in the request body.

### Endpoint

`POST /api/v1/generate-pdf`

### Request

**Content-Type:** `application/json`

**Body:** The full JSON object returned by a successful `/analyze/file` or `/analyze/text` request.

```json
{
  "analysis_id": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
  "timestamp": "2025-11-17T15:01:00.123456",
  "classification": { ... },
  "clauses": [ ... ],
  "risk_analysis": { ... },
  "unfavorable_terms": [ ... ],
  "missing_protections": [ ... ],
  "clause_interpretations": [ ... ],
  "negotiation_points": [ ... ],
  "market_comparisons": [ ... ],
  "executive_summary": "...",
  "metadata": { ... },
  "pdf_available": true
}
```

### Response

**Status Code:** `200 OK`

**Content-Type:** `application/pdf`

**Headers:**

- `Content-Disposition`: `attachment; filename=contract_analysis_<analysis_id>.pdf`

The response body contains the binary PDF data.

### Error Response

**Status Code:** `500 Internal Server Error`

**Content-Type:** `application/json`

```json
{
  "error": "Internal server error",
  "detail": "Failed to generate PDF: Some error message",
  "timestamp": "2025-11-17T15:02:00.123456"
}
```

---

## Validate Contract File

Validates if an uploaded file is a potentially valid contract document.

### Endpoint

`POST /api/v1/validate/file`

### Request

**Content-Type:** `multipart/form-data`

**Form Data:**

- `file`: **(Required)** The contract file to validate (PDF, DOCX, TXT).

### Response

**Status Code:** `200 OK`

**Content-Type:** `application/json`

```json
{
  "valid": true,
  "message": "Contract appears valid",
  "confidence": 85.0,
  "report": {
    "scores": {
      "total": 85.0,
      "has_parties": 90.0,
      "has_date": 80.0,
      "has_terms": 90.0
    },
    "found_indicators": ["agreement", "party", "terms"],
    "found_anti_patterns": [],
    "text_statistics": {
      "length": 15000,
      "word_count": 2500,
      "line_count": 300
    }
  }
}
```

### Error Response

**Status Code:** `400 Bad Request`

**Content-Type:** `application/json`

```json
{
  "error": "Validation failed",
  "detail": "File too large. Max size: 10.0MB",
  "timestamp": "2025-11-17T15:03:00.123456"
}
```

---

## Validate Contract Text

Validates if a provided text string is a potentially valid contract.

### Endpoint

`POST /api/v1/validate/text`

### Request

**Content-Type:** `application/x-www-form-urlencoded`

**Form Data:**

- `contract_text`: **(Required, String)** The text to validate.

### Response

**Status Code:** `200 OK`

**Content-Type:** `application/json`

```json
{
  "valid": true,
  "message": "Contract appears valid",
  "confidence": 78.0,
  "report": {
    "scores": {
      "total": 78.0,
      "has_parties": 85.0,
      "has_date": 70.0,
      "has_terms": 80.0
    },
    "found_indicators": ["agreement", "party", "payment"],
    "found_anti_patterns": [],
    "text_statistics": {
      "length": 1200,
      "word_count": 200,
      "line_count": 25
    }
  }
}
```

### Error Response

**Status Code:** `400 Bad Request`

**Content-Type:** `application/json`

```json
{
  "error": "Validation failed",
  "detail": "Contract text too short. Minimum 300 characters required.",
  "timestamp": "2025-11-17T15:04:00.123456"
}
```

---

## Notes

- All timestamps are in ISO 8601 format
- All risk scores are integers from 0-100
- The API uses custom JSON serialization to handle NumPy types
- CORS is enabled for all origins in development
- Maximum file upload size is configurable via settings (default: 10MB)
- Minimum contract text length: 300 characters (configurable)
- Maximum contract text length: configurable via settings