Jul 20 - Update codes to comply with some framework (SOC 2 TYPE 2, ISO, etc)
This commit is contained in:
+32
-3
@@ -123,6 +123,33 @@ FAQS = [
|
||||
_KB_MAX_CHARS = 6000
|
||||
|
||||
|
||||
# ── PII redaction for the outbound Groq payload ───────────────────────────────
|
||||
# Groq is a third-party processor. Customers may type identifying details
|
||||
# (their own email/phone, or a coworker's) into a support question; there is
|
||||
# no need for that to leave the app to get a helpful, generic answer. This
|
||||
# scrubs a best-effort set of PII patterns from the copy of the text sent to
|
||||
# Groq only — the original text is still saved as-is in support_chat_messages
|
||||
# so the customer's own conversation history reads normally in the app.
|
||||
import re as _re
|
||||
|
||||
_PII_PATTERNS = [
|
||||
(_re.compile(r'[\w.+-]+@[\w-]+\.[\w.-]+'), '[redacted-email]'),
|
||||
(_re.compile(r'\b\d{3}-\d{2}-\d{4}\b'), '[redacted-ssn]'),
|
||||
(_re.compile(r'\b(?:\d[ -]?){13,19}\b'), '[redacted-number]'),
|
||||
(_re.compile(r'\b(?:\+?1[ .-]?)?\(?\d{3}\)?[ .-]?\d{3}[ .-]?\d{4}\b'), '[redacted-phone]'),
|
||||
]
|
||||
|
||||
|
||||
def _redact_pii(text):
|
||||
"""Best-effort scrub of email/phone/SSN/card-like sequences from outbound text."""
|
||||
if not text:
|
||||
return text
|
||||
redacted = text
|
||||
for pattern, placeholder in _PII_PATTERNS:
|
||||
redacted = pattern.sub(placeholder, redacted)
|
||||
return redacted
|
||||
|
||||
|
||||
def _system_prompt_with_kb():
|
||||
"""Return the base system prompt plus all ACTIVE admin knowledge entries
|
||||
(phase38), so staff can curate the chatbot's knowledge without code changes.
|
||||
@@ -214,11 +241,13 @@ def chat_message():
|
||||
client = Groq(api_key=api_key)
|
||||
|
||||
messages = [{'role': 'system', 'content': _system_prompt_with_kb()}]
|
||||
# Append prior conversation (cap at last 20 turns to control token usage)
|
||||
# Append prior conversation (cap at last 20 turns to control token usage).
|
||||
# Redact PII-shaped text before it leaves the app for the Groq API —
|
||||
# the unredacted originals stay in support_chat_messages below.
|
||||
for m in history[-20:]:
|
||||
if m.get('role') in ('user', 'assistant') and m.get('content'):
|
||||
messages.append({'role': m['role'], 'content': m['content']})
|
||||
messages.append({'role': 'user', 'content': user_message})
|
||||
messages.append({'role': m['role'], 'content': _redact_pii(m['content'])})
|
||||
messages.append({'role': 'user', 'content': _redact_pii(user_message)})
|
||||
|
||||
model = os.environ.get('GROQ_MODEL', 'llama-3.3-70b-versatile')
|
||||
completion = client.chat.completions.create(
|
||||
|
||||
Reference in New Issue
Block a user