-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintent_detector.py
More file actions
62 lines (48 loc) · 1.76 KB
/
intent_detector.py
File metadata and controls
62 lines (48 loc) · 1.76 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
"""
Intent detection using MiniLM embeddings.
Classifies employee reports into predefined HR categories.
"""
import ollama
import numpy as np
# Intent categories with descriptions
INTENTS = {
"harassment": "Harassment, intimidation, or misconduct at work",
"burnout": "Employee burnout, stress, or mental health risk",
"policy_violation": "Internal policy or ethics violation",
"performance": "Performance or role-related concern",
"noise": "Irrelevant or non-HR related input"
}
class IntentDetector:
"""Detects intent from employee reports using embedding similarity."""
def __init__(self):
"""
Initialize the intent detector.
Args:
"""
self.intents = INTENTS
self.intent_keys = list(INTENTS.keys())
self.intent_texts = list(INTENTS.values())
print(f"[INIT] Loading HR intent embeddings..", end="")
self.intent_embeddings = [
ollama.embeddings(model="all-minilm", prompt=text)["embedding"]
for text in self.intent_texts
]
print("..done")
@staticmethod
def cosine_sim(a, b):
"""Calculate cosine similarity between two vectors."""
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
def detect(self, text: str) -> str:
"""
Detect the intent of an employee report.
Args:
text: The employee report text
Returns:
The detected intent category
"""
emb = ollama.embeddings(
model="all-minilm",
prompt=text
)["embedding"]
scores = [self.cosine_sim(emb, e) for e in self.intent_embeddings]
return self.intent_keys[int(np.argmax(scores))]