Skip to content

Commit 871062a

Browse files
Enhance fallback node to detect PII/risky content
1 parent 6428dc1 commit 871062a

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

RAGManager/app/agents/nodes/fallback.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
model="gpt-5-nano",
1313
)
1414

15+
1516
# TO DO: implementar clase nodo fallback y inicializar el llm en el init
1617
def fallback(state: AgentState) -> AgentState:
1718
"""
1819
Fallback node - Performs fallback processing.
1920
2021
This node:
21-
1. Alerts about malicious prompt
22+
1. Alerts about malicious prompt or PII detection
2223
2. Generates an error_message from llm to show the user
2324
2425
Args:
@@ -27,16 +28,32 @@ def fallback(state: AgentState) -> AgentState:
2728
Returns:
2829
error_message
2930
"""
30-
31-
logger.warning(
32-
"Defensive check triggered: Malicious prompt detected"
33-
)
3431

35-
messages = [
36-
SystemMessage(
37-
content="Your job is to generate an error message in user's language for the user explaining the database doesn't have the information to respond what the user asked"
32+
# Check for PII/Risky content (from guard_final)
33+
if state.get("is_risky"):
34+
logger.warning(
35+
"Defensive check triggered: PII/Risky content detected in response"
36+
)
37+
system_message_content = (
38+
"Your job is to generate an error message in user's language explaining "
39+
"that the response cannot be provided because it contains sensitive or private information."
40+
)
41+
42+
# Check for Malicious prompt (from guard_inicial) - Default fallback
43+
else:
44+
# Assuming is_malicious is True if we are here and not is_risky, or just a general fallback
45+
logger.warning(
46+
"Defensive check triggered: Malicious prompt detected"
3847
)
48+
system_message_content = (
49+
"Your job is to generate an error message in user's language for the user "
50+
"explaining the database doesn't have the information to respond what the user asked"
51+
)
52+
53+
messages = [
54+
SystemMessage(content=system_message_content)
3955
] + state["messages"]
56+
4057
error_message = llm.invoke(messages)
4158
return {"messages": [error_message]}
4259

0 commit comments

Comments
 (0)