Spaces:
Sleeping
Sleeping
File size: 20,072 Bytes
d4d9e63 425199f d4d9e63 f54ffdd d4d9e63 425199f d4d9e63 425199f d4d9e63 425199f d4d9e63 425199f d4d9e63 425199f d4d9e63 425199f d4d9e63 425199f d4d9e63 425199f d4d9e63 | 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | import os
import uuid
import tempfile
from typing import Annotated, List, TypedDict, Dict, Any
import streamlit as st
from langchain_core.documents import Document
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage, ToolMessage
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
try:
from langchain_chroma import Chroma
except Exception:
from langchain_community.vectorstores import Chroma
try:
from langchain_community.tools.tavily_search import TavilySearchResults
except Exception:
TavilySearchResults = None
from langgraph.graph import StateGraph, END
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode
from langgraph.checkpoint.memory import MemorySaver
st.set_page_config(
page_title="Legal Agentic AI Project",
page_icon="βοΈ",
layout="wide",
)
SAMPLE_CASE = '''
Case No: 2024/CR/12345
Court: District Court, US
Facts:
The defendant, Mr. Alex Carter, was charged with breach of contract dated January 15, 2023.
The plaintiff, ABC Corporation, claims that the defendant failed to deliver 1000 units of
electronic components as per the signed agreement. The contract specified delivery by March 31, 2023.
The defendant claims that due to unforeseen supply chain disruptions caused by the pandemic,
delivery was impossible. However, no force majeure clause was explicitly invoked in writing
within the stipulated 30-day notice period as mentioned in Section 12 of the contract.
The plaintiff seeks damages of Rs. 50,00,000 for business losses incurred due to delayed delivery.
The defendant argues that the damages claimed are excessive and not proven with adequate documentation.
Key Issues:
1. Was there a valid force majeure event?
2. Did the defendant provide proper notice?
3. Are the damages claimed reasonable and substantiated?
4. Was there any contributory negligence by the plaintiff?
'''
LEGAL_KNOWLEDGE_BASE = [
'''
Precedent Case: Smith v. Johnson (2022)
Court ruled that force majeure must be invoked in writing within the contractual notice period.
Failure to do so waives the right to claim force majeure defense.
Citation: [2022] 3 SCC 145
''',
'''
Precedent Case: ABC Corp v. XYZ Ltd (2021)
In breach of contract cases, damages must be proven with documentary evidence.
Speculative damages without supporting documentation are not admissible.
The burden of proof lies with the plaintiff.
Citation: [2021] 2 BomHC 78
''',
'''
Legal Principle: Contributory Negligence
If the plaintiff's actions contributed to the breach or losses, damages may be reduced proportionally.
Courts have recognized this principle in commercial contract disputes.
Reference: US Contract Act, 1872, Section 73
''',
'''
Force Majeure During Pandemic
Courts have taken a nuanced view of COVID-19 as force majeure.
The invoking party must demonstrate: (1) impossibility, not mere difficulty,
(2) proper notice, and (3) mitigation efforts.
Citation: Energy Watchdog v. CERC (2020)
''',
'''
Force Majeure Invocation - Notice Requirement
Relevant Precedent: Smith v. Johnson (2022)
Citation: [2022] 3 SCC 145
Legal Principle:
Force majeure must be invoked in writing within the contractual notice period.
Failure to comply with the notice requirement waives the right to claim force majeure defense.
Application to Current Case:
The contract specified a 30-day notice period under Section 12.
Mr. Alex Carter failed to invoke force majeure in writing within this stipulated period.
This precedent strongly supports the plaintiff's position.
''',
'''
Burden of Proof for Damages
Relevant Precedent: ABC Corp v. XYZ Ltd (2021)
Citation: [2021] 2 BomHC 78
Legal Principle:
In breach of contract cases, damages must be proven with documentary evidence.
Speculative damages without supporting documentation are not admissible.
The burden of proof lies with the plaintiff.
Application to Current Case:
ABC Corporation claims Rs. 50,00,000 in damages.
The plaintiff must provide concrete evidence such as lost orders, financial statements,
alternative procurement costs, and documented business loss.
''',
'''
Relevant Precedent: Energy Watchdog v. CERC (2020)
Legal Principle:
Courts apply a nuanced, three-part test for COVID-19 related force majeure claims:
1. Impossibility, not mere difficulty or inconvenience
2. Proper notice as per contractual terms
3. Mitigation efforts undertaken by the affected party
Application to Current Case:
The defendant must demonstrate absolute impossibility of performance, not merely increased cost or delay.
The defendant must also show good faith mitigation efforts, such as seeking alternative suppliers.
''',
]
class AgentState(TypedDict):
messages: Annotated[list, add_messages]
case_text: str
final_analysis: str
def secrets_ready() -> bool:
return bool(os.getenv("OPENAI_API_KEY"))
@st.cache_resource(show_spinner=False)
def create_vectorstore(openai_key: str):
docs = [Document(page_content=doc) for doc in LEGAL_KNOWLEDGE_BASE]
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
)
splits = text_splitter.split_documents(docs)
embedding_model = OpenAIEmbeddings(
model="text-embedding-3-small",
api_key=openai_key,
)
persist_dir = tempfile.mkdtemp(prefix="legal_agent_chroma_")
vectorstore = Chroma.from_documents(
documents=splits,
embedding=embedding_model,
collection_name=f"legal_cases_{uuid.uuid4().hex[:8]}",
persist_directory=persist_dir,
)
return vectorstore, len(splits)
def build_legal_agent(model_name: str, temperature: float = 0.0):
openai_key = os.getenv("OPENAI_API_KEY")
tavily_key = os.getenv("TAVILY_API_KEY")
llm = ChatOpenAI(
model=model_name,
temperature=temperature,
api_key=openai_key,
)
vectorstore, chunk_count = create_vectorstore(openai_key)
@tool
def rag_search_tool(query: str) -> str:
"""Search legal case documents using RAG for similar precedents and relevant information."""
docs = vectorstore.similarity_search(query, k=3)
if not docs:
return "No relevant legal precedents found in the knowledge base."
results = []
for i, doc in enumerate(docs, 1):
results.append(f"Precedent {i}:\n{doc.page_content}")
return "\n\n".join(results)
@tool
def tavily_web_search_tool(query: str) -> str:
"""Search the web using Tavily for recent legal cases and legal developments."""
if not tavily_key:
return (
"Tavily web search is not configured. "
"Add TAVILY_API_KEY in Hugging Face Space secrets to enable live web search."
)
if TavilySearchResults is None:
return "Tavily package is not installed. Add tavily-python to requirements.txt."
tavily_tool = TavilySearchResults(max_results=3)
results = tavily_tool.invoke({"query": query})
if not results:
return "No web search results found."
formatted_results = []
for i, result in enumerate(results, 1):
content = result.get("content", "No content available")
url = result.get("url", "No URL")
formatted_results.append(f"Result {i}:\n{content}\nSource: {url}")
return "\n\n".join(formatted_results)
@tool
def analyze_loopholes_tool(case_summary: str) -> str:
"""Analyze legal case to identify potential loopholes using LLM reasoning."""
analysis_prompt = f'''
You are a senior legal analyst. Analyze the following case and identify potential loopholes.
Case Summary:
{case_summary}
Identify:
1. Procedural loopholes
2. Evidentiary gaps
3. Jurisdictional issues
4. Statute of limitations concerns
5. Constitutional challenges
Provide a detailed analysis with reasoning.
'''
response = llm.invoke([HumanMessage(content=analysis_prompt)])
return response.content
tools = [rag_search_tool, tavily_web_search_tool, analyze_loopholes_tool]
def agent_node(state: AgentState) -> Dict[str, Any]:
system_prompt = SystemMessage(
content=f'''
You are an expert legal analysis AI agent specializing in identifying loopholes and weaknesses in legal cases.
Case to Analyze:
{state["case_text"]}
Your Task:
Conduct a comprehensive legal analysis by:
1. Searching for similar precedent cases using rag_search_tool
2. Finding recent legal developments using tavily_web_search_tool
3. Analyzing loopholes using analyze_loopholes_tool
Analysis Framework:
- Force majeure defenses and procedural requirements
- Notice requirements and timelines
- Burden of proof and evidence standards
- Contributory negligence considerations
- Damages calculation and substantiation
After gathering all information, provide a comprehensive final analysis covering:
- Case summary
- Relevant precedents found
- Current legal developments, if available
- Procedural loopholes
- Evidentiary weaknesses
- Risk assessment
- Strategic recommendations for plaintiff and defendant
Use the tools before concluding.
'''
)
llm_with_tools = llm.bind_tools(tools)
response = llm_with_tools.invoke([system_prompt] + state["messages"])
return {"messages": [response]}
def should_continue(state: AgentState) -> str:
last_message = state["messages"][-1]
if isinstance(last_message, AIMessage) and getattr(last_message, "tool_calls", None):
return "tools"
return "end"
workflow = StateGraph(AgentState)
workflow.add_node("agent", agent_node)
workflow.add_node("tools", ToolNode(tools))
workflow.set_entry_point("agent")
workflow.add_conditional_edges(
"agent",
should_continue,
{
"tools": "tools",
"end": END,
},
)
workflow.add_edge("tools", "agent")
memory = MemorySaver()
graph = workflow.compile(checkpointer=memory)
return graph, chunk_count
def run_legal_analysis(case_text: str, model_name: str, temperature: float):
graph, chunk_count = build_legal_agent(model_name, temperature)
thread_id = f"legal_case_{uuid.uuid4().hex[:8]}"
config = {"configurable": {"thread_id": thread_id}}
initial_state = {
"messages": [HumanMessage(content="Analyze this legal case for loopholes and weaknesses.")],
"case_text": case_text,
"final_analysis": "",
}
trace = []
final_answer = ""
for step_output in graph.stream(initial_state, config, stream_mode="values"):
last_message = step_output["messages"][-1]
if isinstance(last_message, AIMessage):
tool_calls = getattr(last_message, "tool_calls", None)
if tool_calls:
for call in tool_calls:
trace.append(
{
"type": "tool_call",
"name": call.get("name", "Unknown Tool"),
"args": call.get("args", {}),
}
)
elif last_message.content:
trace.append(
{
"type": "agent_message",
"content": last_message.content,
}
)
final_answer = last_message.content
elif isinstance(last_message, ToolMessage):
trace.append(
{
"type": "tool_result",
"name": getattr(last_message, "name", "Tool"),
"content": last_message.content,
}
)
final_state = graph.get_state(config)
final_message = final_state.values["messages"][-1]
if isinstance(final_message, AIMessage) and final_message.content:
final_answer = final_message.content
return {
"final_answer": final_answer,
"trace": trace,
"chunk_count": chunk_count,
"thread_id": thread_id,
}
def display_trace(trace: List[Dict[str, Any]]):
if not trace:
st.info("No trace available yet.")
return
for i, item in enumerate(trace, 1):
if item["type"] == "tool_call":
with st.expander(f"Step {i}: Agent called tool β {item['name']}", expanded=True):
st.json(item["args"])
elif item["type"] == "tool_result":
with st.expander(f"Step {i}: Tool result β {item['name']}", expanded=False):
st.write(item["content"])
elif item["type"] == "agent_message":
with st.expander(f"Step {i}: Agent response", expanded=False):
st.write(item["content"])
st.title("βοΈ Legal Agentic AI Project")
st.caption("LangGraph ReAct Agent + RAG + Web Search + LLM Reasoning | Hugging Face Spaces Deployment")
with st.sidebar:
st.header("Configuration")
model_name = st.selectbox(
"OpenAI Model",
["gpt-4o-mini", "gpt-4o", "gpt-4.1-mini"],
index=0,
)
temperature = st.slider(
"Temperature",
min_value=0.0,
max_value=1.0,
value=0.0,
step=0.1,
)
st.divider()
st.subheader("Secrets Status")
if os.getenv("OPENAI_API_KEY"):
st.success("OPENAI_API_KEY found")
else:
st.error("OPENAI_API_KEY missing")
if os.getenv("TAVILY_API_KEY"):
st.success("TAVILY_API_KEY found")
else:
st.warning("TAVILY_API_KEY missing - web search will be skipped")
st.divider()
st.markdown(
"""
**Agent Tools**
1. RAG Search Tool
2. Tavily Web Search Tool
3. Loophole Analysis Tool
"""
)
tab_demo, tab_architecture, tab_code, tab_deployment = st.tabs(
["π Live Demo", "π§ Architecture", "π§© Code Walkthrough", "βοΈ HF Deployment"]
)
with tab_demo:
st.subheader("Run the Legal Agent")
st.markdown(
"""
This demo takes a legal case, searches an internal legal knowledge base,
optionally searches the web using Tavily, analyzes loopholes, and produces a final legal analysis.
"""
)
case_text = st.text_area(
"Legal Case Input",
value=SAMPLE_CASE,
height=360,
)
col1, col2 = st.columns([1, 2])
with col1:
run_button = st.button("Run Legal Agent", type="primary", use_container_width=True)
with col2:
st.info("For learners: watch the trace below to understand Observe β Reason β Tool Use β Final Answer.")
if run_button:
if not secrets_ready():
st.error("OPENAI_API_KEY is missing. Add it in Hugging Face Space β Settings β Repository secrets.")
elif not case_text.strip():
st.error("Please enter a legal case.")
else:
with st.spinner("Agent is analyzing the case..."):
try:
result = run_legal_analysis(case_text, model_name, temperature)
st.session_state["last_result"] = result
except Exception as e:
st.exception(e)
if "last_result" in st.session_state:
result = st.session_state["last_result"]
st.success(f"Analysis complete. RAG initialized with {result['chunk_count']} document chunks.")
st.markdown("## Final Legal Analysis")
st.write(result["final_answer"])
st.markdown("## Agent Execution Trace")
display_trace(result["trace"])
with tab_architecture:
st.subheader("How the Agent Works")
st.markdown(
"""
The notebook project has been converted into a deployable Streamlit app.
The core design is a **ReAct Agent** built using **LangGraph**.
**Flow:**
```text
User Case
β
Agent Node
β
Decide: Need tools?
β
Tool Node
βββ RAG Search Tool
βββ Tavily Web Search Tool
βββ Loophole Analysis Tool
β
Agent Node again
β
Final Legal Analysis
```
"""
)
st.markdown("### What each tool does")
st.table(
[
{
"Tool": "rag_search_tool",
"Purpose": "Searches internal legal knowledge base for precedents.",
"Example": "Force majeure notice requirement",
},
{
"Tool": "tavily_web_search_tool",
"Purpose": "Searches current web information using Tavily.",
"Example": "Recent force majeure legal developments",
},
{
"Tool": "analyze_loopholes_tool",
"Purpose": "Uses LLM reasoning to identify loopholes and weaknesses.",
"Example": "Procedural gaps, evidentiary issues, damages weakness",
},
]
)
st.markdown("### Teaching point")
st.info(
"This is not just a chatbot. It is an agent because it can reason, decide which tool to use, call tools, observe results, and then continue reasoning."
)
with tab_code:
st.subheader("Code Walkthrough for Learners")
st.markdown("### 1. State")
st.code(
'''
class AgentState(TypedDict):
messages: Annotated[list, add_messages]
case_text: str
final_analysis: str
''',
language="python",
)
st.markdown("### 2. Tools")
st.code(
'''
tools = [
rag_search_tool,
tavily_web_search_tool,
analyze_loopholes_tool
]
''',
language="python",
)
st.markdown("### 3. Agent Node")
st.code(
'''
def agent_node(state):
system_prompt = SystemMessage(content="Legal analysis instructions...")
llm_with_tools = llm.bind_tools(tools)
response = llm_with_tools.invoke([system_prompt] + state["messages"])
return {"messages": [response]}
''',
language="python",
)
st.markdown("### 4. Conditional Routing")
st.code(
'''
def should_continue(state):
last_message = state["messages"][-1]
if last_message.tool_calls:
return "tools"
return "end"
''',
language="python",
)
st.markdown("### 5. LangGraph Workflow")
st.code(
'''
workflow = StateGraph(AgentState)
workflow.add_node("agent", agent_node)
workflow.add_node("tools", ToolNode(tools))
workflow.set_entry_point("agent")
workflow.add_conditional_edges("agent", should_continue, {
"tools": "tools",
"end": END
})
workflow.add_edge("tools", "agent")
graph = workflow.compile(checkpointer=MemorySaver())
''',
language="python",
)
with tab_deployment:
st.subheader("Hugging Face Spaces Deployment")
st.markdown(
"""
Your Space should have this structure:
```text
Hands-On-Agentic-Project/
β
βββ app.py
βββ Dockerfile
βββ requirements.txt
βββ README.md
β
βββ src/
βββ streamlit_app.py
```
**Secrets required:**
```text
OPENAI_API_KEY
TAVILY_API_KEY
```
`OPENAI_API_KEY` is required.
`TAVILY_API_KEY` is optional but recommended for the web-search tool.
After uploading this file inside the `src` folder, run:
**Factory rebuild** in Hugging Face Spaces.
"""
)
st.warning(
"This app is for educational demonstration. It is not legal advice and should not be used as a substitute for a qualified legal professional."
)
|