pub struct SemanticDeduplicator { /* private fields */ }Expand description
Near-duplicate request detector using SimHash locality-sensitive hashing.
Thread-safe and lock-free on the hot path via DashMap.
§Parameters
similarity_threshold— Maximum Hamming distance for two prompts to be considered duplicates. Typical values: 0 (exact), 3 (paraphrases), 6 (loose semantic equivalence).window— How long fingerprints are kept before expiry.
Implementations§
Source§impl SemanticDeduplicator
impl SemanticDeduplicator
Sourcepub fn new(similarity_threshold: u32, window: Duration) -> Self
pub fn new(similarity_threshold: u32, window: Duration) -> Self
Create a new SemanticDeduplicator.
§Arguments
similarity_threshold— Hamming-distance threshold. Prompts whose fingerprints differ by ≤ this many bits are treated as duplicates. Use0for exact-content dedup only.window— TTL for cached fingerprints.
Sourcepub fn is_novel(&self, text: &str) -> bool
pub fn is_novel(&self, text: &str) -> bool
Returns true if text has not been seen recently (or is not a
near-duplicate of a recently seen prompt).
This is a read-only check; call register to record
a novel request.
Sourcepub fn register(&self, text: &str)
pub fn register(&self, text: &str)
Record text as a recently processed request.
Call this after confirming a request is novel (via is_novel) and
sending it to the LLM.
Sourcepub fn check_and_register(&self, text: &str) -> bool
pub fn check_and_register(&self, text: &str) -> bool
Check if text is novel and, if so, register it atomically.
Returns true if the request was novel and has been registered.
Returns false if it matched a cached near-duplicate.
Sourcepub fn cached_count(&self) -> usize
pub fn cached_count(&self) -> usize
Number of fingerprints currently cached (including possibly expired ones not yet evicted).
Sourcepub fn evict_expired(&self)
pub fn evict_expired(&self)
Manually evict all expired entries. Called automatically on every
register and check_and_register call.
Trait Implementations§
Source§impl Clone for SemanticDeduplicator
impl Clone for SemanticDeduplicator
Source§fn clone(&self) -> SemanticDeduplicator
fn clone(&self) -> SemanticDeduplicator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for SemanticDeduplicator
impl !UnwindSafe for SemanticDeduplicator
impl Freeze for SemanticDeduplicator
impl Send for SemanticDeduplicator
impl Sync for SemanticDeduplicator
impl Unpin for SemanticDeduplicator
impl UnsafeUnpin for SemanticDeduplicator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request