Spaces:
Running
on
T4
Running
on
T4
File size: 1,961 Bytes
7b7257a |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
"""<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),
}
|