forked from ChelseaKR/permit-bearings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
344 lines (291 loc) · 11.5 KB
/
Copy pathservice.py
File metadata and controls
344 lines (291 loc) · 11.5 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
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
"""The optional runtime AI service (ADR 0004).
A small FastAPI application that the static project-check page calls when it
is running. It exposes natural-language intake extraction, grounded
explanation, and staff-question drafting over the committed rules and
corpus. It keeps no applicant content: request bodies live in process memory
for one request and are never written to disk or logs.
Run locally with ``python -m permit_pathways.ai`` (or ``make serve-ai``).
The provider credential is read from the environment by the ``anthropic``
SDK; see ``ProviderSettings`` for the ``PERMIT_AI_*`` variables.
"""
import os
from collections.abc import Callable, Mapping
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from ..screening import Rule, load_rules
from . import explain as explain_module
from . import facts
from . import intake as intake_module
from . import staff_questions as staff_module
from .budget import Budget, BudgetExhausted, budget_from_env
from .corpus import CorpusIndex
from .intake import IntakeError
from .provider import Provider, ProviderError, ProviderSettings, provider_from_settings
DEFAULT_ORIGINS = ("http://localhost:8765", "http://127.0.0.1:8765", "null")
DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 8787
MAX_INTAKE_VALUE_CHARS = 64
INTAKE_KEYS = frozenset({"project_type", "jurisdiction", *facts.FACT_NAMES})
def repository_root() -> Path:
override = os.environ.get("PERMIT_AI_ROOT", "").strip()
if override:
return Path(override).resolve()
return Path(__file__).resolve().parents[3]
@dataclass(frozen=True)
class ServiceContext:
root: Path
rules: tuple[Rule, ...]
corpus: CorpusIndex
registry: tuple[intake_module.JurisdictionEntry, ...]
provider: Provider
allowed_origins: tuple[str, ...]
budget: Budget | None = None
@classmethod
def load(
cls,
*,
root: Path,
provider: Provider,
allowed_origins: tuple[str, ...] = DEFAULT_ORIGINS,
budget: Budget | None = None,
) -> "ServiceContext":
import json
rules = tuple(load_rules(root / "data" / "rules"))
corpus = CorpusIndex.load(root)
registry_payload = json.loads(
(root / "data" / "jurisdictions" / "registry.json").read_text(
encoding="utf-8"
)
)
registry = intake_module.load_jurisdictions(registry_payload["jurisdictions"])
return cls(root, rules, corpus, registry, provider, allowed_origins, budget)
def health(self) -> dict[str, Any]:
return {
"status": "ok",
"service": "permit-bearings-ai",
"provider": self.provider.name,
"model": self.provider.model,
"prompt_versions": {
"intake": intake_module.PROMPT_VERSION,
"explain": explain_module.PROMPT_VERSION,
"ask": explain_module.ASK_PROMPT_VERSION,
"staff_questions": staff_module.PROMPT_VERSION,
},
"daily_cap": self.budget.daily_cap if self.budget else None,
"rules": len(self.rules),
"corpus_documents": len(self.corpus.documents),
"corpus_skipped": sorted(self.corpus.skipped),
"stores_applicant_content": False,
}
class RequestError(ValueError):
"""A request body the service will not act on."""
def validate_intake(payload: Mapping[str, Any]) -> dict[str, str]:
"""Accept only the matcher's own fact names with allowed values."""
cleaned: dict[str, str] = {}
for key, value in payload.items():
if key not in INTAKE_KEYS:
raise RequestError(f"unknown intake field: {key}")
if not isinstance(value, str) or len(value) > MAX_INTAKE_VALUE_CHARS:
raise RequestError(f"intake field {key} must be a short string")
if key != "jurisdiction" and value not in facts.allowed_values(key):
raise RequestError(
f"intake field {key} has a value outside the allowed list"
)
cleaned[key] = value
if "project_type" not in cleaned:
raise RequestError("intake must include project_type")
return cleaned
def allowed_origins_from_env(
environ: Mapping[str, str] | None = None,
) -> tuple[str, ...]:
env = os.environ if environ is None else environ
raw = env.get("PERMIT_AI_ALLOWED_ORIGINS", "").strip()
if not raw:
return DEFAULT_ORIGINS
return tuple(origin.strip() for origin in raw.split(",") if origin.strip())
def _http_error(exception_type: Any, status: int, exc: Exception, code: str) -> Any:
return exception_type(
status_code=status, detail={"error": code, "message": str(exc)}
)
def guarded(
compute: Callable[[], dict[str, Any]], exception_type: Any
) -> dict[str, Any]:
"""Run one request; translate domain errors to HTTP status codes without
echoing request content into the response beyond the validation message."""
try:
return compute()
except explain_module.MatcherDisagreement as exc:
raise _http_error(exception_type, 409, exc, "matcher_disagreement") from exc
except (RequestError, explain_module.ExplainError, IntakeError) as exc:
raise _http_error(exception_type, 400, exc, "invalid_request") from exc
except ProviderError as exc:
raise _http_error(exception_type, 502, exc, "provider_unavailable") from exc
except BudgetExhausted as exc:
raise _http_error(exception_type, 429, exc, "budget_exhausted") from exc
def charge(context: ServiceContext, client_id: str) -> None:
"""Consume one model-backed request from the budget, if one is configured."""
if context.budget is not None:
context.budget.charge(client_id)
def run_intake(context: ServiceContext, text: str, language: str) -> dict[str, Any]:
extraction = intake_module.extract_intake(
text, language=language, provider=context.provider, registry=context.registry
)
payload = extraction.to_dict()
payload["draft_intake"] = extraction.draft_intake()
return payload
def run_explain(
context: ServiceContext,
intake: Mapping[str, Any],
language: str,
matched_rule_ids: list[str] | None,
) -> dict[str, Any]:
return explain_module.explain_result(
intake=validate_intake(intake),
rules=context.rules,
corpus=context.corpus,
provider=context.provider,
language=language,
expected_rule_ids=matched_rule_ids,
).to_dict()
def run_ask(
context: ServiceContext,
question: str,
intake: Mapping[str, Any],
language: str,
matched_rule_ids: list[str] | None,
) -> dict[str, Any]:
return explain_module.answer_question(
question=question,
intake=validate_intake(intake),
rules=context.rules,
corpus=context.corpus,
provider=context.provider,
language=language,
expected_rule_ids=matched_rule_ids,
).to_dict()
def run_staff_questions(
context: ServiceContext,
intake: Mapping[str, Any],
language: str,
matched_rule_ids: list[str] | None,
) -> dict[str, Any]:
return staff_module.draft_staff_questions(
intake=validate_intake(intake),
rules=context.rules,
provider=context.provider,
language=language,
expected_rule_ids=matched_rule_ids,
).to_dict()
def create_app(context: ServiceContext) -> Any:
"""Build the FastAPI app around a loaded context. FastAPI is imported
lazily so the rest of the package works without the `ai` extra."""
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
class IntakeRequest(BaseModel):
text: str = Field(min_length=1, max_length=intake_module.MAX_TEXT_CHARS)
language: str = "en"
class ResultRequest(BaseModel):
intake: dict[str, str]
language: str = "en"
matched_rule_ids: list[str] | None = None
class AskRequest(ResultRequest):
question: str = Field(
min_length=1, max_length=explain_module.MAX_QUESTION_CHARS
)
def client_id(request: Request) -> str:
forwarded = request.headers.get("x-forwarded-for", "")
if forwarded:
return forwarded.split(",")[0].strip()
return request.client.host if request.client else "unknown"
app = FastAPI(
title="Permit Bearings AI service",
version=f"{intake_module.PROMPT_VERSION}+{explain_module.PROMPT_VERSION}",
docs_url=None,
redoc_url=None,
)
app.add_middleware(
CORSMiddleware,
allow_origins=list(context.allowed_origins),
allow_methods=["GET", "POST"],
allow_headers=["Content-Type"],
)
@app.get("/health")
def health() -> dict[str, Any]:
return context.health()
def metered(http: Request, compute: Callable[[], dict[str, Any]]) -> dict[str, Any]:
def charged() -> dict[str, Any]:
charge(context, client_id(http))
return compute()
return guarded(charged, HTTPException)
@app.post("/intake/extract")
def intake_extract(request: IntakeRequest, http: Request) -> dict[str, Any]:
return metered(
http, lambda: run_intake(context, request.text, request.language)
)
@app.post("/explain")
def explain(request: ResultRequest, http: Request) -> dict[str, Any]:
return metered(
http,
lambda: run_explain(
context, request.intake, request.language, request.matched_rule_ids
),
)
@app.post("/ask")
def ask(request: AskRequest, http: Request) -> dict[str, Any]:
return metered(
http,
lambda: run_ask(
context,
request.question,
request.intake,
request.language,
request.matched_rule_ids,
),
)
@app.post("/staff-questions")
def staff_questions(request: ResultRequest, http: Request) -> dict[str, Any]:
return metered(
http,
lambda: run_staff_questions(
context, request.intake, request.language, request.matched_rule_ids
),
)
return app
def load_context_from_env(environ: Mapping[str, str] | None = None) -> ServiceContext:
settings = ProviderSettings.from_environ(environ)
provider = provider_from_settings(settings)
return ServiceContext.load(
root=repository_root(),
provider=provider,
allowed_origins=allowed_origins_from_env(environ),
budget=budget_from_env(environ),
)
def main(
argv: list[str] | None = None,
) -> int: # pragma: no cover - process entry point
import argparse
import uvicorn
parser = argparse.ArgumentParser(description="Run the Permit Bearings AI service.")
parser.add_argument(
"--host", default=os.environ.get("PERMIT_AI_HOST", DEFAULT_HOST)
)
parser.add_argument(
"--port", type=int, default=int(os.environ.get("PERMIT_AI_PORT", DEFAULT_PORT))
)
args = parser.parse_args(argv)
try:
context = load_context_from_env()
except Exception as exc:
print(f"permit-bearings-ai: cannot start: {exc}")
return 2
print(
f"permit-bearings-ai: provider={context.provider.name} model={context.provider.model} "
f"rules={len(context.rules)} corpus_documents={len(context.corpus.documents)} "
f"origins={','.join(context.allowed_origins)}"
)
uvicorn.run(
create_app(context), host=args.host, port=args.port, log_level="warning"
)
return 0