File size: 2,393 Bytes
afd56bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
import re


def safe_extract_text(content) -> str:
    """Bezpiecznie wyci膮ga tekst z odpowiedzi LLM (obs艂uguje zar贸wno str jak i list z Gemini)."""
    if isinstance(content, str):
        return content.strip()

    if isinstance(content, list):
        texts = []
        for item in content:
            if isinstance(item, dict) and "text" in item:
                texts.append(str(item["text"]))
            elif isinstance(item, str):
                texts.append(item)
        return " ".join(texts).strip()

    # Fallback dla innych typ贸w
    return str(content).strip()


def extract_markdown_and_sanitize(raw_content: str, min_length: int = 50) -> str:
    """
    Ekstrahuje blok Markdown i przeprowadza sanity checks (d艂ugo艣膰, typowe odmowy LLM).
    Zg艂asza ValueError, co pozwala mechanizmowi Watchdog na ponowienie pr贸by.
    """
    if "[NO_CHANGE]" in raw_content:
        return "[NO_CHANGE]"

    # Ekstrakcja bloku Markdown (je艣li obecny)
    md_match = re.search(
        r"```(?:markdown|md|html)?\s*\n?(.*?)```",
        raw_content,
        re.DOTALL | re.IGNORECASE,
    )
    if md_match:
        extracted = md_match.group(1).strip()
    else:
        # Awaryjne usuwanie backtick贸w
        extracted = raw_content.strip()
        if extracted.startswith("```"):
            first_newline = extracted.find("\n")
            if first_newline != -1 and first_newline < 20:
                extracted = extracted[first_newline + 1 :]
            else:
                extracted = extracted[3:]
        if extracted.endswith("```"):
            extracted = extracted[:-3]
        extracted = extracted.strip()

    # Sanity checks
    if len(extracted) < min_length:
        raise ValueError(
            f"Wygenerowany tekst jest zbyt kr贸tki ({len(extracted)} znak贸w). Prawdopodobnie b艂膮d generowania."
        )

    refusals = [
        "nie mog臋",
        "przykro mi",
        "as an ai",
        "jako model j臋zykowy",
        "i cannot",
        "nie jestem w stanie",
        "przepraszam",
        "nie potrafi臋",
    ]
    extracted_lower = extracted.lower()

    # Je艣li odpowied藕 jest bardzo kr贸tka (np. < 500 znak贸w) i zawiera fraz臋 odmowy
    if len(extracted) < 500 and any(r in extracted_lower for r in refusals):
        raise ValueError("LLM zwr贸ci艂 odmow臋 lub halucynacj臋 zamiast poprawnej tre艣ci.")

    return extracted