-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
langchain[minor],docs[minor]: Add
MatryoshkaRetriever
(#4458)
* langchain[minor],docs[minor]: Add AdaptiveRetrieval to lc experimental * chore: lint files * cr * chore: lint files * cr * Update docs/core_docs/docs/modules/experimental/retrievers/matryoshka_retrieval.mdx * chore: lint files * Major improvements & fixes * tests * cr * cr * rm deno lock * fixed tests * rm build artifacts * chore: lint files * add note about stringified stuff * cleanup docs * chore: lint files * retrieval to retriever * docs file name retrieval to retriever * correct import * cr
- Loading branch information
1 parent
9490454
commit 36c03e4
Showing
18 changed files
with
493 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
48 changes: 48 additions & 0 deletions
48
docs/core_docs/docs/modules/data_connection/retrievers/matryoshka_retriever.mdx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Matryoshka Retriever | ||
|
||
This is an implementation of the [Supabase](https://supabase.com/) blog post | ||
["Matryoshka embeddings: faster OpenAI vector search using Adaptive Retrieval"](https://supabase.com/blog/matryoshka-embeddings). | ||
|
||
### Overview | ||
|
||
This class performs "Adaptive Retrieval" for searching text embeddings efficiently using the | ||
Matryoshka Representation Learning (MRL) technique. It retrieves documents similar to a query | ||
embedding in two steps: | ||
|
||
- **First-pass**: Uses a lower dimensional sub-vector from the MRL embedding for an initial, fast, | ||
but less accurate search. | ||
|
||
- **Second-pass**: Re-ranks the top results from the first pass using the full, high-dimensional | ||
embedding for higher accuracy. | ||
|
||
This code demonstrates using MRL embeddings for efficient vector search by combining faster, | ||
lower-dimensional initial search with accurate, high-dimensional re-ranking. | ||
|
||
## Example | ||
|
||
### Setup | ||
|
||
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; | ||
|
||
<IntegrationInstallTooltip></IntegrationInstallTooltip> | ||
|
||
```bash npm2yarn | ||
npm install @langchain/openai @langchain/community | ||
``` | ||
|
||
To follow the example below, you need an OpenAI API key: | ||
|
||
```bash | ||
export OPENAI_API_KEY=your-api-key | ||
``` | ||
|
||
We'll also be using `chroma` for our vector store. Follow the instructions [here](/docs/integrations/vectorstores/chroma) to setup. | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/retrievers/matryoshka_retriever.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> | ||
|
||
:::note | ||
Due to the constraints of some vector stores, the large embedding metadata field is stringified (`JSON.stringify`) before being stored. This means that the metadata field will need to be parsed (`JSON.parse`) when retrieved from the vector store. | ||
::: |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { MatryoshkaRetriever } from "langchain/retrievers/matryoshka_retriever"; | ||
import { Chroma } from "@langchain/community/vectorstores/chroma"; | ||
import { OpenAIEmbeddings } from "@langchain/openai"; | ||
import { Document } from "@langchain/core/documents"; | ||
import { faker } from "@faker-js/faker"; | ||
|
||
const smallEmbeddings = new OpenAIEmbeddings({ | ||
modelName: "text-embedding-3-small", | ||
dimensions: 512, // Min num for small | ||
}); | ||
const largeEmbeddings = new OpenAIEmbeddings({ | ||
modelName: "text-embedding-3-large", | ||
dimensions: 3072, // Max num for large | ||
}); | ||
|
||
const vectorStore = new Chroma(smallEmbeddings, { | ||
numDimensions: 512, | ||
}); | ||
|
||
const retriever = new MatryoshkaRetriever({ | ||
vectorStore, | ||
largeEmbeddingModel: largeEmbeddings, | ||
largeK: 5, | ||
}); | ||
|
||
const irrelevantDocs = Array.from({ length: 250 }).map( | ||
() => | ||
new Document({ | ||
pageContent: faker.lorem.word(7), // Similar length to the relevant docs | ||
}) | ||
); | ||
const relevantDocs = [ | ||
new Document({ | ||
pageContent: "LangChain is an open source github repo", | ||
}), | ||
new Document({ | ||
pageContent: "There are JS and PY versions of the LangChain github repos", | ||
}), | ||
new Document({ | ||
pageContent: "LangGraph is a new open source library by the LangChain team", | ||
}), | ||
new Document({ | ||
pageContent: "LangChain announced GA of LangSmith last week!", | ||
}), | ||
new Document({ | ||
pageContent: "I heart LangChain", | ||
}), | ||
]; | ||
const allDocs = [...irrelevantDocs, ...relevantDocs]; | ||
|
||
/** | ||
* IMPORTANT: | ||
* The `addDocuments` method on `MatryoshkaRetriever` will | ||
* generate the small AND large embeddings for all documents. | ||
*/ | ||
await retriever.addDocuments(allDocs); | ||
|
||
const query = "What is LangChain?"; | ||
const results = await retriever.getRelevantDocuments(query); | ||
console.log(results.map(({ pageContent }) => pageContent).join("\n")); | ||
/** | ||
I heart LangChain | ||
LangGraph is a new open source library by the LangChain team | ||
LangChain is an open source github repo | ||
LangChain announced GA of LangSmith last week! | ||
There are JS and PY versions of the LangChain github repos | ||
*/ |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.