@langchain/qdrant
Advanced tools
Comparing version 0.0.4 to 0.0.5
import { QdrantClient } from "@qdrant/js-client-rest"; | ||
import type { Schemas as QdrantSchemas } from "@qdrant/js-client-rest"; | ||
import type { EmbeddingsInterface } from "@langchain/core/embeddings"; | ||
import { VectorStore } from "@langchain/core/vectorstores"; | ||
import { type MaxMarginalRelevanceSearchOptions, VectorStore } from "@langchain/core/vectorstores"; | ||
import { Document } from "@langchain/core/documents"; | ||
@@ -75,2 +75,17 @@ /** | ||
/** | ||
* Return documents selected using the maximal marginal relevance. | ||
* Maximal marginal relevance optimizes for similarity to the query AND diversity | ||
* among selected documents. | ||
* | ||
* @param {string} query - Text to look up documents similar to. | ||
* @param {number} options.k - Number of documents to return. | ||
* @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm. Defaults to 20. | ||
* @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results, | ||
* where 0 corresponds to maximum diversity and 1 to minimum diversity. | ||
* @param {this["FilterType"]} options.filter - Optional filter to apply to the search results. | ||
* | ||
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance. | ||
*/ | ||
maxMarginalRelevanceSearch(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>): Promise<Document[]>; | ||
/** | ||
* Method to ensure the existence of a collection in the Qdrant database. | ||
@@ -77,0 +92,0 @@ * If the collection does not exist, it is created. |
import { QdrantClient } from "@qdrant/js-client-rest"; | ||
import { v4 as uuid } from "uuid"; | ||
import { VectorStore } from "@langchain/core/vectorstores"; | ||
import { VectorStore, } from "@langchain/core/vectorstores"; | ||
import { Document } from "@langchain/core/documents"; | ||
import { getEnvironmentVariable } from "@langchain/core/utils/env"; | ||
import { maximalMarginalRelevance } from "@langchain/core/utils/math"; | ||
const CONTENT_KEY = "content"; | ||
@@ -137,2 +138,4 @@ const METADATA_KEY = "metadata"; | ||
filter, | ||
with_payload: [this.metadataPayloadKey, this.contentPayloadKey], | ||
with_vector: false, | ||
}); | ||
@@ -150,2 +153,39 @@ const result = results.map((res) => [ | ||
/** | ||
* Return documents selected using the maximal marginal relevance. | ||
* Maximal marginal relevance optimizes for similarity to the query AND diversity | ||
* among selected documents. | ||
* | ||
* @param {string} query - Text to look up documents similar to. | ||
* @param {number} options.k - Number of documents to return. | ||
* @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm. Defaults to 20. | ||
* @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results, | ||
* where 0 corresponds to maximum diversity and 1 to minimum diversity. | ||
* @param {this["FilterType"]} options.filter - Optional filter to apply to the search results. | ||
* | ||
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance. | ||
*/ | ||
async maxMarginalRelevanceSearch(query, options) { | ||
if (!query) { | ||
return []; | ||
} | ||
const queryEmbedding = await this.embeddings.embedQuery(query); | ||
await this.ensureCollection(); | ||
const results = await this.client.search(this.collectionName, { | ||
vector: queryEmbedding, | ||
limit: options?.fetchK ?? 20, | ||
filter: options?.filter, | ||
with_payload: [this.metadataPayloadKey, this.contentPayloadKey], | ||
with_vector: true, | ||
}); | ||
const embeddingList = results.map((res) => res.vector); | ||
const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, options?.lambda, options.k); | ||
const topMmrMatches = mmrIndexes.map((idx) => results[idx]); | ||
const result = topMmrMatches.map((res) => new Document({ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
metadata: res.payload[this.metadataPayloadKey], | ||
pageContent: res.payload[this.contentPayloadKey], | ||
})); | ||
return result; | ||
} | ||
/** | ||
* Method to ensure the existence of a collection in the Qdrant database. | ||
@@ -152,0 +192,0 @@ * If the collection does not exist, it is created. |
{ | ||
"name": "@langchain/qdrant", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "LangChain.js integration for the Qdrant vector database", | ||
@@ -17,3 +17,4 @@ "type": "module", | ||
"scripts": { | ||
"build": "yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", | ||
"build": "yarn turbo:command build:internal --filter=@langchain/qdrant", | ||
"build:internal": "yarn lc-build:v2 --create-entrypoints --pre --tree-shaking", | ||
"build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rm -rf dist/tests dist/**/tests", | ||
@@ -27,3 +28,3 @@ "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rm -rf dist-cjs", | ||
"lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm", | ||
"clean": "rm -rf dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn lc-build --config ./langchain.config.js --create-entrypoints --pre", | ||
"clean": "rm -rf .turbo dist/", | ||
"prepack": "yarn build", | ||
@@ -43,3 +44,3 @@ "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", | ||
"dependencies": { | ||
"@langchain/core": ">0.1.0 <0.3.0", | ||
"@langchain/core": ">0.1.0 <0.3.0", | ||
"@qdrant/js-client-rest": "^1.9.0", | ||
@@ -51,3 +52,3 @@ "uuid": "^9.0.1" | ||
"@jest/globals": "^29.5.0", | ||
"@langchain/scripts": "~0.0", | ||
"@langchain/scripts": "~0.0.14", | ||
"@swc/core": "^1.3.90", | ||
@@ -54,0 +55,0 @@ "@swc/jest": "^0.2.29", |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
37911
668
1