feat(server): add workbench API, DSH planner worker and RAG pipeline
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"""Bounded source excerpts with relevant middle/tail evidence and honest omissions."""
|
||||
import re
|
||||
|
||||
|
||||
def source_excerpt(text: str, query: str = "", limit: int = 2400) -> str:
|
||||
text = str(text or "")
|
||||
if len(text) <= limit:
|
||||
return text
|
||||
marker = "\n[…此处省略,未展示内容不能作为已知事实…]\n"
|
||||
# Keep boundary conditions and conclusion as well as query-related evidence.
|
||||
edge = min(350, limit // 5)
|
||||
spans = [(0, edge), (len(text) - edge, len(text))]
|
||||
terms = re.findall(r"[A-Za-z0-9_]+|[\u3400-\u9fff]{2,}", str(query).lower())
|
||||
terms += [term[i:i + 2] for term in list(terms) if len(term) > 2
|
||||
for i in range(len(term) - 1)]
|
||||
lower = text.lower()
|
||||
candidates = []
|
||||
for term in dict.fromkeys(terms):
|
||||
position = lower.find(term)
|
||||
if position >= edge and position < len(text) - edge:
|
||||
candidates.append((position, len(term)))
|
||||
candidates.sort(key=lambda pair: (-pair[1], pair[0]))
|
||||
remaining = max(0, limit - 2 * edge - 3 * len(marker))
|
||||
for position, _ in candidates:
|
||||
if remaining < 120:
|
||||
break
|
||||
if any(start <= position < end for start, end in spans):
|
||||
continue
|
||||
size = min(remaining, 900)
|
||||
start = max(0, position - size // 3)
|
||||
spans.append((start, min(len(text), start + size)))
|
||||
remaining -= size + len(marker)
|
||||
if len(spans) == 2 and remaining:
|
||||
spans.append((edge, min(len(text) - edge, edge + remaining)))
|
||||
merged = []
|
||||
for start, end in sorted(spans):
|
||||
if merged and start <= merged[-1][1]:
|
||||
merged[-1] = (merged[-1][0], max(end, merged[-1][1]))
|
||||
else:
|
||||
merged.append((start, end))
|
||||
return marker.join(text[start:end] for start, end in merged)
|
||||
Reference in New Issue
Block a user