Spaces:
Sleeping
Sleeping
Download src/generation/response_parser.py from Harshavard21/FinRAG: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/Harshavard21/FinRAG/resolve/main/src/generation/response_parser.py
- Command line
-
hf download hf://spaces/Harshavard21/FinRAG/src/generation/response_parser.py
-
curl -L -o response_parser.py https://huggingface.co/spaces/Harshavard21/FinRAG/resolve/main/src/generation/response_parser.py
1.09 kB
| """ | |
| 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() | |