langchain 0.0.196 → 0.0.197-rc.0

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 (136) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/openai/index.cjs +6 -2
  3. package/dist/agents/openai/index.js +6 -2
  4. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  5. package/dist/base_language/count_tokens.cjs +4 -4
  6. package/dist/base_language/count_tokens.d.ts +1 -1
  7. package/dist/base_language/count_tokens.js +1 -1
  8. package/dist/base_language/index.cjs +3 -3
  9. package/dist/base_language/index.d.ts +1 -1
  10. package/dist/base_language/index.js +1 -1
  11. package/dist/cache/base.cjs +1 -1
  12. package/dist/cache/base.d.ts +1 -1
  13. package/dist/cache/base.js +1 -1
  14. package/dist/cache/index.cjs +2 -2
  15. package/dist/cache/index.d.ts +1 -1
  16. package/dist/cache/index.js +1 -1
  17. package/dist/callbacks/handlers/console.cjs +1 -1
  18. package/dist/callbacks/handlers/console.d.ts +1 -1
  19. package/dist/callbacks/handlers/console.js +1 -1
  20. package/dist/callbacks/handlers/initialize.cjs +1 -1
  21. package/dist/callbacks/handlers/initialize.d.ts +1 -1
  22. package/dist/callbacks/handlers/initialize.js +1 -1
  23. package/dist/callbacks/handlers/log_stream.cjs +1 -1
  24. package/dist/callbacks/handlers/log_stream.d.ts +1 -1
  25. package/dist/callbacks/handlers/log_stream.js +1 -1
  26. package/dist/callbacks/handlers/run_collector.cjs +1 -1
  27. package/dist/callbacks/handlers/run_collector.d.ts +1 -1
  28. package/dist/callbacks/handlers/run_collector.js +1 -1
  29. package/dist/callbacks/handlers/tracer.cjs +1 -1
  30. package/dist/callbacks/handlers/tracer.d.ts +1 -1
  31. package/dist/callbacks/handlers/tracer.js +1 -1
  32. package/dist/callbacks/handlers/tracer_langchain.cjs +1 -1
  33. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -1
  34. package/dist/callbacks/handlers/tracer_langchain.js +1 -1
  35. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +1 -1
  36. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -1
  37. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -1
  38. package/dist/chains/openai_functions/structured_output.cjs +1 -1
  39. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  40. package/dist/chains/openai_functions/structured_output.js +1 -1
  41. package/dist/chat_models/anthropic.cjs +15 -348
  42. package/dist/chat_models/anthropic.d.ts +1 -156
  43. package/dist/chat_models/anthropic.js +1 -346
  44. package/dist/chat_models/base.cjs +1 -1
  45. package/dist/chat_models/base.d.ts +1 -1
  46. package/dist/chat_models/base.js +1 -1
  47. package/dist/chat_models/bedrock/web.cjs +21 -1
  48. package/dist/chat_models/bedrock/web.d.ts +1 -1
  49. package/dist/chat_models/bedrock/web.js +21 -1
  50. package/dist/document.cjs +2 -2
  51. package/dist/document.d.ts +1 -1
  52. package/dist/document.js +1 -1
  53. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  54. package/dist/document_loaders/web/github.cjs +105 -0
  55. package/dist/document_loaders/web/github.d.ts +26 -0
  56. package/dist/document_loaders/web/github.js +105 -0
  57. package/dist/document_loaders/web/s3.d.ts +1 -1
  58. package/dist/embeddings/base.cjs +1 -1
  59. package/dist/embeddings/base.d.ts +1 -1
  60. package/dist/embeddings/base.js +1 -1
  61. package/dist/embeddings/cache_backed.cjs +1 -1
  62. package/dist/embeddings/cache_backed.js +1 -1
  63. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  64. package/dist/llms/base.cjs +1 -1
  65. package/dist/llms/base.d.ts +1 -1
  66. package/dist/llms/base.js +1 -1
  67. package/dist/llms/bedrock/web.cjs +21 -1
  68. package/dist/llms/bedrock/web.d.ts +1 -1
  69. package/dist/llms/bedrock/web.js +21 -1
  70. package/dist/memory/base.cjs +2 -2
  71. package/dist/memory/base.d.ts +2 -2
  72. package/dist/memory/base.js +2 -2
  73. package/dist/output_parsers/list.cjs +4 -122
  74. package/dist/output_parsers/list.d.ts +1 -57
  75. package/dist/output_parsers/list.js +1 -119
  76. package/dist/output_parsers/openai_functions.cjs +1 -1
  77. package/dist/output_parsers/openai_functions.d.ts +1 -1
  78. package/dist/output_parsers/openai_functions.js +1 -1
  79. package/dist/prompts/base.cjs +8 -8
  80. package/dist/prompts/base.d.ts +3 -3
  81. package/dist/prompts/base.js +3 -3
  82. package/dist/prompts/chat.cjs +13 -15
  83. package/dist/prompts/chat.d.ts +2 -1
  84. package/dist/prompts/chat.js +2 -1
  85. package/dist/prompts/few_shot.cjs +4 -15
  86. package/dist/prompts/few_shot.d.ts +1 -1
  87. package/dist/prompts/few_shot.js +1 -1
  88. package/dist/prompts/index.cjs +2 -2
  89. package/dist/prompts/index.d.ts +1 -1
  90. package/dist/prompts/index.js +1 -1
  91. package/dist/prompts/pipeline.cjs +3 -15
  92. package/dist/prompts/pipeline.d.ts +1 -1
  93. package/dist/prompts/pipeline.js +1 -1
  94. package/dist/prompts/prompt.cjs +3 -15
  95. package/dist/prompts/prompt.d.ts +1 -1
  96. package/dist/prompts/prompt.js +1 -1
  97. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +3 -15
  98. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -1
  99. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -1
  100. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +1 -1
  101. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -1
  102. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -1
  103. package/dist/prompts/selectors/conditional.cjs +6 -15
  104. package/dist/prompts/selectors/conditional.d.ts +1 -1
  105. package/dist/prompts/selectors/conditional.js +1 -1
  106. package/dist/prompts/serde.cjs +0 -15
  107. package/dist/prompts/serde.d.ts +1 -1
  108. package/dist/prompts/serde.js +1 -1
  109. package/dist/prompts/template.cjs +9 -15
  110. package/dist/prompts/template.d.ts +1 -1
  111. package/dist/prompts/template.js +1 -1
  112. package/dist/schema/document.cjs +3 -3
  113. package/dist/schema/document.d.ts +1 -1
  114. package/dist/schema/document.js +1 -1
  115. package/dist/schema/index.cjs +12 -12
  116. package/dist/schema/index.d.ts +10 -10
  117. package/dist/schema/index.js +7 -7
  118. package/dist/schema/output_parser.cjs +1 -1
  119. package/dist/schema/output_parser.d.ts +1 -1
  120. package/dist/schema/output_parser.js +1 -1
  121. package/dist/schema/retriever.cjs +1 -1
  122. package/dist/schema/retriever.d.ts +1 -1
  123. package/dist/schema/retriever.js +1 -1
  124. package/dist/schema/storage.cjs +1 -1
  125. package/dist/schema/storage.d.ts +1 -1
  126. package/dist/schema/storage.js +1 -1
  127. package/dist/util/async_caller.cjs +1 -1
  128. package/dist/util/async_caller.d.ts +1 -1
  129. package/dist/util/async_caller.js +1 -1
  130. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  131. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  132. package/dist/vectorstores/momento_vector_index.js +40 -1
  133. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  134. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  135. package/dist/vectorstores/mongodb_atlas.js +22 -2
  136. package/package.json +9 -8
