Spaces:
Sleeping
Sleeping
Update processor_llm.py
Browse files- processor_llm.py +6 -15
processor_llm.py
CHANGED
|
@@ -16,7 +16,6 @@ LLM_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
|
|
| 16 |
|
| 17 |
VALID_CATEGORIES = ["Workflow Error", "Deprecation Warning"]
|
| 18 |
|
| 19 |
-
# Retry / timeout config
|
| 20 |
MAX_RETRIES = 2
|
| 21 |
RETRY_DELAY_SEC = 1.0
|
| 22 |
REQUEST_TIMEOUT = 5
|
|
@@ -42,7 +41,6 @@ FEW_SHOT_EXAMPLES = [
|
|
| 42 |
},
|
| 43 |
]
|
| 44 |
|
| 45 |
-
# ββ Prompt builder βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 46 |
def _build_messages(log_msg: str) -> list[dict]:
|
| 47 |
categories_str = ", ".join(f'"{c}"' for c in VALID_CATEGORIES)
|
| 48 |
user_content = (
|
|
@@ -58,7 +56,6 @@ def _build_messages(log_msg: str) -> list[dict]:
|
|
| 58 |
{"role": "user", "content": user_content},
|
| 59 |
]
|
| 60 |
|
| 61 |
-
# ββ Normalize raw LLM output βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 62 |
def _normalize(raw: str) -> str:
|
| 63 |
raw = raw.strip().strip('"').strip("'")
|
| 64 |
for cat in VALID_CATEGORIES:
|
|
@@ -66,14 +63,7 @@ def _normalize(raw: str) -> str:
|
|
| 66 |
return cat
|
| 67 |
return "Unclassified"
|
| 68 |
|
| 69 |
-
# ββ Main classify function ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 70 |
def classify_with_llm(log_msg: str) -> str:
|
| 71 |
-
"""
|
| 72 |
-
Tier 3 LLM classifier with:
|
| 73 |
-
- Timeout (REQUEST_TIMEOUT seconds)
|
| 74 |
-
- Retry with exponential backoff (MAX_RETRIES attempts)
|
| 75 |
-
- Explicit fallback to "Unclassified" on all error paths
|
| 76 |
-
"""
|
| 77 |
if not HF_TOKEN:
|
| 78 |
logger.warning("[LLM] HF_TOKEN not set β returning Unclassified")
|
| 79 |
return "Unclassified"
|
|
@@ -98,13 +88,14 @@ def classify_with_llm(log_msg: str) -> str:
|
|
| 98 |
return label
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
-
#
|
| 102 |
-
if
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
if attempt <= MAX_RETRIES:
|
| 107 |
-
logger.warning(f"[LLM] Attempt {attempt} failed ({e}), retrying in {delay:.1f}sβ¦")
|
| 108 |
time.sleep(delay)
|
| 109 |
delay *= 2
|
| 110 |
else:
|
|
|
|
| 16 |
|
| 17 |
VALID_CATEGORIES = ["Workflow Error", "Deprecation Warning"]
|
| 18 |
|
|
|
|
| 19 |
MAX_RETRIES = 2
|
| 20 |
RETRY_DELAY_SEC = 1.0
|
| 21 |
REQUEST_TIMEOUT = 5
|
|
|
|
| 41 |
},
|
| 42 |
]
|
| 43 |
|
|
|
|
| 44 |
def _build_messages(log_msg: str) -> list[dict]:
|
| 45 |
categories_str = ", ".join(f'"{c}"' for c in VALID_CATEGORIES)
|
| 46 |
user_content = (
|
|
|
|
| 56 |
{"role": "user", "content": user_content},
|
| 57 |
]
|
| 58 |
|
|
|
|
| 59 |
def _normalize(raw: str) -> str:
|
| 60 |
raw = raw.strip().strip('"').strip("'")
|
| 61 |
for cat in VALID_CATEGORIES:
|
|
|
|
| 63 |
return cat
|
| 64 |
return "Unclassified"
|
| 65 |
|
|
|
|
| 66 |
def classify_with_llm(log_msg: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if not HF_TOKEN:
|
| 68 |
logger.warning("[LLM] HF_TOKEN not set β returning Unclassified")
|
| 69 |
return "Unclassified"
|
|
|
|
| 88 |
return label
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
+
# FIX: Safely check for HTTP 402 object attributes instead of raw string matching
|
| 92 |
+
if hasattr(e, 'response') and e.response is not None:
|
| 93 |
+
if getattr(e.response, 'status_code', None) == 402:
|
| 94 |
+
logger.error(f"[LLM] Credits Finished (402). Returning Unclassified.")
|
| 95 |
+
return "Unclassified"
|
| 96 |
|
| 97 |
if attempt <= MAX_RETRIES:
|
| 98 |
+
logger.warning(f"[LLM] Attempt {attempt} failed ({type(e).__name__}), retrying in {delay:.1f}sβ¦")
|
| 99 |
time.sleep(delay)
|
| 100 |
delay *= 2
|
| 101 |
else:
|