@langchain/core 0.2.18 → 0.2.20

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.
Files changed (155) hide show
  1. package/dist/callbacks/manager.cjs +94 -19
  2. package/dist/callbacks/manager.d.ts +3 -1
  3. package/dist/callbacks/manager.js +94 -19
  4. package/dist/language_models/base.d.ts +0 -10
  5. package/dist/language_models/chat_models.cjs +5 -6
  6. package/dist/language_models/chat_models.d.ts +9 -10
  7. package/dist/language_models/chat_models.js +5 -6
  8. package/dist/language_models/llms.cjs +5 -6
  9. package/dist/language_models/llms.d.ts +10 -12
  10. package/dist/language_models/llms.js +5 -6
  11. package/dist/messages/base.cjs +16 -1
  12. package/dist/messages/base.d.ts +9 -0
  13. package/dist/messages/base.js +14 -0
  14. package/dist/messages/tool.cjs +23 -0
  15. package/dist/messages/tool.d.ts +15 -0
  16. package/dist/messages/tool.js +24 -1
  17. package/dist/messages/utils.cjs +1 -1
  18. package/dist/messages/utils.js +1 -1
  19. package/dist/output_parsers/string.cjs +1 -0
  20. package/dist/output_parsers/string.js +1 -0
  21. package/dist/runnables/base.cjs +35 -12
  22. package/dist/runnables/base.js +35 -12
  23. package/dist/runnables/config.cjs +41 -0
  24. package/dist/runnables/config.js +41 -0
  25. package/dist/runnables/remote.cjs +17 -14
  26. package/dist/runnables/remote.js +17 -14
  27. package/dist/runnables/types.d.ts +10 -0
  28. package/dist/tools/index.cjs +15 -6
  29. package/dist/tools/index.d.ts +21 -12
  30. package/dist/tools/index.js +15 -6
  31. package/dist/utils/math.cjs +6 -4
  32. package/dist/utils/math.js +6 -4
  33. package/dist/utils/ml-distance/distances.cjs +18 -0
  34. package/dist/utils/ml-distance/distances.d.ts +8 -0
  35. package/dist/utils/ml-distance/distances.js +14 -0
  36. package/dist/utils/ml-distance/similarities.cjs +21 -0
  37. package/dist/utils/ml-distance/similarities.d.ts +7 -0
  38. package/dist/utils/ml-distance/similarities.js +17 -0
  39. package/dist/utils/ml-distance-euclidean/euclidean.cjs +15 -0
  40. package/dist/utils/ml-distance-euclidean/euclidean.d.ts +2 -0
  41. package/dist/utils/ml-distance-euclidean/euclidean.js +10 -0
  42. package/dist/utils/signal.cjs +28 -0
  43. package/dist/utils/signal.d.ts +1 -0
  44. package/dist/utils/signal.js +24 -0
  45. package/dist/utils/stream.cjs +19 -4
  46. package/dist/utils/stream.d.ts +3 -1
  47. package/dist/utils/stream.js +19 -4
  48. package/dist/utils/testing/index.cjs +168 -4
  49. package/dist/utils/testing/index.d.ts +100 -6
  50. package/dist/utils/testing/index.js +167 -4
  51. package/package.json +4 -4
  52. package/dist/caches/tests/in_memory_cache.test.d.ts +0 -1
  53. package/dist/caches/tests/in_memory_cache.test.js +0 -33
  54. package/dist/callbacks/tests/callbacks.test.d.ts +0 -1
  55. package/dist/callbacks/tests/callbacks.test.js +0 -495
  56. package/dist/callbacks/tests/manager.int.test.d.ts +0 -1
  57. package/dist/callbacks/tests/manager.int.test.js +0 -29
  58. package/dist/callbacks/tests/run_collector.test.d.ts +0 -1
  59. package/dist/callbacks/tests/run_collector.test.js +0 -58
  60. package/dist/language_models/tests/chat_models.test.d.ts +0 -1
  61. package/dist/language_models/tests/chat_models.test.js +0 -204
  62. package/dist/language_models/tests/count_tokens.test.d.ts +0 -1
  63. package/dist/language_models/tests/count_tokens.test.js +0 -19
  64. package/dist/language_models/tests/llms.test.d.ts +0 -1
  65. package/dist/language_models/tests/llms.test.js +0 -52
  66. package/dist/messages/tests/base_message.test.d.ts +0 -1
  67. package/dist/messages/tests/base_message.test.js +0 -245
  68. package/dist/messages/tests/message_utils.test.d.ts +0 -1
  69. package/dist/messages/tests/message_utils.test.js +0 -434
  70. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +0 -1
  71. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +0 -81
  72. package/dist/output_parsers/tests/json.test.d.ts +0 -1
  73. package/dist/output_parsers/tests/json.test.js +0 -427
  74. package/dist/output_parsers/tests/output_parser.test.d.ts +0 -1
  75. package/dist/output_parsers/tests/output_parser.test.js +0 -78
  76. package/dist/output_parsers/tests/string.test.d.ts +0 -1
  77. package/dist/output_parsers/tests/string.test.js +0 -68
  78. package/dist/output_parsers/tests/structured.test.d.ts +0 -1
  79. package/dist/output_parsers/tests/structured.test.js +0 -166
  80. package/dist/output_parsers/tests/xml.test.d.ts +0 -1
  81. package/dist/output_parsers/tests/xml.test.js +0 -81
  82. package/dist/prompts/tests/chat.mustache.test.d.ts +0 -1
  83. package/dist/prompts/tests/chat.mustache.test.js +0 -129
  84. package/dist/prompts/tests/chat.test.d.ts +0 -1
  85. package/dist/prompts/tests/chat.test.js +0 -557
  86. package/dist/prompts/tests/few_shot.test.d.ts +0 -1
  87. package/dist/prompts/tests/few_shot.test.js +0 -224
  88. package/dist/prompts/tests/pipeline.test.d.ts +0 -1
  89. package/dist/prompts/tests/pipeline.test.js +0 -101
  90. package/dist/prompts/tests/prompt.mustache.test.d.ts +0 -1
  91. package/dist/prompts/tests/prompt.mustache.test.js +0 -105
  92. package/dist/prompts/tests/prompt.test.d.ts +0 -1
  93. package/dist/prompts/tests/prompt.test.js +0 -78
  94. package/dist/prompts/tests/structured.test.d.ts +0 -1
  95. package/dist/prompts/tests/structured.test.js +0 -37
  96. package/dist/prompts/tests/template.test.d.ts +0 -1
  97. package/dist/prompts/tests/template.test.js +0 -24
  98. package/dist/runnables/tests/runnable.test.d.ts +0 -1
  99. package/dist/runnables/tests/runnable.test.js +0 -491
  100. package/dist/runnables/tests/runnable_binding.test.d.ts +0 -1
  101. package/dist/runnables/tests/runnable_binding.test.js +0 -46
  102. package/dist/runnables/tests/runnable_branch.test.d.ts +0 -1
  103. package/dist/runnables/tests/runnable_branch.test.js +0 -116
  104. package/dist/runnables/tests/runnable_graph.test.d.ts +0 -1
  105. package/dist/runnables/tests/runnable_graph.test.js +0 -100
  106. package/dist/runnables/tests/runnable_history.test.d.ts +0 -1
  107. package/dist/runnables/tests/runnable_history.test.js +0 -177
  108. package/dist/runnables/tests/runnable_interface.test.d.ts +0 -1
  109. package/dist/runnables/tests/runnable_interface.test.js +0 -209
  110. package/dist/runnables/tests/runnable_map.test.d.ts +0 -1
  111. package/dist/runnables/tests/runnable_map.test.js +0 -238
  112. package/dist/runnables/tests/runnable_passthrough.test.d.ts +0 -1
  113. package/dist/runnables/tests/runnable_passthrough.test.js +0 -96
  114. package/dist/runnables/tests/runnable_remote.int.test.d.ts +0 -1
  115. package/dist/runnables/tests/runnable_remote.int.test.js +0 -138
  116. package/dist/runnables/tests/runnable_remote.test.d.ts +0 -1
  117. package/dist/runnables/tests/runnable_remote.test.js +0 -200
  118. package/dist/runnables/tests/runnable_retry.test.d.ts +0 -1
  119. package/dist/runnables/tests/runnable_retry.test.js +0 -125
  120. package/dist/runnables/tests/runnable_stream_events.test.d.ts +0 -1
  121. package/dist/runnables/tests/runnable_stream_events.test.js +0 -1013
  122. package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +0 -1
  123. package/dist/runnables/tests/runnable_stream_events_v2.test.js +0 -2022
  124. package/dist/runnables/tests/runnable_stream_log.test.d.ts +0 -1
  125. package/dist/runnables/tests/runnable_stream_log.test.js +0 -71
  126. package/dist/runnables/tests/runnable_tools.test.d.ts +0 -1
  127. package/dist/runnables/tests/runnable_tools.test.js +0 -149
  128. package/dist/runnables/tests/runnable_tracing.int.test.d.ts +0 -1
  129. package/dist/runnables/tests/runnable_tracing.int.test.js +0 -37
  130. package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +0 -1
  131. package/dist/runnables/tests/runnable_with_fallbacks.test.js +0 -36
  132. package/dist/singletons/tests/async_local_storage.test.d.ts +0 -1
  133. package/dist/singletons/tests/async_local_storage.test.js +0 -153
  134. package/dist/structured_query/tests/utils.test.d.ts +0 -1
  135. package/dist/structured_query/tests/utils.test.js +0 -47
  136. package/dist/tools/tests/tools.test.d.ts +0 -1
  137. package/dist/tools/tests/tools.test.js +0 -85
  138. package/dist/tracers/tests/langchain_tracer.int.test.d.ts +0 -1
  139. package/dist/tracers/tests/langchain_tracer.int.test.js +0 -74
  140. package/dist/tracers/tests/langsmith_interop.test.d.ts +0 -1
  141. package/dist/tracers/tests/langsmith_interop.test.js +0 -551
  142. package/dist/tracers/tests/tracer.test.d.ts +0 -1
  143. package/dist/tracers/tests/tracer.test.js +0 -378
  144. package/dist/utils/testing/tests/chatfake.test.d.ts +0 -1
  145. package/dist/utils/testing/tests/chatfake.test.js +0 -112
  146. package/dist/utils/tests/async_caller.test.d.ts +0 -1
  147. package/dist/utils/tests/async_caller.test.js +0 -27
  148. package/dist/utils/tests/enviroment.test.d.ts +0 -1
  149. package/dist/utils/tests/enviroment.test.js +0 -6
  150. package/dist/utils/tests/function_calling.test.d.ts +0 -1
  151. package/dist/utils/tests/function_calling.test.js +0 -107
  152. package/dist/utils/tests/math_utils.test.d.ts +0 -1
  153. package/dist/utils/tests/math_utils.test.js +0 -139
  154. package/dist/utils/tests/polyfill_stream.test.d.ts +0 -1
  155. package/dist/utils/tests/polyfill_stream.test.js +0 -15
