Skip to main content

Module utils

Module utils 

Source

Enums§

SanitizeAction
Sanitize result: allow / redact (content cleaned) / discard (reject write).

Functions§

content_hash
cosine_similarity
Cosine similarity between two equal-length slices. Returns 0.0 on zero norms. Single-pass fold: computes dot product and both norms in one traversal.
dot_product
Dot product of two equal-length slices. For unit vectors this equals the cosine similarity.
estimate_tokens
Rough token estimate: 1 token ≈ 4 chars.
gen_uuid
hex
Lowercase hex encoding of a byte slice. Replaces the old format!("{:x}", …) over a digest output: RustCrypto digest 0.11 returns hybrid_array::Array, which no longer implements LowerHex.
l2_normalize
In-place L2 normalisation. Zero vectors are left unchanged (all zeros). Pre-normalising stored vectors lets vector search reduce cosine similarity to a plain dot product in its O(N) inner loop.
pack_embedding
Pack a Vec into bytes (little-endian f32 array).
sanitize
Public sanitize function used by KnowledgeBase (§二·六). Returns (cleaned_content, action).
unpack_embedding
Unpack bytes into Vec.
utc_now_iso