@langchain/pinecone
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -45,4 +45,133 @@ import { RecordMetadata, Index as PineconeIndex } from "@pinecone-database/pinecone"; | ||
/** | ||
* Class for managing and operating vector search applications with | ||
* Pinecone, the cloud-native high-scale vector database | ||
* Pinecone vector store integration. | ||
* | ||
* Setup: | ||
* Install `@langchain/pinecone` and `@pinecone-database/pinecone` to pass a client in. | ||
* | ||
* ```bash | ||
* npm install @langchain/pinecone @pinecone-database/pinecone | ||
* ``` | ||
* | ||
* ## [Constructor args](https://api.js.langchain.com/classes/_langchain_pinecone.PineconeStore.html#constructor) | ||
* | ||
* <details open> | ||
* <summary><strong>Instantiate</strong></summary> | ||
* | ||
* ```typescript | ||
* import { PineconeStore } from '@langchain/pinecone'; | ||
* // Or other embeddings | ||
* import { OpenAIEmbeddings } from '@langchain/openai'; | ||
* | ||
* import { Pinecone as PineconeClient } from "@pinecone-database/pinecone"; | ||
* | ||
* const pinecone = new PineconeClient(); | ||
* | ||
* // Will automatically read the PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars | ||
* const pineconeIndex = pinecone.Index(process.env.PINECONE_INDEX!); | ||
* | ||
* const embeddings = new OpenAIEmbeddings({ | ||
* model: "text-embedding-3-small", | ||
* }); | ||
* | ||
* const vectorStore = await PineconeStore.fromExistingIndex(embeddings, { | ||
* pineconeIndex, | ||
* // Maximum number of batch requests to allow at once. Each batch is 1000 vectors. | ||
* maxConcurrency: 5, | ||
* // You can pass a namespace here too | ||
* // namespace: "foo", | ||
* }); | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* <details> | ||
* <summary><strong>Add documents</strong></summary> | ||
* | ||
* ```typescript | ||
* import type { Document } from '@langchain/core/documents'; | ||
* | ||
* const document1 = { pageContent: "foo", metadata: { baz: "bar" } }; | ||
* const document2 = { pageContent: "thud", metadata: { bar: "baz" } }; | ||
* const document3 = { pageContent: "i will be deleted :(", metadata: {} }; | ||
* | ||
* const documents: Document[] = [document1, document2, document3]; | ||
* const ids = ["1", "2", "3"]; | ||
* await vectorStore.addDocuments(documents, { ids }); | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* <details> | ||
* <summary><strong>Delete documents</strong></summary> | ||
* | ||
* ```typescript | ||
* await vectorStore.delete({ ids: ["3"] }); | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* <details> | ||
* <summary><strong>Similarity search</strong></summary> | ||
* | ||
* ```typescript | ||
* const results = await vectorStore.similaritySearch("thud", 1); | ||
* for (const doc of results) { | ||
* console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`); | ||
* } | ||
* // Output: * thud [{"baz":"bar"}] | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* | ||
* <details> | ||
* <summary><strong>Similarity search with filter</strong></summary> | ||
* | ||
* ```typescript | ||
* const resultsWithFilter = await vectorStore.similaritySearch("thud", 1, { baz: "bar" }); | ||
* | ||
* for (const doc of resultsWithFilter) { | ||
* console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`); | ||
* } | ||
* // Output: * foo [{"baz":"bar"}] | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* | ||
* <details> | ||
* <summary><strong>Similarity search with score</strong></summary> | ||
* | ||
* ```typescript | ||
* const resultsWithScore = await vectorStore.similaritySearchWithScore("qux", 1); | ||
* for (const [doc, score] of resultsWithScore) { | ||
* console.log(`* [SIM=${score.toFixed(6)}] ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`); | ||
* } | ||
* // Output: * [SIM=0.000000] qux [{"bar":"baz","baz":"bar"}] | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* <details> | ||
* <summary><strong>As a retriever</strong></summary> | ||
* | ||
* ```typescript | ||
* const retriever = vectorStore.asRetriever({ | ||
* searchType: "mmr", // Leave blank for standard similarity search | ||
* k: 1, | ||
* }); | ||
* const resultAsRetriever = await retriever.invoke("thud"); | ||
* console.log(resultAsRetriever); | ||
* | ||
* // Output: [Document({ metadata: { "baz":"bar" }, pageContent: "thud" })] | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
*/ | ||
@@ -91,2 +220,8 @@ export declare class PineconeStore extends VectorStore { | ||
/** | ||
* Format the matching results from the Pinecone query. | ||
* @param matches Matching results from the Pinecone query. | ||
* @returns An array of arrays, where each inner array contains a document and its score. | ||
*/ | ||
private _formatMatches; | ||
/** | ||
* Method that performs a similarity search in the Pinecone database and | ||
@@ -93,0 +228,0 @@ * returns the results along with their scores. |
@@ -10,4 +10,133 @@ import * as uuid from "uuid"; | ||
/** | ||
* Class for managing and operating vector search applications with | ||
* Pinecone, the cloud-native high-scale vector database | ||
* Pinecone vector store integration. | ||
* | ||
* Setup: | ||
* Install `@langchain/pinecone` and `@pinecone-database/pinecone` to pass a client in. | ||
* | ||
* ```bash | ||
* npm install @langchain/pinecone @pinecone-database/pinecone | ||
* ``` | ||
* | ||
* ## [Constructor args](https://api.js.langchain.com/classes/_langchain_pinecone.PineconeStore.html#constructor) | ||
* | ||
* <details open> | ||
* <summary><strong>Instantiate</strong></summary> | ||
* | ||
* ```typescript | ||
* import { PineconeStore } from '@langchain/pinecone'; | ||
* // Or other embeddings | ||
* import { OpenAIEmbeddings } from '@langchain/openai'; | ||
* | ||
* import { Pinecone as PineconeClient } from "@pinecone-database/pinecone"; | ||
* | ||
* const pinecone = new PineconeClient(); | ||
* | ||
* // Will automatically read the PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars | ||
* const pineconeIndex = pinecone.Index(process.env.PINECONE_INDEX!); | ||
* | ||
* const embeddings = new OpenAIEmbeddings({ | ||
* model: "text-embedding-3-small", | ||
* }); | ||
* | ||
* const vectorStore = await PineconeStore.fromExistingIndex(embeddings, { | ||
* pineconeIndex, | ||
* // Maximum number of batch requests to allow at once. Each batch is 1000 vectors. | ||
* maxConcurrency: 5, | ||
* // You can pass a namespace here too | ||
* // namespace: "foo", | ||
* }); | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* <details> | ||
* <summary><strong>Add documents</strong></summary> | ||
* | ||
* ```typescript | ||
* import type { Document } from '@langchain/core/documents'; | ||
* | ||
* const document1 = { pageContent: "foo", metadata: { baz: "bar" } }; | ||
* const document2 = { pageContent: "thud", metadata: { bar: "baz" } }; | ||
* const document3 = { pageContent: "i will be deleted :(", metadata: {} }; | ||
* | ||
* const documents: Document[] = [document1, document2, document3]; | ||
* const ids = ["1", "2", "3"]; | ||
* await vectorStore.addDocuments(documents, { ids }); | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* <details> | ||
* <summary><strong>Delete documents</strong></summary> | ||
* | ||
* ```typescript | ||
* await vectorStore.delete({ ids: ["3"] }); | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* <details> | ||
* <summary><strong>Similarity search</strong></summary> | ||
* | ||
* ```typescript | ||
* const results = await vectorStore.similaritySearch("thud", 1); | ||
* for (const doc of results) { | ||
* console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`); | ||
* } | ||
* // Output: * thud [{"baz":"bar"}] | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* | ||
* <details> | ||
* <summary><strong>Similarity search with filter</strong></summary> | ||
* | ||
* ```typescript | ||
* const resultsWithFilter = await vectorStore.similaritySearch("thud", 1, { baz: "bar" }); | ||
* | ||
* for (const doc of resultsWithFilter) { | ||
* console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`); | ||
* } | ||
* // Output: * foo [{"baz":"bar"}] | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* | ||
* <details> | ||
* <summary><strong>Similarity search with score</strong></summary> | ||
* | ||
* ```typescript | ||
* const resultsWithScore = await vectorStore.similaritySearchWithScore("qux", 1); | ||
* for (const [doc, score] of resultsWithScore) { | ||
* console.log(`* [SIM=${score.toFixed(6)}] ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`); | ||
* } | ||
* // Output: * [SIM=0.000000] qux [{"bar":"baz","baz":"bar"}] | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
* | ||
* <details> | ||
* <summary><strong>As a retriever</strong></summary> | ||
* | ||
* ```typescript | ||
* const retriever = vectorStore.asRetriever({ | ||
* searchType: "mmr", // Leave blank for standard similarity search | ||
* k: 1, | ||
* }); | ||
* const resultAsRetriever = await retriever.invoke("thud"); | ||
* console.log(resultAsRetriever); | ||
* | ||
* // Output: [Document({ metadata: { "baz":"bar" }, pageContent: "thud" })] | ||
* ``` | ||
* </details> | ||
* | ||
* <br /> | ||
*/ | ||
@@ -186,2 +315,26 @@ export class PineconeStore extends VectorStore { | ||
/** | ||
* Format the matching results from the Pinecone query. | ||
* @param matches Matching results from the Pinecone query. | ||
* @returns An array of arrays, where each inner array contains a document and its score. | ||
*/ | ||
_formatMatches(matches = []) { | ||
const documentsWithScores = []; | ||
for (const record of matches) { | ||
const { id, score, metadata: { [this.textKey]: pageContent, ...metadata } = { | ||
[this.textKey]: "", | ||
}, } = record; | ||
if (score) { | ||
documentsWithScores.push([ | ||
new Document({ | ||
id, | ||
pageContent: pageContent.toString(), | ||
metadata, | ||
}), | ||
score, | ||
]); | ||
} | ||
} | ||
return documentsWithScores; | ||
} | ||
/** | ||
* Method that performs a similarity search in the Pinecone database and | ||
@@ -195,14 +348,5 @@ * returns the results along with their scores. | ||
async similaritySearchVectorWithScore(query, k, filter) { | ||
const results = await this._runPineconeQuery(query, k, filter); | ||
const result = []; | ||
if (results.matches) { | ||
for (const res of results.matches) { | ||
const { [this.textKey]: pageContent, ...metadata } = (res.metadata ?? | ||
{}); | ||
if (res.score) { | ||
result.push([new Document({ metadata, pageContent }), res.score]); | ||
} | ||
} | ||
} | ||
return result; | ||
const { matches = [] } = await this._runPineconeQuery(query, k, filter); | ||
const records = this._formatMatches(matches); | ||
return records; | ||
} | ||
@@ -226,15 +370,8 @@ /** | ||
const results = await this._runPineconeQuery(queryEmbedding, options.fetchK ?? 20, options.filter, { includeValues: true }); | ||
const matches = results?.matches ?? []; | ||
const { matches = [] } = results; | ||
const embeddingList = matches.map((match) => match.values); | ||
const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, options.lambda, options.k); | ||
const topMmrMatches = mmrIndexes.map((idx) => matches[idx]); | ||
const finalResult = []; | ||
for (const res of topMmrMatches) { | ||
const { [this.textKey]: pageContent, ...metadata } = (res.metadata ?? | ||
{}); | ||
if (res.score) { | ||
finalResult.push(new Document({ metadata, pageContent })); | ||
} | ||
} | ||
return finalResult; | ||
const records = this._formatMatches(topMmrMatches); | ||
return records.map(([doc, _score]) => doc); | ||
} | ||
@@ -241,0 +378,0 @@ /** |
{ | ||
"name": "@langchain/pinecone", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "LangChain integration for Pinecone's vector database", | ||
@@ -18,3 +18,3 @@ "type": "module", | ||
"build": "yarn turbo:command build:internal --filter=@langchain/pinecone", | ||
"build:internal": "yarn lc-build:v2 --create-entrypoints --pre --tree-shaking", | ||
"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", | ||
@@ -52,3 +52,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", | ||
"@langchain/openai": "workspace:*", | ||
"@langchain/scripts": "~0.0.14", | ||
"@langchain/scripts": "~0.0.20", | ||
"@swc/core": "^1.3.90", | ||
@@ -71,3 +71,3 @@ "@swc/jest": "^0.2.29", | ||
"prettier": "^2.8.3", | ||
"release-it": "^15.10.1", | ||
"release-it": "^17.6.0", | ||
"rollup": "^4.5.2", | ||
@@ -74,0 +74,0 @@ "ts-jest": "^29.1.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55372
1288