@langchain/core
Advanced tools
Comparing version 0.3.17 to 0.3.18
@@ -160,32 +160,4 @@ import { AgentAction, AgentFinish } from "../agents.js"; | ||
/** | ||
* @example | ||
* ```typescript | ||
* const prompt = PromptTemplate.fromTemplate(`What is the answer to {question}?`); | ||
* | ||
* // Example of using LLMChain to process a series of questions | ||
* const chain = new LLMChain({ | ||
* llm: new ChatOpenAI({ temperature: 0.9 }), | ||
* prompt, | ||
* }); | ||
* | ||
* // Process questions using the chain | ||
* const processQuestions = async (questions) => { | ||
* for (const question of questions) { | ||
* const result = await chain.call({ question }); | ||
* console.log(result); | ||
* } | ||
* }; | ||
* | ||
* // Example questions | ||
* const questions = [ | ||
* "What is your name?", | ||
* "What is your quest?", | ||
* "What is your favorite color?", | ||
* ]; | ||
* | ||
* // Run the example | ||
const logFunction = handler.raiseError ? console.error : console.warn; | ||
* processQuestions(questions).catch(consolelogFunction; | ||
* | ||
* ``` | ||
* @deprecated Use [`traceable`](https://docs.smith.langchain.com/observability/how_to_guides/tracing/annotate_code) | ||
* from "langsmith" instead. | ||
*/ | ||
@@ -192,0 +164,0 @@ export declare class TraceGroup { |
@@ -780,32 +780,4 @@ import { v4 as uuidv4 } from "uuid"; | ||
/** | ||
* @example | ||
* ```typescript | ||
* const prompt = PromptTemplate.fromTemplate(`What is the answer to {question}?`); | ||
* | ||
* // Example of using LLMChain to process a series of questions | ||
* const chain = new LLMChain({ | ||
* llm: new ChatOpenAI({ temperature: 0.9 }), | ||
* prompt, | ||
* }); | ||
* | ||
* // Process questions using the chain | ||
* const processQuestions = async (questions) => { | ||
* for (const question of questions) { | ||
* const result = await chain.call({ question }); | ||
* console.log(result); | ||
* } | ||
* }; | ||
* | ||
* // Example questions | ||
* const questions = [ | ||
* "What is your name?", | ||
* "What is your quest?", | ||
* "What is your favorite color?", | ||
* ]; | ||
* | ||
* // Run the example | ||
const logFunction = handler.raiseError ? console.error : console.warn; | ||
* processQuestions(questions).catch(consolelogFunction; | ||
* | ||
* ``` | ||
* @deprecated Use [`traceable`](https://docs.smith.langchain.com/observability/how_to_guides/tracing/annotate_code) | ||
* from "langsmith" instead. | ||
*/ | ||
@@ -812,0 +784,0 @@ export class TraceGroup { |
@@ -1,11 +0,2 @@ | ||
/** | ||
* Consume a promise, either adding it to the queue or waiting for it to resolve | ||
* @param promiseFn Promise to consume | ||
* @param wait Whether to wait for the promise to resolve or resolve immediately | ||
*/ | ||
export declare function consumeCallback<T>(promiseFn: () => Promise<T> | T | void, wait: boolean): Promise<void>; | ||
/** | ||
* Waits for all promises in the queue to resolve. If the queue is | ||
* undefined, it immediately resolves a promise. | ||
*/ | ||
export declare function awaitAllCallbacks(): Promise<void>; | ||
import { awaitAllCallbacks, consumeCallback } from "../singletons/callbacks.js"; | ||
export { awaitAllCallbacks, consumeCallback }; |
@@ -1,37 +0,2 @@ | ||
import PQueueMod from "p-queue"; | ||
let queue; | ||
/** | ||
* Creates a queue using the p-queue library. The queue is configured to | ||
* auto-start and has a concurrency of 1, meaning it will process tasks | ||
* one at a time. | ||
*/ | ||
function createQueue() { | ||
const PQueue = "default" in PQueueMod ? PQueueMod.default : PQueueMod; | ||
return new PQueue({ | ||
autoStart: true, | ||
concurrency: 1, | ||
}); | ||
} | ||
/** | ||
* Consume a promise, either adding it to the queue or waiting for it to resolve | ||
* @param promiseFn Promise to consume | ||
* @param wait Whether to wait for the promise to resolve or resolve immediately | ||
*/ | ||
export async function consumeCallback(promiseFn, wait) { | ||
if (wait === true) { | ||
await promiseFn(); | ||
} | ||
else { | ||
if (typeof queue === "undefined") { | ||
queue = createQueue(); | ||
} | ||
void queue.add(promiseFn); | ||
} | ||
} | ||
/** | ||
* Waits for all promises in the queue to resolve. If the queue is | ||
* undefined, it immediately resolves a promise. | ||
*/ | ||
export function awaitAllCallbacks() { | ||
return typeof queue !== "undefined" ? queue.onIdle() : Promise.resolve(); | ||
} | ||
import { awaitAllCallbacks, consumeCallback } from "../singletons/callbacks.js"; | ||
export { awaitAllCallbacks, consumeCallback }; |
@@ -110,2 +110,4 @@ import type { TiktokenModel } from "js-tiktoken/lite"; | ||
includeRaw?: IncludeRaw; | ||
/** Whether to use strict mode. Currently only supported by OpenAI models. */ | ||
strict?: boolean; | ||
}; | ||
@@ -112,0 +114,0 @@ /** @deprecated Use StructuredOutputMethodOptions instead */ |
@@ -465,2 +465,5 @@ import { zodToJsonSchema } from "zod-to-json-schema"; | ||
} | ||
if (config?.strict) { | ||
throw new Error(`"strict" mode is not supported for this model by default.`); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
@@ -467,0 +470,0 @@ const schema = outputSchema; |
@@ -6,3 +6,20 @@ import { BaseCallbackConfig, CallbackManagerForRetrieverRun, Callbacks } from "../callbacks/manager.js"; | ||
/** | ||
* Base Retriever class. All indexes should extend this class. | ||
* Input configuration options for initializing a retriever that extends | ||
* the `BaseRetriever` class. This interface provides base properties | ||
* common to all retrievers, allowing customization of callback functions, | ||
* tagging, metadata, and logging verbosity. | ||
* | ||
* Fields: | ||
* - `callbacks` (optional): An array of callback functions that handle various | ||
* events during retrieval, such as logging, error handling, or progress updates. | ||
* | ||
* - `tags` (optional): An array of strings used to add contextual tags to | ||
* retrieval operations, allowing for easier categorization and tracking. | ||
* | ||
* - `metadata` (optional): A record of key-value pairs to store additional | ||
* contextual information for retrieval operations, which can be useful | ||
* for logging or auditing purposes. | ||
* | ||
* - `verbose` (optional): A boolean flag that, if set to `true`, enables | ||
* detailed logging and output during the retrieval process. Defaults to `false`. | ||
*/ | ||
@@ -15,15 +32,61 @@ export interface BaseRetrieverInput { | ||
} | ||
/** | ||
* Interface for a base retriever that defines core functionality for | ||
* retrieving relevant documents from a source based on a query. | ||
* | ||
* The `BaseRetrieverInterface` standardizes the `getRelevantDocuments` method, | ||
* enabling retrieval of documents that match the query criteria. | ||
* | ||
* @template Metadata - The type of metadata associated with each document, | ||
* defaulting to `Record<string, any>`. | ||
*/ | ||
export interface BaseRetrieverInterface<Metadata extends Record<string, any> = Record<string, any>> extends RunnableInterface<string, DocumentInterface<Metadata>[]> { | ||
/** | ||
* Retrieves documents relevant to a given query, allowing optional | ||
* configurations for customization. | ||
* | ||
* @param query - A string representing the query to search for relevant documents. | ||
* @param config - (optional) Configuration options for the retrieval process, | ||
* which may include callbacks and additional context settings. | ||
* @returns A promise that resolves to an array of `DocumentInterface` instances, | ||
* each containing metadata specified by the `Metadata` type parameter. | ||
*/ | ||
getRelevantDocuments(query: string, config?: Callbacks | BaseCallbackConfig): Promise<DocumentInterface<Metadata>[]>; | ||
} | ||
/** | ||
* Abstract base class for a Document retrieval system. A retrieval system | ||
* is defined as something that can take string queries and return the | ||
* most 'relevant' Documents from some source. | ||
* Abstract base class for a document retrieval system, designed to | ||
* process string queries and return the most relevant documents from a source. | ||
* | ||
* `BaseRetriever` provides common properties and methods for derived retrievers, | ||
* such as callbacks, tagging, and verbose logging. Custom retrieval systems | ||
* should extend this class and implement `_getRelevantDocuments` to define | ||
* the specific retrieval logic. | ||
* | ||
* @template Metadata - The type of metadata associated with each document, | ||
* defaulting to `Record<string, any>`. | ||
*/ | ||
export declare abstract class BaseRetriever<Metadata extends Record<string, any> = Record<string, any>> extends Runnable<string, DocumentInterface<Metadata>[]> implements BaseRetrieverInterface { | ||
/** | ||
* Optional callbacks to handle various events in the retrieval process. | ||
*/ | ||
callbacks?: Callbacks; | ||
/** | ||
* Tags to label or categorize the retrieval operation. | ||
*/ | ||
tags?: string[]; | ||
/** | ||
* Metadata to provide additional context or information about the retrieval | ||
* operation. | ||
*/ | ||
metadata?: Record<string, unknown>; | ||
/** | ||
* If set to `true`, enables verbose logging for the retrieval process. | ||
*/ | ||
verbose?: boolean; | ||
/** | ||
* Constructs a new `BaseRetriever` instance with optional configuration fields. | ||
* | ||
* @param fields - Optional input configuration that can include `callbacks`, | ||
* `tags`, `metadata`, and `verbose` settings for custom retriever behavior. | ||
*/ | ||
constructor(fields?: BaseRetrieverInput); | ||
@@ -35,3 +98,26 @@ /** | ||
*/ | ||
/** | ||
* Placeholder method for retrieving relevant documents based on a query. | ||
* | ||
* This method is intended to be implemented by subclasses and will be | ||
* converted to an abstract method in the next major release. Currently, it | ||
* throws an error if not implemented, ensuring that custom retrievers define | ||
* the specific retrieval logic. | ||
* | ||
* @param _query - The query string used to search for relevant documents. | ||
* @param _callbacks - (optional) Callback manager for managing callbacks | ||
* during retrieval. | ||
* @returns A promise resolving to an array of `DocumentInterface` instances relevant to the query. | ||
* @throws {Error} Throws an error indicating the method is not implemented. | ||
*/ | ||
_getRelevantDocuments(_query: string, _callbacks?: CallbackManagerForRetrieverRun): Promise<DocumentInterface<Metadata>[]>; | ||
/** | ||
* Executes a retrieval operation. | ||
* | ||
* @param input - The query string used to search for relevant documents. | ||
* @param options - (optional) Configuration options for the retrieval run, | ||
* which may include callbacks, tags, and metadata. | ||
* @returns A promise that resolves to an array of `DocumentInterface` instances | ||
* representing the most relevant documents to the query. | ||
*/ | ||
invoke(input: string, options?: RunnableConfig): Promise<DocumentInterface<Metadata>[]>; | ||
@@ -38,0 +124,0 @@ /** |
@@ -5,9 +5,25 @@ import { CallbackManager, parseCallbackConfigArg, } from "../callbacks/manager.js"; | ||
/** | ||
* Abstract base class for a Document retrieval system. A retrieval system | ||
* is defined as something that can take string queries and return the | ||
* most 'relevant' Documents from some source. | ||
* Abstract base class for a document retrieval system, designed to | ||
* process string queries and return the most relevant documents from a source. | ||
* | ||
* `BaseRetriever` provides common properties and methods for derived retrievers, | ||
* such as callbacks, tagging, and verbose logging. Custom retrieval systems | ||
* should extend this class and implement `_getRelevantDocuments` to define | ||
* the specific retrieval logic. | ||
* | ||
* @template Metadata - The type of metadata associated with each document, | ||
* defaulting to `Record<string, any>`. | ||
*/ | ||
export class BaseRetriever extends Runnable { | ||
/** | ||
* Constructs a new `BaseRetriever` instance with optional configuration fields. | ||
* | ||
* @param fields - Optional input configuration that can include `callbacks`, | ||
* `tags`, `metadata`, and `verbose` settings for custom retriever behavior. | ||
*/ | ||
constructor(fields) { | ||
super(fields); | ||
/** | ||
* Optional callbacks to handle various events in the retrieval process. | ||
*/ | ||
Object.defineProperty(this, "callbacks", { | ||
@@ -19,2 +35,5 @@ enumerable: true, | ||
}); | ||
/** | ||
* Tags to label or categorize the retrieval operation. | ||
*/ | ||
Object.defineProperty(this, "tags", { | ||
@@ -26,2 +45,6 @@ enumerable: true, | ||
}); | ||
/** | ||
* Metadata to provide additional context or information about the retrieval | ||
* operation. | ||
*/ | ||
Object.defineProperty(this, "metadata", { | ||
@@ -33,2 +56,5 @@ enumerable: true, | ||
}); | ||
/** | ||
* If set to `true`, enables verbose logging for the retrieval process. | ||
*/ | ||
Object.defineProperty(this, "verbose", { | ||
@@ -50,5 +76,28 @@ enumerable: true, | ||
*/ | ||
/** | ||
* Placeholder method for retrieving relevant documents based on a query. | ||
* | ||
* This method is intended to be implemented by subclasses and will be | ||
* converted to an abstract method in the next major release. Currently, it | ||
* throws an error if not implemented, ensuring that custom retrievers define | ||
* the specific retrieval logic. | ||
* | ||
* @param _query - The query string used to search for relevant documents. | ||
* @param _callbacks - (optional) Callback manager for managing callbacks | ||
* during retrieval. | ||
* @returns A promise resolving to an array of `DocumentInterface` instances relevant to the query. | ||
* @throws {Error} Throws an error indicating the method is not implemented. | ||
*/ | ||
_getRelevantDocuments(_query, _callbacks) { | ||
throw new Error("Not implemented!"); | ||
} | ||
/** | ||
* Executes a retrieval operation. | ||
* | ||
* @param input - The query string used to search for relevant documents. | ||
* @param options - (optional) Configuration options for the retrieval run, | ||
* which may include callbacks, tags, and metadata. | ||
* @returns A promise that resolves to an array of `DocumentInterface` instances | ||
* representing the most relevant documents to the query. | ||
*/ | ||
async invoke(input, options) { | ||
@@ -55,0 +104,0 @@ return this.getRelevantDocuments(input, ensureConfig(options)); |
@@ -1,19 +0,2 @@ | ||
export interface AsyncLocalStorageInterface { | ||
getStore: () => any | undefined; | ||
run: <T>(store: any, callback: () => T) => T; | ||
enterWith: (store: any) => void; | ||
} | ||
export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface { | ||
getStore(): any; | ||
run<T>(_store: any, callback: () => T): T; | ||
enterWith(_store: any): undefined; | ||
} | ||
export declare const _CONTEXT_VARIABLES_KEY: unique symbol; | ||
declare class AsyncLocalStorageProvider { | ||
getInstance(): AsyncLocalStorageInterface; | ||
getRunnableConfig(): any; | ||
runWithConfig<T>(config: any, callback: () => T, avoidCreatingRootRunTree?: boolean): T; | ||
initializeGlobalInstance(instance: AsyncLocalStorageInterface): void; | ||
} | ||
declare const AsyncLocalStorageProviderSingleton: AsyncLocalStorageProvider; | ||
export { AsyncLocalStorageProviderSingleton }; | ||
import { type AsyncLocalStorageInterface, AsyncLocalStorageProviderSingleton, _CONTEXT_VARIABLES_KEY, MockAsyncLocalStorage } from "./async_local_storage/index.js"; | ||
export { type AsyncLocalStorageInterface, AsyncLocalStorageProviderSingleton, _CONTEXT_VARIABLES_KEY, MockAsyncLocalStorage, }; |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { RunTree } from "langsmith"; | ||
import { CallbackManager } from "../callbacks/manager.js"; | ||
export class MockAsyncLocalStorage { | ||
getStore() { | ||
return undefined; | ||
} | ||
run(_store, callback) { | ||
return callback(); | ||
} | ||
enterWith(_store) { | ||
return undefined; | ||
} | ||
} | ||
const mockAsyncLocalStorage = new MockAsyncLocalStorage(); | ||
const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage"); | ||
const LC_CHILD_KEY = Symbol.for("lc:child_config"); | ||
export const _CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables"); | ||
class AsyncLocalStorageProvider { | ||
getInstance() { | ||
return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage; | ||
} | ||
getRunnableConfig() { | ||
const storage = this.getInstance(); | ||
// this has the runnable config | ||
// which means that we should also have an instance of a LangChainTracer | ||
// with the run map prepopulated | ||
return storage.getStore()?.extra?.[LC_CHILD_KEY]; | ||
} | ||
runWithConfig(config, callback, avoidCreatingRootRunTree) { | ||
const callbackManager = CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata); | ||
const storage = this.getInstance(); | ||
const previousValue = storage.getStore(); | ||
const parentRunId = callbackManager?.getParentRunId(); | ||
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer"); | ||
let runTree; | ||
if (langChainTracer && parentRunId) { | ||
runTree = langChainTracer.convertToRunTree(parentRunId); | ||
} | ||
else if (!avoidCreatingRootRunTree) { | ||
runTree = new RunTree({ | ||
name: "<runnable_lambda>", | ||
tracingEnabled: false, | ||
}); | ||
} | ||
if (runTree) { | ||
runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config }; | ||
} | ||
if (previousValue !== undefined && | ||
previousValue[_CONTEXT_VARIABLES_KEY] !== undefined) { | ||
runTree[_CONTEXT_VARIABLES_KEY] = | ||
previousValue[_CONTEXT_VARIABLES_KEY]; | ||
} | ||
return storage.run(runTree, callback); | ||
} | ||
initializeGlobalInstance(instance) { | ||
if (globalThis[TRACING_ALS_KEY] === undefined) { | ||
globalThis[TRACING_ALS_KEY] = instance; | ||
} | ||
} | ||
} | ||
const AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider(); | ||
export { AsyncLocalStorageProviderSingleton }; | ||
import { AsyncLocalStorageProviderSingleton, _CONTEXT_VARIABLES_KEY, MockAsyncLocalStorage, } from "./async_local_storage/index.js"; | ||
export { AsyncLocalStorageProviderSingleton, _CONTEXT_VARIABLES_KEY, MockAsyncLocalStorage, }; |
@@ -1,2 +0,1 @@ | ||
import { Client } from "langsmith"; | ||
import { RunTree } from "langsmith/run_trees"; | ||
@@ -6,2 +5,3 @@ import { getCurrentRunTree } from "langsmith/singletons/traceable"; | ||
import { BaseTracer } from "./base.js"; | ||
import { getDefaultLangChainClientSingleton } from "../singletons/tracer.js"; | ||
export class LangChainTracer extends BaseTracer { | ||
@@ -40,9 +40,3 @@ constructor(fields = {}) { | ||
this.exampleId = exampleId; | ||
const clientParams = getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false" | ||
? { | ||
// LangSmith has its own backgrounding system | ||
blockOnRootRunFinalization: true, | ||
} | ||
: {}; | ||
this.client = client ?? new Client(clientParams); | ||
this.client = client ?? getDefaultLangChainClientSingleton(); | ||
const traceableTree = LangChainTracer.getTraceableRunTree(); | ||
@@ -49,0 +43,0 @@ if (traceableTree) { |
@@ -11,3 +11,30 @@ import type { EmbeddingsInterface } from "./embeddings.js"; | ||
/** | ||
* Type for options when performing a maximal marginal relevance search. | ||
* Options for configuring a maximal marginal relevance (MMR) search. | ||
* | ||
* MMR search optimizes for both similarity to the query and diversity | ||
* among the results, balancing the retrieval of relevant documents | ||
* with variation in the content returned. | ||
* | ||
* Fields: | ||
* | ||
* - `fetchK` (optional): The initial number of documents to retrieve from the | ||
* vector store before applying the MMR algorithm. This larger set provides a | ||
* pool of documents from which the algorithm can select the most diverse | ||
* results based on relevance to the query. | ||
* | ||
* - `filter` (optional): A filter of type `FilterType` to refine the search | ||
* results, allowing additional conditions to target specific subsets | ||
* of documents. | ||
* | ||
* - `k`: The number of documents to return in the final results. This is the | ||
* primary count of documents that are most relevant to the query. | ||
* | ||
* - `lambda` (optional): A value between 0 and 1 that determines the balance | ||
* between relevance and diversity: | ||
* - A `lambda` of 0 emphasizes diversity, maximizing content variation. | ||
* - A `lambda` of 1 emphasizes similarity to the query, focusing on relevance. | ||
* Values between 0 and 1 provide a mix of relevance and diversity. | ||
* | ||
* @template FilterType - The type used for filtering results, as defined | ||
* by the vector store. | ||
*/ | ||
@@ -21,4 +48,19 @@ export type MaxMarginalRelevanceSearchOptions<FilterType> = { | ||
/** | ||
* Type for options when performing a maximal marginal relevance search | ||
* with the VectorStoreRetriever. | ||
* Options for configuring a maximal marginal relevance (MMR) search | ||
* when using the `VectorStoreRetriever`. | ||
* | ||
* These parameters control how the MMR algorithm balances relevance to the | ||
* query and diversity among the retrieved documents. | ||
* | ||
* Fields: | ||
* - `fetchK` (optional): Specifies the initial number of documents to fetch | ||
* before applying the MMR algorithm. This larger set provides a pool of | ||
* documents from which the algorithm can select the most diverse results | ||
* based on relevance to the query. | ||
* | ||
* - `lambda` (optional): A value between 0 and 1 that determines the balance | ||
* between relevance and diversity: | ||
* - A `lambda` of 0 maximizes diversity among the results, prioritizing varied content. | ||
* - A `lambda` of 1 maximizes similarity to the query, prioritizing relevance. | ||
* Values between 0 and 1 provide a mix of relevance and diversity. | ||
*/ | ||
@@ -30,3 +72,46 @@ export type VectorStoreRetrieverMMRSearchKwargs = { | ||
/** | ||
* Type for input when creating a VectorStoreRetriever instance. | ||
* Input configuration options for creating a `VectorStoreRetriever` instance. | ||
* | ||
* This type combines properties from `BaseRetrieverInput` with specific settings | ||
* for the `VectorStoreRetriever`, including options for similarity or maximal | ||
* marginal relevance (MMR) search types. | ||
* | ||
* Fields: | ||
* | ||
* - `callbacks` (optional): An array of callback functions that handle various | ||
* events during retrieval, such as logging, error handling, or progress updates. | ||
* | ||
* - `tags` (optional): An array of strings used to add contextual tags to | ||
* retrieval operations, allowing for easier categorization and tracking. | ||
* | ||
* - `metadata` (optional): A record of key-value pairs to store additional | ||
* contextual information for retrieval operations, which can be useful | ||
* for logging or auditing purposes. | ||
* | ||
* - `verbose` (optional): A boolean flag that, if set to `true`, enables | ||
* detailed logging and output during the retrieval process. Defaults to `false`. | ||
* | ||
* - `vectorStore`: The `VectorStore` instance implementing `VectorStoreInterface` | ||
* that will be used for document storage and retrieval. | ||
* | ||
* - `k` (optional): Specifies the number of documents to retrieve per search | ||
* query. Defaults to 4 if not specified. | ||
* | ||
* - `filter` (optional): A filter of type `FilterType` (defined by the vector store) | ||
* to refine the set of documents returned, allowing for targeted search results. | ||
* | ||
* - `searchType`: Determines the type of search to perform: | ||
* - `"similarity"`: Executes a similarity search, retrieving documents based purely | ||
* on vector similarity to the query. | ||
* - `"mmr"`: Executes a maximal marginal relevance (MMR) search, balancing similarity | ||
* and diversity in the search results. | ||
* | ||
* - `searchKwargs` (optional): Used only if `searchType` is `"mmr"`, this object | ||
* provides additional options for MMR search, including: | ||
* - `fetchK`: Specifies the number of documents to initially fetch before applying | ||
* the MMR algorithm, providing a pool from which the most diverse results are selected. | ||
* - `lambda`: A diversity parameter, where 0 emphasizes diversity and 1 emphasizes | ||
* relevance to the query. Values between 0 and 1 provide a balance of relevance and diversity. | ||
* | ||
* @template V - The type of vector store implementing `VectorStoreInterface`. | ||
*/ | ||
@@ -45,9 +130,41 @@ export type VectorStoreRetrieverInput<V extends VectorStoreInterface> = BaseRetrieverInput & ({ | ||
}); | ||
/** | ||
* Interface for a retriever that uses a vector store to store and retrieve | ||
* document embeddings. This retriever interface allows for adding documents | ||
* to the underlying vector store and conducting retrieval operations. | ||
* | ||
* `VectorStoreRetrieverInterface` extends `BaseRetrieverInterface` to provide | ||
* document retrieval capabilities based on vector similarity. | ||
* | ||
* @interface VectorStoreRetrieverInterface | ||
* @extends BaseRetrieverInterface | ||
*/ | ||
export interface VectorStoreRetrieverInterface<V extends VectorStoreInterface = VectorStoreInterface> extends BaseRetrieverInterface { | ||
vectorStore: V; | ||
/** | ||
* Adds an array of documents to the vector store. | ||
* | ||
* This method embeds the provided documents and stores them within the | ||
* vector store. Additional options can be specified for custom behavior | ||
* during the addition process. | ||
* | ||
* @param documents - An array of documents to embed and add to the vector store. | ||
* @param options - Optional settings to customize document addition. | ||
* @returns A promise that resolves to an array of document IDs or `void`, | ||
* depending on the implementation. | ||
*/ | ||
addDocuments(documents: DocumentInterface[], options?: AddDocumentOptions): Promise<string[] | void>; | ||
} | ||
/** | ||
* Class for performing document retrieval from a VectorStore. Can perform | ||
* similarity search or maximal marginal relevance search. | ||
* Class for retrieving documents from a `VectorStore` based on vector similarity | ||
* or maximal marginal relevance (MMR). | ||
* | ||
* `VectorStoreRetriever` extends `BaseRetriever`, implementing methods for | ||
* adding documents to the underlying vector store and performing document | ||
* retrieval with optional configurations. | ||
* | ||
* @class VectorStoreRetriever | ||
* @extends BaseRetriever | ||
* @implements VectorStoreRetrieverInterface | ||
* @template V - Type of vector store implementing `VectorStoreInterface`. | ||
*/ | ||
@@ -57,21 +174,208 @@ export declare class VectorStoreRetriever<V extends VectorStoreInterface = VectorStoreInterface> extends BaseRetriever implements VectorStoreRetrieverInterface { | ||
get lc_namespace(): string[]; | ||
/** | ||
* The instance of `VectorStore` used for storing and retrieving document embeddings. | ||
* This vector store must implement the `VectorStoreInterface` to be compatible | ||
* with the retriever’s operations. | ||
*/ | ||
vectorStore: V; | ||
/** | ||
* Specifies the number of documents to retrieve for each search query. | ||
* Defaults to 4 if not specified, providing a basic result count for similarity or MMR searches. | ||
*/ | ||
k: number; | ||
/** | ||
* Determines the type of search operation to perform on the vector store. | ||
* | ||
* - `"similarity"` (default): Conducts a similarity search based purely on vector similarity | ||
* to the query. | ||
* - `"mmr"`: Executes a maximal marginal relevance (MMR) search, balancing relevance and | ||
* diversity in the retrieved results. | ||
*/ | ||
searchType: string; | ||
/** | ||
* Additional options specific to maximal marginal relevance (MMR) search, applicable | ||
* only if `searchType` is set to `"mmr"`. | ||
* | ||
* Includes: | ||
* - `fetchK`: The initial number of documents fetched before applying the MMR algorithm, | ||
* allowing for a larger selection from which to choose the most diverse results. | ||
* - `lambda`: A parameter between 0 and 1 to adjust the relevance-diversity balance, | ||
* where 0 prioritizes diversity and 1 prioritizes relevance. | ||
*/ | ||
searchKwargs?: VectorStoreRetrieverMMRSearchKwargs; | ||
/** | ||
* Optional filter applied to search results, defined by the `FilterType` of the vector store. | ||
* Allows for refined, targeted results by restricting the returned documents based | ||
* on specified filter criteria. | ||
*/ | ||
filter?: V["FilterType"]; | ||
/** | ||
* Returns the type of vector store, as defined by the `vectorStore` instance. | ||
* | ||
* @returns {string} The vector store type. | ||
*/ | ||
_vectorstoreType(): string; | ||
/** | ||
* Initializes a new instance of `VectorStoreRetriever` with the specified configuration. | ||
* | ||
* This constructor configures the retriever to interact with a given `VectorStore` | ||
* and supports different retrieval strategies, including similarity search and maximal | ||
* marginal relevance (MMR) search. Various options allow customization of the number | ||
* of documents retrieved per query, filtering based on conditions, and fine-tuning | ||
* MMR-specific parameters. | ||
* | ||
* @param fields - Configuration options for setting up the retriever: | ||
* | ||
* - `vectorStore` (required): The `VectorStore` instance implementing `VectorStoreInterface` | ||
* that will be used to store and retrieve document embeddings. This is the core component | ||
* of the retriever, enabling vector-based similarity and MMR searches. | ||
* | ||
* - `k` (optional): Specifies the number of documents to retrieve per search query. If not | ||
* provided, defaults to 4. This count determines the number of most relevant documents returned | ||
* for each search operation, balancing performance with comprehensiveness. | ||
* | ||
* - `searchType` (optional): Defines the search approach used by the retriever, allowing for | ||
* flexibility between two methods: | ||
* - `"similarity"` (default): A similarity-based search, retrieving documents with high vector | ||
* similarity to the query. This type prioritizes relevance and is often used when diversity | ||
* among results is less critical. | ||
* - `"mmr"`: Maximal Marginal Relevance search, which combines relevance with diversity. MMR | ||
* is useful for scenarios where varied content is essential, as it selects results that | ||
* both match the query and introduce content diversity. | ||
* | ||
* - `filter` (optional): A filter of type `FilterType`, defined by the vector store, that allows | ||
* for refined and targeted search results. This filter applies specified conditions to limit | ||
* which documents are eligible for retrieval, offering control over the scope of results. | ||
* | ||
* - `searchKwargs` (optional, applicable only if `searchType` is `"mmr"`): Additional settings | ||
* for configuring MMR-specific behavior. These parameters allow further tuning of the MMR | ||
* search process: | ||
* - `fetchK`: The initial number of documents fetched from the vector store before the MMR | ||
* algorithm is applied. Fetching a larger set enables the algorithm to select a more | ||
* diverse subset of documents. | ||
* - `lambda`: A parameter controlling the relevance-diversity balance, where 0 emphasizes | ||
* diversity and 1 prioritizes relevance. Intermediate values provide a blend of the two, | ||
* allowing customization based on the importance of content variety relative to query relevance. | ||
*/ | ||
constructor(fields: VectorStoreRetrieverInput<V>); | ||
/** | ||
* Retrieves relevant documents based on the specified query, using either | ||
* similarity or maximal marginal relevance (MMR) search. | ||
* | ||
* If `searchType` is set to `"mmr"`, performs an MMR search to balance | ||
* similarity and diversity among results. If `searchType` is `"similarity"`, | ||
* retrieves results purely based on similarity to the query. | ||
* | ||
* @param query - The query string used to find relevant documents. | ||
* @param runManager - Optional callback manager for tracking retrieval progress. | ||
* @returns A promise that resolves to an array of `DocumentInterface` instances | ||
* representing the most relevant documents to the query. | ||
* @throws {Error} Throws an error if MMR search is requested but not supported | ||
* by the vector store. | ||
* @protected | ||
*/ | ||
_getRelevantDocuments(query: string, runManager?: CallbackManagerForRetrieverRun): Promise<DocumentInterface[]>; | ||
/** | ||
* Adds an array of documents to the vector store, embedding them as part of | ||
* the storage process. | ||
* | ||
* This method delegates document embedding and storage to the `addDocuments` | ||
* method of the underlying vector store. | ||
* | ||
* @param documents - An array of documents to embed and add to the vector store. | ||
* @param options - Optional settings to customize document addition. | ||
* @returns A promise that resolves to an array of document IDs or `void`, | ||
* depending on the vector store's implementation. | ||
*/ | ||
addDocuments(documents: DocumentInterface[], options?: AddDocumentOptions): Promise<string[] | void>; | ||
} | ||
/** | ||
* Interface defining the structure and operations of a vector store, which | ||
* facilitates the storage, retrieval, and similarity search of document vectors. | ||
* | ||
* `VectorStoreInterface` provides methods for adding, deleting, and searching | ||
* documents based on vector embeddings, including support for similarity | ||
* search with optional filtering and relevance-based retrieval. | ||
* | ||
* @extends Serializable | ||
*/ | ||
export interface VectorStoreInterface extends Serializable { | ||
/** | ||
* Defines the filter type used in search and delete operations. Can be an | ||
* object for structured conditions or a string for simpler filtering. | ||
*/ | ||
FilterType: object | string; | ||
/** | ||
* Instance of `EmbeddingsInterface` used to generate vector embeddings for | ||
* documents, enabling vector-based search operations. | ||
*/ | ||
embeddings: EmbeddingsInterface; | ||
/** | ||
* Returns a string identifying the type of vector store implementation, | ||
* useful for distinguishing between different vector storage backends. | ||
* | ||
* @returns {string} A string indicating the vector store type. | ||
*/ | ||
_vectorstoreType(): string; | ||
/** | ||
* Adds precomputed vectors and their corresponding documents to the vector store. | ||
* | ||
* @param vectors - An array of vectors, with each vector representing a document. | ||
* @param documents - An array of `DocumentInterface` instances corresponding to each vector. | ||
* @param options - Optional configurations for adding documents, potentially covering indexing or metadata handling. | ||
* @returns A promise that resolves to an array of document IDs or void, depending on implementation. | ||
*/ | ||
addVectors(vectors: number[][], documents: DocumentInterface[], options?: AddDocumentOptions): Promise<string[] | void>; | ||
/** | ||
* Adds an array of documents to the vector store. | ||
* | ||
* @param documents - An array of documents to be embedded and stored in the vector store. | ||
* @param options - Optional configurations for embedding and storage operations. | ||
* @returns A promise that resolves to an array of document IDs or void, depending on implementation. | ||
*/ | ||
addDocuments(documents: DocumentInterface[], options?: AddDocumentOptions): Promise<string[] | void>; | ||
/** | ||
* Deletes documents from the vector store based on the specified parameters. | ||
* | ||
* @param _params - A flexible object containing key-value pairs that define | ||
* the conditions for selecting documents to delete. | ||
* @returns A promise that resolves once the deletion operation is complete. | ||
*/ | ||
delete(_params?: Record<string, any>): Promise<void>; | ||
/** | ||
* Searches for documents similar to a given vector query and returns them | ||
* with similarity scores. | ||
* | ||
* @param query - A vector representing the query for similarity search. | ||
* @param k - The number of similar documents to return. | ||
* @param filter - Optional filter based on `FilterType` to restrict results. | ||
* @returns A promise that resolves to an array of tuples, each containing a | ||
* `DocumentInterface` and its corresponding similarity score. | ||
*/ | ||
similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[DocumentInterface, number][]>; | ||
/** | ||
* Searches for documents similar to a text query, embedding the query | ||
* and retrieving documents based on vector similarity. | ||
* | ||
* @param query - The text query to search for. | ||
* @param k - Optional number of similar documents to return. | ||
* @param filter - Optional filter based on `FilterType` to restrict results. | ||
* @param callbacks - Optional callbacks for tracking progress or events | ||
* during the search process. | ||
* @returns A promise that resolves to an array of `DocumentInterface` | ||
* instances representing similar documents. | ||
*/ | ||
similaritySearch(query: string, k?: number, filter?: this["FilterType"], callbacks?: Callbacks): Promise<DocumentInterface[]>; | ||
/** | ||
* Searches for documents similar to a text query and includes similarity | ||
* scores in the result. | ||
* | ||
* @param query - The text query to search for. | ||
* @param k - Optional number of similar documents to return. | ||
* @param filter - Optional filter based on `FilterType` to restrict results. | ||
* @param callbacks - Optional callbacks for tracking progress or events | ||
* during the search process. | ||
* @returns A promise that resolves to an array of tuples, each containing | ||
* a `DocumentInterface` and its similarity score. | ||
*/ | ||
similaritySearchWithScore(query: string, k?: number, filter?: this["FilterType"], callbacks?: Callbacks): Promise<[DocumentInterface, number][]>; | ||
@@ -94,20 +398,120 @@ /** | ||
maxMarginalRelevanceSearch?(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>, callbacks: Callbacks | undefined): Promise<DocumentInterface[]>; | ||
/** | ||
* Converts the vector store into a retriever, making it suitable for use in | ||
* retrieval-based workflows and allowing additional configuration. | ||
* | ||
* @param kOrFields - Optional parameter for specifying either the number of | ||
* documents to retrieve or partial retriever configurations. | ||
* @param filter - Optional filter based on `FilterType` for retrieval restriction. | ||
* @param callbacks - Optional callbacks for tracking retrieval events or progress. | ||
* @param tags - General-purpose tags to add contextual information to the retriever. | ||
* @param metadata - General-purpose metadata providing additional context | ||
* for retrieval. | ||
* @param verbose - If `true`, enables detailed logging during retrieval. | ||
* @returns An instance of `VectorStoreRetriever` configured with the specified options. | ||
*/ | ||
asRetriever(kOrFields?: number | Partial<VectorStoreRetrieverInput<this>>, filter?: this["FilterType"], callbacks?: Callbacks, tags?: string[], metadata?: Record<string, unknown>, verbose?: boolean): VectorStoreRetriever<this>; | ||
} | ||
/** | ||
* Abstract class representing a store of vectors. Provides methods for | ||
* adding vectors and documents, deleting from the store, and searching | ||
* the store. | ||
* Abstract class representing a vector storage system for performing | ||
* similarity searches on embedded documents. | ||
* | ||
* `VectorStore` provides methods for adding precomputed vectors or documents, | ||
* removing documents based on criteria, and performing similarity searches | ||
* with optional scoring. Subclasses are responsible for implementing specific | ||
* storage mechanisms and the exact behavior of certain abstract methods. | ||
* | ||
* @abstract | ||
* @extends Serializable | ||
* @implements VectorStoreInterface | ||
*/ | ||
export declare abstract class VectorStore extends Serializable implements VectorStoreInterface { | ||
FilterType: object | string; | ||
/** | ||
* Namespace within LangChain to uniquely identify this vector store's | ||
* location, based on the vector store type. | ||
* | ||
* @internal | ||
*/ | ||
lc_namespace: string[]; | ||
/** | ||
* Embeddings interface for generating vector embeddings from text queries, | ||
* enabling vector-based similarity searches. | ||
*/ | ||
embeddings: EmbeddingsInterface; | ||
/** | ||
* Initializes a new vector store with embeddings and database configuration. | ||
* | ||
* @param embeddings - Instance of `EmbeddingsInterface` used to embed queries. | ||
* @param dbConfig - Configuration settings for the database or storage system. | ||
*/ | ||
constructor(embeddings: EmbeddingsInterface, dbConfig: Record<string, any>); | ||
/** | ||
* Returns a string representing the type of vector store, which subclasses | ||
* must implement to identify their specific vector storage type. | ||
* | ||
* @returns {string} A string indicating the vector store type. | ||
* @abstract | ||
*/ | ||
abstract _vectorstoreType(): string; | ||
/** | ||
* Adds precomputed vectors and corresponding documents to the vector store. | ||
* | ||
* @param vectors - An array of vectors representing each document. | ||
* @param documents - Array of documents associated with each vector. | ||
* @param options - Optional configuration for adding vectors, such as indexing. | ||
* @returns A promise resolving to an array of document IDs or void, based on implementation. | ||
* @abstract | ||
*/ | ||
abstract addVectors(vectors: number[][], documents: DocumentInterface[], options?: AddDocumentOptions): Promise<string[] | void>; | ||
/** | ||
* Adds documents to the vector store, embedding them first through the | ||
* `embeddings` instance. | ||
* | ||
* @param documents - Array of documents to embed and add. | ||
* @param options - Optional configuration for embedding and storing documents. | ||
* @returns A promise resolving to an array of document IDs or void, based on implementation. | ||
* @abstract | ||
*/ | ||
abstract addDocuments(documents: DocumentInterface[], options?: AddDocumentOptions): Promise<string[] | void>; | ||
/** | ||
* Deletes documents from the vector store based on the specified parameters. | ||
* | ||
* @param _params - Flexible key-value pairs defining conditions for document deletion. | ||
* @returns A promise that resolves once the deletion is complete. | ||
*/ | ||
delete(_params?: Record<string, any>): Promise<void>; | ||
/** | ||
* Performs a similarity search using a vector query and returns results | ||
* along with their similarity scores. | ||
* | ||
* @param query - Vector representing the search query. | ||
* @param k - Number of similar results to return. | ||
* @param filter - Optional filter based on `FilterType` to restrict results. | ||
* @returns A promise resolving to an array of tuples containing documents and their similarity scores. | ||
* @abstract | ||
*/ | ||
abstract similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[DocumentInterface, number][]>; | ||
/** | ||
* Searches for documents similar to a text query by embedding the query and | ||
* performing a similarity search on the resulting vector. | ||
* | ||
* @param query - Text query for finding similar documents. | ||
* @param k - Number of similar results to return. Defaults to 4. | ||
* @param filter - Optional filter based on `FilterType`. | ||
* @param _callbacks - Optional callbacks for monitoring search progress | ||
* @returns A promise resolving to an array of `DocumentInterface` instances representing similar documents. | ||
*/ | ||
similaritySearch(query: string, k?: number, filter?: this["FilterType"] | undefined, _callbacks?: Callbacks | undefined): Promise<DocumentInterface[]>; | ||
/** | ||
* Searches for documents similar to a text query by embedding the query, | ||
* and returns results with similarity scores. | ||
* | ||
* @param query - Text query for finding similar documents. | ||
* @param k - Number of similar results to return. Defaults to 4. | ||
* @param filter - Optional filter based on `FilterType`. | ||
* @param _callbacks - Optional callbacks for monitoring search progress | ||
* @returns A promise resolving to an array of tuples, each containing a | ||
* document and its similarity score. | ||
*/ | ||
similaritySearchWithScore(query: string, k?: number, filter?: this["FilterType"] | undefined, _callbacks?: Callbacks | undefined): Promise<[DocumentInterface, number][]>; | ||
@@ -130,14 +534,118 @@ /** | ||
maxMarginalRelevanceSearch?(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>, _callbacks: Callbacks | undefined): Promise<DocumentInterface[]>; | ||
/** | ||
* Creates a `VectorStore` instance from an array of text strings and optional | ||
* metadata, using the specified embeddings and database configuration. | ||
* | ||
* Subclasses must implement this method to define how text and metadata | ||
* are embedded and stored in the vector store. Throws an error if not overridden. | ||
* | ||
* @param _texts - Array of strings representing the text documents to be stored. | ||
* @param _metadatas - Metadata for the texts, either as an array (one for each text) | ||
* or a single object (applied to all texts). | ||
* @param _embeddings - Instance of `EmbeddingsInterface` to embed the texts. | ||
* @param _dbConfig - Database configuration settings. | ||
* @returns A promise that resolves to a new `VectorStore` instance. | ||
* @throws {Error} Throws an error if this method is not overridden by a subclass. | ||
*/ | ||
static fromTexts(_texts: string[], _metadatas: object[] | object, _embeddings: EmbeddingsInterface, _dbConfig: Record<string, any>): Promise<VectorStore>; | ||
/** | ||
* Creates a `VectorStore` instance from an array of documents, using the specified | ||
* embeddings and database configuration. | ||
* | ||
* Subclasses must implement this method to define how documents are embedded | ||
* and stored. Throws an error if not overridden. | ||
* | ||
* @param _docs - Array of `DocumentInterface` instances representing the documents to be stored. | ||
* @param _embeddings - Instance of `EmbeddingsInterface` to embed the documents. | ||
* @param _dbConfig - Database configuration settings. | ||
* @returns A promise that resolves to a new `VectorStore` instance. | ||
* @throws {Error} Throws an error if this method is not overridden by a subclass. | ||
*/ | ||
static fromDocuments(_docs: DocumentInterface[], _embeddings: EmbeddingsInterface, _dbConfig: Record<string, any>): Promise<VectorStore>; | ||
/** | ||
* Creates a `VectorStoreRetriever` instance with flexible configuration options. | ||
* | ||
* @param kOrFields | ||
* - If a number is provided, it sets the `k` parameter (number of items to retrieve). | ||
* - If an object is provided, it should contain various configuration options. | ||
* @param filter | ||
* - Optional filter criteria to limit the items retrieved based on the specified filter type. | ||
* @param callbacks | ||
* - Optional callbacks that may be triggered at specific stages of the retrieval process. | ||
* @param tags | ||
* - Tags to categorize or label the `VectorStoreRetriever`. Defaults to an empty array if not provided. | ||
* @param metadata | ||
* - Additional metadata as key-value pairs to add contextual information for the retrieval process. | ||
* @param verbose | ||
* - If `true`, enables detailed logging for the retrieval process. Defaults to `false`. | ||
* | ||
* @returns | ||
* - A configured `VectorStoreRetriever` instance based on the provided parameters. | ||
* | ||
* @example | ||
* Basic usage with a `k` value: | ||
* ```typescript | ||
* const retriever = myVectorStore.asRetriever(5); | ||
* ``` | ||
* | ||
* Usage with a configuration object: | ||
* ```typescript | ||
* const retriever = myVectorStore.asRetriever({ | ||
* k: 10, | ||
* filter: myFilter, | ||
* tags: ['example', 'test'], | ||
* verbose: true, | ||
* searchType: 'mmr', | ||
* searchKwargs: { alpha: 0.5 }, | ||
* }); | ||
* ``` | ||
*/ | ||
asRetriever(kOrFields?: number | Partial<VectorStoreRetrieverInput<this>>, filter?: this["FilterType"], callbacks?: Callbacks, tags?: string[], metadata?: Record<string, unknown>, verbose?: boolean): VectorStoreRetriever<this>; | ||
} | ||
/** | ||
* Abstract class extending VectorStore with functionality for saving and | ||
* loading the vector store. | ||
* Abstract class extending `VectorStore` that defines a contract for saving | ||
* and loading vector store instances. | ||
* | ||
* The `SaveableVectorStore` class allows vector store implementations to | ||
* persist their data and retrieve it when needed.The format for saving and | ||
* loading data is left to the implementing subclass. | ||
* | ||
* Subclasses must implement the `save` method to handle their custom | ||
* serialization logic, while the `load` method enables reconstruction of a | ||
* vector store from saved data, requiring compatible embeddings through the | ||
* `EmbeddingsInterface`. | ||
* | ||
* @abstract | ||
* @extends VectorStore | ||
*/ | ||
export declare abstract class SaveableVectorStore extends VectorStore { | ||
/** | ||
* Saves the current state of the vector store to the specified directory. | ||
* | ||
* This method must be implemented by subclasses to define their own | ||
* serialization process for persisting vector data. The implementation | ||
* determines the structure and format of the saved data. | ||
* | ||
* @param directory - The directory path where the vector store data | ||
* will be saved. | ||
* @abstract | ||
*/ | ||
abstract save(directory: string): Promise<void>; | ||
/** | ||
* Loads a vector store instance from the specified directory, using the | ||
* provided embeddings to ensure compatibility. | ||
* | ||
* This static method reconstructs a `SaveableVectorStore` from previously | ||
* saved data. Implementations should interpret the saved data format to | ||
* recreate the vector store instance. | ||
* | ||
* @param _directory - The directory path from which the vector store | ||
* data will be loaded. | ||
* @param _embeddings - An instance of `EmbeddingsInterface` to align | ||
* the embeddings with the loaded vector data. | ||
* @returns A promise that resolves to a `SaveableVectorStore` instance | ||
* constructed from the saved data. | ||
*/ | ||
static load(_directory: string, _embeddings: EmbeddingsInterface): Promise<SaveableVectorStore>; | ||
} | ||
export {}; |
import { BaseRetriever, } from "./retrievers/index.js"; | ||
import { Serializable } from "./load/serializable.js"; | ||
/** | ||
* Class for performing document retrieval from a VectorStore. Can perform | ||
* similarity search or maximal marginal relevance search. | ||
* Class for retrieving documents from a `VectorStore` based on vector similarity | ||
* or maximal marginal relevance (MMR). | ||
* | ||
* `VectorStoreRetriever` extends `BaseRetriever`, implementing methods for | ||
* adding documents to the underlying vector store and performing document | ||
* retrieval with optional configurations. | ||
* | ||
* @class VectorStoreRetriever | ||
* @extends BaseRetriever | ||
* @implements VectorStoreRetrieverInterface | ||
* @template V - Type of vector store implementing `VectorStoreInterface`. | ||
*/ | ||
@@ -14,7 +23,59 @@ export class VectorStoreRetriever extends BaseRetriever { | ||
} | ||
/** | ||
* Returns the type of vector store, as defined by the `vectorStore` instance. | ||
* | ||
* @returns {string} The vector store type. | ||
*/ | ||
_vectorstoreType() { | ||
return this.vectorStore._vectorstoreType(); | ||
} | ||
/** | ||
* Initializes a new instance of `VectorStoreRetriever` with the specified configuration. | ||
* | ||
* This constructor configures the retriever to interact with a given `VectorStore` | ||
* and supports different retrieval strategies, including similarity search and maximal | ||
* marginal relevance (MMR) search. Various options allow customization of the number | ||
* of documents retrieved per query, filtering based on conditions, and fine-tuning | ||
* MMR-specific parameters. | ||
* | ||
* @param fields - Configuration options for setting up the retriever: | ||
* | ||
* - `vectorStore` (required): The `VectorStore` instance implementing `VectorStoreInterface` | ||
* that will be used to store and retrieve document embeddings. This is the core component | ||
* of the retriever, enabling vector-based similarity and MMR searches. | ||
* | ||
* - `k` (optional): Specifies the number of documents to retrieve per search query. If not | ||
* provided, defaults to 4. This count determines the number of most relevant documents returned | ||
* for each search operation, balancing performance with comprehensiveness. | ||
* | ||
* - `searchType` (optional): Defines the search approach used by the retriever, allowing for | ||
* flexibility between two methods: | ||
* - `"similarity"` (default): A similarity-based search, retrieving documents with high vector | ||
* similarity to the query. This type prioritizes relevance and is often used when diversity | ||
* among results is less critical. | ||
* - `"mmr"`: Maximal Marginal Relevance search, which combines relevance with diversity. MMR | ||
* is useful for scenarios where varied content is essential, as it selects results that | ||
* both match the query and introduce content diversity. | ||
* | ||
* - `filter` (optional): A filter of type `FilterType`, defined by the vector store, that allows | ||
* for refined and targeted search results. This filter applies specified conditions to limit | ||
* which documents are eligible for retrieval, offering control over the scope of results. | ||
* | ||
* - `searchKwargs` (optional, applicable only if `searchType` is `"mmr"`): Additional settings | ||
* for configuring MMR-specific behavior. These parameters allow further tuning of the MMR | ||
* search process: | ||
* - `fetchK`: The initial number of documents fetched from the vector store before the MMR | ||
* algorithm is applied. Fetching a larger set enables the algorithm to select a more | ||
* diverse subset of documents. | ||
* - `lambda`: A parameter controlling the relevance-diversity balance, where 0 emphasizes | ||
* diversity and 1 prioritizes relevance. Intermediate values provide a blend of the two, | ||
* allowing customization based on the importance of content variety relative to query relevance. | ||
*/ | ||
constructor(fields) { | ||
super(fields); | ||
/** | ||
* The instance of `VectorStore` used for storing and retrieving document embeddings. | ||
* This vector store must implement the `VectorStoreInterface` to be compatible | ||
* with the retriever’s operations. | ||
*/ | ||
Object.defineProperty(this, "vectorStore", { | ||
@@ -26,2 +87,6 @@ enumerable: true, | ||
}); | ||
/** | ||
* Specifies the number of documents to retrieve for each search query. | ||
* Defaults to 4 if not specified, providing a basic result count for similarity or MMR searches. | ||
*/ | ||
Object.defineProperty(this, "k", { | ||
@@ -33,2 +98,10 @@ enumerable: true, | ||
}); | ||
/** | ||
* Determines the type of search operation to perform on the vector store. | ||
* | ||
* - `"similarity"` (default): Conducts a similarity search based purely on vector similarity | ||
* to the query. | ||
* - `"mmr"`: Executes a maximal marginal relevance (MMR) search, balancing relevance and | ||
* diversity in the retrieved results. | ||
*/ | ||
Object.defineProperty(this, "searchType", { | ||
@@ -40,2 +113,12 @@ enumerable: true, | ||
}); | ||
/** | ||
* Additional options specific to maximal marginal relevance (MMR) search, applicable | ||
* only if `searchType` is set to `"mmr"`. | ||
* | ||
* Includes: | ||
* - `fetchK`: The initial number of documents fetched before applying the MMR algorithm, | ||
* allowing for a larger selection from which to choose the most diverse results. | ||
* - `lambda`: A parameter between 0 and 1 to adjust the relevance-diversity balance, | ||
* where 0 prioritizes diversity and 1 prioritizes relevance. | ||
*/ | ||
Object.defineProperty(this, "searchKwargs", { | ||
@@ -47,2 +130,7 @@ enumerable: true, | ||
}); | ||
/** | ||
* Optional filter applied to search results, defined by the `FilterType` of the vector store. | ||
* Allows for refined, targeted results by restricting the returned documents based | ||
* on specified filter criteria. | ||
*/ | ||
Object.defineProperty(this, "filter", { | ||
@@ -62,2 +150,18 @@ enumerable: true, | ||
} | ||
/** | ||
* Retrieves relevant documents based on the specified query, using either | ||
* similarity or maximal marginal relevance (MMR) search. | ||
* | ||
* If `searchType` is set to `"mmr"`, performs an MMR search to balance | ||
* similarity and diversity among results. If `searchType` is `"similarity"`, | ||
* retrieves results purely based on similarity to the query. | ||
* | ||
* @param query - The query string used to find relevant documents. | ||
* @param runManager - Optional callback manager for tracking retrieval progress. | ||
* @returns A promise that resolves to an array of `DocumentInterface` instances | ||
* representing the most relevant documents to the query. | ||
* @throws {Error} Throws an error if MMR search is requested but not supported | ||
* by the vector store. | ||
* @protected | ||
*/ | ||
async _getRelevantDocuments(query, runManager) { | ||
@@ -76,2 +180,14 @@ if (this.searchType === "mmr") { | ||
} | ||
/** | ||
* Adds an array of documents to the vector store, embedding them as part of | ||
* the storage process. | ||
* | ||
* This method delegates document embedding and storage to the `addDocuments` | ||
* method of the underlying vector store. | ||
* | ||
* @param documents - An array of documents to embed and add to the vector store. | ||
* @param options - Optional settings to customize document addition. | ||
* @returns A promise that resolves to an array of document IDs or `void`, | ||
* depending on the vector store's implementation. | ||
*/ | ||
async addDocuments(documents, options) { | ||
@@ -82,10 +198,30 @@ return this.vectorStore.addDocuments(documents, options); | ||
/** | ||
* Abstract class representing a store of vectors. Provides methods for | ||
* adding vectors and documents, deleting from the store, and searching | ||
* the store. | ||
* Abstract class representing a vector storage system for performing | ||
* similarity searches on embedded documents. | ||
* | ||
* `VectorStore` provides methods for adding precomputed vectors or documents, | ||
* removing documents based on criteria, and performing similarity searches | ||
* with optional scoring. Subclasses are responsible for implementing specific | ||
* storage mechanisms and the exact behavior of certain abstract methods. | ||
* | ||
* @abstract | ||
* @extends Serializable | ||
* @implements VectorStoreInterface | ||
*/ | ||
export class VectorStore extends Serializable { | ||
/** | ||
* Initializes a new vector store with embeddings and database configuration. | ||
* | ||
* @param embeddings - Instance of `EmbeddingsInterface` used to embed queries. | ||
* @param dbConfig - Configuration settings for the database or storage system. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
constructor(embeddings, dbConfig) { | ||
super(dbConfig); | ||
/** | ||
* Namespace within LangChain to uniquely identify this vector store's | ||
* location, based on the vector store type. | ||
* | ||
* @internal | ||
*/ | ||
// Only ever instantiated in main LangChain | ||
@@ -98,2 +234,6 @@ Object.defineProperty(this, "lc_namespace", { | ||
}); | ||
/** | ||
* Embeddings interface for generating vector embeddings from text queries, | ||
* enabling vector-based similarity searches. | ||
*/ | ||
Object.defineProperty(this, "embeddings", { | ||
@@ -107,2 +247,8 @@ enumerable: true, | ||
} | ||
/** | ||
* Deletes documents from the vector store based on the specified parameters. | ||
* | ||
* @param _params - Flexible key-value pairs defining conditions for document deletion. | ||
* @returns A promise that resolves once the deletion is complete. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
@@ -112,2 +258,12 @@ async delete(_params) { | ||
} | ||
/** | ||
* Searches for documents similar to a text query by embedding the query and | ||
* performing a similarity search on the resulting vector. | ||
* | ||
* @param query - Text query for finding similar documents. | ||
* @param k - Number of similar results to return. Defaults to 4. | ||
* @param filter - Optional filter based on `FilterType`. | ||
* @param _callbacks - Optional callbacks for monitoring search progress | ||
* @returns A promise resolving to an array of `DocumentInterface` instances representing similar documents. | ||
*/ | ||
async similaritySearch(query, k = 4, filter = undefined, _callbacks = undefined // implement passing to embedQuery later | ||
@@ -118,2 +274,13 @@ ) { | ||
} | ||
/** | ||
* Searches for documents similar to a text query by embedding the query, | ||
* and returns results with similarity scores. | ||
* | ||
* @param query - Text query for finding similar documents. | ||
* @param k - Number of similar results to return. Defaults to 4. | ||
* @param filter - Optional filter based on `FilterType`. | ||
* @param _callbacks - Optional callbacks for monitoring search progress | ||
* @returns A promise resolving to an array of tuples, each containing a | ||
* document and its similarity score. | ||
*/ | ||
async similaritySearchWithScore(query, k = 4, filter = undefined, _callbacks = undefined // implement passing to embedQuery later | ||
@@ -123,2 +290,17 @@ ) { | ||
} | ||
/** | ||
* Creates a `VectorStore` instance from an array of text strings and optional | ||
* metadata, using the specified embeddings and database configuration. | ||
* | ||
* Subclasses must implement this method to define how text and metadata | ||
* are embedded and stored in the vector store. Throws an error if not overridden. | ||
* | ||
* @param _texts - Array of strings representing the text documents to be stored. | ||
* @param _metadatas - Metadata for the texts, either as an array (one for each text) | ||
* or a single object (applied to all texts). | ||
* @param _embeddings - Instance of `EmbeddingsInterface` to embed the texts. | ||
* @param _dbConfig - Database configuration settings. | ||
* @returns A promise that resolves to a new `VectorStore` instance. | ||
* @throws {Error} Throws an error if this method is not overridden by a subclass. | ||
*/ | ||
static fromTexts(_texts, _metadatas, _embeddings, | ||
@@ -129,2 +311,15 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} | ||
/** | ||
* Creates a `VectorStore` instance from an array of documents, using the specified | ||
* embeddings and database configuration. | ||
* | ||
* Subclasses must implement this method to define how documents are embedded | ||
* and stored. Throws an error if not overridden. | ||
* | ||
* @param _docs - Array of `DocumentInterface` instances representing the documents to be stored. | ||
* @param _embeddings - Instance of `EmbeddingsInterface` to embed the documents. | ||
* @param _dbConfig - Database configuration settings. | ||
* @returns A promise that resolves to a new `VectorStore` instance. | ||
* @throws {Error} Throws an error if this method is not overridden by a subclass. | ||
*/ | ||
static fromDocuments(_docs, _embeddings, | ||
@@ -135,2 +330,40 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} | ||
/** | ||
* Creates a `VectorStoreRetriever` instance with flexible configuration options. | ||
* | ||
* @param kOrFields | ||
* - If a number is provided, it sets the `k` parameter (number of items to retrieve). | ||
* - If an object is provided, it should contain various configuration options. | ||
* @param filter | ||
* - Optional filter criteria to limit the items retrieved based on the specified filter type. | ||
* @param callbacks | ||
* - Optional callbacks that may be triggered at specific stages of the retrieval process. | ||
* @param tags | ||
* - Tags to categorize or label the `VectorStoreRetriever`. Defaults to an empty array if not provided. | ||
* @param metadata | ||
* - Additional metadata as key-value pairs to add contextual information for the retrieval process. | ||
* @param verbose | ||
* - If `true`, enables detailed logging for the retrieval process. Defaults to `false`. | ||
* | ||
* @returns | ||
* - A configured `VectorStoreRetriever` instance based on the provided parameters. | ||
* | ||
* @example | ||
* Basic usage with a `k` value: | ||
* ```typescript | ||
* const retriever = myVectorStore.asRetriever(5); | ||
* ``` | ||
* | ||
* Usage with a configuration object: | ||
* ```typescript | ||
* const retriever = myVectorStore.asRetriever({ | ||
* k: 10, | ||
* filter: myFilter, | ||
* tags: ['example', 'test'], | ||
* verbose: true, | ||
* searchType: 'mmr', | ||
* searchKwargs: { alpha: 0.5 }, | ||
* }); | ||
* ``` | ||
*/ | ||
asRetriever(kOrFields, filter, callbacks, tags, metadata, verbose) { | ||
@@ -170,6 +403,33 @@ if (typeof kOrFields === "number") { | ||
/** | ||
* Abstract class extending VectorStore with functionality for saving and | ||
* loading the vector store. | ||
* Abstract class extending `VectorStore` that defines a contract for saving | ||
* and loading vector store instances. | ||
* | ||
* The `SaveableVectorStore` class allows vector store implementations to | ||
* persist their data and retrieve it when needed.The format for saving and | ||
* loading data is left to the implementing subclass. | ||
* | ||
* Subclasses must implement the `save` method to handle their custom | ||
* serialization logic, while the `load` method enables reconstruction of a | ||
* vector store from saved data, requiring compatible embeddings through the | ||
* `EmbeddingsInterface`. | ||
* | ||
* @abstract | ||
* @extends VectorStore | ||
*/ | ||
export class SaveableVectorStore extends VectorStore { | ||
/** | ||
* Loads a vector store instance from the specified directory, using the | ||
* provided embeddings to ensure compatibility. | ||
* | ||
* This static method reconstructs a `SaveableVectorStore` from previously | ||
* saved data. Implementations should interpret the saved data format to | ||
* recreate the vector store instance. | ||
* | ||
* @param _directory - The directory path from which the vector store | ||
* data will be loaded. | ||
* @param _embeddings - An instance of `EmbeddingsInterface` to align | ||
* the embeddings with the loaded vector data. | ||
* @returns A promise that resolves to a `SaveableVectorStore` instance | ||
* constructed from the saved data. | ||
*/ | ||
static load(_directory, _embeddings) { | ||
@@ -176,0 +436,0 @@ throw new Error("Not implemented"); |
{ | ||
"name": "@langchain/core", | ||
"version": "0.3.17", | ||
"version": "0.3.18", | ||
"description": "Core LangChain.js abstractions and schemas", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
2311779
683
57681