Skip to content

Commit 370aba3

Browse files
committed
⚖️ Integrate CourtListener API for real case law research
1 parent 960212f commit 370aba3

3 files changed

Lines changed: 511 additions & 42 deletions

File tree

backend/services/ai_virtual_paralegal_service.py

Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -126,62 +126,99 @@ async def _analyze_pending_cases(self):
126126
self._log("info", "Using fallback analysis mode", "Case Analyzer")
127127

128128
async def _research_case_law(self):
129-
"""Research relevant case law for active cases."""
130-
self._log("info", "Starting case law research", "Research Agent")
129+
"""Research relevant case law for active cases using CourtListener API."""
130+
self._log("info", "Starting case law research with CourtListener API", "Research Agent")
131131

132132
try:
133-
# Import the real AI service
133+
# Import the real services
134134
from .unified_ai_service import unified_ai_service
135+
from .courtlistener_service import courtlistener_service
135136

136137
# Get pending cases to research
137138
pending_cases = await self._get_pending_cases()
138139

139140
research_results = []
141+
total_cases_found = 0
142+
140143
for case in pending_cases:
141-
# Create research prompt for case law
142-
research_prompt = f"""
143-
Research relevant case law for the following legal case:
144-
145-
Case Title: {case.get('title', 'Unknown')}
146-
Case Type: {case.get('type', 'Unknown')}
147-
Client Name: {case.get('client_name', 'Unknown')}
148-
149-
Please research and provide:
150-
1. Relevant case law precedents
151-
2. Similar cases and their outcomes
152-
3. Key legal principles that apply
153-
4. Recent developments in this area of law
154-
5. Potential legal arguments and counter-arguments
155-
6. Relevant statutes and regulations
144+
self._log("info", f"Researching case law for: {case.get('title', 'Unknown')}", "Research Agent")
156145

157-
Focus on cases that are most relevant to the client's situation and provide specific case citations where possible.
158-
"""
159-
160-
# Use real AI to research case law
161-
ai_response = unified_ai_service.generate_legal_response(
162-
message=research_prompt,
163-
task_type="research",
164-
model="claude-3-5-sonnet"
146+
# Step 1: Search CourtListener API for similar cases
147+
courtlistener_results = courtlistener_service.search_similar_cases(
148+
case_data=case,
149+
limit=10
165150
)
166151

167-
if ai_response.get('success'):
168-
research_result = {
169-
"case_id": case.get('id'),
170-
"case_title": case.get('title'),
171-
"research": ai_response.get('response', ''),
172-
"ai_model_used": ai_response.get('model_used', 'unknown'),
173-
"timestamp": datetime.now().isoformat()
174-
}
175-
research_results.append(research_result)
176-
self._log("success", f"Researched case law for: {case.get('title', 'Unknown')} using {research_result['ai_model_used']}", "Research Agent")
152+
if courtlistener_results.get('success'):
153+
similar_cases = courtlistener_results.get('similar_cases', [])
154+
total_cases_found += len(similar_cases)
155+
156+
self._log("success", f"Found {len(similar_cases)} similar cases from CourtListener API", "Research Agent")
157+
158+
# Step 2: Use AI to analyze the found cases
159+
if similar_cases:
160+
# Create a detailed research prompt with real case data
161+
case_summaries = []
162+
for similar_case in similar_cases[:5]: # Use top 5 most relevant
163+
case_summaries.append(f"""
164+
Case: {similar_case.get('case_name', 'Unknown')}
165+
Court: {similar_case.get('court', 'Unknown')}
166+
Date: {similar_case.get('date_filed', 'Unknown')}
167+
Citation: {similar_case.get('citation', 'N/A')}
168+
Precedential: {similar_case.get('precedential', False)}
169+
Snippet: {similar_case.get('snippet', 'N/A')}
170+
""")
171+
172+
research_prompt = f"""
173+
Analyze the following real case law data from CourtListener API for this legal case:
174+
175+
TARGET CASE:
176+
Title: {case.get('title', 'Unknown')}
177+
Type: {case.get('type', 'Unknown')}
178+
Client: {case.get('client_name', 'Unknown')}
179+
180+
SIMILAR CASES FOUND:
181+
{chr(10).join(case_summaries)}
182+
183+
Please provide:
184+
1. Analysis of how these cases relate to the target case
185+
2. Key legal principles that apply
186+
3. Potential precedents and their relevance
187+
4. Legal arguments that could be used
188+
5. Recent developments in this area of law
189+
6. Recommendations for the client's case
190+
191+
Focus on the most relevant cases and provide specific citations and legal analysis.
192+
"""
193+
194+
# Use AI to analyze the real case data
195+
ai_response = unified_ai_service.generate_legal_response(
196+
message=research_prompt,
197+
task_type="research",
198+
model="claude-3-5-sonnet"
199+
)
200+
201+
if ai_response.get('success'):
202+
research_result = {
203+
"case_id": case.get('id'),
204+
"case_title": case.get('title'),
205+
"ai_analysis": ai_response.get('response', ''),
206+
"similar_cases_found": len(similar_cases),
207+
"courtlistener_cases": similar_cases[:3], # Store top 3 for reference
208+
"ai_model_used": ai_response.get('model_used', 'unknown'),
209+
"timestamp": datetime.now().isoformat()
210+
}
211+
research_results.append(research_result)
212+
self._log("success", f"AI analysis completed for: {case.get('title', 'Unknown')} using {research_result['ai_model_used']}", "Research Agent")
213+
else:
214+
self._log("warning", f"AI analysis failed for: {case.get('title', 'Unknown')}", "Research Agent")
215+
else:
216+
self._log("warning", f"No similar cases found for: {case.get('title', 'Unknown')}", "Research Agent")
177217
else:
178-
self._log("warning", f"Case law research failed for: {case.get('title', 'Unknown')}", "Research Agent")
179-
180-
# Simulate additional research from external APIs
181-
await asyncio.sleep(1) # Simulate API calls to CourtListener, etc.
218+
self._log("warning", f"CourtListener API search failed for: {case.get('title', 'Unknown')}", "Research Agent")
182219

183-
self._log("success", f"Researched {len(research_results)} cases - found relevant case law and precedents", "Research Agent")
184-
self._log("info", "Completed comprehensive case law research using AI analysis", "Research Agent")
220+
self._log("success", f"Completed case law research: {len(research_results)} cases analyzed, {total_cases_found} similar cases found from CourtListener API", "Research Agent")
221+
self._log("info", "Real case law research completed using CourtListener API and AI analysis", "Research Agent")
185222

186223
except Exception as e:
187224
self._log("error", f"Case law research failed: {str(e)}", "Research Agent")

0 commit comments

Comments
 (0)