feat(embedder): support native OpenAI embedding dimensions instead of truncating (#1087) - #1722
Open
Anai-Guo wants to merge 1 commit into
Open
feat(embedder): support native OpenAI embedding dimensions instead of truncating (#1087)#1722Anai-Guo wants to merge 1 commit into
Anai-Guo wants to merge 1 commit into
Conversation
… truncating OpenAIEmbedder sliced the returned vector to embedding_dim, which yields an un-renormalized (and thus lower-quality) embedding for text-embedding-3-* models. Add an opt-in dimensions config field that is forwarded to the OpenAI dimensions parameter so the API returns a natively reduced, renormalized vector. Defaults to None, preserving current behavior and support for models/endpoints that do not accept the parameter (e.g. ada-002, custom base_url). Related to getzep#1087
Author
|
Picking this up per @danielchalef's note on #1087 (the original reporter offered a PR ~8 months ago but none was filed). Kept it strictly opt-in so the default truncation behavior — and support for endpoints that don't accept |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds an opt-in
dimensionsfield toOpenAIEmbedderConfigsoOpenAIEmbeddercan request natively reduced embeddings via the OpenAIdimensionsparameter, instead of naively slicing the full vector.Related to #1087.
Why
Today
create/create_batchreturnresult.data[0].embedding[: self.config.embedding_dim]. Slicing atext-embedding-3-*vector drops the trailing components without renormalizing, so the resulting vector is no longer unit-length and cosine similarity degrades — the exact retrieval-quality issue raised in #1087 (most severe fortext-embedding-3-large, 3072 → 1024 = 66% dropped).The OpenAI models support generating the embedding at a target dimensionality server-side, which returns a properly normalized reduced vector. As @danielchalef noted on the issue, truncation is intentional for storage cost, so this is added as an opt-in rather than a behavior change.
Behavior
dimensions=None(default): unchanged — full vector requested and truncated toembedding_dim. Safe for models/endpoints that don't accept the parameter (e.g.text-embedding-ada-002, custombase_urldeployments).dimensions=N:Nis forwarded to the API and the natively sized, renormalized vector is returned without truncation. Users setting this should matchembedding_dimfor downstream storage consistency (documented on the field).Tests
Extends
tests/embedder/test_openai.py:dimensionsis not sent when unconfigured;createandcreate_batch.All four pass; the two new tests fail against the pre-change implementation, confirming they exercise the new path.
ruff checkandruff format --checkare clean.🤖 Generated with Claude Code