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.
Instance Method Summary collapse
Instance Method Details
#complete ⇒ Object
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 = .to_h { |msg| [msg, msg.content] } payload = Payload.new() payload.filter response = super response.content = payload.restore(response.content) response rescue RubyLLM::Error raise rescue => e raise RubyLLM::TopSecret::Error, e. ensure originals&.each { |msg, content| msg.content = content } end |