Skip to main content

embed_key/
lib.rs

1//! # embed-key
2//!
3//! Deterministic 64-char cache key for an embedding request.
4//!
5//! Mixes the text with `(provider, model, dimensionality)` so a single
6//! shared cache can hold embeddings from multiple providers/models
7//! without false hits.
8//!
9//! ## Example
10//!
11//! ```
12//! use embed_key::key;
13//! let k = key("openai", "text-embedding-3-large", 3072, "hello world");
14//! assert_eq!(k.len(), 64);
15//! ```
16
17#![deny(missing_docs)]
18
19mod sha256;
20
21/// Build a 64-char hex cache key.
22pub fn key(provider: &str, model: &str, dim: usize, text: &str) -> String {
23    let mut buf = String::with_capacity(text.len() + 64);
24    buf.push_str("p=");
25    buf.push_str(provider);
26    buf.push('\n');
27    buf.push_str("m=");
28    buf.push_str(model);
29    buf.push('\n');
30    buf.push_str("d=");
31    buf.push_str(&dim.to_string());
32    buf.push('\n');
33    buf.push_str("t=");
34    buf.push_str(text);
35    sha256::hex(buf.as_bytes())
36}