-
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.
Adds search type to vectorstore retrievers for MMR, adds docs (#2170)
* Refactored MongoDBAtlasVectorSearch to use abstract similaritySearchVectorWithScore for MMR Co-authored-by: Simone DM <simone.dellamarca@mongodb.com> * Fixed formatting * Fixed formatting * Fixed formatting * Added optional maxMarginalRelevanceSearch method to base vectorstore * Change max marginal relevance args * Docs * Adds search type to vectorstore retrievers, docs * Update docs * Docs * Add back in mongo * Format * Fix bug * Fix unit test * Fix bug --------- Co-authored-by: archie-swif <artem.ryabokon@gmail.com> Co-authored-by: Simone DM <simone.dellamarca@mongodb.com>
- Loading branch information
1 parent
96c9fe5
commit ac0e6c6
Showing
9 changed files
with
173 additions
and
157 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
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,37 @@ | ||
import { MongoDBAtlasVectorSearch } from "langchain/vectorstores/mongodb_atlas"; | ||
import { CohereEmbeddings } from "langchain/embeddings/cohere"; | ||
import { MongoClient } from "mongodb"; | ||
|
||
const client = new MongoClient(process.env.MONGODB_ATLAS_URI || ""); | ||
const namespace = "langchain.test"; | ||
const [dbName, collectionName] = namespace.split("."); | ||
const collection = client.db(dbName).collection(collectionName); | ||
|
||
const vectorStore = new MongoDBAtlasVectorSearch(new CohereEmbeddings(), { | ||
collection, | ||
indexName: "default", // The name of the Atlas search index. Defaults to "default" | ||
textKey: "text", // The name of the collection field containing the raw content. Defaults to "text" | ||
embeddingKey: "embedding", // The name of the collection field containing the embedded text. Defaults to "embedding" | ||
}); | ||
|
||
const resultOne = await vectorStore.maxMarginalRelevanceSearch("Hello world", { | ||
k: 4, | ||
fetchK: 20, // The number of documents to return on initial fetch | ||
}); | ||
console.log(resultOne); | ||
|
||
// Using MMR in a vector store retriever | ||
|
||
const retriever = await vectorStore.asRetriever({ | ||
searchType: "mmr", | ||
searchKwargs: { | ||
fetchK: 20, | ||
lambda: 0.1, | ||
}, | ||
}); | ||
|
||
const retrieverOutput = await retriever.getRelevantDocuments("Hello world"); | ||
|
||
console.log(retrieverOutput); | ||
|
||
await client.close(); |
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.
ac0e6c6
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.
Successfully deployed to the following URLs:
langchainjs-docs – ./
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-langchain.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
js.langchain.com