@langchain/core 0.2.18 → 0.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/callbacks/manager.cjs +94 -19
- package/dist/callbacks/manager.d.ts +3 -1
- package/dist/callbacks/manager.js +94 -19
- package/dist/messages/base.cjs +16 -1
- package/dist/messages/base.d.ts +9 -0
- package/dist/messages/base.js +14 -0
- package/dist/messages/tool.cjs +23 -0
- package/dist/messages/tool.d.ts +15 -0
- package/dist/messages/tool.js +24 -1
- package/dist/messages/utils.cjs +1 -1
- package/dist/messages/utils.js +1 -1
- package/dist/output_parsers/string.cjs +1 -0
- package/dist/output_parsers/string.js +1 -0
- package/dist/runnables/base.cjs +1 -3
- package/dist/runnables/base.js +1 -3
- package/dist/runnables/remote.cjs +3 -1
- package/dist/runnables/remote.js +3 -1
- package/dist/tools/index.cjs +15 -6
- package/dist/tools/index.d.ts +21 -12
- package/dist/tools/index.js +15 -6
- package/dist/utils/testing/index.cjs +161 -3
- package/dist/utils/testing/index.d.ts +94 -3
- package/dist/utils/testing/index.js +160 -3
- package/package.json +4 -3
- package/dist/caches/tests/in_memory_cache.test.d.ts +0 -1
- package/dist/caches/tests/in_memory_cache.test.js +0 -33
- package/dist/callbacks/tests/callbacks.test.d.ts +0 -1
- package/dist/callbacks/tests/callbacks.test.js +0 -495
- package/dist/callbacks/tests/manager.int.test.d.ts +0 -1
- package/dist/callbacks/tests/manager.int.test.js +0 -29
- package/dist/callbacks/tests/run_collector.test.d.ts +0 -1
- package/dist/callbacks/tests/run_collector.test.js +0 -58
- package/dist/language_models/tests/chat_models.test.d.ts +0 -1
- package/dist/language_models/tests/chat_models.test.js +0 -204
- package/dist/language_models/tests/count_tokens.test.d.ts +0 -1
- package/dist/language_models/tests/count_tokens.test.js +0 -19
- package/dist/language_models/tests/llms.test.d.ts +0 -1
- package/dist/language_models/tests/llms.test.js +0 -52
- package/dist/messages/tests/base_message.test.d.ts +0 -1
- package/dist/messages/tests/base_message.test.js +0 -245
- package/dist/messages/tests/message_utils.test.d.ts +0 -1
- package/dist/messages/tests/message_utils.test.js +0 -434
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +0 -1
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +0 -81
- package/dist/output_parsers/tests/json.test.d.ts +0 -1
- package/dist/output_parsers/tests/json.test.js +0 -427
- package/dist/output_parsers/tests/output_parser.test.d.ts +0 -1
- package/dist/output_parsers/tests/output_parser.test.js +0 -78
- package/dist/output_parsers/tests/string.test.d.ts +0 -1
- package/dist/output_parsers/tests/string.test.js +0 -68
- package/dist/output_parsers/tests/structured.test.d.ts +0 -1
- package/dist/output_parsers/tests/structured.test.js +0 -166
- package/dist/output_parsers/tests/xml.test.d.ts +0 -1
- package/dist/output_parsers/tests/xml.test.js +0 -81
- package/dist/prompts/tests/chat.mustache.test.d.ts +0 -1
- package/dist/prompts/tests/chat.mustache.test.js +0 -129
- package/dist/prompts/tests/chat.test.d.ts +0 -1
- package/dist/prompts/tests/chat.test.js +0 -557
- package/dist/prompts/tests/few_shot.test.d.ts +0 -1
- package/dist/prompts/tests/few_shot.test.js +0 -224
- package/dist/prompts/tests/pipeline.test.d.ts +0 -1
- package/dist/prompts/tests/pipeline.test.js +0 -101
- package/dist/prompts/tests/prompt.mustache.test.d.ts +0 -1
- package/dist/prompts/tests/prompt.mustache.test.js +0 -105
- package/dist/prompts/tests/prompt.test.d.ts +0 -1
- package/dist/prompts/tests/prompt.test.js +0 -78
- package/dist/prompts/tests/structured.test.d.ts +0 -1
- package/dist/prompts/tests/structured.test.js +0 -37
- package/dist/prompts/tests/template.test.d.ts +0 -1
- package/dist/prompts/tests/template.test.js +0 -24
- package/dist/runnables/tests/runnable.test.d.ts +0 -1
- package/dist/runnables/tests/runnable.test.js +0 -491
- package/dist/runnables/tests/runnable_binding.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_binding.test.js +0 -46
- package/dist/runnables/tests/runnable_branch.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_branch.test.js +0 -116
- package/dist/runnables/tests/runnable_graph.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_graph.test.js +0 -100
- package/dist/runnables/tests/runnable_history.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_history.test.js +0 -177
- package/dist/runnables/tests/runnable_interface.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_interface.test.js +0 -209
- package/dist/runnables/tests/runnable_map.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_map.test.js +0 -238
- package/dist/runnables/tests/runnable_passthrough.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_passthrough.test.js +0 -96
- package/dist/runnables/tests/runnable_remote.int.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_remote.int.test.js +0 -138
- package/dist/runnables/tests/runnable_remote.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_remote.test.js +0 -200
- package/dist/runnables/tests/runnable_retry.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_retry.test.js +0 -125
- package/dist/runnables/tests/runnable_stream_events.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_events.test.js +0 -1013
- package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_events_v2.test.js +0 -2022
- package/dist/runnables/tests/runnable_stream_log.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_log.test.js +0 -71
- package/dist/runnables/tests/runnable_tools.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_tools.test.js +0 -149
- package/dist/runnables/tests/runnable_tracing.int.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_tracing.int.test.js +0 -37
- package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_with_fallbacks.test.js +0 -36
- package/dist/singletons/tests/async_local_storage.test.d.ts +0 -1
- package/dist/singletons/tests/async_local_storage.test.js +0 -153
- package/dist/structured_query/tests/utils.test.d.ts +0 -1
- package/dist/structured_query/tests/utils.test.js +0 -47
- package/dist/tools/tests/tools.test.d.ts +0 -1
- package/dist/tools/tests/tools.test.js +0 -85
- package/dist/tracers/tests/langchain_tracer.int.test.d.ts +0 -1
- package/dist/tracers/tests/langchain_tracer.int.test.js +0 -74
- package/dist/tracers/tests/langsmith_interop.test.d.ts +0 -1
- package/dist/tracers/tests/langsmith_interop.test.js +0 -551
- package/dist/tracers/tests/tracer.test.d.ts +0 -1
- package/dist/tracers/tests/tracer.test.js +0 -378
- package/dist/utils/testing/tests/chatfake.test.d.ts +0 -1
- package/dist/utils/testing/tests/chatfake.test.js +0 -112
- package/dist/utils/tests/async_caller.test.d.ts +0 -1
- package/dist/utils/tests/async_caller.test.js +0 -27
- package/dist/utils/tests/enviroment.test.d.ts +0 -1
- package/dist/utils/tests/enviroment.test.js +0 -6
- package/dist/utils/tests/function_calling.test.d.ts +0 -1
- package/dist/utils/tests/function_calling.test.js +0 -107
- package/dist/utils/tests/math_utils.test.d.ts +0 -1
- package/dist/utils/tests/math_utils.test.js +0 -139
- package/dist/utils/tests/polyfill_stream.test.d.ts +0 -1
- package/dist/utils/tests/polyfill_stream.test.js +0 -15
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { similarity as ml_distance_similarity } from "ml-distance";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
import { BaseCallbackConfig, CallbackManagerForLLMRun, CallbackManagerForToolRun } from "../../callbacks/manager.js";
|
|
3
4
|
import { BaseChatMessageHistory, BaseListChatMessageHistory } from "../../chat_history.js";
|
|
@@ -11,8 +12,9 @@ import { BaseRetriever } from "../../retrievers/index.js";
|
|
|
11
12
|
import { Runnable } from "../../runnables/base.js";
|
|
12
13
|
import { StructuredTool, ToolParams } from "../../tools/index.js";
|
|
13
14
|
import { BaseTracer, Run } from "../../tracers/base.js";
|
|
14
|
-
import { Embeddings, EmbeddingsParams } from "../../embeddings.js";
|
|
15
|
+
import { Embeddings, EmbeddingsInterface, EmbeddingsParams } from "../../embeddings.js";
|
|
15
16
|
import { StructuredOutputMethodParams, BaseLanguageModelInput, StructuredOutputMethodOptions } from "../../language_models/base.js";
|
|
17
|
+
import { VectorStore } from "../../vectorstores.js";
|
|
16
18
|
/**
|
|
17
19
|
* Parser for comma-separated values. It splits the input text by commas
|
|
18
20
|
* and trims the resulting values.
|
|
@@ -87,6 +89,7 @@ export interface FakeChatInput extends BaseChatModelParams {
|
|
|
87
89
|
responses: string[];
|
|
88
90
|
/** Time to sleep in milliseconds between responses */
|
|
89
91
|
sleep?: number;
|
|
92
|
+
emitCustomEvent?: boolean;
|
|
90
93
|
}
|
|
91
94
|
/**
|
|
92
95
|
* A fake Chat Model that returns a predefined list of responses. It can be used
|
|
@@ -113,10 +116,11 @@ export declare class FakeListChatModel extends BaseChatModel {
|
|
|
113
116
|
responses: string[];
|
|
114
117
|
i: number;
|
|
115
118
|
sleep?: number;
|
|
116
|
-
|
|
119
|
+
emitCustomEvent: boolean;
|
|
120
|
+
constructor({ responses, sleep, emitCustomEvent }: FakeChatInput);
|
|
117
121
|
_combineLLMOutput(): never[];
|
|
118
122
|
_llmType(): string;
|
|
119
|
-
_generate(_messages: BaseMessage[], options?: this["ParsedCallOptions"]): Promise<ChatResult>;
|
|
123
|
+
_generate(_messages: BaseMessage[], options?: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
120
124
|
_formatGeneration(text: string): {
|
|
121
125
|
message: AIMessage;
|
|
122
126
|
text: string;
|
|
@@ -228,4 +232,91 @@ export declare class SingleRunExtractor extends BaseTracer {
|
|
|
228
232
|
persistRun(run: Run): Promise<void>;
|
|
229
233
|
extract(): Promise<Run>;
|
|
230
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Interface representing a vector in memory. It includes the content
|
|
237
|
+
* (text), the corresponding embedding (vector), and any associated
|
|
238
|
+
* metadata.
|
|
239
|
+
*/
|
|
240
|
+
interface MemoryVector {
|
|
241
|
+
content: string;
|
|
242
|
+
embedding: number[];
|
|
243
|
+
metadata: Record<string, any>;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Interface for the arguments that can be passed to the
|
|
247
|
+
* `FakeVectorStore` constructor. It includes an optional `similarity`
|
|
248
|
+
* function.
|
|
249
|
+
*/
|
|
250
|
+
export interface FakeVectorStoreArgs {
|
|
251
|
+
similarity?: typeof ml_distance_similarity.cosine;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Class that extends `VectorStore` to store vectors in memory. Provides
|
|
255
|
+
* methods for adding documents, performing similarity searches, and
|
|
256
|
+
* creating instances from texts, documents, or an existing index.
|
|
257
|
+
*/
|
|
258
|
+
export declare class FakeVectorStore extends VectorStore {
|
|
259
|
+
FilterType: (doc: Document) => boolean;
|
|
260
|
+
memoryVectors: MemoryVector[];
|
|
261
|
+
similarity: typeof ml_distance_similarity.cosine;
|
|
262
|
+
_vectorstoreType(): string;
|
|
263
|
+
constructor(embeddings: EmbeddingsInterface, { similarity, ...rest }?: FakeVectorStoreArgs);
|
|
264
|
+
/**
|
|
265
|
+
* Method to add documents to the memory vector store. It extracts the
|
|
266
|
+
* text from each document, generates embeddings for them, and adds the
|
|
267
|
+
* resulting vectors to the store.
|
|
268
|
+
* @param documents Array of `Document` instances to be added to the store.
|
|
269
|
+
* @returns Promise that resolves when all documents have been added.
|
|
270
|
+
*/
|
|
271
|
+
addDocuments(documents: Document[]): Promise<void>;
|
|
272
|
+
/**
|
|
273
|
+
* Method to add vectors to the memory vector store. It creates
|
|
274
|
+
* `MemoryVector` instances for each vector and document pair and adds
|
|
275
|
+
* them to the store.
|
|
276
|
+
* @param vectors Array of vectors to be added to the store.
|
|
277
|
+
* @param documents Array of `Document` instances corresponding to the vectors.
|
|
278
|
+
* @returns Promise that resolves when all vectors have been added.
|
|
279
|
+
*/
|
|
280
|
+
addVectors(vectors: number[][], documents: Document[]): Promise<void>;
|
|
281
|
+
/**
|
|
282
|
+
* Method to perform a similarity search in the memory vector store. It
|
|
283
|
+
* calculates the similarity between the query vector and each vector in
|
|
284
|
+
* the store, sorts the results by similarity, and returns the top `k`
|
|
285
|
+
* results along with their scores.
|
|
286
|
+
* @param query Query vector to compare against the vectors in the store.
|
|
287
|
+
* @param k Number of top results to return.
|
|
288
|
+
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
289
|
+
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
290
|
+
*/
|
|
291
|
+
similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[Document, number][]>;
|
|
292
|
+
/**
|
|
293
|
+
* Static method to create a `FakeVectorStore` instance from an array of
|
|
294
|
+
* texts. It creates a `Document` for each text and metadata pair, and
|
|
295
|
+
* adds them to the store.
|
|
296
|
+
* @param texts Array of texts to be added to the store.
|
|
297
|
+
* @param metadatas Array or single object of metadata corresponding to the texts.
|
|
298
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the texts.
|
|
299
|
+
* @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
|
|
300
|
+
* @returns Promise that resolves with a new `FakeVectorStore` instance.
|
|
301
|
+
*/
|
|
302
|
+
static fromTexts(texts: string[], metadatas: object[] | object, embeddings: EmbeddingsInterface, dbConfig?: FakeVectorStoreArgs): Promise<FakeVectorStore>;
|
|
303
|
+
/**
|
|
304
|
+
* Static method to create a `FakeVectorStore` instance from an array of
|
|
305
|
+
* `Document` instances. It adds the documents to the store.
|
|
306
|
+
* @param docs Array of `Document` instances to be added to the store.
|
|
307
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
308
|
+
* @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
|
|
309
|
+
* @returns Promise that resolves with a new `FakeVectorStore` instance.
|
|
310
|
+
*/
|
|
311
|
+
static fromDocuments(docs: Document[], embeddings: EmbeddingsInterface, dbConfig?: FakeVectorStoreArgs): Promise<FakeVectorStore>;
|
|
312
|
+
/**
|
|
313
|
+
* Static method to create a `FakeVectorStore` instance from an existing
|
|
314
|
+
* index. It creates a new `FakeVectorStore` instance without adding any
|
|
315
|
+
* documents or vectors.
|
|
316
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
317
|
+
* @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
|
|
318
|
+
* @returns Promise that resolves with a new `FakeVectorStore` instance.
|
|
319
|
+
*/
|
|
320
|
+
static fromExistingIndex(embeddings: EmbeddingsInterface, dbConfig?: FakeVectorStoreArgs): Promise<FakeVectorStore>;
|
|
321
|
+
}
|
|
231
322
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* eslint-disable no-promise-executor-return */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
4
|
+
import { similarity as ml_distance_similarity } from "ml-distance";
|
|
4
5
|
import { BaseChatMessageHistory, BaseListChatMessageHistory, } from "../../chat_history.js";
|
|
5
6
|
import { Document } from "../../documents/document.js";
|
|
6
7
|
import { BaseChatModel, } from "../../language_models/chat_models.js";
|
|
@@ -12,7 +13,8 @@ import { BaseRetriever } from "../../retrievers/index.js";
|
|
|
12
13
|
import { Runnable, RunnableLambda } from "../../runnables/base.js";
|
|
13
14
|
import { StructuredTool } from "../../tools/index.js";
|
|
14
15
|
import { BaseTracer } from "../../tracers/base.js";
|
|
15
|
-
import { Embeddings } from "../../embeddings.js";
|
|
16
|
+
import { Embeddings, } from "../../embeddings.js";
|
|
17
|
+
import { VectorStore } from "../../vectorstores.js";
|
|
16
18
|
/**
|
|
17
19
|
* Parser for comma-separated values. It splits the input text by commas
|
|
18
20
|
* and trims the resulting values.
|
|
@@ -297,7 +299,7 @@ export class FakeListChatModel extends BaseChatModel {
|
|
|
297
299
|
static lc_name() {
|
|
298
300
|
return "FakeListChatModel";
|
|
299
301
|
}
|
|
300
|
-
constructor({ responses, sleep }) {
|
|
302
|
+
constructor({ responses, sleep, emitCustomEvent }) {
|
|
301
303
|
super({});
|
|
302
304
|
Object.defineProperty(this, "responses", {
|
|
303
305
|
enumerable: true,
|
|
@@ -317,8 +319,15 @@ export class FakeListChatModel extends BaseChatModel {
|
|
|
317
319
|
writable: true,
|
|
318
320
|
value: void 0
|
|
319
321
|
});
|
|
322
|
+
Object.defineProperty(this, "emitCustomEvent", {
|
|
323
|
+
enumerable: true,
|
|
324
|
+
configurable: true,
|
|
325
|
+
writable: true,
|
|
326
|
+
value: false
|
|
327
|
+
});
|
|
320
328
|
this.responses = responses;
|
|
321
329
|
this.sleep = sleep;
|
|
330
|
+
this.emitCustomEvent = emitCustomEvent ?? this.emitCustomEvent;
|
|
322
331
|
}
|
|
323
332
|
_combineLLMOutput() {
|
|
324
333
|
return [];
|
|
@@ -326,8 +335,13 @@ export class FakeListChatModel extends BaseChatModel {
|
|
|
326
335
|
_llmType() {
|
|
327
336
|
return "fake-list";
|
|
328
337
|
}
|
|
329
|
-
async _generate(_messages, options) {
|
|
338
|
+
async _generate(_messages, options, runManager) {
|
|
330
339
|
await this._sleepIfRequested();
|
|
340
|
+
if (this.emitCustomEvent) {
|
|
341
|
+
await runManager?.handleCustomEvent("some_test_event", {
|
|
342
|
+
someval: true,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
331
345
|
if (options?.stop?.length) {
|
|
332
346
|
return {
|
|
333
347
|
generations: [this._formatGeneration(options.stop[0])],
|
|
@@ -351,6 +365,11 @@ export class FakeListChatModel extends BaseChatModel {
|
|
|
351
365
|
async *_streamResponseChunks(_messages, _options, runManager) {
|
|
352
366
|
const response = this._currentResponse();
|
|
353
367
|
this._incrementResponse();
|
|
368
|
+
if (this.emitCustomEvent) {
|
|
369
|
+
await runManager?.handleCustomEvent("some_test_event", {
|
|
370
|
+
someval: true,
|
|
371
|
+
});
|
|
372
|
+
}
|
|
354
373
|
for await (const text of response) {
|
|
355
374
|
await this._sleepIfRequested();
|
|
356
375
|
const chunk = this._createResponseChunk(text);
|
|
@@ -618,3 +637,141 @@ export class SingleRunExtractor extends BaseTracer {
|
|
|
618
637
|
return this.runPromise;
|
|
619
638
|
}
|
|
620
639
|
}
|
|
640
|
+
/**
|
|
641
|
+
* Class that extends `VectorStore` to store vectors in memory. Provides
|
|
642
|
+
* methods for adding documents, performing similarity searches, and
|
|
643
|
+
* creating instances from texts, documents, or an existing index.
|
|
644
|
+
*/
|
|
645
|
+
export class FakeVectorStore extends VectorStore {
|
|
646
|
+
_vectorstoreType() {
|
|
647
|
+
return "memory";
|
|
648
|
+
}
|
|
649
|
+
constructor(embeddings, { similarity, ...rest } = {}) {
|
|
650
|
+
super(embeddings, rest);
|
|
651
|
+
Object.defineProperty(this, "memoryVectors", {
|
|
652
|
+
enumerable: true,
|
|
653
|
+
configurable: true,
|
|
654
|
+
writable: true,
|
|
655
|
+
value: []
|
|
656
|
+
});
|
|
657
|
+
Object.defineProperty(this, "similarity", {
|
|
658
|
+
enumerable: true,
|
|
659
|
+
configurable: true,
|
|
660
|
+
writable: true,
|
|
661
|
+
value: void 0
|
|
662
|
+
});
|
|
663
|
+
this.similarity = similarity ?? ml_distance_similarity.cosine;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Method to add documents to the memory vector store. It extracts the
|
|
667
|
+
* text from each document, generates embeddings for them, and adds the
|
|
668
|
+
* resulting vectors to the store.
|
|
669
|
+
* @param documents Array of `Document` instances to be added to the store.
|
|
670
|
+
* @returns Promise that resolves when all documents have been added.
|
|
671
|
+
*/
|
|
672
|
+
async addDocuments(documents) {
|
|
673
|
+
const texts = documents.map(({ pageContent }) => pageContent);
|
|
674
|
+
return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Method to add vectors to the memory vector store. It creates
|
|
678
|
+
* `MemoryVector` instances for each vector and document pair and adds
|
|
679
|
+
* them to the store.
|
|
680
|
+
* @param vectors Array of vectors to be added to the store.
|
|
681
|
+
* @param documents Array of `Document` instances corresponding to the vectors.
|
|
682
|
+
* @returns Promise that resolves when all vectors have been added.
|
|
683
|
+
*/
|
|
684
|
+
async addVectors(vectors, documents) {
|
|
685
|
+
const memoryVectors = vectors.map((embedding, idx) => ({
|
|
686
|
+
content: documents[idx].pageContent,
|
|
687
|
+
embedding,
|
|
688
|
+
metadata: documents[idx].metadata,
|
|
689
|
+
}));
|
|
690
|
+
this.memoryVectors = this.memoryVectors.concat(memoryVectors);
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Method to perform a similarity search in the memory vector store. It
|
|
694
|
+
* calculates the similarity between the query vector and each vector in
|
|
695
|
+
* the store, sorts the results by similarity, and returns the top `k`
|
|
696
|
+
* results along with their scores.
|
|
697
|
+
* @param query Query vector to compare against the vectors in the store.
|
|
698
|
+
* @param k Number of top results to return.
|
|
699
|
+
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
700
|
+
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
701
|
+
*/
|
|
702
|
+
async similaritySearchVectorWithScore(query, k, filter) {
|
|
703
|
+
const filterFunction = (memoryVector) => {
|
|
704
|
+
if (!filter) {
|
|
705
|
+
return true;
|
|
706
|
+
}
|
|
707
|
+
const doc = new Document({
|
|
708
|
+
metadata: memoryVector.metadata,
|
|
709
|
+
pageContent: memoryVector.content,
|
|
710
|
+
});
|
|
711
|
+
return filter(doc);
|
|
712
|
+
};
|
|
713
|
+
const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
|
|
714
|
+
const searches = filteredMemoryVectors
|
|
715
|
+
.map((vector, index) => ({
|
|
716
|
+
similarity: this.similarity(query, vector.embedding),
|
|
717
|
+
index,
|
|
718
|
+
}))
|
|
719
|
+
.sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
|
|
720
|
+
.slice(0, k);
|
|
721
|
+
const result = searches.map((search) => [
|
|
722
|
+
new Document({
|
|
723
|
+
metadata: filteredMemoryVectors[search.index].metadata,
|
|
724
|
+
pageContent: filteredMemoryVectors[search.index].content,
|
|
725
|
+
}),
|
|
726
|
+
search.similarity,
|
|
727
|
+
]);
|
|
728
|
+
return result;
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Static method to create a `FakeVectorStore` instance from an array of
|
|
732
|
+
* texts. It creates a `Document` for each text and metadata pair, and
|
|
733
|
+
* adds them to the store.
|
|
734
|
+
* @param texts Array of texts to be added to the store.
|
|
735
|
+
* @param metadatas Array or single object of metadata corresponding to the texts.
|
|
736
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the texts.
|
|
737
|
+
* @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
|
|
738
|
+
* @returns Promise that resolves with a new `FakeVectorStore` instance.
|
|
739
|
+
*/
|
|
740
|
+
static async fromTexts(texts, metadatas, embeddings, dbConfig) {
|
|
741
|
+
const docs = [];
|
|
742
|
+
for (let i = 0; i < texts.length; i += 1) {
|
|
743
|
+
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
|
|
744
|
+
const newDoc = new Document({
|
|
745
|
+
pageContent: texts[i],
|
|
746
|
+
metadata,
|
|
747
|
+
});
|
|
748
|
+
docs.push(newDoc);
|
|
749
|
+
}
|
|
750
|
+
return FakeVectorStore.fromDocuments(docs, embeddings, dbConfig);
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Static method to create a `FakeVectorStore` instance from an array of
|
|
754
|
+
* `Document` instances. It adds the documents to the store.
|
|
755
|
+
* @param docs Array of `Document` instances to be added to the store.
|
|
756
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
757
|
+
* @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
|
|
758
|
+
* @returns Promise that resolves with a new `FakeVectorStore` instance.
|
|
759
|
+
*/
|
|
760
|
+
static async fromDocuments(docs, embeddings, dbConfig) {
|
|
761
|
+
const instance = new this(embeddings, dbConfig);
|
|
762
|
+
await instance.addDocuments(docs);
|
|
763
|
+
return instance;
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Static method to create a `FakeVectorStore` instance from an existing
|
|
767
|
+
* index. It creates a new `FakeVectorStore` instance without adding any
|
|
768
|
+
* documents or vectors.
|
|
769
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
770
|
+
* @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
|
|
771
|
+
* @returns Promise that resolves with a new `FakeVectorStore` instance.
|
|
772
|
+
*/
|
|
773
|
+
static async fromExistingIndex(embeddings, dbConfig) {
|
|
774
|
+
const instance = new this(embeddings, dbConfig);
|
|
775
|
+
return instance;
|
|
776
|
+
}
|
|
777
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.19",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"homepage": "https://github.com/langchain-ai/langchainjs/tree/main/langchain-core/",
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "yarn turbo:command build:internal --filter=@langchain/core",
|
|
18
|
-
"build:internal": "yarn
|
|
18
|
+
"build:internal": "yarn lc_build_v2 --create-entrypoints --pre --tree-shaking",
|
|
19
19
|
"clean": "rm -rf .turbo dist/",
|
|
20
20
|
"build:deps": "yarn turbo build",
|
|
21
21
|
"build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist/**/tests",
|
|
@@ -56,9 +56,10 @@
|
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@jest/globals": "^29.5.0",
|
|
59
|
-
"@langchain/scripts": "~0.0.
|
|
59
|
+
"@langchain/scripts": "~0.0.20",
|
|
60
60
|
"@swc/core": "^1.3.90",
|
|
61
61
|
"@swc/jest": "^0.2.29",
|
|
62
|
+
"@types/decamelize": "^1.2.0",
|
|
62
63
|
"@types/mustache": "^4",
|
|
63
64
|
"dpdm": "^3.12.0",
|
|
64
65
|
"eslint": "^8.33.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { InMemoryCache } from "../base.js";
|
|
2
|
-
test("InMemoryCache works", async () => {
|
|
3
|
-
const cache = new InMemoryCache();
|
|
4
|
-
await cache.update("prompt", "key1", [
|
|
5
|
-
{
|
|
6
|
-
text: "text1",
|
|
7
|
-
},
|
|
8
|
-
]);
|
|
9
|
-
const result = await cache.lookup("prompt", "key1");
|
|
10
|
-
expect(result).toBeDefined();
|
|
11
|
-
if (!result) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
expect(result[0].text).toBe("text1");
|
|
15
|
-
});
|
|
16
|
-
test("InMemoryCache works with complex message types", async () => {
|
|
17
|
-
const cache = new InMemoryCache();
|
|
18
|
-
await cache.update("prompt", "key1", [
|
|
19
|
-
{
|
|
20
|
-
type: "text",
|
|
21
|
-
text: "text1",
|
|
22
|
-
},
|
|
23
|
-
]);
|
|
24
|
-
const result = await cache.lookup("prompt", "key1");
|
|
25
|
-
expect(result).toBeDefined();
|
|
26
|
-
if (!result) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
expect(result[0]).toEqual({
|
|
30
|
-
type: "text",
|
|
31
|
-
text: "text1",
|
|
32
|
-
});
|
|
33
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|