Spaces:
Sleeping
Sleeping
Create agents/legal_agent.py
Browse files- agents/legal_agent.py +65 -0
agents/legal_agent.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# agents/legal_agent.py
|
| 2 |
+
from typing import Dict, List, Optional
|
| 3 |
+
from utils.llm_gateway import LLMGateway
|
| 4 |
+
from utils.vector_store import VectorStore
|
| 5 |
+
|
| 6 |
+
class LegalAgent:
|
| 7 |
+
def __init__(self, llm: LLMGateway, vector_store: VectorStore):
|
| 8 |
+
self.llm = llm
|
| 9 |
+
self.vector_store = vector_store
|
| 10 |
+
|
| 11 |
+
def process_query(self, query: str) -> str:
|
| 12 |
+
"""Process a legal research query"""
|
| 13 |
+
# Search for relevant documents
|
| 14 |
+
relevant_docs = self.vector_store.search(query, n_results=3)
|
| 15 |
+
|
| 16 |
+
# Construct prompt
|
| 17 |
+
prompt = self._construct_prompt(query, relevant_docs)
|
| 18 |
+
|
| 19 |
+
# Generate response
|
| 20 |
+
system_prompt = """You are a legal research assistant.
|
| 21 |
+
Provide clear, well-structured responses with citations to relevant documents.
|
| 22 |
+
Focus on factual analysis and avoid legal advice."""
|
| 23 |
+
|
| 24 |
+
response = self.llm.generate(
|
| 25 |
+
prompt=prompt,
|
| 26 |
+
system_prompt=system_prompt,
|
| 27 |
+
temperature=0.7
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return response
|
| 31 |
+
|
| 32 |
+
def _construct_prompt(self, query: str, relevant_docs: List[Dict]) -> str:
|
| 33 |
+
"""Construct prompt with context"""
|
| 34 |
+
prompt = f"""Query: {query}\n\nRelevant documents:\n"""
|
| 35 |
+
|
| 36 |
+
for i, doc in enumerate(relevant_docs, 1):
|
| 37 |
+
prompt += f"\nDocument {i}:\n{doc['text']}\n"
|
| 38 |
+
|
| 39 |
+
prompt += """\nPlease provide:
|
| 40 |
+
1. Summary of relevant facts
|
| 41 |
+
2. Analysis of key legal points
|
| 42 |
+
3. Citations to relevant documents
|
| 43 |
+
4. Suggested next steps for research"""
|
| 44 |
+
|
| 45 |
+
return prompt
|
| 46 |
+
|
| 47 |
+
def generate_analysis(self, template: str, docs: List[Dict], case_type: str) -> str:
|
| 48 |
+
"""Generate analysis based on template"""
|
| 49 |
+
prompt = f"""Template: {template}
|
| 50 |
+
Case Type: {case_type}
|
| 51 |
+
|
| 52 |
+
Relevant documents:
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
for i, doc in enumerate(docs, 1):
|
| 56 |
+
prompt += f"\nDocument {i}:\n{doc['text']}\n"
|
| 57 |
+
|
| 58 |
+
system_prompt = """Generate a structured analysis following the template.
|
| 59 |
+
Include specific citations and maintain professional legal terminology."""
|
| 60 |
+
|
| 61 |
+
return self.llm.generate(
|
| 62 |
+
prompt=prompt,
|
| 63 |
+
system_prompt=system_prompt,
|
| 64 |
+
temperature=0.7
|
| 65 |
+
)
|