Skip to content

OPENNLP-1937: Expose text embeddings as a generic SPI interface - #1290

Draft
krickert wants to merge 2 commits into
apache:mainfrom
ai-pipestream:OPENNLP-1937-text-embedder-api
Draft

krickert wants to merge 2 commits into
apache:mainfrom
ai-pipestream:OPENNLP-1937-text-embedder-api

Conversation

@krickert

@krickert krickert commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Adds opennlp.tools.embeddings.TextEmbedder to opennlp-api and makes SentenceVectorsDL implement it.

Core lacks a contract for "text in, vector out". WordVectorTable looks up a stored vector for one word, and SentenceVectorsDL exposes vectors only through the concrete class, so no API can accept an arbitrary embedder.

The interface: embed(CharSequence), a defaulted embedAll(List) for implementations that batch more efficiently, and dimension().

In SentenceVectorsDL:

  • embed adapts getVectors, which now also rejects null input.
  • embedAll buckets inputs by token count and runs one session per bucket, so a batch has no padding and each result matches what the single-input path would produce.
  • dimension() uses the model output metadata, falling back to a cached measurement when a model sets that value at run time.

SentenceVectorsDLEmbedderTest exercises this through a real ONNX session. The bundled graph is 373 bytes and computes output[b][t] = float(input_ids[b][t]) * [0.5, -1, 2], so expected vectors follow directly from the ids. The generator script is committed alongside. rat-excludes and .gitignore gain entries for the binary, since *.onnx is ignored repo-wide and would drop the fixture with no warning.

No experimental marker on the interface: it is a small contract over primitive arrays.

Motivation: a static embedding add-on is planned, and an add-on can only implement a contract present in a released core version. Otherwise add-ons each invent an embedder type and none interoperate.

Verification, offline, with checkstyle:

./mvnw -o -pl opennlp-api,opennlp-core/opennlp-ml/opennlp-dl -am test

opennlp-api 387 tests, opennlp-dl 93 tests, no failures, no checkstyle violations.

@krickert
krickert marked this pull request as ready for review September 8, 2026 12:51
@rzo1

rzo1 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

This needs to wait after M6.

@rzo1
rzo1 marked this pull request as draft September 8, 2026 12:59
krickert added a commit to ai-pipestream/opennlp that referenced this pull request Sep 9, 2026
OPENNLP-1937 (apache#1290) proposes this interface for opennlp-api without the
experimental marker. This branch had an earlier copy that still used it, so the
integration build hit an add/add conflict on the file and could resolve it
either way.

Take the upstream file as is. No behaviour changes: an annotation, an import,
and a javadoc paragraph.
@krickert
krickert marked this pull request as ready for review September 12, 2026 02:52
@krickert
krickert marked this pull request as draft September 14, 2026 02:23
@krickert

Copy link
Copy Markdown
Contributor Author

Putting this in draft. We should separate ONNX and CLI from the embedding library. ONNX can remain the default through an SPI, while downstream projects can exclude it and supply another implementation. Any native integration currently pulls in both unnecessarily. This would fix that concern.

I’ll include the split in this ticket. It should be a focused packaging change that preserves the default runtime behavior and makes add-ons easier to use. It's not a lot of work to do.

Select model factories by provider ID, retain ONNX as the default, and
allow independently owned models through the closeable TextEmbedder API.
Keep model initialization out of provider discovery and preserve the
existing SentenceVectorsDL inference path.

Tests cover multiple providers, missing and duplicate IDs, independent
model lifetimes, and ONNX output parity. The root model-path regression
first failed with NullPointerException; it now rejects the invalid path.
The SPI tests initially failed to compile before the API was introduced.

Validation: targeted Maven verify with dependencies, apache-rat:check,
and final path-validation regression tests passed.
@krickert krickert changed the title OPENNLP-1937: Expose text embeddings as a generic interface OPENNLP-1937: Expose text embeddings as a generic SPI interface Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants