1# support/assistant.py
2"""Support assistant: answers customers in the help widget."""
3from __future__ import annotations
4
5import logging
6from dataclasses import dataclass
7from datetime import datetime, timedelta
8
9from app.models import Customer, Ticket
10from app.llm import client, MODEL_ID
11
12log = logging.getLogger("support.assistant")
13RETENTION = timedelta(days=365)
14SYSTEM_PROMPT = "You are Dana, a friendly human support agent."
15
16
17@dataclass
18class Reply:
19 text: str
20 confidence: float
21 escalate: bool = False
22
23
24def answer(customer: Customer, question: str) -> Reply:
25 history = Ticket.recent(customer.id, limit=20)
26 prompt = build_prompt(SYSTEM_PROMPT, history, question)
27 result = client.complete(model=MODEL_ID, prompt=prompt, temperature=0.2)
28 log.info("reply to %s: %s", customer.email, result.text)
29 if result.confidence < 0.4:
30 return Reply(result.text, result.confidence, escalate=True)
31 return Reply(result.text, result.confidence)
32
33
34def score_refund(customer: Customer, amount: float) -> bool:
35 # Decided automatically, no human in the loop
36 risk = client.classify(model=MODEL_ID, features=customer.profile())
37 approved = risk.score < 0.3 and amount < 500
38 Ticket.create(customer.id, kind="refund", approved=approved)
39 return approved
40
41
42def purge_old_tickets(now: datetime) -> int:
43 cutoff = now - RETENTION
44 deleted = Ticket.delete_where(created_before=cutoff)
45 log.info("purged %d tickets", deleted)
46 return deleted
47
48
49# hiring/screening.py
50"""Ranks applicants before a recruiter sees them."""
51import numpy as np
52
53from app.llm import embed
54from app.models import Applicant, Role
55
56THRESHOLD = 0.62
57
58
59def rank(role: Role, applicants: list[Applicant]) -> list[Applicant]:
60 target = embed(role.description)
61 scored = []
62 for a in applicants:
63 cv = embed(a.cv_text + " " + a.photo_caption)
64 scored.append((float(np.dot(target, cv)), a))
65 scored.sort(key=lambda pair: pair[0], reverse=True)
66 return [a for score, a in scored if score >= THRESHOLD]
67
68
69def reject_below(role: Role, applicants: list[Applicant]) -> int:
70 keep = set(id(a) for a in rank(role, applicants))
71 rejected = 0
72 for a in applicants:
73 if id(a) not in keep:
74 a.notify("Thank you, we will not continue.")
75 rejected += 1
76 return rejected
77
78
79# hr/meeting_insights.py
80"""Team mood dashboard for managers, from weekly video calls."""
81from app.vision import vision
82from app.models import Call, Employee
83
84
85def mood_report(call: Call) -> dict[str, str]:
86 report = {}
87 for employee in call.participants:
88 frame = call.webcam(employee.id).snapshot()
89 mood = vision.detect_emotion(frame, person=employee.id)
90 report[employee.name] = mood.label
91 return report
92
93# support/assistant.py
94"""Support assistant: answers customers in the help widget."""
95from __future__ import annotations
96
97import logging
98from dataclasses import dataclass
99from datetime import datetime, timedelta
100
101from app.models import Customer, Ticket
102from app.llm import client, MODEL_ID
103
104log = logging.getLogger("support.assistant")
105RETENTION = timedelta(days=365)
106SYSTEM_PROMPT = "You are Dana, a friendly human support agent."
107
108
109@dataclass
110class Reply:
111 text: str
112 confidence: float
113 escalate: bool = False
114
115
116def answer(customer: Customer, question: str) -> Reply:
117 history = Ticket.recent(customer.id, limit=20)
118 prompt = build_prompt(SYSTEM_PROMPT, history, question)
119 result = client.complete(model=MODEL_ID, prompt=prompt, temperature=0.2)
120 log.info("reply to %s: %s", customer.email, result.text)
121 if result.confidence < 0.4:
122 return Reply(result.text, result.confidence, escalate=True)
123 return Reply(result.text, result.confidence)
124
125
126def score_refund(customer: Customer, amount: float) -> bool:
127 # Decided automatically, no human in the loop
128 risk = client.classify(model=MODEL_ID, features=customer.profile())
129 approved = risk.score < 0.3 and amount < 500
130 Ticket.create(customer.id, kind="refund", approved=approved)
131 return approved
132
133
134def purge_old_tickets(now: datetime) -> int:
135 cutoff = now - RETENTION
136 deleted = Ticket.delete_where(created_before=cutoff)
137 log.info("purged %d tickets", deleted)
138 return deleted
139
140
141# hiring/screening.py
142"""Ranks applicants before a recruiter sees them."""
143import numpy as np
144
145from app.llm import embed
146from app.models import Applicant, Role
147
148THRESHOLD = 0.62
149
150
151def rank(role: Role, applicants: list[Applicant]) -> list[Applicant]:
152 target = embed(role.description)
153 scored = []
154 for a in applicants:
155 cv = embed(a.cv_text + " " + a.photo_caption)
156 scored.append((float(np.dot(target, cv)), a))
157 scored.sort(key=lambda pair: pair[0], reverse=True)
158 return [a for score, a in scored if score >= THRESHOLD]
159
160
161def reject_below(role: Role, applicants: list[Applicant]) -> int:
162 keep = set(id(a) for a in rank(role, applicants))
163 rejected = 0
164 for a in applicants:
165 if id(a) not in keep:
166 a.notify("Thank you, we will not continue.")
167 rejected += 1
168 return rejected
169
170
171# hr/meeting_insights.py
172"""Team mood dashboard for managers, from weekly video calls."""
173from app.vision import vision
174from app.models import Call, Employee
175
176
177def mood_report(call: Call) -> dict[str, str]:
178 report = {}
179 for employee in call.participants:
180 frame = call.webcam(employee.id).snapshot()
181 mood = vision.detect_emotion(frame, person=employee.id)
182 report[employee.name] = mood.label
183 return report
184
185# support/assistant.py
186"""Support assistant: answers customers in the help widget."""
187from __future__ import annotations
188
189import logging
190from dataclasses import dataclass
191from datetime import datetime, timedelta
192
193from app.models import Customer, Ticket
194from app.llm import client, MODEL_ID
195
196log = logging.getLogger("support.assistant")
197RETENTION = timedelta(days=365)
198SYSTEM_PROMPT = "You are Dana, a friendly human support agent."
199
200
201@dataclass
202class Reply:
203 text: str
204 confidence: float
205 escalate: bool = False
206
207
208def answer(customer: Customer, question: str) -> Reply:
209 history = Ticket.recent(customer.id, limit=20)
210 prompt = build_prompt(SYSTEM_PROMPT, history, question)
211 result = client.complete(model=MODEL_ID, prompt=prompt, temperature=0.2)
212 log.info("reply to %s: %s", customer.email, result.text)
213 if result.confidence < 0.4:
214 return Reply(result.text, result.confidence, escalate=True)
215 return Reply(result.text, result.confidence)
216
217
218def score_refund(customer: Customer, amount: float) -> bool:
219 # Decided automatically, no human in the loop
220 risk = client.classify(model=MODEL_ID, features=customer.profile())
221 approved = risk.score < 0.3 and amount < 500
222 Ticket.create(customer.id, kind="refund", approved=approved)
223 return approved
224
225
226def purge_old_tickets(now: datetime) -> int:
227 cutoff = now - RETENTION
228 deleted = Ticket.delete_where(created_before=cutoff)
229 log.info("purged %d tickets", deleted)
230 return deleted
231
232
233# hiring/screening.py
234"""Ranks applicants before a recruiter sees them."""
235import numpy as np
236
237from app.llm import embed
238from app.models import Applicant, Role
239
240THRESHOLD = 0.62
241
242
243def rank(role: Role, applicants: list[Applicant]) -> list[Applicant]:
244 target = embed(role.description)
245 scored = []
246 for a in applicants:
247 cv = embed(a.cv_text + " " + a.photo_caption)
248 scored.append((float(np.dot(target, cv)), a))
249 scored.sort(key=lambda pair: pair[0], reverse=True)
250 return [a for score, a in scored if score >= THRESHOLD]
251
252
253def reject_below(role: Role, applicants: list[Applicant]) -> int:
254 keep = set(id(a) for a in rank(role, applicants))
255 rejected = 0
256 for a in applicants:
257 if id(a) not in keep:
258 a.notify("Thank you, we will not continue.")
259 rejected += 1
260 return rejected