|
| 1 | +"""Fixture-first prospect research with a private scoring-policy boundary.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import csv |
| 7 | +import json |
| 8 | +import urllib.request |
| 9 | +from dataclasses import asdict, dataclass |
| 10 | +from pathlib import Path |
| 11 | +from typing import Iterable, Protocol |
| 12 | + |
| 13 | + |
| 14 | +@dataclass(frozen=True) |
| 15 | +class Prospect: |
| 16 | + name: str |
| 17 | + organization: str |
| 18 | + role: str |
| 19 | + source_url: str |
| 20 | + signals: tuple[str, ...] |
| 21 | + notes: str = "" |
| 22 | + |
| 23 | + |
| 24 | +@dataclass(frozen=True) |
| 25 | +class ReviewResult: |
| 26 | + prospect: Prospect |
| 27 | + status: str |
| 28 | + reasons: tuple[str, ...] |
| 29 | + |
| 30 | + |
| 31 | +class ScoringPolicy(Protocol): |
| 32 | + """Private implementations can provide weighted or model-assisted qualification.""" |
| 33 | + |
| 34 | + def evaluate(self, prospect: Prospect) -> ReviewResult: ... |
| 35 | + |
| 36 | + |
| 37 | +class PublicDemoPolicy: |
| 38 | + """Validate evidence completeness without publishing qualification weights.""" |
| 39 | + |
| 40 | + def evaluate(self, prospect: Prospect) -> ReviewResult: |
| 41 | + if not prospect.signals: |
| 42 | + return ReviewResult( |
| 43 | + prospect=prospect, |
| 44 | + status="needs_evidence", |
| 45 | + reasons=("no labeled public business signal was supplied",), |
| 46 | + ) |
| 47 | + return ReviewResult( |
| 48 | + prospect=prospect, |
| 49 | + status="human_review", |
| 50 | + reasons=("public source recorded", "labeled business signals require human review"), |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def load_fixture_prospects(path: str | Path) -> list[Prospect]: |
| 55 | + """Load fictional or consented records from a documented JSON fixture.""" |
| 56 | + payload = json.loads(Path(path).read_text(encoding="utf-8")) |
| 57 | + if not isinstance(payload, list): |
| 58 | + raise ValueError("fixture must be a JSON array") |
| 59 | + prospects: list[Prospect] = [] |
| 60 | + for index, item in enumerate(payload): |
| 61 | + required = {"name", "organization", "role", "source_url", "signals"} |
| 62 | + if not required.issubset(item): |
| 63 | + raise ValueError(f"fixture record {index} is missing required fields") |
| 64 | + if not str(item["source_url"]).startswith(("https://", "http://")): |
| 65 | + raise ValueError(f"fixture record {index} requires a public source URL") |
| 66 | + prospects.append( |
| 67 | + Prospect( |
| 68 | + name=str(item["name"]), |
| 69 | + organization=str(item["organization"]), |
| 70 | + role=str(item["role"]), |
| 71 | + source_url=str(item["source_url"]), |
| 72 | + signals=tuple(str(signal) for signal in item["signals"]), |
| 73 | + notes=str(item.get("notes", "")), |
| 74 | + ) |
| 75 | + ) |
| 76 | + return prospects |
| 77 | + |
| 78 | + |
| 79 | +def evaluate( |
| 80 | + prospects: Iterable[Prospect], |
| 81 | + policy: ScoringPolicy | None = None, |
| 82 | +) -> list[ReviewResult]: |
| 83 | + active_policy = policy or PublicDemoPolicy() |
| 84 | + return sorted( |
| 85 | + (active_policy.evaluate(item) for item in prospects), |
| 86 | + key=lambda item: (item.status != "human_review", item.prospect.organization), |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def ollama_explanation(item: ReviewResult, model: str, host: str) -> str: |
| 91 | + """Optionally ask a local model to summarize visible evidence, not qualify the lead.""" |
| 92 | + prompt = ( |
| 93 | + "Summarize this supplied public-business evidence in two factual sentences. " |
| 94 | + "Do not infer sensitive traits, purchase intent, or missing facts. " |
| 95 | + f"Record: {json.dumps(asdict(item.prospect))}. " |
| 96 | + f"Review status: {item.status}; reasons: {list(item.reasons)}" |
| 97 | + ) |
| 98 | + body = json.dumps({"model": model, "prompt": prompt, "stream": False}).encode() |
| 99 | + request = urllib.request.Request( |
| 100 | + f"{host.rstrip('/')}/api/generate", |
| 101 | + data=body, |
| 102 | + headers={"Content-Type": "application/json"}, |
| 103 | + ) |
| 104 | + with urllib.request.urlopen(request, timeout=60) as response: |
| 105 | + return str(json.load(response).get("response", "")).strip() |
| 106 | + |
| 107 | + |
| 108 | +def write_outputs( |
| 109 | + items: list[ReviewResult], |
| 110 | + output_dir: str | Path, |
| 111 | + explanations: dict[str, str] | None = None, |
| 112 | +) -> None: |
| 113 | + destination = Path(output_dir) |
| 114 | + destination.mkdir(parents=True, exist_ok=True) |
| 115 | + explanations = explanations or {} |
| 116 | + with (destination / "prospects.csv").open("w", newline="", encoding="utf-8") as handle: |
| 117 | + writer = csv.DictWriter( |
| 118 | + handle, |
| 119 | + fieldnames=["organization", "name", "role", "review_status", "reasons", "source_url"], |
| 120 | + ) |
| 121 | + writer.writeheader() |
| 122 | + for item in items: |
| 123 | + writer.writerow( |
| 124 | + { |
| 125 | + "organization": item.prospect.organization, |
| 126 | + "name": item.prospect.name, |
| 127 | + "role": item.prospect.role, |
| 128 | + "review_status": item.status, |
| 129 | + "reasons": "; ".join(item.reasons), |
| 130 | + "source_url": item.prospect.source_url, |
| 131 | + } |
| 132 | + ) |
| 133 | + lines = [ |
| 134 | + "# Prospect review", |
| 135 | + "", |
| 136 | + "> Research output only. Human approval is required before qualification or outreach.", |
| 137 | + "", |
| 138 | + ] |
| 139 | + for item in items: |
| 140 | + lines.extend( |
| 141 | + [ |
| 142 | + f"## {item.prospect.organization} — {item.status}", |
| 143 | + "", |
| 144 | + f"**Contact:** {item.prospect.name}, {item.prospect.role}", |
| 145 | + f"**Public source:** {item.prospect.source_url}", |
| 146 | + f"**Evidence boundary:** {'; '.join(item.reasons)}", |
| 147 | + ] |
| 148 | + ) |
| 149 | + if explanations.get(item.prospect.organization): |
| 150 | + lines.append(f"**Local-model summary:** {explanations[item.prospect.organization]}") |
| 151 | + lines.append("") |
| 152 | + (destination / "prospects.md").write_text("\n".join(lines), encoding="utf-8") |
| 153 | + |
| 154 | + |
| 155 | +def build_parser() -> argparse.ArgumentParser: |
| 156 | + parser = argparse.ArgumentParser(description=__doc__) |
| 157 | + parser.add_argument( |
| 158 | + "--fixture", |
| 159 | + required=True, |
| 160 | + help="JSON file containing fictional or consented public-source records", |
| 161 | + ) |
| 162 | + parser.add_argument("--output", default="outputs/sample", help="Output directory") |
| 163 | + parser.add_argument("--ollama-model", help="Optional local model used only to summarize supplied evidence") |
| 164 | + parser.add_argument("--ollama-host", default="http://127.0.0.1:11434") |
| 165 | + return parser |
| 166 | + |
| 167 | + |
| 168 | +def main(argv: list[str] | None = None) -> int: |
| 169 | + args = build_parser().parse_args(argv) |
| 170 | + items = evaluate(load_fixture_prospects(args.fixture)) |
| 171 | + explanations: dict[str, str] = {} |
| 172 | + if args.ollama_model: |
| 173 | + for item in items: |
| 174 | + explanations[item.prospect.organization] = ollama_explanation( |
| 175 | + item, args.ollama_model, args.ollama_host |
| 176 | + ) |
| 177 | + write_outputs(items, args.output, explanations) |
| 178 | + print(f"Wrote {len(items)} review records to {args.output}") |
| 179 | + return 0 |
| 180 | + |
| 181 | + |
| 182 | +if __name__ == "__main__": |
| 183 | + raise SystemExit(main()) |
0 commit comments