@@ -1,7 +1,7 @@
1
1
  import { IVectorIndexClient } from "@gomomento/sdk-core";
2
2
  import { Document } from "../document.js";
3
3
  import { Embeddings } from "../embeddings/base.js";
4
- import { VectorStore } from "./base.js";
4
+ import { MaxMarginalRelevanceSearchOptions, VectorStore } from "./base.js";
5
5
  export interface DocumentProps {
6
6
  ids: string[];
7
7
  }
@@ -103,6 +103,22 @@ export declare class MomentoVectorIndex extends VectorStore {
103
103
  * to the query vector.
104
104
  */
105
105
  similaritySearchVectorWithScore(query: number[], k: number): Promise<[Document<Record<string, any>>, number][]>;
106
+ /**
107
+ * Return documents selected using the maximal marginal relevance.
108
+ * Maximal marginal relevance optimizes for similarity to the query AND diversity
109
+ * among selected documents.
110
+ *
111
+ * @param {string} query - Text to look up documents similar to.
112
+ * @param {number} options.k - Number of documents to return.
113
+ * @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm.
114
+ * @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results,
115
+ * where 0 corresponds to maximum diversity and 1 to minimum diversity.
116
+ * @param {this["FilterType"]} options.filter - Optional filter
117
+ * @param _callbacks
118
+ *
119
+ * @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
120
+ */
121
+ maxMarginalRelevanceSearch(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>): Promise<Document[]>;
106
122
  /**
107
123
  * Stores the documents in the index.
108
124
  *
@@ -1,9 +1,10 @@
1
1
  /* eslint-disable no-instanceof/no-instanceof */
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
- import { ALL_VECTOR_METADATA, CreateVectorIndex, VectorUpsertItemBatch, VectorDeleteItemBatch, VectorSearch, } from "@gomomento/sdk-core";
3
+ import { ALL_VECTOR_METADATA, CreateVectorIndex, VectorUpsertItemBatch, VectorDeleteItemBatch, VectorSearch, VectorSearchAndFetchVectors, } from "@gomomento/sdk-core";
4
4
  import * as uuid from "uuid";
5
5
  import { Document } from "../document.js";
6
6
  import { VectorStore } from "./base.js";
7
+ import { maximalMarginalRelevance } from "../util/math.js";
7
8
  /**
8
9
  * A vector store that uses the Momento Vector Index.
9
10
  *
@@ -208,6 +209,44 @@ export class MomentoVectorIndex extends VectorStore {
208
209
  throw new Error(`Unknown response type: ${response.toString()}`);
209
210
  }
210
211
  }
212
+ /**
213
+ * Return documents selected using the maximal marginal relevance.
214
+ * Maximal marginal relevance optimizes for similarity to the query AND diversity
215
+ * among selected documents.
216
+ *
217
+ * @param {string} query - Text to look up documents similar to.
218
+ * @param {number} options.k - Number of documents to return.
219
+ * @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm.
220
+ * @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results,
221
+ * where 0 corresponds to maximum diversity and 1 to minimum diversity.
222
+ * @param {this["FilterType"]} options.filter - Optional filter
223
+ * @param _callbacks
224
+ *
225
+ * @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
226
+ */
227
+ async maxMarginalRelevanceSearch(query, options) {
228
+ const queryEmbedding = await this.embeddings.embedQuery(query);
229
+ const response = await this.client.searchAndFetchVectors(this.indexName, queryEmbedding, { topK: options.fetchK ?? 20, metadataFields: ALL_VECTOR_METADATA });
230
+ if (response instanceof VectorSearchAndFetchVectors.Success) {
231
+ const hits = response.hits();
232
+ // Gather the embeddings of the search results
233
+ const embeddingList = hits.map((hit) => hit.vector);
234
+ // Gather the ids of the most relevant results when applying MMR
235
+ const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, options.lambda, options.k);
236
+ const finalResult = mmrIndexes.map((index) => {
237
+ const hit = hits[index];
238
+ const { [this.textField]: pageContent, ...metadata } = hit.metadata;
239
+ return new Document({ metadata, pageContent: pageContent });
240
+ });
241
+ return finalResult;
242
+ }
243
+ else if (response instanceof VectorSearchAndFetchVectors.Error) {
244
+ throw new Error(response.toString());
245
+ }
246
+ else {
247
+ throw new Error(`Unknown response type: ${response.toString()}`);
248
+ }
249
+ }
211
250
  /**
212
251
  * Stores the documents in the index.
213
252
  *
@@ -99,7 +99,7 @@ class MongoDBAtlasVectorSearch extends base_js_1.VectorStore {
99
99
  const pipeline = [
100
100
  {
101
101
  $vectorSearch: {
102
- queryVector: query,
102
+ queryVector: MongoDBAtlasVectorSearch.fixArrayPrecision(query),
103
103
  index: this.indexName,
104
104
  path: this.embeddingKey,
105
105
  limit: k,
@@ -148,7 +148,7 @@ class MongoDBAtlasVectorSearch extends base_js_1.VectorStore {
148
148
  ...filter,
149
149
  includeEmbeddings: true,
150
150
  };
151
- const resultDocs = await this.similaritySearchVectorWithScore(queryEmbedding, fetchK, includeEmbeddingsFilter);
151
+ const resultDocs = await this.similaritySearchVectorWithScore(MongoDBAtlasVectorSearch.fixArrayPrecision(queryEmbedding), fetchK, includeEmbeddingsFilter);
152
152
  const embeddingList = resultDocs.map((doc) => doc[0].metadata[this.embeddingKey]);
153
153
  const mmrIndexes = (0, math_js_1.maximalMarginalRelevance)(queryEmbedding, embeddingList, lambda, k);
154
154
  return mmrIndexes.map((idx) => {
@@ -196,5 +196,25 @@ class MongoDBAtlasVectorSearch extends base_js_1.VectorStore {
196
196
  await instance.addDocuments(docs);
197
197
  return instance;
198
198
  }
199
+ /**
200
+ * Static method to fix the precision of the array that ensures that
201
+ * every number in this array is always float when casted to other types.
202
+ * This is needed since MongoDB Atlas Vector Search does not cast integer
203
+ * inside vector search to float automatically.
204
+ * This method shall introduce a hint of error but should be safe to use
205
+ * since introduced error is very small, only applies to integer numbers
206
+ * returned by embeddings, and most embeddings shall not have precision
207
+ * as high as 15 decimal places.
208
+ * @param array Array of number to be fixed.
209
+ * @returns
210
+ */
211
+ static fixArrayPrecision(array) {
212
+ return array.map((value) => {
213
+ if (Number.isInteger(value)) {
214
+ return value + 0.000000000000001;
215
+ }
216
+ return value;
217
+ });
218
+ }
199
219
  }
