Module: RubyLLM::TopSecret::Patches::Chat

Defined in:
lib/ruby_llm/top_secret/patches/chat.rb

Overview

Middleware that intercepts Chat#complete to filter sensitive information from messages before they are sent to the LLM provider, and restore placeholders in the response with original values.

Filtering only runs when opted in via RubyLLM::TopSecret.with_filtering. Delegates to RubyLLM::TopSecret::Payload for filtering and restoring content. Original message content is preserved after the call via ensure.

Examples:

RubyLLM::TopSecret.with_filtering do
  chat = RubyLLM.chat
  chat.ask("My name is Ralph")
  # => Provider receives "My name is [PERSON_1]"
  # => Response "[PERSON_1] is a great name" becomes "Ralph is a great name"
end

Raises:

Instance Method Summary collapse

Instance Method Details

#completeObject

See Also:

  • Chat#complete


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_llm/top_secret/patches/chat.rb', line 25

def complete(&)
  return super unless RubyLLM::TopSecret.filtering?

  originals = messages.to_h { |msg| [msg, msg.content] }

  payload = Payload.new(messages)
  payload.filter

  response = super

  response.content = payload.restore(response.content)

  response
rescue RubyLLM::Error
  raise
rescue => e
  raise RubyLLM::TopSecret::Error, e.message
ensure
  originals&.each { |msg, content| msg.content = content }
end