Spaces:
Sleeping
Sleeping
File size: 1,089 Bytes
454f118 | 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 | """
src/generation/response_parser.py
===================================
Parses LLM responses to extract structured citation info.
"""
from __future__ import annotations
import re
def extract_citations(answer_text: str) -> list[dict]:
"""
Extract [SOURCE N] citation markers from LLM answer text.
Returns list of dicts with citation info parsed from the answer.
"""
citations = []
pattern = r'\[SOURCE\s+(\d+)\]'
matches = re.finditer(pattern, answer_text)
for match in matches:
idx = int(match.group(1))
if not any(c["index"] == idx for c in citations):
citations.append({"index": idx, "marker": match.group(0)})
return citations
def clean_answer(answer_text: str) -> str:
"""Remove redundant whitespace from LLM output."""
lines = answer_text.split("\n")
cleaned = []
prev_blank = False
for line in lines:
is_blank = not line.strip()
if is_blank and prev_blank:
continue
cleaned.append(line)
prev_blank = is_blank
return "\n".join(cleaned).strip()
|