NOT-OMEGA commited on
Commit
a1af93d
Β·
verified Β·
1 Parent(s): 40c02b8

Update processor_llm.py

Browse files
Files changed (1) hide show
  1. processor_llm.py +1 -50
processor_llm.py CHANGED
@@ -1,22 +1,18 @@
1
  """
2
  processor_llm.py β€” Tier 3: LLM-based Classifier
3
-
4
  Used for:
5
  - LegacyCRM logs (Workflow Error, Deprecation Warning)
6
  - BERT fallback when confidence < threshold
7
-
8
  Production hardening in V3:
9
  - Timeout (configurable, default 5s)
10
  - Retry with exponential backoff (max 2 retries)
11
  - Explicit failure modes: returns "Unclassified" on all error paths
12
- - Caching for repeated log patterns (hash-based, in-memory)
13
  - Token budget enforcement (max_tokens=15)
14
  """
15
  from __future__ import annotations
16
  import os
17
  import re
18
  import time
19
- import hashlib
20
  import logging
21
 
22
  from typing import Optional
@@ -34,10 +30,6 @@ MAX_RETRIES = 2
34
  RETRY_DELAY_SEC = 1.0 # doubles on each retry (exponential backoff)
35
  REQUEST_TIMEOUT = 5 # seconds β€” fail fast, do not hang pipeline
36
 
37
- # In-memory cache to avoid redundant LLM calls for repeated logs
38
- _RESPONSE_CACHE: dict[str, str] = {}
39
- MAX_CACHE_SIZE = 1000 # evict oldest when full (simple FIFO)
40
-
41
  SYSTEM_PROMPT = (
42
  "You are an enterprise log classifier. "
43
  "Classify log messages into exactly one category. "
@@ -59,29 +51,6 @@ FEW_SHOT_EXAMPLES = [
59
  },
60
  ]
61
 
62
-
63
- # ── Cache helpers ────────────────────────────────────────────────────────────
64
- def _cache_key(log_msg: str) -> str:
65
- return hashlib.md5(log_msg.strip().encode()).hexdigest()
66
-
67
-
68
- def _cache_get(log_msg: str) -> Optional[str]:
69
- return _RESPONSE_CACHE.get(_cache_key(log_msg))
70
-
71
-
72
- def _cache_set(log_msg: str, label: str) -> None:
73
- key = _cache_key(log_msg)
74
- if len(_RESPONSE_CACHE) >= MAX_CACHE_SIZE:
75
- # Evict oldest (first inserted) key
76
- oldest = next(iter(_RESPONSE_CACHE))
77
- del _RESPONSE_CACHE[oldest]
78
- _RESPONSE_CACHE[key] = label
79
-
80
-
81
- def get_cache_stats() -> dict:
82
- return {"size": len(_RESPONSE_CACHE), "max_size": MAX_CACHE_SIZE}
83
-
84
-
85
  # ── Prompt builder ───────────────────────────────────────────────────────────
86
  def _build_messages(log_msg: str) -> list[dict]:
87
  categories_str = ", ".join(f'"{c}"' for c in VALID_CATEGORIES)
@@ -113,19 +82,10 @@ def _normalize(raw: str) -> str:
113
  def classify_with_llm(log_msg: str) -> str:
114
  """
115
  Tier 3 LLM classifier with:
116
- - In-memory cache (avoids duplicate API calls)
117
  - Timeout (REQUEST_TIMEOUT seconds)
118
  - Retry with exponential backoff (MAX_RETRIES attempts)
119
  - Explicit fallback to "Unclassified" on all error paths
120
-
121
- Latency: 500–2000ms on cache miss; ~0ms on cache hit.
122
  """
123
- # ── Cache hit ────────────────────────────────────────────────────────────
124
- cached = _cache_get(log_msg)
125
- if cached is not None:
126
- logger.debug(f"[LLM] Cache hit for: {log_msg[:60]}")
127
- return cached
128
-
129
  # ── Inference with retry ─────────────────────────────────────────────────
130
  if not HF_TOKEN:
131
  logger.warning("[LLM] HF_TOKEN not set β€” returning Unclassified")
@@ -148,7 +108,6 @@ def classify_with_llm(log_msg: str) -> str:
148
  raw = response.choices[0].message.content
149
  label = _normalize(raw)
150
 
151
- _cache_set(log_msg, label)
152
  logger.debug(f"[LLM] Attempt {attempt}: '{raw.strip()}' β†’ '{label}'")
153
  return label
154
 
@@ -187,12 +146,4 @@ if __name__ == "__main__":
187
  ]
188
  for log in test_logs:
189
  result = classify_with_llm(log)
190
- print(f"{result:25s} | {log[:80]}")
191
-
192
- # Cache hit test
193
- print("\n── Cache hit test ──")
194
- t0 = time.perf_counter()
195
- classify_with_llm(test_logs[0])
196
- t1 = time.perf_counter()
197
- print(f"Cache hit latency: {(t1-t0)*1000:.2f}ms")
198
- print(f"Cache stats: {get_cache_stats()}")
 
1
  """
2
  processor_llm.py β€” Tier 3: LLM-based Classifier
 
3
  Used for:
4
  - LegacyCRM logs (Workflow Error, Deprecation Warning)
5
  - BERT fallback when confidence < threshold
 
6
  Production hardening in V3:
7
  - Timeout (configurable, default 5s)
8
  - Retry with exponential backoff (max 2 retries)
9
  - Explicit failure modes: returns "Unclassified" on all error paths
 
10
  - Token budget enforcement (max_tokens=15)
11
  """
12
  from __future__ import annotations
13
  import os
14
  import re
15
  import time
 
16
  import logging
17
 
18
  from typing import Optional
 
30
  RETRY_DELAY_SEC = 1.0 # doubles on each retry (exponential backoff)
31
  REQUEST_TIMEOUT = 5 # seconds β€” fail fast, do not hang pipeline
32
 
 
 
 
 
33
  SYSTEM_PROMPT = (
34
  "You are an enterprise log classifier. "
35
  "Classify log messages into exactly one category. "
 
51
  },
52
  ]
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # ── Prompt builder ───────────────────────────────────────────────────────────
55
  def _build_messages(log_msg: str) -> list[dict]:
56
  categories_str = ", ".join(f'"{c}"' for c in VALID_CATEGORIES)
 
82
  def classify_with_llm(log_msg: str) -> str:
83
  """
84
  Tier 3 LLM classifier with:
 
85
  - Timeout (REQUEST_TIMEOUT seconds)
86
  - Retry with exponential backoff (MAX_RETRIES attempts)
87
  - Explicit fallback to "Unclassified" on all error paths
 
 
88
  """
 
 
 
 
 
 
89
  # ── Inference with retry ─────────────────────────────────────────────────
90
  if not HF_TOKEN:
91
  logger.warning("[LLM] HF_TOKEN not set β€” returning Unclassified")
 
108
  raw = response.choices[0].message.content
109
  label = _normalize(raw)
110
 
 
111
  logger.debug(f"[LLM] Attempt {attempt}: '{raw.strip()}' β†’ '{label}'")
112
  return label
113
 
 
146
  ]
147
  for log in test_logs:
148
  result = classify_with_llm(log)
149
+ print(f"{result:25s} | {log[:80]}")