Class OnnxEmbeddingClient

java.lang.Object
org.snmp4j.mcp.tool.mib.semantic.OnnxEmbeddingClient
All Implemented Interfaces:
AutoCloseable, EmbeddingClient

public class OnnxEmbeddingClient extends Object implements EmbeddingClient, AutoCloseable
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 Details

  • Constructor Details

    • OnnxEmbeddingClient

      public OnnxEmbeddingClient() throws IOException, ai.onnxruntime.OrtException
      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 read
      ai.onnxruntime.OrtException - if the ONNX Runtime session cannot be created
  • Method Details

    • embed

      public float[] embed(String text) throws IOException
      Embeds a single text chunk and returns a unit-length float vector.

      Synchronizes on this because the ONNX Runtime OrtSession is not thread-safe. Only passes token_type_ids if the loaded model graph actually declares it as an input — some quantized exports omit it.

      Specified by:
      embed in interface EmbeddingClient
      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

      public List<float[]> batchEmbed(List<String> texts) throws IOException
      Embeds multiple text chunks in one or more batched calls.

      The returned list has the same size and ordering as texts. Calls embed(String) sequentially; ONNX Runtime does not support true batching for sentence transformers via this interface.

      Specified by:
      batchEmbed in interface EmbeddingClient
      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: EmbeddingClient
      Gets the number of dimensions in every returned vector.
      Specified by:
      getDimension in interface EmbeddingClient
      Returns:
      384
    • getModelId

      public String getModelId()
      Description copied from interface: EmbeddingClient
      Gets the model identifier for this embedding client.
      Specified by:
      getModelId in interface EmbeddingClient
      Returns:
      "sentence-transformers/all-MiniLM-L6-v2"
    • close

      public void close()
      Closes the ONNX session and tokenizer, releasing native resources.
      Specified by:
      close in interface AutoCloseable
    • createIfAvailable

      public static OnnxEmbeddingClient createIfAvailable()
      Factory method that returns null (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 null if local ONNX embedding is unavailable