Spaces:
Running
Running
| """思考链抽取:从 <thought>...</thought> 标签或 reasoning_content 字段提取。""" | |
| from __future__ import annotations | |
| import re | |
| from typing import Optional | |
| _THOUGHT_TAG_RE = re.compile( | |
| r"<(?:thought|think)>\s*([\s\S]*?)\s*</(?:thought|think)>", | |
| re.IGNORECASE, | |
| ) | |
| _THOUGHT_TAG_FULL_RE = re.compile( | |
| r"<(?:thought|think)>[\s\S]*?</(?:thought|think)>\s*", | |
| re.IGNORECASE, | |
| ) | |
| def extract_thought_and_answer(raw: str) -> tuple[str, str]: | |
| """从原始输出中抽出 thought 与 answer。 | |
| Returns: | |
| (thought, answer) | |
| """ | |
| if not raw: | |
| return "", "" | |
| thoughts = [] | |
| for m in _THOUGHT_TAG_RE.finditer(raw): | |
| if m.group(1): | |
| thoughts.append(m.group(1).strip()) | |
| answer = _THOUGHT_TAG_FULL_RE.sub("", raw).strip() | |
| return "\n\n".join(thoughts), answer | |
| def extract_thought_from_reasoning_content(reasoning_content: Optional[str]) -> str: | |
| if not reasoning_content: | |
| return "" | |
| return reasoning_content.strip() | |
| def wrap_thought(thought: str) -> str: | |
| """把 thought 包裹成 <thought>...</thought> 嵌入 content。""" | |
| if not thought: | |
| return "" | |
| return f"<thought>\n{thought}\n</thought>\n\n" | |