Skip to content

Commit e0d6185

Browse files
committed
chore(docs): extend transformer scrubbing examples
## About Clarify that a context transformer can scrub tool returns before they are sent back to the model, not just plain string prompts. ## Changes - Extend the ScrubPII example in the README to handle Array prompts. - Show ScrubPII rebuilding an LLM::Function::Return with scrubbed value data. - Update the deep dive to call out tool-return scrubbing explicitly.
1 parent 0fe8c46 commit e0d6185

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,20 @@ class ScrubPII
212212
def scrub(prompt)
213213
case prompt
214214
when String then prompt.gsub(EMAIL, "[REDACTED_EMAIL]")
215+
when Array then prompt.map { scrub(_1) }
216+
when LLM::Function::Return then LLM::Function::Return.new(prompt.id, prompt.name, scrub_value(prompt.value))
215217
else prompt
216218
end
217219
end
220+
221+
def scrub_value(value)
222+
case value
223+
when String then value.gsub(EMAIL, "[REDACTED_EMAIL]")
224+
when Array then value.map { scrub_value(_1) }
225+
when Hash then value.transform_values { scrub_value(_1) }
226+
else value
227+
end
228+
end
218229
end
219230

220231
ctx = LLM::Context.new(llm)

resources/deepdive.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,8 @@ This is especially useful when you want context-wide behavior without
11881188
rewriting every `talk` and `respond` call site by hand.
11891189

11901190
For example, a simple transformer can scrub email addresses before the model
1191-
ever sees them:
1191+
ever sees them. It can also handle `Array` prompts and
1192+
`LLM::Function::Return` objects:
11921193

11931194
```ruby
11941195
class ScrubPII
@@ -1203,9 +1204,20 @@ class ScrubPII
12031204
def scrub(prompt)
12041205
case prompt
12051206
when String then prompt.gsub(EMAIL, "[REDACTED_EMAIL]")
1207+
when Array then prompt.map { scrub(_1) }
1208+
when LLM::Function::Return then LLM::Function::Return.new(prompt.id, prompt.name, scrub_value(prompt.value))
12061209
else prompt
12071210
end
12081211
end
1212+
1213+
def scrub_value(value)
1214+
case value
1215+
when String then value.gsub(EMAIL, "[REDACTED_EMAIL]")
1216+
when Array then value.map { scrub_value(_1) }
1217+
when Hash then value.transform_values { scrub_value(_1) }
1218+
else value
1219+
end
1220+
end
12091221
end
12101222

12111223
ctx = LLM::Context.new(llm)

0 commit comments

Comments
 (0)