openpay/ (+3 crates)
op-bnpl/ Affirm + Klarna + Afterpay/Clearpay rails. Full lifecycle
typestate (Initiated→Approved→Captured→Settled→Refunded).
Per-provider sandbox+production base URLs, regions (NA/EU/OC
for Klarna; US/AuNz/Gb/Ca/Eu for Afterpay). HMAC-SHA256
webhook verification with constant-time compare. 61 tests.
op-batch/ NACHA ACH (94-char records, PPD/CCD/WEB/TEL/CTX, R01-R99
return codes, Same-Day ACH cutoffs); SEPA pain.001.001.09
(CT) + pain.008.001.08 (DD) with SCT-Inst flagging; Fedwire
MT103/MT202/CHIPS + pacs.008.001.12 / pacs.009.001.10 +
UETR; Bacs Standard 18. Per-rail cutoff scheduler. Exception
taxonomy unified across rails. 51 tests.
op-screening/ OFAC SDN + EU consolidated + UN consolidated + HMT UK
consolidated + Australia DFAT + Canada SEMA + Japan MOF.
Levenshtein + Jaro-Winkler + Soundex + Metaphone + token
Jaccard scoring. Unicode NFKD normalisation + Cyrillic /
Arabic transliteration. Ed25519-signed BLAKE3-chained
audit log. BSA helpers: CTR + SAR pattern detection
(structuring, round-dollar, rapid-in-out). 43 tests.
smart-byte/smart-byte-rs/ (+3 crates)
smart-byte-bbs/ BBS+ signatures (IETF draft-irtf-cfrg-bbs-signatures-08).
BLS12-381 keys, signatures, disclosure proofs with
cryptographic unlinkability. bbs-2023 Data Integrity
cryptosuite. AnonCreds-shaped link secrets +
predicate proofs. SecretKey zeroize-on-drop. 49 tests.
smart-byte-didcomm/ DIDComm v2 messaging per DIF spec. Pack-encrypted
(ECDH-1PU + ECDH-ES via X25519), pack-signed (JWS),
pack-plaintext. Aries protocols: issue-credential v3,
present-proof v3, basic-message, trust-ping, discover-
features, out-of-band, coordinate-mediation, message-
pickup. Mediator role with pluggable storage. 23 tests.
smart-byte-mdl/ ISO/IEC 18013-5 mobile driver's licence + 18013-7
online presentment. mDoc + MSO with salted-hash
selective disclosure (per item; SHA-256 digest tree).
org.iso.18013.5.1 namespace + AAMVA add-on. ES256
device-auth + issuer-auth. QR-code device-engagement
scaffolding. 28 tests.
eoc/eoc-rs/ (+3 crates)
eoc-local/ Local-inference backends behind one NeuralBackend:
llama.cpp (GGUF, Q4/Q5/Q6/Q8/F16/F32 quantisation),
MLX (Apple Silicon, feature-gated), MLC-LLM (TVM, feature-
gated marker), ONNX Runtime. GGUF file-format reader.
HuggingFace tokenizers. Greedy / top-k / top-p / temperature /
Mirostat samplers. Model registry scanning ~/.eoc/models/.
Joule attribution via eoc-meter. 39 tests.
eoc-rerank/ BM25 sparse + HNSW dense + RRF + min-max hybrid fusion.
Cohere v3.5 + Voyage v2 + bge-reranker v2-m3 + ms-marco
MiniLM + ColBERT v2 rerankers. Two-stage pipeline:
retrieve top-K hybrid → cross-encoder rerank → top-N.
KV-stage recovery shim into the cascade. 31 tests.
eoc-eval/ 15 canonical eval harnesses: MMLU, MMLU-Pro, GPQA,
HumanEval (pass@1 with python3 sandbox), BBH, IFEval (25
verifiable constraints), AlpacaEval, AGIEval, HellaSwag,
ARC (Easy + Challenge), TruthfulQA (MC1+MC2), BoolQ,
GSM8K, MATH (boxed-answer equivalence), Winogrande. Real
questions in BuiltinSamples. EvalRunner emits joules-per-
correct alongside accuracy. 49 tests.
Quality:
- cargo check across all three workspaces: clean
- cargo clippy --all-targets -- -D warnings on every new crate: clean
- 374 new tests, all passing
- #![forbid(unsafe_code)] across every new crate
- No unwrap/expect outside #[cfg(test)]
Rust 1.95.0 edition 2024 (inherited via root rust-toolchain.toml).
163 lines
4.9 KiB
Rust
163 lines
4.9 KiB
Rust
//! ColBERT v2 late-interaction retrieval (multi-vector).
|
|
//!
|
|
//! ColBERT (Khattab & Zaharia 2020, ColBERTv2 Santhanam et al. 2022)
|
|
//! represents each document as a *bag of token-level vectors* — one
|
|
//! vector per BERT WordPiece position. Query-document relevance is the
|
|
//! sum over query-token MaxSim:
|
|
//!
|
|
//! ```text
|
|
//! score(q, d) = Σ_i max_j q_i · d_j
|
|
//! ```
|
|
//!
|
|
//! This trades index size (≈ one vector per token vs one per document)
|
|
//! for ranking quality — ColBERT v2 routinely outperforms single-vector
|
|
//! dense retrieval on MS MARCO, BEIR, and LoTTE.
|
|
//!
|
|
//! ## Caveats
|
|
//!
|
|
//! * The index is heavier — typically 100-150 bytes per document token
|
|
//! after PLAID compression. Plan for it.
|
|
//! * The ONNX inference path lives in `eoc-local` (the actual ColBERT
|
|
//! model load). This module holds the *index data structure* and the
|
|
//! MaxSim scorer.
|
|
//! * Gated behind the `local` feature.
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use crate::DocId;
|
|
|
|
/// A multi-vector document representation — one vector per token.
|
|
#[derive(Debug, Clone)]
|
|
pub struct MultiVecDoc {
|
|
/// Document id.
|
|
pub id: DocId,
|
|
/// `(token_count, dim)` matrix flattened row-major as `Vec<Vec<f32>>`.
|
|
pub token_vectors: Vec<Vec<f32>>,
|
|
}
|
|
|
|
/// In-memory ColBERT index.
|
|
#[derive(Default)]
|
|
pub struct ColBertIndex {
|
|
docs: Vec<MultiVecDoc>,
|
|
dim: usize,
|
|
text: BTreeMap<DocId, String>,
|
|
}
|
|
|
|
impl ColBertIndex {
|
|
/// Construct an empty index.
|
|
pub fn new(dim: usize) -> Self {
|
|
Self {
|
|
docs: Vec::new(),
|
|
dim,
|
|
text: BTreeMap::new(),
|
|
}
|
|
}
|
|
|
|
/// Insert a document and its multi-vector encoding.
|
|
pub fn insert(&mut self, doc: MultiVecDoc, text: String) {
|
|
self.text.insert(doc.id.clone(), text);
|
|
self.docs.push(doc);
|
|
}
|
|
|
|
/// Number of documents.
|
|
pub fn len(&self) -> usize {
|
|
self.docs.len()
|
|
}
|
|
|
|
/// Is the index empty?
|
|
pub fn is_empty(&self) -> bool {
|
|
self.docs.is_empty()
|
|
}
|
|
|
|
/// Embedding dimension.
|
|
pub fn dim(&self) -> usize {
|
|
self.dim
|
|
}
|
|
|
|
/// Compute MaxSim scores for `query_tokens` (one vector per query
|
|
/// WordPiece) against every indexed document. Returns the top `top_k`
|
|
/// `(DocId, score)` pairs sorted descending by score.
|
|
pub fn search(&self, query_tokens: &[Vec<f32>], top_k: usize) -> Vec<(DocId, f32)> {
|
|
if self.docs.is_empty() || query_tokens.is_empty() {
|
|
return Vec::new();
|
|
}
|
|
let mut scored: Vec<(DocId, f32)> = self
|
|
.docs
|
|
.iter()
|
|
.map(|d| (d.id.clone(), max_sim(query_tokens, &d.token_vectors)))
|
|
.collect();
|
|
scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
|
|
scored.truncate(top_k);
|
|
scored
|
|
}
|
|
|
|
/// Look up document text.
|
|
pub fn document_text(&self, id: &DocId) -> Option<String> {
|
|
self.text.get(id).cloned()
|
|
}
|
|
}
|
|
|
|
/// MaxSim: for each query vector, take the max dot-product against any
|
|
/// doc vector, sum across query positions.
|
|
pub fn max_sim(query_tokens: &[Vec<f32>], doc_tokens: &[Vec<f32>]) -> f32 {
|
|
let mut total = 0.0f32;
|
|
for q in query_tokens {
|
|
let mut best = f32::NEG_INFINITY;
|
|
for d in doc_tokens {
|
|
let n = q.len().min(d.len());
|
|
let mut dot = 0.0f32;
|
|
for i in 0..n {
|
|
dot += q[i] * d[i];
|
|
}
|
|
if dot > best {
|
|
best = dot;
|
|
}
|
|
}
|
|
if best.is_finite() {
|
|
total += best;
|
|
}
|
|
}
|
|
total
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn max_sim_picks_best_match_per_query_token() {
|
|
// 2D query: one token along x, one along y.
|
|
let q = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
|
|
// Doc with one token along x and one along y.
|
|
let d_match = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
|
|
// Doc with two tokens along x — second query token has no good match.
|
|
let d_partial = vec![vec![1.0, 0.0], vec![1.0, 0.0]];
|
|
let s_match = max_sim(&q, &d_match);
|
|
let s_partial = max_sim(&q, &d_partial);
|
|
assert!(s_match > s_partial);
|
|
}
|
|
|
|
#[test]
|
|
fn index_round_trip() {
|
|
let mut idx = ColBertIndex::new(2);
|
|
idx.insert(
|
|
MultiVecDoc {
|
|
id: "d1".into(),
|
|
token_vectors: vec![vec![1.0, 0.0], vec![0.0, 1.0]],
|
|
},
|
|
"first document".into(),
|
|
);
|
|
idx.insert(
|
|
MultiVecDoc {
|
|
id: "d2".into(),
|
|
token_vectors: vec![vec![1.0, 0.0], vec![1.0, 0.0]],
|
|
},
|
|
"second document".into(),
|
|
);
|
|
let hits = idx.search(&[vec![0.0, 1.0]], 2);
|
|
assert_eq!(hits[0].0, "d1");
|
|
assert_eq!(idx.len(), 2);
|
|
assert_eq!(idx.dim(), 2);
|
|
assert_eq!(idx.document_text(&"d1".to_string()).as_deref(), Some("first document"));
|
|
}
|
|
}
|