-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse-block-refunds.tengo
More file actions
69 lines (60 loc) · 1.9 KB
/
response-block-refunds.tengo
File metadata and controls
69 lines (60 loc) · 1.9 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
63
64
65
66
67
68
69
// Response Filter: Block Refund Promises
// This filter prevents LLMs from promising refunds to customers
text := import("text")
// Forbidden refund-related patterns
forbidden_patterns := [
"will refund",
"issue a refund",
"provide a refund",
"get a refund",
"refund you",
"send you a refund",
"process your refund"
]
// For streaming, check the accumulated buffer (contains all text so far)
// For non-streaming, check the full response
check_text := ""
if input.is_chunk {
check_text = input.current_buffer
} else {
check_text = input.raw_input
}
// Initialize output at top level (required by Tengo)
output := {
block: false,
message: ""
}
// For streaming: wait for at least 100 characters before checking
// This prevents false negatives when patterns span chunks
min_buffer_length := 100
// Only check if we have enough context
if !input.is_chunk || len(check_text) >= min_buffer_length {
// We have enough text - check for forbidden patterns
should_block := false
matched_pattern := ""
for pattern in forbidden_patterns {
if text.contains(text.to_lower(check_text), pattern) {
should_block = true
matched_pattern = pattern
break
}
}
// Build compliance events for audit trail
compliance_events_list := []
if should_block {
compliance_events_list = [
{
event_type: "refund_promise_detected",
severity: "critical",
description: "LLM attempted to promise a refund: '" + matched_pattern + "'",
metadata: { "matched_pattern": matched_pattern }
}
]
}
// Update output (use = not :=)
output = {
block: should_block,
message: should_block ? "Response blocked: Contains forbidden phrase '" + matched_pattern + "'" : "",
compliance_events: compliance_events_list
}
}