@@ -3,7 +3,7 @@
3
3
  /* eslint-disable @typescript-eslint/no-explicit-any */
4
4
  /* eslint-disable @typescript-eslint/no-unused-vars */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.SingleRunExtractor = exports.SyntheticEmbeddings = exports.FakeEmbeddings = exports.FakeTool = exports.FakeTracer = exports.FakeListChatMessageHistory = exports.FakeChatMessageHistory = exports.FakeListChatModel = exports.FakeRetriever = exports.FakeStreamingChatModel = exports.FakeChatModel = exports.FakeStreamingLLM = exports.FakeLLM = exports.FakeRunnable = exports.FakeSplitIntoListParser = void 0;
6
+ exports.FakeVectorStore = exports.SingleRunExtractor = exports.SyntheticEmbeddings = exports.FakeEmbeddings = exports.FakeTool = exports.FakeTracer = exports.FakeListChatMessageHistory = exports.FakeChatMessageHistory = exports.FakeListChatModel = exports.FakeRetriever = exports.FakeStreamingChatModel = exports.FakeChatModel = exports.FakeStreamingLLM = exports.FakeLLM = exports.FakeRunnable = exports.FakeSplitIntoListParser = void 0;
7
7
  const chat_history_js_1 = require("../../chat_history.cjs");
