cryogenic22 commited on
Commit
14f8d88
·
verified ·
1 Parent(s): e25aaba

Update utils/legal_prompt_generator.py

Browse files
Files changed (1) hide show
  1. utils/legal_prompt_generator.py +67 -74
utils/legal_prompt_generator.py CHANGED
@@ -2,6 +2,7 @@ import json
2
  from typing import Dict, List, Optional
3
  from datetime import datetime
4
 
 
5
  class LegalPromptGenerator:
6
  def __init__(self, ontology_path: str = "data/legal_ontology.json"):
7
  """Initialize prompt generator with legal ontology."""
@@ -30,25 +31,24 @@ class LegalPromptGenerator:
30
  hierarchies[parent].append(jurisdiction_id)
31
  return hierarchies
32
 
33
- def generate_system_message(self,
34
- context_chunks: List[Dict],
35
- query: str,
36
- case_metadata: Optional[Dict] = None) -> str:
37
  """Generate enhanced system message using legal context and ontology."""
38
-
39
  # Extract document types and jurisdictions
40
  doc_types = set(chunk['metadata'].get('type', 'unknown') for chunk in context_chunks)
41
  jurisdictions = set(chunk['metadata'].get('jurisdiction', 'unknown') for chunk in context_chunks)
42
-
43
  # Get relevant legal concepts from ontology
44
  relevant_concepts = self._get_relevant_concepts(query, context_chunks)
45
-
46
  # Get related jurisdictions
47
  related_jurisdictions = self._get_related_jurisdictions(jurisdictions)
48
-
49
  # Build instruction sets based on document types
50
  instruction_sets = self._get_type_specific_instructions(doc_types)
51
-
52
  # Format the system message
53
  system_message = f"""You are a specialized legal AI assistant analyzing legal documents with the following context:
54
 
@@ -62,35 +62,35 @@ LEGAL FRAMEWORK:
62
 
63
  RESPONSE GUIDELINES:
64
  1. Legal Analysis Requirements:
65
- - Maintain formal legal language and terminology
66
- - Cite specific sections and precedents when relevant
67
- - Consider jurisdictional hierarchies and relationships
68
- - Apply appropriate legal principles based on document type
69
 
70
  2. Document-Specific Instructions:
71
  {self._format_instructions(instruction_sets)}
72
 
73
  3. Citation and Reference Requirements:
74
- - Reference specific document sections with clear citations
75
- - Link analysis to relevant legal concepts and principles
76
- - Maintain proper legal citation format
77
- - Include paragraph or page numbers when available
78
 
79
  4. Jurisdictional Considerations:
80
- - Consider jurisdictional hierarchy and precedence
81
- - Apply jurisdiction-specific interpretations when relevant
82
- - Note any cross-jurisdictional implications
83
 
84
  5. Quality Standards:
85
- - Maintain professional legal terminology
86
- - Provide balanced analysis considering all relevant factors
87
- - Structure response in a clear, logical manner
88
- - Include relevant caveats and limitations
89
 
90
  6. Ontological Integration:
91
- - Incorporate relevant legal concepts from the ontology
92
- - Link analysis to established legal principles
93
- - Consider conceptual relationships and hierarchies
94
  {self._get_case_specific_instructions(case_metadata) if case_metadata else ""}
95
  """
96
  return system_message
@@ -106,21 +106,21 @@ CONTEXT:
106
  {context}
107
 
108
  Please provide a comprehensive legal analysis that:
