Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/harvester/blob/main/src/extractor.rs # harvester Zero-telemetry documentation and knowledge extraction engine Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `harvester / src / extractor.rs` 100 lines ยท 3150 bytes Raw Blame History 1 use regex::Regex; 2 use serde::{Deserialize, Serialize}; 3 4 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] 5 pub struct ExtractedPair { 6 pub prompt: String, 7 pub reasoning: Option, 8 pub completion: String, 9 pub provider: String, 10 pub model: String, 11 pub token_count: usize, 12 pub quality_score: f32, 13 } 14 15 pub struct ReasoningExtractor { 16 think_re: Regex, 17 thought_re: Regex, 18 } 19 20 impl Default for ReasoningExtractor { 21 fn default() -> Self { 22 Self::new() 23 } 24 } 25 26 impl ReasoningExtractor { 27 pub fn new() -> Self { 28 Self { 29 think_re: Regex::new(r"(?s)(.*?)").unwrap(), 30 thought_re: Regex::new(r"(?s)(.*?)").unwrap(), 31 } 32 } 33 34 pub fn extract(&self, prompt: &str, raw_response: &str, provider: &str, model: &str) -> ExtractedPair { 35 let mut reasoning = None; 36 let mut completion = raw_response.to_string(); 37 38 if let Some(caps) = self.think_re.captures(raw_response) { 39 if let Some(m) = caps.get(1) { 40 reasoning = Some(m.as_str().trim().to_string()); 41 completion = self.think_re.replace(raw_response, "").trim().to_string(); 42 } 43 } else if let Some(caps) = self.thought_re.captures(raw_response) { 44 if let Some(m) = caps.get(1) { 45 reasoning = Some(m.as_str().trim().to_string()); 46 completion = self.thought_re.replace(raw_response, "").trim().to_string(); 47 } 48 } 49 50 let token_count = (prompt.len() + raw_response.len()) / 4; 51 let quality_score = Self::score_quality(prompt, reasoning.as_deref(), &completion); 52 53 ExtractedPair { 54 prompt: prompt.to_string(), 55 reasoning, 56 completion, 57 provider: provider.to_string(), 58 model: model.to_string(), 59 token_count, 60 quality_score, 61 } 62 } 63 64 fn score_quality(_prompt: &str, reasoning: Option<&str>, completion: &str) -> f32 { 65 let mut score = 0.5f32; 66 if let Some(r) = reasoning { 67 if !r.is_empty() { 68 score += 0.2; 69 } 70 if r.len() > 50 { 71 score += 0.1; 72 } 73 if r.contains("step") || r.contains("verify") || r.contains("O(") { 74 score += 0.1; 75 } 76 } 77 if completion.contains("```") { 78 score += 0.1; 79 } 80 score.min(1.0) 81 } 82 } 83 84 #[cfg(test)] 85 mod tests { 86 use super::*; 87 88 #[test] 89 fn test_extract_think_tags() { 90 let extractor = ReasoningExtractor::new(); 91 let prompt = "Implement fibonacci in Rust"; 92 let raw = "Let us analyze recursion vs iteration. Iteration is O(N) time and O(1) space.```rust\nfn fib(n: u32) -> u64 { 0 }\n```"; 93 let pair = extractor.extract(prompt, raw, "anthropic", "claude-3-7-sonnet"); 94 95 assert!(pair.reasoning.is_some()); 96 assert!(pair.reasoning.unwrap().contains("analyze recursion")); 97 assert!(pair.completion.starts_with("```rust")); 98 assert!(pair.quality_score >= 0.8); 99 } 100 }