-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor_llm.py
More file actions
63 lines (51 loc) · 2.09 KB
/
processor_llm.py
File metadata and controls
63 lines (51 loc) · 2.09 KB
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
import os
import json
import re
from dotenv import load_dotenv
from groq import Groq, GroqError
load_dotenv(override=True)
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
groq = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
def classify_with_llm(log_msg):
"""
Generate a variant of the input sentence. For example,
If input sentence is "User session timed out unexpectedly, user ID: 9250.",
variant would be "Session timed out for user 9251"
"""
if not groq:
return "Unclassified", 0.0
prompt = f'''Classify the log message into one of these categories:
(1) Workflow Error, (2) Deprecation Warning.
If you can't figure out a category, use "Unclassified".
Put the category inside <category> </category> tags.
Log message: {log_msg}'''
try:
chat_completion = groq.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="deepseek-r1-distill-llama-70b",
temperature=0.5
)
except GroqError:
return "Unclassified", 0.0
content = chat_completion.choices[0].message.content
match = re.search(r'<category>(.*)<\/category>', content, flags=re.DOTALL)
category = "Unclassified"
confidence = 0.5
if match:
category = match.group(1).strip()
if category != "Unclassified":
confidence = 0.85
else:
confidence = 0.5
else:
confidence = 0.5
return category, confidence
if __name__ == "__main__":
label, conf = classify_with_llm(
"Case escalation for ticket ID 7324 failed because the assigned support agent is no longer active.")
print(f"Case escalation... -> {label} (confidence: {conf:.2f})")
label, conf = classify_with_llm(
"The 'ReportGenerator' module will be retired in version 4.0. Please migrate to the 'AdvancedAnalyticsSuite' by Dec 2025")
print(f"ReportGenerator... -> {label} (confidence: {conf:.2f})")
label, conf = classify_with_llm("System reboot initiated by user 12345.")
print(f"System reboot... -> {label} (confidence: {conf:.2f})")