-
-
Notifications
You must be signed in to change notification settings - Fork 24.7k
feat: add matryoshka embedding truncation #6606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { EmbeddingsInterface } from '@langchain/core/embeddings' | ||
| import { applyMatryoshkaTruncation } from './matryoshkaEmbeddings' | ||
|
|
||
| const makeEmbeddings = (): EmbeddingsInterface<number[]> => ({ | ||
| embedDocuments: jest.fn(async () => [ | ||
| [1, 2, 3, 4], | ||
| [5, 6, 7, 8] | ||
| ]), | ||
| embedQuery: jest.fn(async () => [9, 10, 11, 12]) | ||
| }) | ||
|
|
||
| describe('applyMatryoshkaTruncation', () => { | ||
| it('returns the original embeddings when no truncate dimension is configured', async () => { | ||
| const embeddings = makeEmbeddings() | ||
| const result = applyMatryoshkaTruncation(embeddings, undefined) | ||
|
|
||
| expect(result).toBe(embeddings) | ||
| await expect(result.embedQuery('hello')).resolves.toEqual([9, 10, 11, 12]) | ||
| }) | ||
|
|
||
| it('truncates document and query embeddings to the configured dimension', async () => { | ||
| const embeddings = makeEmbeddings() | ||
| const result = applyMatryoshkaTruncation(embeddings, '2') | ||
|
|
||
| await expect(result.embedDocuments(['a', 'b'])).resolves.toEqual([ | ||
| [1, 2], | ||
| [5, 6] | ||
| ]) | ||
| await expect(result.embedQuery('hello')).resolves.toEqual([9, 10]) | ||
| }) | ||
|
|
||
| it('rejects invalid truncate dimensions', () => { | ||
| expect(() => applyMatryoshkaTruncation(makeEmbeddings(), '0')).toThrow('Matryoshka truncate dimension must be a positive integer') | ||
| expect(() => applyMatryoshkaTruncation(makeEmbeddings(), '2.5')).toThrow('Matryoshka truncate dimension must be a positive integer') | ||
| expect(() => applyMatryoshkaTruncation(makeEmbeddings(), 'abc')).toThrow('Matryoshka truncate dimension must be a positive integer') | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { EmbeddingsInterface } from '@langchain/core/embeddings' | ||
|
|
||
| export const MATRYOSHKA_TRUNCATE_DIMENSIONS = 'matryoshkaTruncateDimensions' | ||
|
|
||
| const parseTruncateDimension = (value: unknown): number | undefined => { | ||
| if (value === undefined || value === null || value === '') return undefined | ||
|
|
||
| const dimension = typeof value === 'number' ? value : Number(value) | ||
| if (!Number.isInteger(dimension) || dimension <= 0) { | ||
| throw new Error('Matryoshka truncate dimension must be a positive integer') | ||
| } | ||
|
|
||
| return dimension | ||
| } | ||
|
|
||
| const truncateVector = (vector: number[], dimension: number): number[] => vector.slice(0, dimension) | ||
|
|
||
| export const applyMatryoshkaTruncation = <T extends EmbeddingsInterface<number[]>>(embeddings: T, truncateDimensions: unknown): T => { | ||
| const dimension = parseTruncateDimension(truncateDimensions) | ||
| if (!dimension) return embeddings | ||
|
|
||
| const wrappedEmbeddings = Object.create(embeddings) as T | ||
|
|
||
| wrappedEmbeddings.embedDocuments = async (documents: string[]): Promise<number[][]> => { | ||
| const vectors = await embeddings.embedDocuments(documents) | ||
| return vectors.map((vector) => truncateVector(vector, dimension)) | ||
| } | ||
|
|
||
| wrappedEmbeddings.embedQuery = async (document: string): Promise<number[]> => { | ||
| const vector = await embeddings.embedQuery(document) | ||
| return truncateVector(vector, dimension) | ||
| } | ||
|
|
||
| return wrappedEmbeddings | ||
|
Comment on lines
+22
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Since the embedding instance is created fresh for each operation and is not shared globally, directly monkey-patching the methods on the original const originalEmbedDocuments = embeddings.embedDocuments.bind(embeddings)
const originalEmbedQuery = embeddings.embedQuery.bind(embeddings)
embeddings.embedDocuments = async (documents: string[]): Promise<number[][]> => {
const vectors = await originalEmbedDocuments(documents)
return vectors.map((vector) => truncateVector(vector, dimension))
}
embeddings.embedQuery = async (document: string): Promise<number[]> => {
const vector = await originalEmbedQuery(document)
return truncateVector(vector, dimension)
}
return embeddings |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the general rules, loose equality (
== null) should be used as a standard idiom for a 'nullish' check that covers bothnullandundefined.References
== null) as a standard idiom for a 'nullish' check that covers both 'null' and 'undefined'.