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),
    }