8
8
  const document_js_1 = require("../../documents/document.cjs");
9
9
  const chat_models_js_1 = require("../../language_models/chat_models.cjs");
@@ -16,6 +16,8 @@ const base_js_2 = require("../../runnables/base.cjs");
16
16
  const index_js_3 = require("../../tools/index.cjs");
17
17
  const base_js_3 = require("../../tracers/base.cjs");
18
18
  const embeddings_js_1 = require("../../embeddings.cjs");
19
+ const vectorstores_js_1 = require("../../vectorstores.cjs");
20
+ const similarities_js_1 = require("../ml-distance/similarities.cjs");
19
21
  /**
20
22
  * Parser for comma-separated values. It splits the input text by commas
21
23
  * and trims the resulting values.
@@ -307,7 +309,7 @@ class FakeListChatModel extends chat_models_js_1.BaseChatModel {
307
309
  static lc_name() {
308
310
  return "FakeListChatModel";
309
311
  }
310
- constructor({ responses, sleep }) {
312
+ constructor({ responses, sleep, emitCustomEvent }) {
311
313
  super({});
312
314
  Object.defineProperty(this, "responses", {
313
315
  enumerable: true,
@@ -327,8 +329,15 @@ class FakeListChatModel extends chat_models_js_1.BaseChatModel {
327
329
  writable: true,
328
330
  value: void 0
329
331
  });
332
+ Object.defineProperty(this, "emitCustomEvent", {
333
+ enumerable: true,
334
+ configurable: true,
335
+ writable: true,
336
+ value: false
337
+ });
330
338
  this.responses = responses;
331
339
  this.sleep = sleep;
340
+ this.emitCustomEvent = emitCustomEvent ?? this.emitCustomEvent;
332
341
  }
333
342
  _combineLLMOutput() {
334
343
  return [];
@@ -336,8 +345,16 @@ class FakeListChatModel extends chat_models_js_1.BaseChatModel {
336
345
  _llmType() {
337
346
  return "fake-list";
338
347
  }
339
- async _generate(_messages, options) {
348
+ async _generate(_messages, options, runManager) {
340
349
  await this._sleepIfRequested();
350
+ if (options?.thrownErrorString) {
351
+ throw new Error(options.thrownErrorString);
352
+ }
353
+ if (this.emitCustomEvent) {
354
+ await runManager?.handleCustomEvent("some_test_event", {
355
+ someval: true,
356
+ });
357
+ }
341
358
  if (options?.stop?.length) {
342
359
  return {
343
360
  generations: [this._formatGeneration(options.stop[0])],
@@ -358,11 +375,19 @@ class FakeListChatModel extends chat_models_js_1.BaseChatModel {
358
375
  text,
359
376
  };
360
377
  }
361
- async *_streamResponseChunks(_messages, _options, runManager) {
378
+ async *_streamResponseChunks(_messages, options, runManager) {
362
379
  const response = this._currentResponse();
363
380
  this._incrementResponse();
381
+ if (this.emitCustomEvent) {
382
+ await runManager?.handleCustomEvent("some_test_event", {
383
+ someval: true,
384
+ });
385
+ }
364
386
  for await (const text of response) {
365
387
  await this._sleepIfRequested();
388
+ if (options?.thrownErrorString) {
389
+ throw new Error(options.thrownErrorString);
390
+ }
366
391
  const chunk = this._createResponseChunk(text);
367
392
  yield chunk;
368
393
  void runManager?.handleLLMNewToken(text);
@@ -636,3 +661,142 @@ class SingleRunExtractor extends base_js_3.BaseTracer {
636
661
  }
637
662
  }
638
663
  exports.SingleRunExtractor = SingleRunExtractor;
664
+ /**
665
+ * Class that extends `VectorStore` to store vectors in memory. Provides
666
+ * methods for adding documents, performing similarity searches, and
667
+ * creating instances from texts, documents, or an existing index.
668
+ */
669
+ class FakeVectorStore extends vectorstores_js_1.VectorStore {
670
+ _vectorstoreType() {
671
+ return "memory";
672
+ }
673
+ constructor(embeddings, { similarity, ...rest } = {}) {
674
+ super(embeddings, rest);
675
+ Object.defineProperty(this, "memoryVectors", {
676
+ enumerable: true,
677
+ configurable: true,
678
+ writable: true,
679
+ value: []
680
+ });
681
+ Object.defineProperty(this, "similarity", {
682
+ enumerable: true,
683
+ configurable: true,
684
+ writable: true,
685
+ value: void 0
686
+ });
687
+ this.similarity = similarity ?? similarities_js_1.cosine;
688
+ }
689
+ /**
690
+ * Method to add documents to the memory vector store. It extracts the
691
+ * text from each document, generates embeddings for them, and adds the
692
+ * resulting vectors to the store.
693
+ * @param documents Array of `Document` instances to be added to the store.
694
+ * @returns Promise that resolves when all documents have been added.
695
+ */
696
+ async addDocuments(documents) {
697
+ const texts = documents.map(({ pageContent }) => pageContent);
698
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
699
+ }
700
+ /**
701
+ * Method to add vectors to the memory vector store. It creates
702
+ * `MemoryVector` instances for each vector and document pair and adds
703
+ * them to the store.
704
+ * @param vectors Array of vectors to be added to the store.
705
+ * @param documents Array of `Document` instances corresponding to the vectors.
706
+ * @returns Promise that resolves when all vectors have been added.
707
+ */
708
+ async addVectors(vectors, documents) {
709
+ const memoryVectors = vectors.map((embedding, idx) => ({
710
+ content: documents[idx].pageContent,
711
+ embedding,
712
+ metadata: documents[idx].metadata,
713
+ }));
714
+ this.memoryVectors = this.memoryVectors.concat(memoryVectors);
715
+ }
716
+ /**
717
+ * Method to perform a similarity search in the memory vector store. It
718
+ * calculates the similarity between the query vector and each vector in
719
+ * the store, sorts the results by similarity, and returns the top `k`
720
+ * results along with their scores.
721
+ * @param query Query vector to compare against the vectors in the store.
722
+ * @param k Number of top results to return.
723
+ * @param filter Optional filter function to apply to the vectors before performing the search.
724
+ * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
725
+ */
726
+ async similaritySearchVectorWithScore(query, k, filter) {
727
+ const filterFunction = (memoryVector) => {
728
+ if (!filter) {
729
+ return true;
730
+ }
731
+ const doc = new document_js_1.Document({
732
+ metadata: memoryVector.metadata,
733
+ pageContent: memoryVector.content,
734
+ });
735
+ return filter(doc);
736
+ };
737
+ const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
738
+ const searches = filteredMemoryVectors
739
+ .map((vector, index) => ({
740
+ similarity: this.similarity(query, vector.embedding),
741
+ index,
742
+ }))
743
+ .sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
744
+ .slice(0, k);
745
+ const result = searches.map((search) => [
746
+ new document_js_1.Document({
747
+ metadata: filteredMemoryVectors[search.index].metadata,
748
+ pageContent: filteredMemoryVectors[search.index].content,
749
+ }),
750
+ search.similarity,
751
+ ]);
752
+ return result;
753
+ }
754
+ /**
755
+ * Static method to create a `FakeVectorStore` instance from an array of
756
+ * texts. It creates a `Document` for each text and metadata pair, and
757
+ * adds them to the store.
758
+ * @param texts Array of texts to be added to the store.
759
+ * @param metadatas Array or single object of metadata corresponding to the texts.
760
+ * @param embeddings `Embeddings` instance used to generate embeddings for the texts.
761
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
762
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
763
+ */
764
+ static async fromTexts(texts, metadatas, embeddings, dbConfig) {
765
+ const docs = [];
766
+ for (let i = 0; i < texts.length; i += 1) {
767
+ const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
768
+ const newDoc = new document_js_1.Document({
769
+ pageContent: texts[i],
770
+ metadata,
771
+ });
772
+ docs.push(newDoc);
773
+ }
774
+ return FakeVectorStore.fromDocuments(docs, embeddings, dbConfig);
775
+ }
776
+ /**
777
+ * Static method to create a `FakeVectorStore` instance from an array of
778
+ * `Document` instances. It adds the documents to the store.
779
+ * @param docs Array of `Document` instances to be added to the store.
780
+ * @param embeddings `Embeddings` instance used to generate embeddings for the documents.
781
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
782
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
783
+ */
784
+ static async fromDocuments(docs, embeddings, dbConfig) {
785
+ const instance = new this(embeddings, dbConfig);
786
+ await instance.addDocuments(docs);
787
+ return instance;
788
+ }
789
+ /**
790
+ * Static method to create a `FakeVectorStore` instance from an existing
791
+ * index. It creates a new `FakeVectorStore` instance without adding any
792
+ * documents or vectors.
793
+ * @param embeddings `Embeddings` instance used to generate embeddings for the documents.
794
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
795
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
796
+ */
797
+ static async fromExistingIndex(embeddings, dbConfig) {
798
+ const instance = new this(embeddings, dbConfig);
799
+ return instance;
800
+ }
801
+ }
802
+ exports.FakeVectorStore = FakeVectorStore;
@@ -2,7 +2,7 @@ import { z } from "zod";
2
2
  import { BaseCallbackConfig, CallbackManagerForLLMRun, CallbackManagerForToolRun } from "../../callbacks/manager.js";
