harvester
Zero-telemetry documentation and knowledge extraction engine
Brought by drakestapleton
| 1 | use sha2::{Digest, Sha256}; |
| 2 | use std::collections::HashSet; |
| 3 | use crate::extractor::ExtractedPair; |
| 4 | |
| 5 | pub struct DeduplicationFilter { |
| 6 | seen_hashes: HashSet<String>, |
| 7 | min_quality: f32, |
| 8 | } |
| 9 | |
| 10 | impl DeduplicationFilter { |
| 11 | pub fn new(min_quality: f32) -> Self { |
| 12 | Self { |
| 13 | seen_hashes: HashSet::new(), |
| 14 | min_quality, |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | pub fn accept(&mut self, pair: &ExtractedPair) -> bool { |
| 19 | if pair.quality_score < self.min_quality { |
| 20 | return false; |
| 21 | } |
| 22 | let hash = self.compute_fingerprint(&pair.prompt, &pair.completion); |
| 23 | self.seen_hashes.insert(hash) |
| 24 | } |
| 25 | |
| 26 | fn compute_fingerprint(&self, prompt: &str, completion: &str) -> String { |
| 27 | let mut hasher = Sha256::new(); |
| 28 | hasher.update(prompt.trim().to_lowercase().as_bytes()); |
| 29 | hasher.update(b"||"); |
| 30 | hasher.update(completion.trim().as_bytes()); |
| 31 | format!("{:x}", hasher.finalize()) |
| 32 | } |
| 33 | |
| 34 | pub fn count(&self) -> usize { |
| 35 | self.seen_hashes.len() |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[cfg(test)] |
| 40 | mod tests { |
| 41 | use super::*; |
| 42 | |
| 43 | #[test] |
| 44 | fn test_dedup_filter() { |
| 45 | let mut filter = DeduplicationFilter::new(0.6); |
| 46 | let pair1 = ExtractedPair { |
| 47 | prompt: "Test prompt".to_string(), |
| 48 | reasoning: Some("Thought".to_string()), |
| 49 | completion: "Result".to_string(), |
| 50 | provider: "openai".to_string(), |
| 51 | model: "o3-mini".to_string(), |
| 52 | token_count: 50, |
| 53 | quality_score: 0.8, |
| 54 | }; |
| 55 | |
| 56 | assert!(filter.accept(&pair1)); |
| 57 | assert!(!filter.accept(&pair1)); |
| 58 | assert_eq!(filter.count(), 1); |
| 59 | } |
| 60 | } |