forked from ChelseaKR/ceqa-preflight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrounding.py
More file actions
208 lines (170 loc) · 7.52 KB
/
Copy pathgrounding.py
File metadata and controls
208 lines (170 loc) · 7.52 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
"""Grounded generation: the model narrates, the corpus is the evidence, a verifier decides.
Every substantive sentence a model produces here must cite one or more corpus passages by
identifier and quote each verbatim. ``verify_claims`` checks every citation against the
passages the model was actually shown, checks every quote against the corpus text, and
checks every sentence for determination language. A claim that fails any check is withheld
and counted. Nothing reaches a reader that did not pass.
"""
from __future__ import annotations
import json
import re
from typing import Any
from pydantic import Field
from ceqa_preflight.ai.client import ModelClient, ModelError
from ceqa_preflight.ai.corpus import Corpus, Passage, normalize_for_match
from ceqa_preflight.ai.guard import determination_language
from ceqa_preflight.models import SourceKind, StrictModel
from ceqa_preflight.rule_catalog import RuleDefinition
# The LCI document-submission page is retrieved for every filing-specific rule, since it
# names the NOD and NOE attachments, even though no rule cites it directly.
_FILING_CONTEXT_DOCUMENT = "lci-sch-document-submission"
class Citation(StrictModel):
passage_id: str = Field(min_length=1)
quote: str = Field(min_length=1)
verified: bool = False
class Claim(StrictModel):
text: str = Field(min_length=1)
citations: list[Citation] = Field(default_factory=list)
class WithheldClaim(StrictModel):
"""A claim that did not pass verification, kept for the count and the audit trail."""
reason: str = Field(min_length=1)
citation_count: int = Field(ge=0)
class SourceSummary(StrictModel):
"""What a reader needs to find the passage a claim quotes."""
passage_id: str
title: str
url: str
kind: SourceKind
heading: str | None = None
def passages_for_rule(
corpus: Corpus, rule: RuleDefinition, query: str, *, limit: int = 6
) -> list[Passage]:
"""Retrieve passages from the documents a rule cites, ranked against ``query``."""
document = corpus.document_for_url(rule.source.url)
document_ids = [] if document is None else [document.id]
for section in rule.guidelines:
held = corpus.document_for_section(section)
if held is not None and held.id not in document_ids:
document_ids.append(held.id)
if len(rule.filing_types) == 1 and any(
item.id == _FILING_CONTEXT_DOCUMENT for item in corpus.documents
):
document_ids.append(_FILING_CONTEXT_DOCUMENT)
return corpus.retrieve(document_ids, query, limit=limit) if document_ids else []
def render_passages(passages: list[Passage]) -> str:
"""Render passages for a prompt with their identifiers, so citations can name them."""
blocks = []
for passage in passages:
heading = f" ({passage.heading})" if passage.heading else ""
blocks.append(f"[{passage.id}]{heading}\n{passage.text}")
return "\n\n".join(blocks)
def parse_claims(text: str) -> list[Claim]:
"""Parse the model's JSON claims strictly; anything unparseable is a model error."""
candidate = text.strip()
fenced = re.match(r"^```(?:json)?\s*(.*?)\s*```$", candidate, re.S)
if fenced:
candidate = fenced.group(1)
try:
parsed: Any = json.loads(candidate)
except json.JSONDecodeError as error:
raise ModelError("model output was not a JSON object") from error
if not isinstance(parsed, dict) or not isinstance(parsed.get("claims"), list):
raise ModelError("model output did not contain a claims list")
claims: list[Claim] = []
for raw in parsed["claims"]:
if not isinstance(raw, dict) or not isinstance(raw.get("text"), str):
continue
text_value = raw["text"].strip()
if not text_value:
continue
citations = [
Citation(passage_id=str(item["passage_id"]), quote=str(item["quote"]))
for item in raw.get("citations") or []
if isinstance(item, dict) and item.get("passage_id") and item.get("quote")
]
claims.append(Claim(text=text_value, citations=citations))
return claims
def quote_in(passage: Passage, quote: str) -> bool:
"""Return whether ``quote`` appears verbatim (modulo whitespace) in the passage."""
needle = normalize_for_match(quote)
return bool(needle) and needle in normalize_for_match(passage.text)
def verify_claims(
shown: list[Passage], claims: list[Claim]
) -> tuple[list[Claim], list[WithheldClaim]]:
"""Keep only claims whose every citation verifies and whose text makes no determination.
A citation verifies only against a passage the model was shown: the quote must appear
verbatim in that passage. Shown passages come from the verified corpus (or, for
questions, from the report itself), so this is also a check against the corpus.
"""
by_id = {passage.id: passage for passage in shown}
verified: list[Claim] = []
withheld: list[WithheldClaim] = []
for claim in claims:
phrase = determination_language(claim.text)
if phrase is not None:
withheld.append(
WithheldClaim(
reason=f"determination language: {phrase!r}",
citation_count=len(claim.citations),
)
)
continue
if not claim.citations:
withheld.append(WithheldClaim(reason="no citation", citation_count=0))
continue
checked = [
citation.model_copy(
update={
"verified": citation.passage_id in by_id
and quote_in(by_id[citation.passage_id], citation.quote)
}
)
for citation in claim.citations
]
if all(citation.verified for citation in checked):
verified.append(claim.model_copy(update={"citations": checked}))
else:
failed = sum(not citation.verified for citation in checked)
withheld.append(
WithheldClaim(
reason=f"{failed} citation(s) did not verify against the corpus",
citation_count=len(checked),
)
)
return verified, withheld
def sources_for(corpus: Corpus, claims: list[Claim]) -> list[SourceSummary]:
"""List each cited passage once, in first-cited order, with its document's provenance."""
seen: dict[str, SourceSummary] = {}
for claim in claims:
for citation in claim.citations:
if citation.passage_id in seen:
continue
passage = corpus.passage(citation.passage_id)
if passage is None:
continue
document = corpus.document(citation.passage_id.split("#", 1)[0])
seen[citation.passage_id] = SourceSummary(
passage_id=citation.passage_id,
title=document.title,
url=document.url,
kind=document.kind,
heading=passage.heading,
)
return list(seen.values())
def generate_grounded(
client: ModelClient,
corpus: Corpus,
*,
system: str,
user: str,
shown: list[Passage],
max_tokens: int,
) -> tuple[list[Claim], list[WithheldClaim], str | None]:
"""Run one grounded generation and return (verified, withheld, model_error)."""
try:
response = client.complete(system=system, user=user, max_tokens=max_tokens)
claims = parse_claims(response.text)
except ModelError as error:
return [], [], str(error)
verified, withheld = verify_claims(shown, claims)
return verified, withheld, None