forked from voytek/tocify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigest.py
More file actions
322 lines (276 loc) · 11 KB
/
Copy pathdigest.py
File metadata and controls
322 lines (276 loc) · 11 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
import os, re, json, time, math, hashlib
from datetime import datetime, timezone, timedelta
import feedparser
import httpx
from dateutil import parser as dtparser
from openai import OpenAI, APITimeoutError, APIConnectionError, RateLimitError
# ---- config (env-tweakable) ----
MODEL = os.getenv("OPENAI_MODEL", "gpt-4o")
MAX_ITEMS_PER_FEED = int(os.getenv("MAX_ITEMS_PER_FEED", "50"))
MAX_TOTAL_ITEMS = int(os.getenv("MAX_TOTAL_ITEMS", "400"))
LOOKBACK_DAYS = int(os.getenv("LOOKBACK_DAYS", "7"))
INTERESTS_MAX_CHARS = int(os.getenv("INTERESTS_MAX_CHARS", "3000"))
SUMMARY_MAX_CHARS = int(os.getenv("SUMMARY_MAX_CHARS", "500"))
PREFILTER_KEEP_TOP = int(os.getenv("PREFILTER_KEEP_TOP", "200"))
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "50"))
MIN_SCORE_READ = float(os.getenv("MIN_SCORE_READ", "0.65"))
MAX_RETURNED = int(os.getenv("MAX_RETURNED", "40"))
SCHEMA = {
"type": "object",
"additionalProperties": False,
"properties": {
"week_of": {"type": "string"},
"notes": {"type": "string"},
"ranked": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": False,
"properties": {
"id": {"type": "string"},
"title": {"type": "string"},
"link": {"type": "string"},
"source": {"type": "string"},
"published_utc": {"type": ["string", "null"]},
"score": {"type": "number"},
"why": {"type": "string"},
"tags": {"type": "array", "items": {"type": "string"}},
},
"required": ["id", "title", "link", "source", "published_utc", "score", "why", "tags"],
},
},
},
"required": ["week_of", "notes", "ranked"],
}
# ---- tiny helpers ----
def load_feeds(path: str) -> list[dict]:
"""
Supports:
- blank lines
- comments starting with #
- optional naming via: Name | URL
Returns list of:
{ "name": "...", "url": "..." }
"""
feeds = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
s = line.strip()
if not s or s.startswith("#"):
continue
# Named feed: "Name | URL"
if "|" in s:
name, url = [x.strip() for x in s.split("|", 1)]
else:
name, url = None, s
feeds.append({
"name": name,
"url": url
})
return feeds
def read_text(path: str) -> str:
with open(path, "r", encoding="utf-8") as f:
return f.read()
def load_prompt_template(path: str = "prompt.txt") -> str:
if not os.path.exists(path):
raise RuntimeError("prompt.txt not found in repo root")
with open(path, "r", encoding="utf-8") as f:
return f.read()
def sha1(s: str) -> str:
return hashlib.sha1(s.encode("utf-8")).hexdigest()
def section(md: str, heading: str) -> str:
m = re.search(rf"(?im)^\s*#{1,6}\s+{re.escape(heading)}\s*$", md)
if not m:
return ""
rest = md[m.end():]
m2 = re.search(r"(?im)^\s*#{1,6}\s+\S", rest)
return (rest[:m2.start()] if m2 else rest).strip()
def parse_interests_md(md: str) -> dict:
keywords = []
for line in section(md, "Keywords").splitlines():
line = re.sub(r"^[\-\*\+]\s+", "", line.strip())
if line:
keywords.append(line)
narrative = section(md, "Narrative").strip()
if len(narrative) > INTERESTS_MAX_CHARS:
narrative = narrative[:INTERESTS_MAX_CHARS] + "…"
return {"keywords": keywords[:200], "narrative": narrative}
# ---- rss ----
def parse_date(entry) -> datetime | None:
for attr in ("published_parsed", "updated_parsed"):
t = getattr(entry, attr, None)
if t:
return datetime(*t[:6], tzinfo=timezone.utc)
for key in ("published", "updated", "created"):
val = entry.get(key)
if val:
try:
dt = dtparser.parse(val)
return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
except Exception:
pass
return None
def fetch_rss_items(feeds: list[dict]) -> list[dict]:
cutoff = datetime.now(timezone.utc) - timedelta(days=LOOKBACK_DAYS)
items = []
for feed in feeds:
url = feed["url"]
d = feedparser.parse(url)
# Priority: manual name > RSS title > URL
source = (
feed.get("name")
or d.feed.get("title")
or url
).strip()
for e in d.entries[:MAX_ITEMS_PER_FEED]:
title = (e.get("title") or "").strip()
link = (e.get("link") or "").strip()
if not (title and link):
continue
dt = parse_date(e)
if dt and dt < cutoff:
continue
summary = re.sub(r"\s+", " ", (e.get("summary") or e.get("description") or "").strip())
if len(summary) > SUMMARY_MAX_CHARS:
summary = summary[:SUMMARY_MAX_CHARS] + "…"
items.append({
"id": sha1(f"{source}|{title}|{link}"),
"source": source,
"title": title,
"link": link,
"published_utc": dt.isoformat() if dt else None,
"summary": summary,
})
# dedupe + newest first
items = list({it["id"]: it for it in items}.values())
items.sort(key=lambda x: x["published_utc"] or "", reverse=True)
return items[:MAX_TOTAL_ITEMS]
# ---- local prefilter ----
def keyword_prefilter(items: list[dict], keywords: list[str], keep_top: int) -> list[dict]:
kws = [k.lower() for k in keywords if k.strip()]
def hits(it):
text = (it.get("title","") + " " + it.get("summary","")).lower()
return sum(1 for k in kws if k in text)
scored = [(hits(it), it) for it in items]
matched = [it for s, it in scored if s > 0]
if len(matched) < min(50, keep_top):
return items[:keep_top]
matched.sort(key=hits, reverse=True)
return matched[:keep_top]
# ---- openai ----
def make_openai_client() -> OpenAI:
key = os.environ.get("OPENAI_API_KEY", "").strip()
if not key.startswith("sk-"):
raise RuntimeError("OPENAI_API_KEY missing/invalid (expected to start with 'sk-').")
http_client = httpx.Client(
timeout=httpx.Timeout(connect=30.0, read=300.0, write=30.0, pool=30.0),
http2=False,
trust_env=False,
headers={"Connection": "close", "Accept-Encoding": "gzip"},
)
return OpenAI(api_key=key, http_client=http_client)
def call_openai_triage(client: OpenAI, interests: dict, items: list[dict]) -> dict:
lean_items = [{
"id": it["id"],
"source": it["source"],
"title": it["title"],
"link": it["link"],
"published_utc": it.get("published_utc"),
"summary": (it.get("summary") or "")[:SUMMARY_MAX_CHARS],
} for it in items]
template = load_prompt_template()
prompt = (
template
.replace("{{KEYWORDS}}", json.dumps(interests["keywords"], ensure_ascii=False))
.replace("{{NARRATIVE}}", interests["narrative"])
.replace("{{ITEMS}}", json.dumps(lean_items, ensure_ascii=False))
)
last = None
for attempt in range(6):
try:
resp = client.responses.create(
model=MODEL,
input=prompt,
text={"format": {"type": "json_schema", "name": "weekly_toc_digest", "schema": SCHEMA, "strict": True}},
)
return json.loads(resp.output_text)
except (APITimeoutError, APIConnectionError, RateLimitError) as e:
last = e
time.sleep(min(60, 2 ** attempt))
raise last
def triage_in_batches(client: OpenAI, interests: dict, items: list[dict], batch_size: int) -> dict:
week_of = datetime.now(timezone.utc).date().isoformat()
total = math.ceil(len(items) / batch_size)
all_ranked, notes_parts = [], []
for i in range(0, len(items), batch_size):
batch = items[i:i + batch_size]
print(f"Triage batch {i // batch_size + 1}/{total} ({len(batch)} items)")
res = call_openai_triage(client, interests, batch)
if res.get("notes", "").strip():
notes_parts.append(res["notes"].strip())
all_ranked.extend(res.get("ranked", []))
best = {}
for r in all_ranked:
rid = r["id"]
if rid not in best or r["score"] > best[rid]["score"]:
best[rid] = r
ranked = sorted(best.values(), key=lambda x: x["score"], reverse=True)
return {"week_of": week_of, "notes": " ".join(dict.fromkeys(notes_parts))[:1000], "ranked": ranked}
# ---- render ----
def render_digest_md(result: dict, items_by_id: dict[str, dict]) -> str:
week_of = result["week_of"]
notes = result.get("notes", "").strip()
ranked = result.get("ranked", [])
kept = [r for r in ranked if r["score"] >= MIN_SCORE_READ][:MAX_RETURNED]
lines = [f"# Weekly ToC Digest (week of {week_of})", ""]
if notes:
lines += [notes, ""]
lines += [
f"**Included:** {len(kept)} (score ≥ {MIN_SCORE_READ:.2f}) ",
f"**Scored:** {len(ranked)} total items",
"",
"---",
"",
]
if not kept:
return "\n".join(lines + ["_No items met the relevance threshold this week._", ""])
for r in kept:
it = items_by_id.get(r["id"], {})
tags = ", ".join(r.get("tags", [])) if r.get("tags") else ""
pub = r.get("published_utc")
summary = (it.get("summary") or "").strip()
lines += [
f"## [{r['title']}]({r['link']})",
f"*{r['source']}* ",
f"Score: **{r['score']:.2f}**" + (f" \nPublished: {pub}" if pub else ""),
(f"Tags: {tags}" if tags else ""),
"",
r["why"].strip(),
"",
]
if summary:
lines += ["<details>", "<summary>RSS summary</summary>", "", summary, "", "</details>", ""]
lines += ["---", ""]
return "\n".join(lines)
def main():
interests = parse_interests_md(read_text("interests.md"))
feeds = load_feeds("feeds.txt")
items = fetch_rss_items(feeds)
print(f"Fetched {len(items)} RSS items (pre-filter)")
today = datetime.now(timezone.utc).date().isoformat()
if not items:
with open("digest.md", "w", encoding="utf-8") as f:
f.write(f"# Weekly ToC Digest (week of {today})\n\n_No RSS items found in the last {LOOKBACK_DAYS} days._\n")
print("No items; wrote digest.md")
return
items = keyword_prefilter(items, interests["keywords"], keep_top=PREFILTER_KEEP_TOP)
print(f"Sending {len(items)} RSS items to model (post-filter)")
items_by_id = {it["id"]: it for it in items}
client = make_openai_client()
result = triage_in_batches(client, interests, items, batch_size=BATCH_SIZE)
md = render_digest_md(result, items_by_id)
with open("digest.md", "w", encoding="utf-8") as f:
f.write(md)
print("Wrote digest.md")
if __name__ == "__main__":
main()