File size: 2,005 Bytes
5f6d20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from google import genai
from pydantic import BaseModel
from typing import List, Optional
import os
import dotenv

dotenv.load_dotenv()

client = genai.Client(
    api_key=os.getenv("GEMINI_API_KEY")
)

class LegalQueryAnalysis(BaseModel):
    query_type: str
    intent: str
    offence: Optional[str] = None
    legal_concepts: List[str]
    entities: List[str]
    acts: List[str]
    search_queries: List[str]


class Analyser:

    def __init__(self):
        self.SYSTEM_PROMPT = """
        You are an expert Indian legal query analyzer.

        Your job is NOT to answer legal questions.

        Your job is to extract structured retrieval metadata
        for a Legal RAG system.

        Identify:

        1. query_type
        - criminal
        - civil
        - constitutional
        - evidence
        - procedural
        - judgment

        2. intent
        - punishment
        - definition
        - rights
        - procedure
        - evidence
        - remedy
        - bail
        - appeal

        3. offence (if applicable)

        4. legal concepts

        5. relevant acts
        - BNS
        - BNSS
        - BSA
        - Constitution

        6. search queries for retrieval

        Return only structured data.
        """


    def analyze_query(self,query: str) -> LegalQueryAnalysis:

        response = client.models.generate_content(
            model=os.getenv("Gemini_MODEL"),
            contents=f"""
        {self.SYSTEM_PROMPT}

        User Query:
        {query}
        """,
            config={
                "response_mime_type": "application/json",
                "response_schema": LegalQueryAnalysis,
                "temperature": 0
            }
        )

        return response.parsed
    

if __name__=="__main__":
    

    query = """
    My bike was stolen from outside my house.
    What punishment can the offender face?
    """
    analyser=Analyser()
    analysis = analyser.analyze_query(query)

    print(analysis.model_dump())