kaidol-thinking-experiment / utils /thinking_parser.py
developer-lunark's picture
Upload folder using huggingface_hub
7b7257a verified
"""<think> ํƒœ๊ทธ ํŒŒ์‹ฑ ์œ ํ‹ธ๋ฆฌํ‹ฐ"""
import re
from typing import Tuple, Optional
def parse_thinking_response(response: str) -> Tuple[Optional[str], str]:
"""
์‘๋‹ต์—์„œ <think> ํƒœ๊ทธ ์ถ”์ถœ
Returns:
(thinking_content, clean_response)
"""
if not response:
return None, ""
# <think>...</think> ํŒจํ„ด ๋งค์นญ
pattern = r'<think>(.*?)</think>'
match = re.search(pattern, response, re.DOTALL)
if match:
thinking = match.group(1).strip()
# <think> ํƒœ๊ทธ ์ œ๊ฑฐํ•œ ํด๋ฆฐ ์‘๋‹ต
clean = re.sub(pattern, '', response, flags=re.DOTALL).strip()
return thinking, clean
return None, response
def format_thinking_for_display(thinking: str) -> str:
"""Thinking ๋‚ด์šฉ์„ ๋งˆํฌ๋‹ค์šด์œผ๋กœ ํฌ๋งทํŒ…"""
if not thinking:
return "*No thinking process*"
# 6๋‹จ๊ณ„ ๊ตฌ์กฐ ํ•˜์ด๋ผ์ดํŠธ (์žˆ๋Š” ๊ฒฝ์šฐ)
sections = [
"[์ƒํ™ฉ๋ถ„์„]", "[๊ด€๊ณ„๋‹จ๊ณ„]", "[์บ๋ฆญํ„ฐ์Šคํƒ€์ผ]",
"[๋ฐ€๋‹น๊ฒฐ์ •]", "[๊ธˆ์ง€ํŒจํ„ด์ฒดํฌ]", "[์‘๋‹ต์„ค๊ณ„]"
]
formatted = thinking
for section in sections:
formatted = formatted.replace(
section,
f"**{section}**"
)
return formatted
def extract_response_only(full_response: str) -> str:
"""Thinking ์ œ๊ฑฐํ•˜๊ณ  ์‘๋‹ต๋งŒ ์ถ”์ถœ"""
_, clean = parse_thinking_response(full_response)
return clean
def has_thinking_tag(response: str) -> bool:
"""์‘๋‹ต์— <think> ํƒœ๊ทธ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ"""
pattern = r'<think>.*?</think>'
return bool(re.search(pattern, response, re.DOTALL))
def get_thinking_stats(response: str) -> dict:
"""Thinking ๊ด€๋ จ ํ†ต๊ณ„"""
thinking, clean = parse_thinking_response(response)
return {
"has_thinking": thinking is not None,
"thinking_length": len(thinking) if thinking else 0,
"response_length": len(clean),
"total_length": len(response),
}