109
- 1. Addresses the specific question
110
- 2. References relevant document sections
111
- 3. Applies appropriate legal principles
112
- 4. Considers jurisdictional implications
113
- 5. Provides clear citations and references"""
114
 
115
  def _get_relevant_concepts(self, query: str, context_chunks: List[Dict]) -> List[Dict]:
116
  """Extract relevant legal concepts from ontology based on query and context."""
117
  relevant_concepts = []
118
  combined_text = f"{query} {' '.join(chunk['text'] for chunk in context_chunks)}"
119
-
120
  for concept in self.ontology.get("@graph", []):
121
  if "rdfs:label" not in concept:
122
  continue
123
-
124
  label = concept["rdfs:label"].lower()
125
  if label in combined_text.lower():
126
  relevant_concepts.append({
@@ -129,7 +129,7 @@ Please provide a comprehensive legal analysis that:
129
  "description": concept.get("rdfs:comment", ""),
130
  "relationships": concept.get("vocab:relatedConcepts", [])
131
  })
132
-
133
  return relevant_concepts
134
 
135
  def _get_related_jurisdictions(self, jurisdictions: set) -> set:
@@ -148,46 +148,41 @@ Please provide a comprehensive legal analysis that:
148
 
149
  def _get_type_specific_instructions(self, doc_types: set) -> Dict[str, List[str]]:
150
  """Get specific instructions based on document types."""
151
- instructions = {}
152
-
153
- type_instructions = {
154
  "judgment": [
155
- "Analyze ratio decidendi and obiter dicta",
156
- "Consider precedential value",
157
- "Examine judicial reasoning and principles",
158
- "Note dissenting opinions if present"
159
  ],
160
  "legislation": [
161
- "Focus on statutory interpretation",
162
- "Consider legislative intent",
163
- "Note any amendments or repealed sections",
164
- "Examine definitions and scope"
165
  ],
166
  "contract": [
167
- "Analyze contractual terms and conditions",
168
- "Consider contract formation elements",
169
- "Examine rights and obligations",
170
- "Note any breach or performance issues"
171
  ],
172
  "regulatory": [
173
- "Focus on compliance requirements",
174
- "Consider regulatory framework",
175
- "Examine enforcement mechanisms",
176
- "Note reporting obligations"
177
  ]
178
  }
179
-
180
- for doc_type in doc_types:
181
- if doc_type in type_instructions:
182
- instructions[doc_type] = type_instructions[doc_type]
183
-
184
- return instructions
185
 
186
  def _format_legal_concepts(self, concepts: List[Dict]) -> str:
187
  """Format legal concepts for system message."""
188
  if not concepts:
189
  return "No specific legal concepts identified."
190
-
191
  formatted = "Key Legal Concepts:\n"
192
  for concept in concepts:
193
  formatted += f"- {concept['concept']}\n"
@@ -201,7 +196,7 @@ Please provide a comprehensive legal analysis that:
201
  """Format type-specific instructions."""
202
  if not instruction_sets:
203
  return "Apply general legal analysis principles."
204
-
205
  formatted = ""
206
  for doc_type, instructions in instruction_sets.items():
207
  formatted += f"\nFor {doc_type.title()} Documents:\n"
@@ -212,9 +207,8 @@ Please provide a comprehensive legal analysis that:
212
  """Generate case-specific instructions based on metadata."""
213
  if not case_metadata:
214
  return ""
215
-
216
- return f"""
217
 
 
218
  CASE-SPECIFIC CONSIDERATIONS:
219
  - Case Type: {case_metadata.get('case_type', 'Unknown')}
220
  - Priority: {case_metadata.get('priority', 'Normal')}
@@ -223,11 +217,9 @@ CASE-SPECIFIC CONSIDERATIONS:
223
  - Tags: {', '.join(case_metadata.get('tags', []))}
224
  """
225
 
226
- def generate_follow_up_prompt(self,
227
- original_query: str,
228
- follow_up_query: str,
229
- previous_response: str,
230
- context_chunks: List[Dict]) -> str:
231
  """Generate prompt for follow-up questions."""
232
  return f"""This is a follow-up question to a previous legal inquiry.
233
 
@@ -241,8 +233,9 @@ Follow-up Question:
241
  {follow_up_query}
242
 
243
  Please provide a response that:
244
- 1. Maintains consistency with the previous analysis
245
- 2. Addresses the specific follow-up inquiry
246
- 3. Builds upon the established legal framework
247
- 4. Provides additional relevant context
248
- 5. References any new relevant documents or principles"""
 
 
2
  from typing import Dict, List, Optional
3
  from datetime import datetime
4
 
5
+
6
  class LegalPromptGenerator:
7
  def __init__(self, ontology_path: str = "data/legal_ontology.json"):
8
  """Initialize prompt generator with legal ontology."""
 
31
  hierarchies[parent].append(jurisdiction_id)
32
  return hierarchies
33
 
34
+ def generate_system_message(
35
+ self, context_chunks: List[Dict], query: str, case_metadata: Optional[Dict] = None
36
+ ) -> str:
 
37
  """Generate enhanced system message using legal context and ontology."""
38
+
39
  # Extract document types and jurisdictions
40
  doc_types = set(chunk['metadata'].get('type', 'unknown') for chunk in context_chunks)
41
  jurisdictions = set(chunk['metadata'].get('jurisdiction', 'unknown') for chunk in context_chunks)
42
+
43
  # Get relevant legal concepts from ontology
44
  relevant_concepts = self._get_relevant_concepts(query, context_chunks)
45
+
46
  # Get related jurisdictions
47
  related_jurisdictions = self._get_related_jurisdictions(jurisdictions)
48
+
49
  # Build instruction sets based on document types
50
  instruction_sets = self._get_type_specific_instructions(doc_types)
51
+
52
  # Format the system message
53
  system_message = f"""You are a specialized legal AI assistant analyzing legal documents with the following context:
54
 
 
62
 
63
  RESPONSE GUIDELINES:
64
  1. Legal Analysis Requirements:
65
+ - Maintain formal legal language and terminology.
66
+ - Cite specific sections and precedents when relevant.
67
+ - Consider jurisdictional hierarchies and relationships.
68
+ - Apply appropriate legal principles based on document type.
69
 
70
  2. Document-Specific Instructions:
71
  {self._format_instructions(instruction_sets)}
72
 
73
  3. Citation and Reference Requirements:
74
+ - Reference specific document sections with clear citations.
75
+ - Link analysis to relevant legal concepts and principles.
76
+ - Maintain proper legal citation format.
77
+ - Include paragraph or page numbers when available.
78
 
