-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_summarizer.py
More file actions
28 lines (21 loc) · 868 Bytes
/
context_summarizer.py
File metadata and controls
28 lines (21 loc) · 868 Bytes
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
from __future__ import annotations
from typing import Iterable
class ContextSummarizer:
"""Simple defensive summarizer that never raises on arbitrary string inputs."""
@staticmethod
def summarize(chunks: Iterable[str]) -> str:
# Defensive handling: accept any iterable of strings and degrade gracefully on empty input.
if chunks is None:
return ""
safe_chunks: list[str] = []
for chunk in chunks:
if chunk is None:
continue
# Normalize to string and trim excessively long segments to bound memory.
text = str(chunk)
safe_chunks.append(text[:5000])
if not safe_chunks:
return ""
summary = "\n".join(safe_chunks)
# Hard cap to keep output manageable while preserving content.
return summary[:20000]