Embedding
embedding
Embedding backends + brute-force cosine neighbor search.
Two registered backends, ported from the dedup-detection POC
(find_duplicates_v7.py + trials/trial_unified.py):
tfidf: TF-IDF over code tokens (scikit-learn). Pure CPU, no torch.st: sentence-transformers MiniLM (all-MiniLM-L6-v2).
torch / sentence_transformers are base dependencies (AXM-2188): the
st backend is the default and runs in-process. The heavy model import is
still done lazily inside the st backend purely for first-call latency, so
the tfidf path stays light and never loads torch.
Neighbor search is an exact brute-force matmul (no ANN): under ~10^5 vectors a single normalized dot product beats an approximate index by 7-30x while staying exact. This calibration is closed (see ticket technical notes) and is not re-litigated here.
code_tokens(src)
Tokenize code text for the TF-IDF backend.
Lowercases identifiers, splits camelCase and snake_case into
sub-tokens, and appends structural keyword hints weighted by frequency
so control-flow shape contributes to the vector.
Source code in packages/axm-echo/src/axm_echo/embedding.py
embed(texts, *, backend='tfidf')
Embed texts into a dense matrix via the chosen backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Sequence[str]
|
Texts to embed (one row per text). |
required |
backend
|
Backend
|
|
'tfidf'
|
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
A |
NDArray[float64]
|
normalized; |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/axm-echo/src/axm_echo/embedding.py
neighbors(query, matrix, *, k=10, threshold=None)
Cosine top-k nearest rows to query (exact brute-force matmul).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
NDArray[float64]
|
A single query vector of shape |
required |
matrix
|
NDArray[float64]
|
Corpus matrix of shape |
required |
k
|
int
|
Maximum number of neighbors to return. |
10
|
threshold
|
float | None
|
If set, drop neighbors with cosine below this value. |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[int, float]]
|
A list of |
list[tuple[int, float]]
|
descending, length |
list[tuple[int, float]]
|
corpus; if |