Repository navigation
Expand file tree
/
Copy pathGroqTest.py
More file actions
308 lines (253 loc) · 12 KB
/
Copy pathGroqTest.py
File metadata and controls
308 lines (253 loc) · 12 KB
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
"""
Section-aware CUAD extraction with a Groq-hosted LLM (via LangChain).
Instead of cutting a contract into blind fixed-width windows, we split it on its
own structure -- ARTICLE / Section / 1.1 / (a) / ALL-CAPS headings -- keep each
header glued to its body, and pack whole sections into chunks up to a size cap.
This keeps clauses intact and headers attached to the text they label, which is
exactly what the per-field extraction needs.
Output is the nbest format consumed by evaluate.py:
python evaluate.py --model_path <out_dir>
"""
import argparse
import asyncio
import json
import os
import re
import time
from pathlib import Path
from typing import Optional
import pandas as pd
from dotenv import load_dotenv
from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
from pydantic import Field, create_model
CATEGORY_CSV = "category_descriptions.csv"
MAX_TOKENS = 1200
CONCURRENCY = 1 # parallel in-flight calls; raise only if not rate-limited
# Safety net only: a section larger than this can't be sent (Groq rejects it
# with 413 "request too large"), so we split just those rare giants. ~97% of
# real sections are far smaller and pass through whole. ~16k chars ≈ 4k tokens.
MAX_CHARS = 16000
SYSTEM_PROMPT = (
"You are a legal contract analyst. For each schema field, return the EXACT "
"verbatim substring from the section that matches that field's description. "
"Copy character-by-character; never paraphrase or invent. Set a field to "
"null if its category does not appear in the section."
)
USER_PROMPT = 'Contract section:\n"""\n{chunk}\n"""'
# --- Schema -----------------------------------------------------------------
def load_categories(csv_path: str) -> dict[str, str]:
"""{label: description} from the CUAD category CSV (col 0 = label, 1 = desc)."""
df = pd.read_csv(csv_path, encoding="utf-8-sig")
cats = {}
for _, row in df.iterrows():
label = str(row.iloc[0]).split("Category:", 1)[-1].strip()
desc = str(row.iloc[1]).split("Description:", 1)[-1].strip()
cats[label] = desc
return cats
def build_schema(categories: dict[str, str]):
"""One Pydantic model with an Optional[str] field per category.
Returns (Schema, {field_name: qa_id_label})."""
fields, field_to_label = {}, {}
for label, desc in categories.items():
fname = re.sub(r"[^a-z0-9]+", "_", label.lower()).strip("_")
field_to_label[fname] = label.title()
fields[fname] = (Optional[str], Field(default=None, description=desc))
return create_model("ContractExtraction", **fields), field_to_label
def build_chain(model: str, schema, max_tokens: int = MAX_TOKENS):
llm = ChatGroq(model=model, temperature=0, max_tokens=max_tokens)
prompt = ChatPromptTemplate.from_messages(
[("system", SYSTEM_PROMPT), ("human", USER_PROMPT)]
)
return prompt | llm.with_structured_output(schema)
# --- Section-aware chunking -------------------------------------------------
# A line is a section header if it looks like: ARTICLE IV / Section 12.3 /
# "1." "1.1" "12.3.4" / "(a)" "(iv)" / an ALL-CAPS heading line.
# Only TOP-LEVEL section starts -- not sub-clauses. We deliberately do NOT match
# "1.1", "1.2.3", "(a)", "(iv)" etc., so a whole section (with all its
# sub-paragraphs) stays together in one chunk.
_HEADER_RE = re.compile(
r"^(?:"
r"(?:ARTICLE|Article|SECTION|Section)\s+[0-9IVXLCDM]+\b" # ARTICLE IV / Section 12
r"|[0-9]{1,2}\.(?!\d)\s" # top-level "1. " / "12. " (not 1.1)
r"|[A-Z][A-Z0-9 ,;:'&/().\-]{3,}\s*$" # ALL-CAPS heading line
r")"
)
def _split_oversized(text: str, limit: int) -> list[str]:
"""Split ONE section that's too big to send, losslessly, on the best
available boundary (paragraph > line > word). Only the rare giant section
ever reaches here."""
if len(text) <= limit:
return [text]
for sep in ("\n\n", "\n", " "):
if sep not in text:
continue
parts = text.split(sep)
out, buf = [], ""
for j, part in enumerate(parts):
piece = part + (sep if j < len(parts) - 1 else "") # keep sep between, not after last
if buf and len(buf) + len(piece) > limit:
out.append(buf)
buf = piece
else:
buf += piece
if buf:
out.append(buf)
# A single piece may still exceed the limit -> recurse with a finer sep.
result = []
for c in out:
result += [c] if len(c) <= limit else _split_oversized(c, limit)
return result
return [text[i:i + limit] for i in range(0, len(text), limit)] # no separators at all
def chunk_by_section(text: str, max_chars: int = MAX_CHARS) -> list[str]:
"""Split the contract on its top-level headers (ARTICLE / Section / "1." /
ALL-CAPS), keeping each header glued to the body beneath it. One section =
one chunk; only a section bigger than `max_chars` is split further so it can
actually be sent. Lossless: ''.join(result) == text exactly."""
sections, cur = [], []
for line in text.splitlines(keepends=True):
if cur and _HEADER_RE.match(line.strip()):
sections.append("".join(cur))
cur = [line]
else:
cur.append(line)
if cur:
sections.append("".join(cur))
chunks = []
for sec in (sections or [text]):
chunks += _split_oversized(sec, max_chars)
return chunks
# --- Extraction -------------------------------------------------------------
def validate_span(span: Optional[str], chunk: str) -> Optional[str]:
"""Keep only spans that literally appear in the chunk (raw extractions)."""
if not span:
return None
span = span.strip().strip('"').strip("'").strip()
if span and span in chunk:
return span
if span and span.lower() in chunk.lower():
idx = chunk.lower().find(span.lower())
return chunk[idx:idx + len(span)]
return None
_RETRY_RE = re.compile(r"try again in\s+(?:(\d+)m\s*)?([\d.]+)\s*s", re.IGNORECASE)
async def call_with_retry(chain, inputs: dict, retries: int = 5):
"""ainvoke with reactive backoff: on a 429, sleep exactly as long as Groq's
'try again in ...' hint asks, then retry."""
for attempt in range(retries + 1):
try:
return await chain.ainvoke(inputs)
except Exception as e:
m = _RETRY_RE.search(str(e))
if m is None or attempt == retries:
raise
wait = float(m.group(2)) + (int(m.group(1)) * 60 if m.group(1) else 0)
print(f" rate-limited, sleeping {wait:.1f}s (attempt {attempt + 1})")
await asyncio.sleep(wait + 0.5)
async def extract_contract(chain, field_to_label, context: str,
labels: list[str], concurrency: int) -> dict[str, list[str]]:
"""One LLM call per section-chunk (capped by `concurrency`); collect the
validated spans per category."""
sem = asyncio.Semaphore(concurrency)
chunks = chunk_by_section(context)
async def process(chunk: str):
async with sem:
try:
return await call_with_retry(chain, {"chunk": chunk})
except Exception as e:
return e # transient -> skip this chunk
results = await asyncio.gather(*(process(c) for c in chunks))
preds = {label: [] for label in labels}
errors, first_err = 0, None
for chunk, res in zip(chunks, results):
if isinstance(res, Exception):
errors += 1
first_err = first_err or res
continue
if res is None:
continue
for fname, label in field_to_label.items():
span = validate_span(getattr(res, fname, None), chunk)
if span:
preds[label].append(span)
if errors:
# Surface the real cause instead of silently returning zeros.
print(f" !! {errors}/{len(chunks)} chunks failed -> e.g. {str(first_err)[:180]}")
return preds
# --- Runner -----------------------------------------------------------------
def load_checkpoint(path: Path) -> dict:
"""Resume an interrupted run: the checkpoint IS the nbest dict so far."""
if path.exists():
print(f"Resuming from checkpoint at {path}")
return json.loads(path.read_text(encoding="utf-8"))
return {}
def save_checkpoint(nbest: dict, path: Path):
path.write_text(json.dumps(nbest, indent=2), encoding="utf-8")
async def run(args):
categories = load_categories(CATEGORY_CSV)
schema, field_to_label = build_schema(categories)
label_set = set(field_to_label.values())
chain = build_chain(args.model, schema, args.max_tokens)
contracts = json.loads(Path(args.data).read_text(encoding="utf-8"))["data"]
out_dir = Path(args.out)
out_dir.mkdir(parents=True, exist_ok=True)
checkpoint_path = out_dir / "_checkpoint.json"
nbest = load_checkpoint(checkpoint_path) # {} on a fresh run
print(f"Groq section-aware eval | model={args.model} | {len(contracts)} contracts "
f"| concurrency={args.concurrency} | already done: {len(nbest)} QAs")
t0, done = time.time(), 0
for ci, contract in enumerate(contracts):
title = contract["title"]
ctx = contract["paragraphs"][0]["context"]
qas = contract["paragraphs"][0]["qas"]
target_ids = [qa["id"] for qa in qas if qa["id"].split("__")[-1] in label_set]
# Resume: contracts are written atomically, so if every target QA is
# already in the checkpoint we skip the whole contract (no LLM calls).
if target_ids and all(qid in nbest for qid in target_ids):
done += len(target_ids)
if args.limit and done >= args.limit:
break
continue
n_chunks = len(chunk_by_section(ctx))
print(f"\n[{ci + 1}/{len(contracts)}] {title[:60]} "
f"({len(ctx):,} chars, {n_chunks} chunks)")
preds = await extract_contract(
chain, field_to_label, ctx, list(label_set), args.concurrency,
)
for qa in qas:
label = qa["id"].split("__")[-1]
if label not in label_set:
continue
spans = list(dict.fromkeys(preds.get(label, []))) # dedupe, keep order
nbest[qa["id"]] = (
[{"text": s, "probability": 1.0} for s in spans]
+ [{"text": "", "probability": 0.0}] # no-answer sentinel
)
done += 1
save_checkpoint(nbest, checkpoint_path) # persist after every contract
hits = sum(1 for p in preds.values() if p)
print(f" {hits}/{len(label_set)} labels hit, {done} QAs total")
if args.limit and done >= args.limit:
print(f"\nReached --limit {args.limit}, stopping")
break
out_path = out_dir / "nbest_predictions_.json"
out_path.write_text(json.dumps(nbest, indent=2), encoding="utf-8")
print(f"\nWrote {len(nbest)} QA predictions to {out_path}")
print(f"Total time: {(time.time() - t0) / 60:.1f} min")
if checkpoint_path.exists(): # finished cleanly -> drop the checkpoint
checkpoint_path.unlink()
print(f"Next: python evaluate.py --model_path {out_dir}")
if __name__ == "__main__":
load_dotenv()
if not os.getenv("GROQ_API_KEY"):
raise SystemExit("GROQ_API_KEY missing -- add it to .env or your shell.")
ap = argparse.ArgumentParser()
ap.add_argument("--data", default="test.json")
ap.add_argument("--model", default="llama-3.3-70b-versatile")
ap.add_argument("--limit", type=int, default=None, help="Cap QAs (smoke test).")
ap.add_argument("--out", default=None, help="Output dir (default trained_models/<model>).")
ap.add_argument("--concurrency", type=int, default=CONCURRENCY)
ap.add_argument("--max_tokens", type=int, default=MAX_TOKENS)
args = ap.parse_args()
if args.out is None:
args.out = "trained_models/" + args.model.replace(":", "-").replace("/", "_") + "__section"
asyncio.run(run(args))