Skip to content

Commit d5af78e

Browse files
Artificial Intelligence: Filter sensitive information
Introduces new section for Artificial Intelligence, and demonstrates how to filter sensitive information from messages.
1 parent 4dac788 commit d5af78e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

artificial-intelligence/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Artificial Intelligence
2+
3+
## Security
4+
5+
- Filter sensitive information before sending data to an LLM or chatbot.
6+
[Example](./how-to/filter_sensitive_information.md).
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Filter Sensitive Information
2+
3+
Before sending free text to an LLM or chatbot, filter all messages for
4+
potentially sensitive information first.
5+
6+
```ruby
7+
require "openai"
8+
require "top_secret"
9+
10+
openai = OpenAI::Client.new(
11+
api_key: Rails.application.credentials.openai.api_key!
12+
)
13+
14+
original_messages = [
15+
"Ralph lives in Boston.",
16+
"You can reach them at [email protected] or 877-976-2687"
17+
]
18+
19+
# Filter all messages
20+
result = TopSecret::Text.filter_all(original_messages)
21+
filtered_messages = result.items.map(&:output)
22+
23+
user_messages = filtered_messages.map { {role: "user", content: it} }
24+
25+
# Instruct LLM how to handle filtered messages
26+
instructions = <<~TEXT
27+
I'm going to send filtered information to you in the form of free text.
28+
If you need to refer to the filtered information in a response, just reference it by the filter.
29+
TEXT
30+
31+
messages = [
32+
{role: "system", content: instructions},
33+
*user_messages
34+
]
35+
36+
chat_completion = openai.chat.completions.create(messages:, model: :"gpt-5")
37+
response = chat_completion.choices.last.message.content
38+
39+
# Restore the response from the mapping
40+
mapping = result.mapping
41+
restored_response = TopSecret::FilteredText.restore(response, mapping:).output
42+
43+
puts(restored_response)
44+
```

0 commit comments

Comments
 (0)