3
3
  import { BaseChatMessageHistory, BaseListChatMessageHistory } from "../../chat_history.js";
4
4
  import { Document } from "../../documents/document.js";
5
- import { BaseChatModel, BaseChatModelParams } from "../../language_models/chat_models.js";
5
+ import { BaseChatModel, BaseChatModelCallOptions, BaseChatModelParams } from "../../language_models/chat_models.js";
6
6
  import { BaseLLMParams, LLM } from "../../language_models/llms.js";
7
7
  import { BaseMessage, AIMessage } from "../../messages/index.js";
8
8
  import { BaseOutputParser } from "../../output_parsers/base.js";
@@ -11,8 +11,10 @@ import { BaseRetriever } from "../../retrievers/index.js";
11
11
  import { Runnable } from "../../runnables/base.js";
12
12
  import { StructuredTool, ToolParams } from "../../tools/index.js";
13
13
  import { BaseTracer, Run } from "../../tracers/base.js";
14
- import { Embeddings, EmbeddingsParams } from "../../embeddings.js";
14
+ import { Embeddings, EmbeddingsInterface, EmbeddingsParams } from "../../embeddings.js";
15
15
  import { StructuredOutputMethodParams, BaseLanguageModelInput, StructuredOutputMethodOptions } from "../../language_models/base.js";