200
220
  exports.MongoDBAtlasVectorSearch = MongoDBAtlasVectorSearch;
@@ -101,5 +101,18 @@ export declare class MongoDBAtlasVectorSearch extends VectorStore {
101
101
  * @returns Promise that resolves to a new instance of MongoDBAtlasVectorSearch.
102
102
  */
103
103
  static fromDocuments(docs: Document[], embeddings: Embeddings, dbConfig: MongoDBAtlasVectorSearchLibArgs): Promise<MongoDBAtlasVectorSearch>;
104
+ /**
105
+ * Static method to fix the precision of the array that ensures that
106
+ * every number in this array is always float when casted to other types.
107
+ * This is needed since MongoDB Atlas Vector Search does not cast integer
108
+ * inside vector search to float automatically.
109
+ * This method shall introduce a hint of error but should be safe to use
110
+ * since introduced error is very small, only applies to integer numbers
111
+ * returned by embeddings, and most embeddings shall not have precision
112
+ * as high as 15 decimal places.
113
+ * @param array Array of number to be fixed.
114
+ * @returns
115
+ */
116
+ static fixArrayPrecision(array: number[]): number[];
104
117
  }
105
118
  export {};
@@ -96,7 +96,7 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
96
96
  const pipeline = [
97
97
  {
98
98
  $vectorSearch: {
99
- queryVector: query,
99
+ queryVector: MongoDBAtlasVectorSearch.fixArrayPrecision(query),
100
100
  index: this.indexName,
101
101
  path: this.embeddingKey,
102
102
  limit: k,
@@ -145,7 +145,7 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
145
145
  ...filter,
146
146
  includeEmbeddings: true,
147
147
  };
148
- const resultDocs = await this.similaritySearchVectorWithScore(queryEmbedding, fetchK, includeEmbeddingsFilter);
148
+ const resultDocs = await this.similaritySearchVectorWithScore(MongoDBAtlasVectorSearch.fixArrayPrecision(queryEmbedding), fetchK, includeEmbeddingsFilter);
149
149
  const embeddingList = resultDocs.map((doc) => doc[0].metadata[this.embeddingKey]);
150
150
  const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, lambda, k);
151
151
  return mmrIndexes.map((idx) => {
@@ -193,4 +193,24 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
193
193
  await instance.addDocuments(docs);
194
194
  return instance;
195
195
  }
196
+ /**
197
+ * Static method to fix the precision of the array that ensures that
198
+ * every number in this array is always float when casted to other types.
199
+ * This is needed since MongoDB Atlas Vector Search does not cast integer
200
+ * inside vector search to float automatically.
201
+ * This method shall introduce a hint of error but should be safe to use
202
+ * since introduced error is very small, only applies to integer numbers
203
+ * returned by embeddings, and most embeddings shall not have precision
204
+ * as high as 15 decimal places.
205
+ * @param array Array of number to be fixed.
206
+ * @returns
207
+ */
208
+ static fixArrayPrecision(array) {
209
+ return array.map((value) => {
210
+ if (Number.isInteger(value)) {
211
+ return value + 0.000000000000001;
212
+ }
213
+ return value;
214
+ });
215
+ }
196
216
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.196",
3
+ "version": "0.0.197-rc.0",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -859,12 +859,13 @@
859
859
  "@faker-js/faker": "^7.6.0",
860
860
  "@getmetal/metal-sdk": "^4.0.0",
861
861
  "@getzep/zep-js": "^0.9.0",
862
- "@gomomento/sdk": "^1.47.1",
863
- "@gomomento/sdk-core": "^1.47.1",
862
+ "@gomomento/sdk": "^1.51.1",
863
+ "@gomomento/sdk-core": "^1.51.1",
864
864
  "@google-ai/generativelanguage": "^0.2.1",
865
865
  "@google-cloud/storage": "^6.10.1",
866
866
  "@huggingface/inference": "^2.6.4",
867
867
  "@jest/globals": "^29.5.0",
868
+ "@langchain/anthropic": "workspace:*",
868
869
  "@mozilla/readability": "^0.4.4",
869
870
  "@notionhq/client": "^2.2.10",
870
871
  "@opensearch-project/opensearch": "^2.2.0",
@@ -997,9 +998,9 @@
997
998
  "@elastic/elasticsearch": "^8.4.0",
998
999
  "@getmetal/metal-sdk": "*",
999
1000
  "@getzep/zep-js": "^0.9.0",
1000
- "@gomomento/sdk": "^1.47.1",
1001
- "@gomomento/sdk-core": "^1.47.1",
1002
- "@gomomento/sdk-web": "^1.47.1",
1001
+ "@gomomento/sdk": "^1.51.1",
1002
+ "@gomomento/sdk-core": "^1.51.1",
1003
+ "@gomomento/sdk-web": "^1.51.1",
1003
1004
  "@google-ai/generativelanguage": "^0.2.1",
1004
1005
  "@google-cloud/storage": "^6.10.1",
1005
1006
  "@huggingface/inference": "^2.6.4",
@@ -1378,14 +1379,14 @@
1378
1379
  }
1379
1380
  },
1380
1381
  "dependencies": {
1381
- "@anthropic-ai/sdk": "^0.9.1",
1382
+ "@langchain/anthropic": "^0.0.2",
1382
1383
  "binary-extensions": "^2.2.0",
1383
1384
  "expr-eval": "^2.0.2",
1384
1385
  "flat": "^5.0.2",
1385
1386
  "js-tiktoken": "^1.0.7",
1386
1387
  "js-yaml": "^4.1.0",
1387
1388
  "jsonpointer": "^5.0.1",
1388
- "langchain-core": "^0.0.1",
1389
+ "langchain-core": "^0.0.4",
1389
1390
  "langchainhub": "~0.0.6",
1390
1391
  "langsmith": "~0.0.48",
1391
1392
  "ml-distance": "^4.0.0",