File size: 3,411 Bytes
4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 4b022af 6ce5004 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 |
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
# Configure Gemini API
genai.configure(api_key=settings.GEMINI_API_KEY)
def analyze_clause_with_gemini(clause_text: str) -> Dict:
"""
Analyze a contract (clause or full text) using Google Gemini AI for risk identification.
Args:
clause_text: The text of the clause or contract to analyze
Returns:
Dictionary containing identified risk IDs and suggestions
"""
prompt = f"""
You are an expert Indian legal consultant specializing in contract analysis and risk assessment.
Analyze the following contract text and identify any legal risks based on the predefined categories.
CONTRACT TEXT 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 contract text
2. Identify which of the above risk categories apply
3. For each identified risk, provide a **short explanation (max 1β2 lines, no long paragraphs)**
4. Suggest a **brief compliant alternative (1β2 lines)**
5. If no risks are found, respond with "No risks identified"
RESPONSE FORMAT (strict JSON only):
{{
"risks": [
{{
"risk_id": "RISK_CATEGORY_ID",
"explanation": "1β2 line explanation of why this risk applies"
}}
],
"suggestion": "1β2 line compliant alternative or modification suggestion"
}}
If no risks are identified, return:
{{
"risks": [],
"suggestion": "No risks identified - contract appears compliant"
}}
"""
try:
# Initialize Gemini model
model = genai.GenerativeModel("gemini-2.5-flash-lite")
# Generate response
response = model.generate_content(prompt)
response_text = response.text.strip()
# Try to parse JSON from the response
try:
json_match = re.search(r"\{.*\}", response_text, re.DOTALL)
if json_match:
json_str = json_match.group()
result = json.loads(json_str)
else:
result = json.loads(response_text)
except json.JSONDecodeError:
result = {
"risks": [],
"suggestion": "Unable to parse AI response - manual review recommended"
}
# Ensure required keys exist
if "risks" not in result:
result["risks"] = []
if "suggestion" not in result:
result["suggestion"] = "No suggestion provided"
# Validate risk IDs against known definitions
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)}"
}
|