Class OnnxEmbeddingClient
java.lang.Object
org.snmp4j.mcp.tool.mib.semantic.OnnxEmbeddingClient
- All Implemented Interfaces:
AutoCloseable, EmbeddingClient
An
EmbeddingClient that runs sentence-transformers/all-MiniLM-L6-v2
locally via ONNX Runtime — no external API or internet connection required after the
model files have been downloaded and cached on first use (~23 MB, quantized).
On first construction the tokenizer (~700 KB) and quantized ONNX model (~23 MB) are
downloaded from HuggingFace and stored in
~/.cache/snmp4j-mcp/all-MiniLM-L6-v2/. Subsequent starts load from the cache.
The implementation is thread-safe: embed(String) synchronizes on the shared
OrtSession because ONNX Runtime sessions are not safe for concurrent access.
- Since:
- 0.1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intOutput vector dimensionality for "sentence-transformers/all-MiniLM-L6-v2".static final StringDefault sentence-transformer model identifier. -
Constructor Summary
ConstructorsConstructorDescriptionDownloads and caches the model on first use, then loads it into ONNX Runtime. -
Method Summary
Modifier and TypeMethodDescriptionList<float[]> batchEmbed(List<String> texts) Embeds multiple text chunks in one or more batched calls.voidclose()Closes the ONNX session and tokenizer, releasing native resources.static OnnxEmbeddingClientFactory method that returnsnull(rather than throwing) if ONNX Runtime or the HuggingFace tokenizer classes are absent, or if model download fails.float[]Embeds a single text chunk and returns a unit-length float vector.intGets the number of dimensions in every returned vector.Gets the model identifier for this embedding client.
-
Field Details
-
DEFAULT_MODEL
-
DEFAULT_DIMENSION
public static final int DEFAULT_DIMENSIONOutput vector dimensionality for "sentence-transformers/all-MiniLM-L6-v2".- See Also:
-
-
Constructor Details
-
OnnxEmbeddingClient
Downloads and caches the model on first use, then loads it into ONNX Runtime.- Throws:
IOException- if the model or tokenizer files cannot be downloaded or readai.onnxruntime.OrtException- if the ONNX Runtime session cannot be created
-
-
Method Details
-
embed
Embeds a single text chunk and returns a unit-length float vector.Synchronizes on
thisbecause the ONNX RuntimeOrtSessionis not thread-safe. Only passestoken_type_idsif the loaded model graph actually declares it as an input — some quantized exports omit it.- Specified by:
embedin interfaceEmbeddingClient- Parameters:
text- the text to embed (non-null, non-blank)- Returns:
- float vector of length
EmbeddingClient.getDimension() - Throws:
IOException- on transport or API errors
-
batchEmbed
Embeds multiple text chunks in one or more batched calls.The returned list has the same size and ordering as
texts. Callsembed(String)sequentially; ONNX Runtime does not support true batching for sentence transformers via this interface.- Specified by:
batchEmbedin interfaceEmbeddingClient- Parameters:
texts- the texts to embed- Returns:
- list of float vectors, one per input text
- Throws:
IOException- on transport or API errors
-
getDimension
public int getDimension()Description copied from interface:EmbeddingClientGets the number of dimensions in every returned vector.- Specified by:
getDimensionin interfaceEmbeddingClient- Returns:
- 384
-
getModelId
Description copied from interface:EmbeddingClientGets the model identifier for this embedding client.- Specified by:
getModelIdin interfaceEmbeddingClient- Returns:
- "sentence-transformers/all-MiniLM-L6-v2"
-
close
public void close()Closes the ONNX session and tokenizer, releasing native resources.- Specified by:
closein interfaceAutoCloseable
-
createIfAvailable
Factory method that returnsnull(rather than throwing) if ONNX Runtime or the HuggingFace tokenizer classes are absent, or if model download fails.- Returns:
- a ready-to-use client, or
nullif local ONNX embedding is unavailable
-