File size: 3,644 Bytes
4b022af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import google.generativeai as genai
from app.config import settings
from app.services.risk_scorer import RISK_DEFINITIONS
import json
import re
from typing import Dict, List


# Configure the Gemini API
genai.configure(api_key=settings.GEMINI_API_KEY)


def analyze_clause_with_gemini(clause_text: str) -> Dict:
    """
    Analyze a contract clause using Google Gemini AI for risk identification.

    Args:
        clause_text: The text of the clause to analyze

    Returns:
        Dictionary containing identified risk IDs and suggestions
    """

    # Create the detailed prompt for Gemini
    prompt = f"""
You are an expert Indian legal consultant specializing in contract analysis and risk assessment. 
Analyze the following contract clause and identify any legal risks based on the predefined risk categories.

CONTRACT CLAUSE TO ANALYZE:
{clause_text}

RISK CATEGORIES TO CHECK FOR:
1. UNLIMITED_LIABILITY: Clause imposes unlimited liability on the client
2. ONE_SIDED_TERMINATION: Termination rights are unfairly one-sided
3. UNCLEAR_JURISDICTION: Governing law or jurisdiction for disputes is ambiguous
4. DPDP_NON_COMPLIANCE: Data protection clause may not comply with the DPDP Act 2023

INSTRUCTIONS:
1. Carefully read the clause text
2. Identify which of the above risk categories apply to this clause
3. For each identified risk, provide a brief explanation
4. Suggest a compliant alternative or modification for any identified risks
5. If no risks are found, respond with "No risks identified"

RESPONSE FORMAT (JSON):
{{
    "risks": [
        {{
            "risk_id": "RISK_CATEGORY_ID",
            "explanation": "Brief explanation of why this risk applies"
        }}
    ],
    "suggestion": "Compliant alternative or modification suggestion"
}}

If no risks are identified, return:
{{
    "risks": [],
    "suggestion": "No risks identified - clause appears compliant"
}}
"""

    try:
        # Initialize the Gemini model
        model = genai.GenerativeModel('gemini-2.5-flash-lite')

        # Generate response
        response = model.generate_content(prompt)

        # Extract the text response
        response_text = response.text.strip()

        # Try to parse JSON from the response
        try:
            # Look for JSON in the response (sometimes Gemini includes extra text)
            json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
            if json_match:
                json_str = json_match.group()
                result = json.loads(json_str)
            else:
                # Fallback: try to parse the entire response as JSON
                result = json.loads(response_text)
        except json.JSONDecodeError:
            # If JSON parsing fails, create a fallback response
            result = {
                "risks": [],
                "suggestion": "Unable to parse AI response - manual review recommended"
            }

        # Validate and clean the response
        if "risks" not in result:
            result["risks"] = []
        if "suggestion" not in result:
            result["suggestion"] = "No suggestion provided"

        # Validate risk IDs
        valid_risks = []
        for risk in result["risks"]:
            if isinstance(risk, dict) and "risk_id" in risk:
                risk_id = risk["risk_id"]
                if risk_id in RISK_DEFINITIONS:
                    valid_risks.append(risk)

        result["risks"] = valid_risks

        return result

    except Exception as e:
        print(f"Error in Gemini analysis: {e}")
        return {
            "risks": [],
            "suggestion": f"Analysis failed: {str(e)}"
        }