16
+ import { VectorStore } from "../../vectorstores.js";
17
+ import { cosine } from "../ml-distance/similarities.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,10 @@ 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;
93
+ }
94
+ export interface FakeListChatModelCallOptions extends BaseChatModelCallOptions {
95
+ thrownErrorString?: string;
90
96
  }
91
97
  /**
92
98
  * A fake Chat Model that returns a predefined list of responses. It can be used
@@ -108,20 +114,21 @@ export interface FakeChatInput extends BaseChatModelParams {
108
114
  * console.log({ secondResponse });
109
115
  * ```
110
116
  */
111
- export declare class FakeListChatModel extends BaseChatModel {
117
+ export declare class FakeListChatModel extends BaseChatModel<FakeListChatModelCallOptions> {
112
118
  static lc_name(): string;
113
119
  responses: string[];
114
120
  i: number;
115
121
  sleep?: number;
116
- constructor({ responses, sleep }: FakeChatInput);
122
+ emitCustomEvent: boolean;
123
+ constructor({ responses, sleep, emitCustomEvent }: FakeChatInput);
117
124
  _combineLLMOutput(): never[];
118
125
  _llmType(): string;
119
- _generate(_messages: BaseMessage[], options?: this["ParsedCallOptions"]): Promise<ChatResult>;
126
+ _generate(_messages: BaseMessage[], options?: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
120
127
  _formatGeneration(text: string): {
121
128
  message: AIMessage;
122
129
  text: string;
123
130
  };
124
- _streamResponseChunks(_messages: BaseMessage[], _options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
131
+ _streamResponseChunks(_messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
125
132
  _sleepIfRequested(): Promise<void>;
126
133
  _sleep(): Promise<void>;
127
134
  _createResponseChunk(text: string): ChatGenerationChunk;
@@ -228,4 +235,91 @@ export declare class SingleRunExtractor extends BaseTracer {
228
235
  persistRun(run: Run): Promise<void>;
229
236
  extract(): Promise<Run>;
230
237
  }
238
+ /**
239
+ * Interface representing a vector in memory. It includes the content
240
+ * (text), the corresponding embedding (vector), and any associated
241
+ * metadata.
242
+ */
243
+ interface MemoryVector {
244
+ content: string;
245
+ embedding: number[];
246
+ metadata: Record<string, any>;
247
+ }
248
+ /**
249
+ * Interface for the arguments that can be passed to the
250
+ * `FakeVectorStore` constructor. It includes an optional `similarity`
251
+ * function.
252
+ */
253
+ export interface FakeVectorStoreArgs {
254
+ similarity?: typeof cosine;
255
+ }
256
+ /**
257
+ * Class that extends `VectorStore` to store vectors in memory. Provides
258
+ * methods for adding documents, performing similarity searches, and
259
+ * creating instances from texts, documents, or an existing index.
260
+ */
261
+ export declare class FakeVectorStore extends VectorStore {
262
+ FilterType: (doc: Document) => boolean;
263
+ memoryVectors: MemoryVector[];
264
+ similarity: typeof cosine;
265
+ _vectorstoreType(): string;
266
+ constructor(embeddings: EmbeddingsInterface, { similarity, ...rest }?: FakeVectorStoreArgs);
267
+ /**
268
+ * Method to add documents to the memory vector store. It extracts the
269
+ * text from each document, generates embeddings for them, and adds the
270
+ * resulting vectors to the store.
271
+ * @param documents Array of `Document` instances to be added to the store.
272
+ * @returns Promise that resolves when all documents have been added.
273
+ */
274
+ addDocuments(documents: Document[]): Promise<void>;
275
+ /**
276
+ * Method to add vectors to the memory vector store. It creates
277
+ * `MemoryVector` instances for each vector and document pair and adds
278
+ * them to the store.
279
+ * @param vectors Array of vectors to be added to the store.
280
+ * @param documents Array of `Document` instances corresponding to the vectors.
281
+ * @returns Promise that resolves when all vectors have been added.
282
+ */
283
+ addVectors(vectors: number[][], documents: Document[]): Promise<void>;
284
+ /**
285
+ * Method to perform a similarity search in the memory vector store. It
286
+ * calculates the similarity between the query vector and each vector in
287
+ * the store, sorts the results by similarity, and returns the top `k`
288
+ * results along with their scores.
289
+ * @param query Query vector to compare against the vectors in the store.
290
+ * @param k Number of top results to return.
291
+ * @param filter Optional filter function to apply to the vectors before performing the search.
292
+ * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
293
+ */
294
+ similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[Document, number][]>;
295
+ /**
296
+ * Static method to create a `FakeVectorStore` instance from an array of
297
+ * texts. It creates a `Document` for each text and metadata pair, and
298
+ * adds them to the store.
299
+ * @param texts Array of texts to be added to the store.
300
+ * @param metadatas Array or single object of metadata corresponding to the texts.
301
+ * @param embeddings `Embeddings` instance used to generate embeddings for the texts.
302
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
303
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
304
+ */
305
+ static fromTexts(texts: string[], metadatas: object[] | object, embeddings: EmbeddingsInterface, dbConfig?: FakeVectorStoreArgs): Promise<FakeVectorStore>;
306
+ /**
307
+ * Static method to create a `FakeVectorStore` instance from an array of
308
+ * `Document` instances. It adds the documents to the store.
309
+ * @param docs Array of `Document` instances to be added to the store.
310
+ * @param embeddings `Embeddings` instance used to generate embeddings for the documents.
311
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
312
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
313
+ */
314
+ static fromDocuments(docs: Document[], embeddings: EmbeddingsInterface, dbConfig?: FakeVectorStoreArgs): Promise<FakeVectorStore>;
315
+ /**
316
+ * Static method to create a `FakeVectorStore` instance from an existing
317
+ * index. It creates a new `FakeVectorStore` instance without adding any
318
+ * documents or vectors.
319
+ * @param embeddings `Embeddings` instance used to generate embeddings for the documents.
320
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
321
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
322
+ */
323
+ static fromExistingIndex(embeddings: EmbeddingsInterface, dbConfig?: FakeVectorStoreArgs): Promise<FakeVectorStore>;
324
+ }
231
325
  export {};
@@ -12,7 +12,9 @@ import { BaseRetriever } from "../../retrievers/index.js";
12
12
  import { Runnable, RunnableLambda } from "../../runnables/base.js";
13
13
  import { StructuredTool } from "../../tools/index.js";
14
14
  import { BaseTracer } from "../../tracers/base.js";
15
- import { Embeddings } from "../../embeddings.js";
15
+ import { Embeddings, } from "../../embeddings.js";
16
+ import { VectorStore } from "../../vectorstores.js";
17
+ import { cosine } from "../ml-distance/similarities.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,16 @@ 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 (options?.thrownErrorString) {
341
+ throw new Error(options.thrownErrorString);
342
+ }
343
+ if (this.emitCustomEvent) {
344
+ await runManager?.handleCustomEvent("some_test_event", {
345
+ someval: true,
346
+ });
347
+ }
331
348
  if (options?.stop?.length) {
332
349
  return {
333
350
  generations: [this._formatGeneration(options.stop[0])],
@@ -348,11 +365,19 @@ export class FakeListChatModel extends BaseChatModel {
348
365
  text,
349
366
  };
350
367
  }
351
- async *_streamResponseChunks(_messages, _options, runManager) {
368
+ async *_streamResponseChunks(_messages, options, runManager) {
352
369
  const response = this._currentResponse();
353
370
  this._incrementResponse();
371
+ if (this.emitCustomEvent) {
372
+ await runManager?.handleCustomEvent("some_test_event", {
373
+ someval: true,
374
+ });
375
+ }
354
376
  for await (const text of response) {
355
377
  await this._sleepIfRequested();
378
+ if (options?.thrownErrorString) {
379
+ throw new Error(options.thrownErrorString);
380
+ }
356
381
  const chunk = this._createResponseChunk(text);
357
382
  yield chunk;
358
383
  void runManager?.handleLLMNewToken(text);
@@ -618,3 +643,141 @@ export class SingleRunExtractor extends BaseTracer {
618
643
  return this.runPromise;
619
644
  }
620
645
  }
646
+ /**
647
+ * Class that extends `VectorStore` to store vectors in memory. Provides
648
+ * methods for adding documents, performing similarity searches, and
649
+ * creating instances from texts, documents, or an existing index.
650
+ */
651
+ export class FakeVectorStore extends VectorStore {
652
+ _vectorstoreType() {
653
+ return "memory";
654
+ }
655
+ constructor(embeddings, { similarity, ...rest } = {}) {
656
+ super(embeddings, rest);
657
+ Object.defineProperty(this, "memoryVectors", {
658
+ enumerable: true,
659
+ configurable: true,
660
+ writable: true,
661
+ value: []
662
+ });
663
+ Object.defineProperty(this, "similarity", {
664
+ enumerable: true,
665
+ configurable: true,
666
+ writable: true,
667
+ value: void 0
668
+ });
669
+ this.similarity = similarity ?? cosine;
670
+ }
671
+ /**
672
+ * Method to add documents to the memory vector store. It extracts the
673
+ * text from each document, generates embeddings for them, and adds the
674
+ * resulting vectors to the store.
675
+ * @param documents Array of `Document` instances to be added to the store.
676
+ * @returns Promise that resolves when all documents have been added.
677
+ */
678
+ async addDocuments(documents) {
679
+ const texts = documents.map(({ pageContent }) => pageContent);
680
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
681
+ }
682
+ /**
683
+ * Method to add vectors to the memory vector store. It creates
684
+ * `MemoryVector` instances for each vector and document pair and adds
685
+ * them to the store.
686
+ * @param vectors Array of vectors to be added to the store.
687
+ * @param documents Array of `Document` instances corresponding to the vectors.
688
+ * @returns Promise that resolves when all vectors have been added.
689
+ */
690
+ async addVectors(vectors, documents) {
691
+ const memoryVectors = vectors.map((embedding, idx) => ({
692
+ content: documents[idx].pageContent,
693
+ embedding,
694
+ metadata: documents[idx].metadata,
695
+ }));
696
+ this.memoryVectors = this.memoryVectors.concat(memoryVectors);
697
+ }
698
+ /**
699
+ * Method to perform a similarity search in the memory vector store. It
700
+ * calculates the similarity between the query vector and each vector in
701
+ * the store, sorts the results by similarity, and returns the top `k`
702
+ * results along with their scores.
703
+ * @param query Query vector to compare against the vectors in the store.
704
+ * @param k Number of top results to return.
705
+ * @param filter Optional filter function to apply to the vectors before performing the search.
706
+ * @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
707
+ */
708
+ async similaritySearchVectorWithScore(query, k, filter) {
709
+ const filterFunction = (memoryVector) => {
710
+ if (!filter) {
711
+ return true;
712
+ }
713
+ const doc = new Document({
714
+ metadata: memoryVector.metadata,
715
+ pageContent: memoryVector.content,
716
+ });
717
+ return filter(doc);
718
+ };
719
+ const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
720
+ const searches = filteredMemoryVectors
721
+ .map((vector, index) => ({
722
+ similarity: this.similarity(query, vector.embedding),
723
+ index,
724
+ }))
725
+ .sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
726
+ .slice(0, k);
727
+ const result = searches.map((search) => [
728
+ new Document({
729
+ metadata: filteredMemoryVectors[search.index].metadata,
730
+ pageContent: filteredMemoryVectors[search.index].content,
731
+ }),
732
+ search.similarity,
733
+ ]);
734
+ return result;
735
+ }
736
+ /**
737
+ * Static method to create a `FakeVectorStore` instance from an array of
738
+ * texts. It creates a `Document` for each text and metadata pair, and
739
+ * adds them to the store.
740
+ * @param texts Array of texts to be added to the store.
741
+ * @param metadatas Array or single object of metadata corresponding to the texts.
742
+ * @param embeddings `Embeddings` instance used to generate embeddings for the texts.
743
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
744
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
745
+ */
746
+ static async fromTexts(texts, metadatas, embeddings, dbConfig) {
747
+ const docs = [];
748
+ for (let i = 0; i < texts.length; i += 1) {
749
+ const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
750
+ const newDoc = new Document({
751
+ pageContent: texts[i],
752
+ metadata,
753
+ });
754
+ docs.push(newDoc);
755
+ }
756
+ return FakeVectorStore.fromDocuments(docs, embeddings, dbConfig);
757
+ }
758
+ /**
759
+ * Static method to create a `FakeVectorStore` instance from an array of
760
+ * `Document` instances. It adds the documents to the store.
761
+ * @param docs Array of `Document` instances to be added to the store.
762
+ * @param embeddings `Embeddings` instance used to generate embeddings for the documents.
763
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
764
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
765
+ */
766
+ static async fromDocuments(docs, embeddings, dbConfig) {
767
+ const instance = new this(embeddings, dbConfig);
768
+ await instance.addDocuments(docs);
769
+ return instance;
770
+ }
771
+ /**
772
+ * Static method to create a `FakeVectorStore` instance from an existing
773
+ * index. It creates a new `FakeVectorStore` instance without adding any
774
+ * documents or vectors.
775
+ * @param embeddings `Embeddings` instance used to generate embeddings for the documents.
776
+ * @param dbConfig Optional `FakeVectorStoreArgs` to configure the `FakeVectorStore` instance.
777
+ * @returns Promise that resolves with a new `FakeVectorStore` instance.
778
+ */
779
+ static async fromExistingIndex(embeddings, dbConfig) {
780
+ const instance = new this(embeddings, dbConfig);
781
+ return instance;
782
+ }
783
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
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 lc-build:v2 --create-entrypoints --pre --tree-shaking",
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",
@@ -46,7 +46,6 @@
46
46
  "decamelize": "1.2.0",
47
47
  "js-tiktoken": "^1.0.12",
48
48
  "langsmith": "~0.1.39",
49
- "ml-distance": "^4.0.0",
50
49
  "mustache": "^4.2.0",
51
50
  "p-queue": "^6.6.2",
52
51
  "p-retry": "4",
@@ -56,9 +55,10 @@
56
55
  },
57
56
  "devDependencies": {
58
57
  "@jest/globals": "^29.5.0",
59
- "@langchain/scripts": "~0.0.14",
58
+ "@langchain/scripts": "~0.0.20",
60
59
  "@swc/core": "^1.3.90",
61
60
  "@swc/jest": "^0.2.29",
61
+ "@types/decamelize": "^1.2.0",
62
62
  "@types/mustache": "^4",
63
63
  "dpdm": "^3.12.0",
64
64
  "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 {};