|
41 | 41 |
|
42 | 42 | use areev_core::error::Result; |
43 | 43 | use areev_core::types::Event; |
| 44 | +use areev_bench::locomo::{parse_locomo, Conv, TfidfEmbed}; |
44 | 45 | use areev_store::{Areev, AreevOptions, DecisionRerank, DecisionRerankStats, EmbedBackend, RecallTuning, RerankBackend}; |
45 | 46 | use std::collections::{BTreeMap, HashMap, HashSet}; |
46 | 47 | use std::sync::{Arc, Mutex}; |
@@ -90,159 +91,6 @@ impl EmbedBackend for CachedEmbed { |
90 | 91 | } |
91 | 92 | } |
92 | 93 |
|
93 | | -// ---------- local TF-IDF (+bigram) embedder (no-API fallback) ---------- |
94 | | -struct TfidfEmbed { |
95 | | - dim: usize, |
96 | | - idf: HashMap<String, f32>, |
97 | | - default_idf: f32, |
98 | | -} |
99 | | -impl TfidfEmbed { |
100 | | - fn build(dim: usize, docs: &[String]) -> Self { |
101 | | - let n = docs.len().max(1); |
102 | | - let mut df: HashMap<String, usize> = HashMap::new(); |
103 | | - for d in docs { |
104 | | - for t in features(d).into_iter().collect::<HashSet<_>>() { |
105 | | - *df.entry(t).or_insert(0) += 1; |
106 | | - } |
107 | | - } |
108 | | - let idf = df |
109 | | - .iter() |
110 | | - .map(|(t, &c)| (t.clone(), ((n as f32 + 1.0) / (c as f32 + 1.0)).ln() + 1.0)) |
111 | | - .collect(); |
112 | | - Self { dim, idf, default_idf: (n as f32 + 1.0).ln() + 1.0 } |
113 | | - } |
114 | | -} |
115 | | -fn tokenize(s: &str) -> Vec<String> { |
116 | | - s.chars() |
117 | | - .map(|c| if c.is_alphanumeric() { c.to_ascii_lowercase() } else { ' ' }) |
118 | | - .collect::<String>() |
119 | | - .split_whitespace() |
120 | | - .filter(|w| w.len() > 1) |
121 | | - .map(|w| w.to_string()) |
122 | | - .collect() |
123 | | -} |
124 | | -/// unigrams + adjacent bigrams |
125 | | -fn features(s: &str) -> Vec<String> { |
126 | | - let toks = tokenize(s); |
127 | | - let mut f = toks.clone(); |
128 | | - for w in toks.windows(2) { |
129 | | - f.push(format!("{}_{}", w[0], w[1])); |
130 | | - } |
131 | | - f |
132 | | -} |
133 | | -fn fnv1a(t: &str) -> u64 { |
134 | | - let mut h = 0xcbf29ce484222325u64; |
135 | | - for b in t.bytes() { |
136 | | - h ^= b as u64; |
137 | | - h = h.wrapping_mul(0x100000001b3); |
138 | | - } |
139 | | - h |
140 | | -} |
141 | | -impl EmbedBackend for TfidfEmbed { |
142 | | - fn dim(&self) -> usize { |
143 | | - self.dim |
144 | | - } |
145 | | - fn embed(&self, text: &str) -> Result<Vec<f32>> { |
146 | | - let mut tf: HashMap<String, f32> = HashMap::new(); |
147 | | - for t in features(text) { |
148 | | - *tf.entry(t).or_insert(0.0) += 1.0; |
149 | | - } |
150 | | - let mut v = vec![0f32; self.dim]; |
151 | | - for (t, c) in tf { |
152 | | - let w = c * self.idf.get(&t).copied().unwrap_or(self.default_idf); |
153 | | - v[(fnv1a(&t) % self.dim as u64) as usize] += w; |
154 | | - } |
155 | | - let norm = v.iter().map(|x| x * x).sum::<f32>().sqrt(); |
156 | | - if norm > 0.0 { |
157 | | - for x in &mut v { |
158 | | - *x /= norm; |
159 | | - } |
160 | | - } |
161 | | - Ok(v) |
162 | | - } |
163 | | - fn model(&self) -> &str { |
164 | | - "tfidf-bigram-2048" |
165 | | - } |
166 | | -} |
167 | | - |
168 | | -// ---------- LoCoMo records ---------- |
169 | | -struct Turn { |
170 | | - dia_id: String, |
171 | | - text: String, |
172 | | -} |
173 | | -struct Qa { |
174 | | - question: String, |
175 | | - answer: String, |
176 | | - evidence: Vec<String>, |
177 | | - category: i64, |
178 | | -} |
179 | | -struct Conv { |
180 | | - turns: Vec<Turn>, |
181 | | - qa: Vec<Qa>, |
182 | | - dates: HashMap<String, String>, // dia_id -> session date (for temporal resolution) |
183 | | -} |
184 | | - |
185 | | -fn parse_locomo(v: &serde_json::Value, limit: usize) -> Vec<Conv> { |
186 | | - let mut out = Vec::new(); |
187 | | - for sample in v.as_array().unwrap_or(&vec![]).iter().take(limit) { |
188 | | - let mut turns = Vec::new(); |
189 | | - let mut dates: HashMap<String, String> = HashMap::new(); |
190 | | - if let Some(conv) = sample.get("conversation").and_then(|c| c.as_object()) { |
191 | | - // keys like session_1, session_2, … (skip session_N_date_time) |
192 | | - let mut skeys: Vec<&String> = conv |
193 | | - .keys() |
194 | | - .filter(|k| k.strip_prefix("session_").map(|r| !r.is_empty() && r.chars().all(|c| c.is_ascii_digit())).unwrap_or(false)) |
195 | | - .collect(); |
196 | | - skeys.sort_by_key(|k| k.strip_prefix("session_").and_then(|r| r.parse::<i64>().ok()).unwrap_or(0)); |
197 | | - for sk in skeys { |
198 | | - let date = conv |
199 | | - .get(&format!("{sk}_date_time")) |
200 | | - .and_then(|d| d.as_str()) |
201 | | - .unwrap_or("") |
202 | | - .to_string(); |
203 | | - if let Some(arr) = conv.get(sk).and_then(|s| s.as_array()) { |
204 | | - for t in arr { |
205 | | - let speaker = t.get("speaker").and_then(|s| s.as_str()).unwrap_or(""); |
206 | | - let text = t.get("text").and_then(|s| s.as_str()).unwrap_or(""); |
207 | | - let dia = t.get("dia_id").and_then(|s| s.as_str()).unwrap_or(""); |
208 | | - if !dia.is_empty() && !text.is_empty() { |
209 | | - dates.insert(dia.to_string(), date.clone()); |
210 | | - turns.push(Turn { dia_id: dia.to_string(), text: format!("{speaker}: {text}") }); |
211 | | - } |
212 | | - } |
213 | | - } |
214 | | - } |
215 | | - } |
216 | | - let mut qa = Vec::new(); |
217 | | - if let Some(arr) = sample.get("qa").and_then(|q| q.as_array()) { |
218 | | - for q in arr { |
219 | | - let evidence: Vec<String> = q |
220 | | - .get("evidence") |
221 | | - .and_then(|e| e.as_array()) |
222 | | - .map(|a| a.iter().filter_map(|x| x.as_str().map(str::to_string)).collect()) |
223 | | - .unwrap_or_default(); |
224 | | - if evidence.is_empty() { |
225 | | - continue; // no gold turn to hit — skip (adversarial abstentions) |
226 | | - } |
227 | | - let answer = q |
228 | | - .get("answer") |
229 | | - .and_then(|a| a.as_str()) |
230 | | - .or_else(|| q.get("adversarial_answer").and_then(|a| a.as_str())) |
231 | | - .unwrap_or("") |
232 | | - .to_string(); |
233 | | - qa.push(Qa { |
234 | | - question: q.get("question").and_then(|s| s.as_str()).unwrap_or("").to_string(), |
235 | | - answer, |
236 | | - evidence, |
237 | | - category: q.get("category").and_then(|c| c.as_i64()).unwrap_or(0), |
238 | | - }); |
239 | | - } |
240 | | - } |
241 | | - out.push(Conv { turns, qa, dates }); |
242 | | - } |
243 | | - out |
244 | | -} |
245 | | - |
246 | 94 | // one end-to-end task: retrieval is done, only the (parallelizable) LLM |
247 | 95 | // reader+judge remain. |
248 | 96 | #[derive(Clone)] |
@@ -603,6 +451,7 @@ fn main() { |
603 | 451 | println!("| failed requests (fell back to fusion) | {} {} |", sum(DecisionRerankStats::failures), if codes.is_empty() { String::new() } else { format!("({codes})") }); |
604 | 452 | println!("| candidates sent / cache hits | {} / {} |", sum(DecisionRerankStats::candidates_sent), sum(DecisionRerankStats::cache_hits)); |
605 | 453 | println!("| input / output tokens (provider-reported) | {} / {} |", sum(DecisionRerankStats::input_tokens), sum(DecisionRerankStats::output_tokens)); |
| 454 | + println!("| cost (provider-reported) | ${:.4} |", sum(DecisionRerankStats::usd_micros) as f64 / 1e6); |
606 | 455 | println!("| summed request latency | {:.1} s |", sum(DecisionRerankStats::latency_ms) as f64 / 1000.0); |
607 | 456 | let samples: Vec<String> = stats.iter().flat_map(|s| s.failure_samples()).take(3).collect(); |
608 | 457 | for m in samples { |
|
0 commit comments