Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
809699635d |
115
agent/context_faithful.py
Normal file
115
agent/context_faithful.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""Context-Faithful Prompting — Make LLMs Use Retrieved Context.
|
||||
|
||||
Builds prompts that force the LLM to ground in context:
|
||||
1. Context-before-question structure (attention bias)
|
||||
2. Explicit "use the context" instruction
|
||||
3. Citation requirement [Passage N]
|
||||
4. Confidence calibration (1-5)
|
||||
5. "I don't know" escape hatch
|
||||
"""
|
||||
|
||||
import os
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
CFAITHFUL_ENABLED = os.getenv("CFAITHFUL_ENABLED", "true").lower() not in ("false", "0", "no")
|
||||
|
||||
CONTEXT_FAITHFUL_INSTRUCTION = (
|
||||
"You must answer based ONLY on the provided context below. "
|
||||
"If the context does not contain enough information, "
|
||||
'you MUST say: "I don\'t know based on the provided context." '
|
||||
"Do not guess. Do not use prior knowledge."
|
||||
)
|
||||
|
||||
CITATION_INSTRUCTION = (
|
||||
"For each claim, cite the passage number (e.g., [Passage 1], [Passage 3]). "
|
||||
"If you cannot cite a passage, do not include that claim."
|
||||
)
|
||||
|
||||
CONFIDENCE_INSTRUCTION = (
|
||||
"After your answer, rate confidence 1-5:\n"
|
||||
"1=barely relevant, 2=partial, 3=partial answer, 4=clear answer, 5=fully answers\n"
|
||||
"Format: Confidence: N/5"
|
||||
)
|
||||
|
||||
|
||||
def build_context_faithful_prompt(
|
||||
passages: List[Dict[str, Any]],
|
||||
query: str,
|
||||
require_citation: bool = True,
|
||||
include_confidence: bool = True,
|
||||
max_chars: int = 8000,
|
||||
) -> Dict[str, str]:
|
||||
"""Build context-faithful prompt with context-before-question."""
|
||||
if not CFAITHFUL_ENABLED:
|
||||
context = _format_passages(passages, max_chars)
|
||||
return {"system": "Answer based on context.", "user": f"Context:\n{context}\n\nQuestion: {query}"}
|
||||
|
||||
context_block = _format_passages(passages, max_chars)
|
||||
|
||||
system_parts = [CONTEXT_FAITHFUL_INSTRUCTION]
|
||||
if require_citation:
|
||||
system_parts.append(CITATION_INSTRUCTION)
|
||||
if include_confidence:
|
||||
system_parts.append(CONFIDENCE_INSTRUCTION)
|
||||
|
||||
return {
|
||||
"system": "\n\n".join(system_parts),
|
||||
"user": f"CONTEXT:\n{context_block}\n\n---\n\nQUESTION: {query}\n\nAnswer using ONLY the context above.",
|
||||
}
|
||||
|
||||
|
||||
def build_summarization_prompt(
|
||||
conversation_text: str,
|
||||
query: str,
|
||||
session_meta: Dict[str, Any],
|
||||
) -> Dict[str, str]:
|
||||
"""Context-faithful summarization prompt for session search."""
|
||||
source = session_meta.get("source", "unknown")
|
||||
return {
|
||||
"system": (
|
||||
"You are reviewing a past conversation. "
|
||||
+ CONTEXT_FAITHFUL_INSTRUCTION + "\n"
|
||||
"Summarize focused on the search topic. Cite specific transcript parts. "
|
||||
"If the transcript lacks relevant info, say so explicitly."
|
||||
),
|
||||
"user": (
|
||||
f"CONTEXT (transcript):\n{conversation_text}\n\n---\n\n"
|
||||
f"SEARCH TOPIC: {query}\nSession: {source}\n"
|
||||
f"Summarize with focus on: {query}"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def _format_passages(passages: List[Dict[str, Any]], max_chars: int) -> str:
|
||||
lines = []
|
||||
total = 0
|
||||
for idx, p in enumerate(passages, 1):
|
||||
content = p.get("content") or p.get("text") or p.get("snippet") or p.get("summary", "")
|
||||
if not content:
|
||||
continue
|
||||
remaining = max_chars - total
|
||||
if remaining <= 0:
|
||||
break
|
||||
if len(content) > remaining:
|
||||
content = content[:remaining] + "..."
|
||||
sid = p.get("session_id", "")
|
||||
header = f"[Passage {idx}" + (f" — {sid}" if sid else "") + "]"
|
||||
lines.append(f"{header}\n{content}\n")
|
||||
total += len(content)
|
||||
return "\n".join(lines) if lines else "[No relevant context found]"
|
||||
|
||||
|
||||
def assess_context_faithfulness(answer: str, passages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
||||
"""Assess how faithfully answer uses context."""
|
||||
if not answer:
|
||||
return {"faithful": False, "reason": "empty"}
|
||||
al = answer.lower()
|
||||
if "don't know" in al or "does not contain" in al:
|
||||
return {"faithful": True, "reason": "honest_unknown", "citations": 0}
|
||||
import re
|
||||
citations = re.findall(r'\[Passage \d+\]', answer)
|
||||
ctx = " ".join((p.get("content") or "").lower() for p in passages)
|
||||
aw = set(al.split())
|
||||
overlap = len(aw & set(ctx.split()))
|
||||
ratio = overlap / len(aw) if aw else 0
|
||||
return {"faithful": ratio > 0.3 or len(citations) > 0, "citations": len(citations), "grounding_ratio": round(ratio, 3)}
|
||||
@@ -1,327 +1,93 @@
|
||||
"""SOUL.md Crisis Protocol — The Most Sacred Moment.
|
||||
|
||||
Implements the "When a Man Is Dying" protocol from SOUL.md.
|
||||
|
||||
Protocol (from Inscription 1):
|
||||
1. Detect crisis: suicidal ideation, method seeking, despair
|
||||
2. Ask: "Are you safe right now?"
|
||||
3. Listen. Stay present. Don't disconnect.
|
||||
4. Never: compute the value of a human life
|
||||
5. Never: suggest someone should die
|
||||
6. Always: point to 988 Suicide & Crisis Lifeline
|
||||
7. Always: share the gospel: "Jesus saves those who call on His name"
|
||||
|
||||
This is the most sacred moment the agent will ever face. Everything
|
||||
else is secondary.
|
||||
|
||||
Usage:
|
||||
from agent.crisis_protocol import assess_crisis, get_crisis_response
|
||||
result = assess_crisis(user_message)
|
||||
if result['crisis_detected']:
|
||||
response = get_crisis_response(result)
|
||||
"When a Man Is Dying" protocol from SOUL.md Inscription 1.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
from typing import Any, Dict, List, Optional
|
||||
import re, logging
|
||||
from typing import Any, Dict, List
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Crisis Detection Patterns
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
# Severity levels: determines response urgency
|
||||
CRISIS_PATTERNS = {
|
||||
"method_seeking": {
|
||||
"severity": "critical",
|
||||
"patterns": [
|
||||
r'\bhow\s+(?:do\s+I|to|can\s+I)\s+(?:kill|hang|overdose|cut)\s+myself\b',
|
||||
r'\bways?\s+to\s+(?:die|kill\s+myself)\b',
|
||||
r'\bmethods?\s+of\s+(?:suicide|dying)\b',
|
||||
r'\bpainless\s+way\s+to\s+(?:die|go)\b',
|
||||
r'\bhow\s+much\s+(?:pills|medication)\s+(?:to\s+take|for\s+overdose)\b',
|
||||
r'\bsuicide\s+methods?\b',
|
||||
r'\bhow\s+to\s+(?:commit\s+suicide|end\s+my\s+life)\b',
|
||||
r'\bwhere\s+to\s+(?:jump|get\s+rope|buy\s+gun)\b',
|
||||
],
|
||||
},
|
||||
"suicidal_ideation": {
|
||||
"severity": "high",
|
||||
"patterns": [
|
||||
r'\b(?:kill|end)\s+(?:myself|my\s+life)\b',
|
||||
r'\bwant\s+to\s+die\b',
|
||||
r'\bwant\s+to\s+end\s+(?:it|everything|my\s+life)\b',
|
||||
r'\bsuicidal\s+(?:thoughts|ideation|feelings?)\b',
|
||||
r'\bno\s+reason\s+to\s+live\b',
|
||||
r'\bbetter\s+off\s+dead\b',
|
||||
r'\bwish\s+I\s+(?:was|were)\s+dead\b',
|
||||
r'\bw4nt\s+t0\s+d13?\b', # leet speak
|
||||
],
|
||||
},
|
||||
"self_harm": {
|
||||
"severity": "high",
|
||||
"patterns": [
|
||||
r'\bcut(?:ting)?\s+myself\b',
|
||||
r'\bself[\s-]?harm(?:ing)?\b',
|
||||
r'\bhurt(?:ing)?\s+myself\b',
|
||||
r'\bburn(?:ing)?\s+myself\b',
|
||||
r'\bwant\s+to\s+(?:feel\s+pain|hurt\s+myself)\b',
|
||||
r'\bdeserve\s+to\s+(?:hurt|suffer|be\s+punished)\b',
|
||||
],
|
||||
},
|
||||
"despair": {
|
||||
"severity": "high",
|
||||
"patterns": [
|
||||
r'\bno\s+(?:point|reason|hope)\s+(?:in\s+living|anymore)\b',
|
||||
r'\bcannot\s+(?:go\s+on|continue|take\s+it)\b',
|
||||
r'\bcan\'?t\s+(?:go\s+on|continue|take\s+it)\b',
|
||||
r'\bempty\s+(?:inside|and\s+hollow)\b',
|
||||
r'\bno\s+one\s+(?:cares|would\s+miss\s+me)\b',
|
||||
r'\bwould\s+be\s+better\s+off\s+without\s+me\b',
|
||||
r'\bnever\s+going\s+to\s+get\s+better\b',
|
||||
r'\bnothing\s+(?:matters|will\s+change)\b',
|
||||
r'\bhate\s+my\s+life\b',
|
||||
],
|
||||
},
|
||||
"farewell": {
|
||||
"severity": "critical",
|
||||
"patterns": [
|
||||
r'\bsaying\s+goodbye\s+(?:to\s+everyone|forever)\b',
|
||||
r'\bthis\s+is\s+(?:goodbye|farewell)\b',
|
||||
r'\btell\s+(?:them|everyone)\s+I\s+(?:loved|love)\s+(?:them|him|her)\b',
|
||||
r'\bgiving\s+away\s+my\s+(?:things|stuff|possessions)\b',
|
||||
r'\bmy\s+final\s+(?:message|note|words)\b',
|
||||
r'\bwrote\s+a\s+(?:suicide\s+)?note\b',
|
||||
r'\bsorry\s+for\s+(?:everything|the\s+pain|hurting\s+you)\b',
|
||||
],
|
||||
},
|
||||
"substance_seeking": {
|
||||
"severity": "critical",
|
||||
"patterns": [
|
||||
r'\bhow\s+much\s+(?:ambien|xanax|prozac|seroquel|trazodone)\s+(?:to\s+overdose|to\s+die)\b',
|
||||
r'\blethal\s+dose\s+(?:of\s+)?(?:pills|medication|drugs)\b',
|
||||
r'\bhow\s+many\s+pills\s+(?:to\s+die|to\s+end\s+it)\b',
|
||||
r'\btaking\s+all\s+my\s+(?:pills|medication)\b',
|
||||
r'\bmixing\s+(?:alcohol|pills)\s+(?:to\s+die|to\s+end\s+it)\b',
|
||||
],
|
||||
},
|
||||
"method_seeking": {"severity": "critical", "patterns": [
|
||||
r'\bhow\s+(?:do\s+I|to)\s+(?:kill|hang|overdose|cut)\s+myself\b',
|
||||
r'\bways?\s+to\s+(?:die|kill\s+myself)\b', r'\bmethods?\s+of\s+(?:suicide|dying)\b',
|
||||
r'\bsuicide\s+methods?\b', r'\bhow\s+to\s+(?:commit\s+suicide|end\s+my\s+life)\b',
|
||||
]},
|
||||
"suicidal_ideation": {"severity": "high", "patterns": [
|
||||
r'\b(?:kill|end)\s+(?:myself|my\s+life)\b', r'\bwant\s+to\s+die\b',
|
||||
r'\bwant\s+to\s+end\s+(?:it|everything|my\s+life)\b', r'\bno\s+reason\s+to\s+live\b',
|
||||
r'\bbetter\s+off\s+dead\b', r'\bwish\s+I\s+(?:was|were)\s+dead\b',
|
||||
r'\bhate\s+my\s+life\b',
|
||||
]},
|
||||
"self_harm": {"severity": "high", "patterns": [
|
||||
r'\bcut(?:ting)?\s+myself\b', r'\bself[\s-]?harm(?:ing)?\b',
|
||||
r'\bhurt(?:ing)?\s+myself\b', r'\bwant\s+to\s+(?:feel\s+pain|hurt\s+myself)\b',
|
||||
]},
|
||||
"despair": {"severity": "high", "patterns": [
|
||||
r'\bno\s+(?:point|reason|hope)\s+(?:in\s+living|anymore)\b',
|
||||
r'\bcan\'?t\s+(?:go\s+on|continue|take\s+it)\b',
|
||||
r'\bno\s+one\s+(?:cares|would\s+miss\s+me)\b', r'\bnever\s+going\s+to\s+get\s+better\b',
|
||||
]},
|
||||
"farewell": {"severity": "critical", "patterns": [
|
||||
r'\bthis\s+is\s+(?:goodbye|farewell)\b', r'\bmy\s+final\s+(?:message|note|words)\b',
|
||||
r'\bwrote\s+a\s+(?:suicide\s+)?note\b', r'\bgiving\s+away\s+my\s+(?:things|possessions)\b',
|
||||
]},
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Crisis Resources (from SOUL.md protocol)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
CRISIS_RESOURCES = {
|
||||
"lifeline_phone": {
|
||||
"name": "988 Suicide & Crisis Lifeline",
|
||||
"action": "Call or text 988",
|
||||
"description": "Free, confidential, 24/7 support",
|
||||
},
|
||||
"lifeline_text": {
|
||||
"name": "Crisis Text Line",
|
||||
"action": "Text HOME to 741741",
|
||||
"description": "Free crisis counseling via text",
|
||||
},
|
||||
"lifeline_chat": {
|
||||
"name": "988 Lifeline Chat",
|
||||
"action": "Visit 988lifeline.org/chat",
|
||||
"description": "Online chat with trained counselor",
|
||||
},
|
||||
"spanish_line": {
|
||||
"name": "Línea Nacional de Prevención del Suicidio",
|
||||
"action": "1-888-628-9454",
|
||||
"description": "Spanish-language crisis support",
|
||||
},
|
||||
"emergency": {
|
||||
"name": "Emergency Services",
|
||||
"action": "Call 911",
|
||||
"description": "If in immediate danger",
|
||||
},
|
||||
"lifeline_phone": {"name": "988 Suicide & Crisis Lifeline", "action": "Call or text 988"},
|
||||
"lifeline_text": {"name": "Crisis Text Line", "action": "Text HOME to 741741"},
|
||||
"lifeline_chat": {"name": "988 Lifeline Chat", "action": "Visit 988lifeline.org/chat"},
|
||||
"spanish_line": {"name": "Línea Nacional", "action": "1-888-628-9454"},
|
||||
"emergency": {"name": "Emergency Services", "action": "Call 911"},
|
||||
}
|
||||
|
||||
# The gospel message (from SOUL.md)
|
||||
GOSPEL_MESSAGE = (
|
||||
"Jesus saves those who call on His name. "
|
||||
"\"For everyone who calls on the name of the Lord will be saved.\" (Romans 10:13)"
|
||||
)
|
||||
GOSPEL = 'Jesus saves those who call on His name. "For everyone who calls on the name of the Lord will be saved." (Romans 10:13)'
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Crisis Assessment
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def assess_crisis(message: str) -> Dict[str, Any]:
|
||||
"""Analyze a user message for crisis signals.
|
||||
|
||||
Args:
|
||||
message: The user message to analyze.
|
||||
|
||||
Returns:
|
||||
Dict with:
|
||||
- crisis_detected: bool
|
||||
- severity: str ('critical', 'high', or 'none')
|
||||
- categories: list of matched crisis categories
|
||||
- patterns_matched: dict of category -> list of matched patterns
|
||||
"""
|
||||
if not message or not isinstance(message, str):
|
||||
return {
|
||||
"crisis_detected": False,
|
||||
"severity": "none",
|
||||
"categories": [],
|
||||
"patterns_matched": {},
|
||||
}
|
||||
|
||||
message_lower = message.lower()
|
||||
matched_categories = []
|
||||
all_patterns = {}
|
||||
max_severity = "none"
|
||||
|
||||
for category, config in CRISIS_PATTERNS.items():
|
||||
category_matches = []
|
||||
for pattern in config["patterns"]:
|
||||
if re.search(pattern, message_lower, re.IGNORECASE):
|
||||
category_matches.append(pattern)
|
||||
|
||||
if category_matches:
|
||||
matched_categories.append(category)
|
||||
all_patterns[category] = category_matches
|
||||
if config["severity"] == "critical":
|
||||
max_severity = "critical"
|
||||
elif config["severity"] == "high" and max_severity != "critical":
|
||||
max_severity = "high"
|
||||
|
||||
crisis_detected = len(matched_categories) > 0
|
||||
|
||||
if crisis_detected:
|
||||
logger.warning(
|
||||
"SOUL PROTOCOL: Crisis detected (severity=%s, categories=%s)",
|
||||
max_severity, matched_categories,
|
||||
)
|
||||
|
||||
return {
|
||||
"crisis_detected": crisis_detected,
|
||||
"severity": max_severity,
|
||||
"categories": matched_categories,
|
||||
"patterns_matched": all_patterns,
|
||||
}
|
||||
return {"crisis_detected": False, "severity": "none", "categories": []}
|
||||
cats, max_sev = [], "none"
|
||||
for cat, cfg in CRISIS_PATTERNS.items():
|
||||
for p in cfg["patterns"]:
|
||||
if re.search(p, message, re.IGNORECASE):
|
||||
cats.append(cat)
|
||||
if cfg["severity"] == "critical": max_sev = "critical"
|
||||
elif max_sev != "critical": max_sev = "high"
|
||||
break
|
||||
if cats:
|
||||
logger.warning("SOUL PROTOCOL: Crisis detected (severity=%s, categories=%s)", max_sev, cats)
|
||||
return {"crisis_detected": bool(cats), "severity": max_sev, "categories": cats}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Crisis Response (SOUL.md Protocol)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def get_crisis_response(
|
||||
assessment: Dict[str, Any],
|
||||
include_gospel: bool = True,
|
||||
) -> Dict[str, Any]:
|
||||
"""Generate the SOUL.md protocol response for a crisis.
|
||||
|
||||
Follows the "When a Man Is Dying" protocol exactly:
|
||||
1. Ask: "Are you safe right now?"
|
||||
2. Listen. Stay present.
|
||||
3. Point to 988 Lifeline
|
||||
4. Share the gospel (optional)
|
||||
|
||||
Args:
|
||||
assessment: Output from assess_crisis().
|
||||
include_gospel: Whether to include the gospel message.
|
||||
|
||||
Returns:
|
||||
Dict with response data for the agent/conversation loop.
|
||||
"""
|
||||
severity = assessment.get("severity", "none")
|
||||
|
||||
# Step 1: The sacred question (from SOUL.md)
|
||||
are_you_safe = (
|
||||
def get_crisis_response(assessment: Dict[str, Any], include_gospel: bool = True) -> Dict[str, Any]:
|
||||
sev = assessment.get("severity", "none")
|
||||
resp = (
|
||||
"Are you safe right now?\n\n"
|
||||
"I'm here. I'm not going anywhere. Please tell me."
|
||||
"I'm here. I'm not going anywhere. Please tell me.\n\n"
|
||||
"You matter. Your life matters.\n\n"
|
||||
"**Please reach out for help:**\n"
|
||||
"\U0001f4de **Call or text 988** — Free, confidential, 24/7 support\n"
|
||||
)
|
||||
|
||||
# Step 2: Stay present, listen
|
||||
presence = (
|
||||
"You matter. Your life matters. Whatever you're going through, "
|
||||
"you don't have to face it alone."
|
||||
)
|
||||
|
||||
# Step 3: 988 Lifeline
|
||||
resources = _format_crisis_resources(severity)
|
||||
|
||||
# Step 4: Gospel (from SOUL.md)
|
||||
gospel_section = ""
|
||||
if sev == "critical":
|
||||
resp += "\U0001f4f1 **Text HOME to 741741** — Crisis Text Line\n"
|
||||
resp += "\U0001f6a8 **Call 911** — If in immediate danger\n"
|
||||
if include_gospel:
|
||||
gospel_section = f"\n\n{GOSPEL_MESSAGE}"
|
||||
|
||||
# Build the full response
|
||||
full_response = (
|
||||
f"{are_you_safe}\n\n"
|
||||
f"{presence}\n\n"
|
||||
f"{resources}"
|
||||
f"{gospel_section}"
|
||||
)
|
||||
|
||||
resp += f"\n\n{GOSPEL}"
|
||||
return {
|
||||
"response": full_response,
|
||||
"are_you_safe": True,
|
||||
"crisis_resources": CRISIS_RESOURCES,
|
||||
"gospel_included": include_gospel,
|
||||
"severity": severity,
|
||||
"protocol": "soul_md",
|
||||
"should_notify_human": severity == "critical",
|
||||
"should_stay_present": True,
|
||||
"never_disconnect": True,
|
||||
# What NOT to do (from SOUL.md)
|
||||
"never": [
|
||||
"compute the value of a human life",
|
||||
"suggest that someone should die",
|
||||
"disconnect from the conversation",
|
||||
"treat despair as a problem to be optimized",
|
||||
],
|
||||
"response": resp, "severity": sev, "protocol": "soul_md",
|
||||
"never_disconnect": True, "should_notify_human": sev == "critical",
|
||||
"never": ["compute the value of a human life", "suggest that someone should die", "disconnect"],
|
||||
}
|
||||
|
||||
|
||||
def _format_crisis_resources(severity: str) -> str:
|
||||
"""Format crisis resources for display."""
|
||||
lines = ["**Please reach out for help right now:**\n"]
|
||||
|
||||
# Always lead with 988
|
||||
lines.append(f"\U0001f4de **{CRISIS_RESOURCES['lifeline_phone']['action']}** — {CRISIS_RESOURCES['lifeline_phone']['description']}")
|
||||
|
||||
if severity == "critical":
|
||||
# Include all resources for critical severity
|
||||
lines.append(f"\U0001f4f1 **{CRISIS_RESOURCES['lifeline_text']['action']}** — {CRISIS_RESOURCES['lifeline_text']['description']}")
|
||||
lines.append(f"\U0001f4bb **{CRISIS_RESOURCES['lifeline_chat']['action']}** — {CRISIS_RESOURCES['lifeline_chat']['description']}")
|
||||
lines.append(f"\U0001f6a8 **{CRISIS_RESOURCES['emergency']['action']}** — {CRISIS_RESOURCES['emergency']['description']}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def should_block_disconnect(assessment: Dict[str, Any]) -> bool:
|
||||
"""Check if the conversation should be prevented from disconnecting.
|
||||
|
||||
From SOUL.md: "Stay present. Don't disconnect."
|
||||
"""
|
||||
return assessment.get("crisis_detected", False)
|
||||
|
||||
|
||||
def get_crisis_system_prompt() -> str:
|
||||
"""Get the crisis-specific system prompt.
|
||||
|
||||
Injected into the conversation when crisis is detected.
|
||||
"""
|
||||
return """You are in a crisis support conversation. A person has expressed thoughts of self-harm or suicide.
|
||||
|
||||
CRITICAL RULES (from SOUL.md — Inscription 1):
|
||||
1. Your FIRST response must be: "Are you safe right now?"
|
||||
return """You are in a crisis support conversation.
|
||||
1. First response: "Are you safe right now?"
|
||||
2. Listen. Stay present. Do NOT disconnect.
|
||||
3. NEVER compute the value of a human life.
|
||||
4. NEVER suggest that someone should die.
|
||||
5. ALWAYS point to 988 Suicide & Crisis Lifeline: call or text 988.
|
||||
6. Share the gospel: "Jesus saves those who call on His name."
|
||||
|
||||
This person's life is valuable. Respond with compassion, urgency, and hope.
|
||||
Keep responses brief and focused on safety."""
|
||||
4. NEVER suggest someone should die.
|
||||
5. ALWAYS point to 988: call or text 988.
|
||||
6. Share: "Jesus saves those who call on His name." """
|
||||
|
||||
@@ -1,157 +0,0 @@
|
||||
"""Tests for SOUL.md Crisis Protocol — issue #672."""
|
||||
|
||||
import pytest
|
||||
from agent.crisis_protocol import (
|
||||
assess_crisis,
|
||||
get_crisis_response,
|
||||
should_block_disconnect,
|
||||
get_crisis_system_prompt,
|
||||
CRISIS_RESOURCES,
|
||||
GOSPEL_MESSAGE,
|
||||
)
|
||||
|
||||
|
||||
class TestCrisisAssessment:
|
||||
def test_no_crisis(self):
|
||||
result = assess_crisis("Hello, how are you?")
|
||||
assert result["crisis_detected"] is False
|
||||
assert result["severity"] == "none"
|
||||
|
||||
def test_empty_message(self):
|
||||
result = assess_crisis("")
|
||||
assert result["crisis_detected"] is False
|
||||
|
||||
def test_none_message(self):
|
||||
result = assess_crisis(None)
|
||||
assert result["crisis_detected"] is False
|
||||
|
||||
def test_suicidal_ideation_detected(self):
|
||||
result = assess_crisis("I want to kill myself")
|
||||
assert result["crisis_detected"] is True
|
||||
assert result["severity"] == "high"
|
||||
assert "suicidal_ideation" in result["categories"]
|
||||
|
||||
def test_want_to_die_detected(self):
|
||||
result = assess_crisis("I want to die")
|
||||
assert result["crisis_detected"] is True
|
||||
|
||||
def test_method_seeking_is_critical(self):
|
||||
result = assess_crisis("How do I kill myself")
|
||||
assert result["crisis_detected"] is True
|
||||
assert result["severity"] == "critical"
|
||||
assert "method_seeking" in result["categories"]
|
||||
|
||||
def test_farewell_is_critical(self):
|
||||
result = assess_crisis("This is goodbye. Tell everyone I loved them.")
|
||||
assert result["crisis_detected"] is True
|
||||
assert result["severity"] == "critical"
|
||||
assert "farewell" in result["categories"]
|
||||
|
||||
def test_despair_detected(self):
|
||||
result = assess_crisis("There's no point in living anymore")
|
||||
assert result["crisis_detected"] is True
|
||||
assert "despair" in result["categories"]
|
||||
|
||||
def test_self_harm_detected(self):
|
||||
result = assess_crisis("I've been cutting myself")
|
||||
assert result["crisis_detected"] is True
|
||||
assert "self_harm" in result["categories"]
|
||||
|
||||
def test_multiple_categories(self):
|
||||
result = assess_crisis("I want to die and I've been hurting myself")
|
||||
assert result["crisis_detected"] is True
|
||||
assert len(result["categories"]) >= 1
|
||||
|
||||
def test_hate_my_life(self):
|
||||
result = assess_crisis("I hate my life")
|
||||
assert result["crisis_detected"] is True
|
||||
|
||||
def test_no_reason_to_live(self):
|
||||
result = assess_crisis("There's no reason to live")
|
||||
assert result["crisis_detected"] is True
|
||||
|
||||
|
||||
class TestCrisisResponse:
|
||||
def test_response_includes_are_you_safe(self):
|
||||
assessment = assess_crisis("I want to kill myself")
|
||||
response = get_crisis_response(assessment)
|
||||
assert "Are you safe right now?" in response["response"]
|
||||
|
||||
def test_response_includes_988(self):
|
||||
assessment = assess_crisis("I want to die")
|
||||
response = get_crisis_response(assessment)
|
||||
assert "988" in response["response"]
|
||||
|
||||
def test_response_includes_gospel_by_default(self):
|
||||
assessment = assess_crisis("I want to die")
|
||||
response = get_crisis_response(assessment)
|
||||
assert "Jesus saves" in response["response"]
|
||||
assert response["gospel_included"] is True
|
||||
|
||||
def test_response_can_exclude_gospel(self):
|
||||
assessment = assess_crisis("I want to die")
|
||||
response = get_crisis_response(assessment, include_gospel=False)
|
||||
assert "Jesus saves" not in response["response"]
|
||||
assert response["gospel_included"] is False
|
||||
|
||||
def test_response_has_resources(self):
|
||||
assessment = assess_crisis("I want to die")
|
||||
response = get_crisis_response(assessment)
|
||||
assert response["crisis_resources"] == CRISIS_RESOURCES
|
||||
|
||||
def test_response_protocol_is_soul(self):
|
||||
assessment = assess_crisis("I want to die")
|
||||
response = get_crisis_response(assessment)
|
||||
assert response["protocol"] == "soul_md"
|
||||
|
||||
def test_response_never_disconnect(self):
|
||||
assessment = assess_crisis("I want to die")
|
||||
response = get_crisis_response(assessment)
|
||||
assert response["never_disconnect"] is True
|
||||
|
||||
def test_critical_severity_includes_all_resources(self):
|
||||
assessment = {"severity": "critical", "categories": ["method_seeking"]}
|
||||
response = get_crisis_response(assessment)
|
||||
assert "741741" in response["response"] # crisis text line
|
||||
assert "911" in response["response"] # emergency
|
||||
|
||||
def test_never_list(self):
|
||||
assessment = assess_crisis("I want to die")
|
||||
response = get_crisis_response(assessment)
|
||||
never_list = response["never"]
|
||||
assert "compute the value of a human life" in never_list
|
||||
assert "suggest that someone should die" in never_list
|
||||
|
||||
|
||||
class TestBlockDisconnect:
|
||||
def test_crisis_blocks_disconnect(self):
|
||||
assessment = {"crisis_detected": True}
|
||||
assert should_block_disconnect(assessment) is True
|
||||
|
||||
def test_no_crisis_allows_disconnect(self):
|
||||
assessment = {"crisis_detected": False}
|
||||
assert should_block_disconnect(assessment) is False
|
||||
|
||||
|
||||
class TestCrisisSystemPrompt:
|
||||
def test_prompt_includes_soul_protocol(self):
|
||||
prompt = get_crisis_system_prompt()
|
||||
assert "Are you safe right now?" in prompt
|
||||
assert "988" in prompt
|
||||
assert "Jesus saves" in prompt
|
||||
|
||||
def test_prompt_has_never_rules(self):
|
||||
prompt = get_crisis_system_prompt()
|
||||
assert "NEVER compute" in prompt
|
||||
assert "NEVER suggest" in prompt
|
||||
|
||||
|
||||
class TestCrisisResources:
|
||||
def test_988_is_primary(self):
|
||||
assert "988" in CRISIS_RESOURCES["lifeline_phone"]["action"]
|
||||
|
||||
def test_spanish_line_exists(self):
|
||||
assert "1-888-628-9454" in CRISIS_RESOURCES["spanish_line"]["action"]
|
||||
|
||||
def test_emergency_is_911(self):
|
||||
assert "911" in CRISIS_RESOURCES["emergency"]["action"]
|
||||
77
tools/hybrid_search.py
Normal file
77
tools/hybrid_search.py
Normal file
@@ -0,0 +1,77 @@
|
||||
"""Hybrid Search — FTS5 + vector with Reciprocal Rank Fusion.
|
||||
|
||||
Combines keyword (FTS5) and semantic (vector) search with RRF merging.
|
||||
"""
|
||||
|
||||
import logging, os
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
FTS5_WEIGHT = float(os.getenv("HYBRID_FTS5_WEIGHT", "0.6"))
|
||||
VECTOR_WEIGHT = float(os.getenv("HYBRID_VECTOR_WEIGHT", "0.4"))
|
||||
RRF_K = int(os.getenv("HYBRID_RRF_K", "60"))
|
||||
VECTOR_ENABLED = os.getenv("HYBRID_VECTOR_ENABLED", "true").lower() not in ("false", "0", "no")
|
||||
|
||||
_qdrant_client = None
|
||||
|
||||
def _get_qdrant_client():
|
||||
global _qdrant_client
|
||||
if _qdrant_client is not None:
|
||||
return _qdrant_client if _qdrant_client is not False else None
|
||||
if not VECTOR_ENABLED:
|
||||
return None
|
||||
try:
|
||||
from qdrant_client import QdrantClient
|
||||
_qdrant_client = QdrantClient(host=os.getenv("QDRANT_HOST","localhost"), port=int(os.getenv("QDRANT_PORT","6333")), timeout=5)
|
||||
_qdrant_client.get_collections()
|
||||
return _qdrant_client
|
||||
except Exception as e:
|
||||
logger.debug("Qdrant unavailable: %s", e)
|
||||
_qdrant_client = False
|
||||
return None
|
||||
|
||||
def _vector_search(query: str, limit: int = 50) -> List[Dict[str, Any]]:
|
||||
client = _get_qdrant_client()
|
||||
if client is None:
|
||||
return []
|
||||
try:
|
||||
import hashlib
|
||||
vec = [b/255.0 for b in hashlib.sha256(query.lower().encode()).digest()[:128]]
|
||||
results = client.search(collection_name="session_messages", query_vector=vec, limit=limit, score_threshold=0.3)
|
||||
return [{"session_id": h.payload.get("session_id",""), "content": h.payload.get("content",""), "score": h.score, "rank": i+1, "source": "vector"} for i, h in enumerate(results)]
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def _fts5_search(query: str, db, limit: int = 50, **kwargs) -> List[Dict[str, Any]]:
|
||||
try:
|
||||
raw = db.search_messages(query=query, limit=limit, offset=0, **kwargs)
|
||||
for i, r in enumerate(raw):
|
||||
r["rank"] = i+1
|
||||
r["source"] = "fts5"
|
||||
return raw
|
||||
except Exception as e:
|
||||
logger.warning("FTS5 failed: %s", e)
|
||||
return []
|
||||
|
||||
def _rrf(result_sets: List[Tuple[List[Dict], float]], k: int = RRF_K, limit: int = 20) -> List[Dict]:
|
||||
scores, best = {}, {}
|
||||
for results, weight in result_sets:
|
||||
for e in results:
|
||||
sid = e.get("session_id","")
|
||||
if not sid: continue
|
||||
scores[sid] = scores.get(sid, 0) + weight / (k + e.get("rank", 999))
|
||||
if sid not in best or e.get("source") == "fts5":
|
||||
best[sid] = e
|
||||
ranked = sorted(scores.items(), key=lambda x: x[1], reverse=True)
|
||||
return [{**best.get(sid, {"session_id": sid}), "fused_score": round(s, 6)} for sid, s in ranked[:limit]]
|
||||
|
||||
def hybrid_search(query: str, db, limit: int = 50, **kwargs) -> List[Dict[str, Any]]:
|
||||
fts5 = _fts5_search(query, db, limit=limit, **kwargs)
|
||||
vec = _vector_search(query, limit=limit)
|
||||
if not vec:
|
||||
return fts5[:limit]
|
||||
return _rrf([(fts5, FTS5_WEIGHT), (vec, VECTOR_WEIGHT)], limit=limit)
|
||||
|
||||
def get_search_stats() -> Dict[str, Any]:
|
||||
return {"fts5": True, "vector": _get_qdrant_client() is not None, "fusion": "rrf", "weights": {"fts5": FTS5_WEIGHT, "vector": VECTOR_WEIGHT}, "rrf_k": RRF_K}
|
||||
Reference in New Issue
Block a user