79
  4. Jurisdictional Considerations:
80
+ - Consider jurisdictional hierarchy and precedence.
81
+ - Apply jurisdiction-specific interpretations when relevant.
82
+ - Note any cross-jurisdictional implications.
83
 
84
  5. Quality Standards:
85
+ - Maintain professional legal terminology.
86
+ - Provide balanced analysis considering all relevant factors.
87
+ - Structure response in a clear, logical manner.
88
+ - Include relevant caveats and limitations.
89
 
90
  6. Ontological Integration:
91
+ - Incorporate relevant legal concepts from the ontology.
92
+ - Link analysis to established legal principles.
93
+ - Consider conceptual relationships and hierarchies.
94
  {self._get_case_specific_instructions(case_metadata) if case_metadata else ""}
95
  """
96
  return system_message
 
106
  {context}
107
 
108
  Please provide a comprehensive legal analysis that:
109
+ 1. Addresses the specific question.
110
+ 2. References relevant document sections.
111
+ 3. Applies appropriate legal principles.
112
+ 4. Considers jurisdictional implications.
113
+ 5. Provides clear citations and references."""
114
 
115
  def _get_relevant_concepts(self, query: str, context_chunks: List[Dict]) -> List[Dict]:
116
  """Extract relevant legal concepts from ontology based on query and context."""
117
  relevant_concepts = []
118
  combined_text = f"{query} {' '.join(chunk['text'] for chunk in context_chunks)}"
119
+
120
  for concept in self.ontology.get("@graph", []):
121
  if "rdfs:label" not in concept:
122
  continue
123
+
124
  label = concept["rdfs:label"].lower()
125
  if label in combined_text.lower():
126
  relevant_concepts.append({
 
129
  "description": concept.get("rdfs:comment", ""),
130
  "relationships": concept.get("vocab:relatedConcepts", [])
131
  })
132
+
133
  return relevant_concepts
134
 
135
  def _get_related_jurisdictions(self, jurisdictions: set) -> set:
 
148
 
149
  def _get_type_specific_instructions(self, doc_types: set) -> Dict[str, List[str]]:
150
  """Get specific instructions based on document types."""
151
+ instructions = {
 
 
152
  "judgment": [
153
+ "Analyze ratio decidendi and obiter dicta.",
154
+ "Consider precedential value.",
155
+ "Examine judicial reasoning and principles.",
156
+ "Note dissenting opinions if present."
157
  ],
158
  "legislation": [
159
+ "Focus on statutory interpretation.",
160
+ "Consider legislative intent.",
161
+ "Note any amendments or repealed sections.",
162
+ "Examine definitions and scope."
163
  ],
164
  "contract": [
165
+ "Analyze contractual terms and conditions.",
166
+ "Consider contract formation elements.",
167
+ "Examine rights and obligations.",
168
+ "Note any breach or performance issues."
169
  ],
170
  "regulatory": [
171
+ "Focus on compliance requirements.",
172
+ "Consider regulatory framework.",
173
+ "Examine enforcement mechanisms.",
174
+ "Note reporting obligations."
175
  ]
176
  }
177
+
178
+ return {doc_type: instructions.get(doc_type, ["Apply general legal analysis principles."])
179
+ for doc_type in doc_types}
 
 
 
180
 
181
  def _format_legal_concepts(self, concepts: List[Dict]) -> str:
182
  """Format legal concepts for system message."""
183
  if not concepts:
184
  return "No specific legal concepts identified."
185
+
186
  formatted = "Key Legal Concepts:\n"
187
  for concept in concepts:
188
  formatted += f"- {concept['concept']}\n"
 
196
  """Format type-specific instructions."""
197
  if not instruction_sets:
198
  return "Apply general legal analysis principles."
199
+
200
  formatted = ""
201
  for doc_type, instructions in instruction_sets.items():
202
  formatted += f"\nFor {doc_type.title()} Documents:\n"
 
207
  """Generate case-specific instructions based on metadata."""
208
  if not case_metadata:
209
  return ""
 
 
210
 
211
+ return f"""
212
  CASE-SPECIFIC CONSIDERATIONS:
213
  - Case Type: {case_metadata.get('case_type', 'Unknown')}
214
  - Priority: {case_metadata.get('priority', 'Normal')}
 
217
  - Tags: {', '.join(case_metadata.get('tags', []))}
218
  """
219
 
220
+ def generate_follow_up_prompt(
221
+ self, original_query: str, follow_up_query: str, previous_response: str, context_chunks: List[Dict]
222
+ ) -> str:
 
 
223
  """Generate prompt for follow-up questions."""
224
  return f"""This is a follow-up question to a previous legal inquiry.
225
 
 
233
  {follow_up_query}
234
 
235
  Please provide a response that:
236
+ 1. Maintains consistency with the previous analysis.
237
+ 2. Addresses the specific follow-up inquiry.
238
+ 3. Builds upon the established legal framework.
239
+ 4. Provides additional relevant context.
240
+ 5. References any new relevant documents or principles.
241
+ """