File size: 7,369 Bytes
0af0679 |
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 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### llm_legal_advisor (Parallelization-pattern)\n",
"\n",
"#### Overview\n",
"This module implements a parallel legal document analysis system using multiple AI agents to process legal documents concurrently."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports \n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown, display\n",
"import concurrent.futures"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"open_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"groq_api_key = os.getenv(\"GROQ_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Helper Functions\n",
"\n",
"##### Technical Details\n",
"- **Concurrency**: Uses ThreadPoolExecutor for parallel processing\n",
"- **API**: Groq API with OpenAI-compatible interface\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### `llm_summarizer`"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"# Summarizes legal documents using AI\n",
"def llm_summarizer(document: str) -> str:\n",
" response = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\").chat.completions.create(\n",
" model=\"llama-3.3-70b-versatile\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a corporate lawyer. Summarize the key points of legal documents clearly.\"},\n",
" {\"role\": \"user\", \"content\": f\"Summarize this document:\\n\\n{document}\"}\n",
" ],\n",
" temperature=0.3,\n",
" )\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### `llm_evaluate_risks`"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"# Identifies and analyzes legal risks in documents\n",
"def llm_evaluate_risks(document: str) -> str:\n",
" response = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\").chat.completions.create(\n",
" model=\"llama-3.3-70b-versatile\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a corporate lawyer. Identify and explain legal risks in the following document.\"},\n",
" {\"role\": \"user\", \"content\": f\"Analyze the legal risks:\\n\\n{document}\"}\n",
" ],\n",
" temperature=0.3,\n",
" )\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### `llm_tag_clauses`"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"# Classifies and tags legal clauses by category\n",
"def llm_tag_clauses(document: str) -> str:\n",
" response = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\").chat.completions.create(\n",
" model=\"llama-3.3-70b-versatile\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a legal clause classifier. Tag each clause with relevant legal and compliance categories.\"},\n",
" {\"role\": \"user\", \"content\": f\"Classify and tag clauses in this document:\\n\\n{document}\"}\n",
" ],\n",
" temperature=0.3,\n",
" )\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### `aggregator`"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"# Organizes and formats multiple AI responses into a structured report\n",
"def aggregator(responses: list[str]) -> str:\n",
" sections = {\n",
" \"summary\": \"[Section 1: Summary]\",\n",
" \"risk\": \"[Section 2: Risk Analysis]\",\n",
" \"clauses\": \"[Section 3: Clause Classification & Compliance Tags]\"\n",
" }\n",
"\n",
" ordered = {\n",
" \"summary\": None,\n",
" \"risk\": None,\n",
" \"clauses\": None\n",
" }\n",
"\n",
" for r in responses:\n",
" content = r.lower()\n",
" if any(keyword in content for keyword in [\"summary\", \"[summary]\"]):\n",
" ordered[\"summary\"] = r\n",
" elif any(keyword in content for keyword in [\"risk\", \"liability\"]):\n",
" ordered[\"risk\"] = r\n",
" else:\n",
" ordered[\"clauses\"] = r\n",
"\n",
" report_sections = [\n",
" f\"{sections[key]}\\n{value.strip()}\"\n",
" for key, value in ordered.items() if value\n",
" ]\n",
"\n",
" return \"\\n\\n\".join(report_sections)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### `coordinator`"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"# Orchestrates parallel execution of all legal analysis agents\n",
"def coordinator(document: str) -> str:\n",
" \"\"\"Dispatch document to agents and aggregate results\"\"\"\n",
" agents = [llm_summarizer, llm_evaluate_risks, llm_tag_clauses]\n",
" with concurrent.futures.ThreadPoolExecutor() as executor:\n",
" futures = [executor.submit(agent, document) for agent in agents]\n",
" results = [f.result() for f in concurrent.futures.as_completed(futures)]\n",
" return aggregator(results)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Lets ask our legal corporate advisor</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dummy_document = \"\"\"\n",
"This agreement is made between ABC Corp and XYZ Ltd. The responsibilities of each party shall be determined as the project progresses.\n",
"ABC Corp may terminate the contract at its discretion. No specific provisions are mentioned regarding data protection or compliance with GDPR.\n",
"For more information, refer the clauses 10 of the agreement.\n",
"\"\"\"\n",
"\n",
"final_report = coordinator(dummy_document)\n",
"print(final_report)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|