langchain 0.0.168 → 0.0.169

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 (42) hide show
  1. package/chat_models/yandex.cjs +1 -0
  2. package/chat_models/yandex.d.ts +1 -0
  3. package/chat_models/yandex.js +1 -0
  4. package/dist/callbacks/handlers/llmonitor.cjs +21 -17
  5. package/dist/callbacks/handlers/llmonitor.js +21 -17
  6. package/dist/chat_models/cloudflare_workersai.cjs +7 -2
  7. package/dist/chat_models/cloudflare_workersai.d.ts +1 -1
  8. package/dist/chat_models/cloudflare_workersai.js +7 -2
  9. package/dist/chat_models/yandex.cjs +117 -0
  10. package/dist/chat_models/yandex.d.ts +16 -0
  11. package/dist/chat_models/yandex.js +113 -0
  12. package/dist/evaluation/comparison/prompt.d.ts +2 -2
  13. package/dist/experimental/chains/violation_of_expectations/index.cjs +5 -0
  14. package/dist/experimental/chains/violation_of_expectations/index.d.ts +1 -0
  15. package/dist/experimental/chains/violation_of_expectations/index.js +1 -0
  16. package/dist/experimental/chains/violation_of_expectations/types.cjs +49 -0
  17. package/dist/experimental/chains/violation_of_expectations/types.d.ts +69 -0
  18. package/dist/experimental/chains/violation_of_expectations/types.js +46 -0
  19. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.cjs +328 -0
  20. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.d.ts +148 -0
  21. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.js +324 -0
  22. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.cjs +49 -0
  23. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.d.ts +5 -0
  24. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.js +46 -0
  25. package/dist/llms/cloudflare_workersai.cjs +14 -7
  26. package/dist/llms/cloudflare_workersai.d.ts +1 -1
  27. package/dist/llms/cloudflare_workersai.js +14 -7
  28. package/dist/load/import_map.cjs +4 -2
  29. package/dist/load/import_map.d.ts +2 -0
  30. package/dist/load/import_map.js +2 -0
  31. package/dist/retrievers/zep.cjs +29 -3
  32. package/dist/retrievers/zep.d.ts +14 -0
  33. package/dist/retrievers/zep.js +29 -3
  34. package/dist/vectorstores/faiss.cjs +38 -6
  35. package/dist/vectorstores/faiss.d.ts +14 -2
  36. package/dist/vectorstores/faiss.js +38 -6
  37. package/dist/vectorstores/weaviate.cjs +13 -2
  38. package/dist/vectorstores/weaviate.js +13 -2
  39. package/experimental/chains/violation_of_expectations.cjs +1 -0
  40. package/experimental/chains/violation_of_expectations.d.ts +1 -0
  41. package/experimental/chains/violation_of_expectations.js +1 -0
  42. package/package.json +25 -9
@@ -4,12 +4,23 @@ import { Document } from "../document.js";
4
4
  /**
5
5
  * Configuration interface for the ZepRetriever class. Extends the
6
6
  * BaseRetrieverInput interface.
7
+ *
8
+ * @argument {string} sessionId - The ID of the Zep session.
9
+ * @argument {string} url - The URL of the Zep API.
10
+ * @argument {number} [topK] - The number of results to return.
11
+ * @argument {string} [apiKey] - The API key for the Zep API.
12
+ * @argument [searchType] [searchType] - The type of search to perform: "similarity" or "mmr".
13
+ * @argument {number} [mmrLambda] - The lambda value for the MMR search.
14
+ * @argument {Record<string, unknown>} [filter] - The metadata filter to apply to the search.
7
15
  */
8
16
  export interface ZepRetrieverConfig extends BaseRetrieverInput {
9
17
  sessionId: string;
10
18
  url: string;
11
19
  topK?: number;
12
20
  apiKey?: string;
21
+ searchType?: "similarity" | "mmr";
22
+ mmrLambda?: number;
23
+ filter?: Record<string, unknown>;
13
24
  }
14
25
  /**
15
26
  * Class for retrieving information from a Zep long-term memory store.
@@ -27,6 +38,9 @@ export declare class ZepRetriever extends BaseRetriever {
27
38
  zepClientPromise: Promise<ZepClient>;
28
39
  private sessionId;
29
40
  private topK?;
41
+ private searchType?;
42
+ private mmrLambda?;
43
+ private filter?;
30
44
  constructor(config: ZepRetrieverConfig);
31
45
  /**
32
46
  * Converts an array of search results to an array of Document objects.
@@ -44,8 +44,29 @@ export class ZepRetriever extends BaseRetriever {
44
44
  writable: true,
45
45
  value: void 0
46
46
  });
47
+ Object.defineProperty(this, "searchType", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "mmrLambda", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "filter", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
47
65
  this.sessionId = config.sessionId;
48
66
  this.topK = config.topK;
67
+ this.searchType = config.searchType;
68
+ this.mmrLambda = config.mmrLambda;
69
+ this.filter = config.filter;
49
70
  this.zepClientPromise = ZepClient.init(config.url, config.apiKey);
50
71
  }
51
72
  /**
@@ -56,9 +77,9 @@ export class ZepRetriever extends BaseRetriever {
56
77
  searchResultToDoc(results) {
57
78
  return results
58
79
  .filter((r) => r.message)
59
- .map(({ message: { content } = {}, ...metadata }, dist) => new Document({
80
+ .map(({ message: { content, metadata: messageMetadata } = {}, dist, ...rest }) => new Document({
60
81
  pageContent: content ?? "",
61
- metadata: { score: dist, ...metadata },
82
+ metadata: { score: dist, ...messageMetadata, ...rest },
62
83
  }));
63
84
  }
64
85
  /**
@@ -67,7 +88,12 @@ export class ZepRetriever extends BaseRetriever {
67
88
  * @returns {Promise<Document[]>} A promise that resolves to an array of relevant Document objects.
68
89
  */
69
90
  async _getRelevantDocuments(query) {
70
- const payload = { text: query, metadata: {} };
91
+ const payload = {
92
+ text: query,
93
+ metadata: this.filter,
94
+ search_type: this.searchType,
95
+ mmr_lambda: this.mmrLambda,
96
+ };
71
97
  // Wait for ZepClient to be initialized
72
98
  const zepClient = await this.zepClientPromise;
73
99
  if (!zepClient) {
@@ -80,9 +80,9 @@ class FaissStore extends base_js_1.SaveableVectorStore {
80
80
  * @param documents An array of Document objects.
81
81
  * @returns A Promise that resolves when the documents have been added.
82
82
  */
83
- async addDocuments(documents) {
83
+ async addDocuments(documents, options) {
84
84
  const texts = documents.map(({ pageContent }) => pageContent);
85
- return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
85
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents, options);
86
86
  }
87
87
  get index() {
88
88
  if (!this._index) {
@@ -100,7 +100,7 @@ class FaissStore extends base_js_1.SaveableVectorStore {
100
100
  * @param documents An array of Document objects corresponding to the vectors.
101
101
  * @returns A Promise that resolves with an array of document IDs when the vectors and documents have been added.
102
102
  */
103
- async addVectors(vectors, documents) {
103
+ async addVectors(vectors, documents, options) {
104
104
  if (vectors.length === 0) {
105
105
  return [];
106
106
  }
@@ -117,10 +117,9 @@ class FaissStore extends base_js_1.SaveableVectorStore {
117
117
  throw new Error(`Vectors must have the same length as the number of dimensions (${d})`);
118
118
  }
119
119
  const docstoreSize = this.index.ntotal();
120
- const documentIds = [];
120
+ const documentIds = options?.ids ?? documents.map(() => uuid.v4());
121
121
  for (let i = 0; i < vectors.length; i += 1) {
122
- const documentId = uuid.v4();
123
- documentIds.push(documentId);
122
+ const documentId = documentIds[i];
124
123
  const id = docstoreSize + i;
125
124
  this.index.add(vectors[i]);
126
125
  this._mapping[id] = documentId;
@@ -169,6 +168,39 @@ class FaissStore extends base_js_1.SaveableVectorStore {
169
168
  ])),
170
169
  ]);
171
170
  }
171
+ /**
172
+ * Method to delete documents.
173
+ * @param params Object containing the IDs of the documents to delete.
174
+ * @returns A promise that resolves when the deletion is complete.
175
+ */
176
+ async delete(params) {
177
+ const documentIds = params.ids;
178
+ if (documentIds == null) {
179
+ throw new Error("No documentIds provided to delete.");
180
+ }
181
+ const mappings = new Map(Object.entries(this._mapping).map(([key, value]) => [
182
+ parseInt(key, 10),
183
+ value,
184
+ ]));
185
+ const reversedMappings = new Map(Array.from(mappings, (entry) => [entry[1], entry[0]]));
186
+ const missingIds = new Set(documentIds.filter((id) => !reversedMappings.has(id)));
187
+ if (missingIds.size > 0) {
188
+ throw new Error(`Some specified documentIds do not exist in the current store. DocumentIds not found: ${Array.from(missingIds).join(", ")}`);
189
+ }
190
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
191
+ const indexIdToDelete = documentIds.map((id) => reversedMappings.get(id));
192
+ // remove from index
193
+ this.index.removeIds(indexIdToDelete);
194
+ // remove from docstore
195
+ documentIds.forEach((id) => {
196
+ this.docstore._docs.delete(id);
197
+ });
198
+ // remove from mappings
199
+ indexIdToDelete.forEach((id) => {
200
+ mappings.delete(id);
201
+ });
202
+ this._mapping = { ...Array.from(mappings.values()) };
203
+ }
172
204
  /**
173
205
  * Merges the current FaissStore with another FaissStore.
174
206
  * @param targetIndex The FaissStore to merge with.
@@ -32,7 +32,9 @@ export declare class FaissStore extends SaveableVectorStore {
32
32
  * @param documents An array of Document objects.
33
33
  * @returns A Promise that resolves when the documents have been added.
34
34
  */
35
- addDocuments(documents: Document[]): Promise<string[]>;
35
+ addDocuments(documents: Document[], options?: {
36
+ ids?: string[];
37
+ }): Promise<string[]>;
36
38
  get index(): IndexFlatL2;
37
39
  private set index(value);
38
40
  /**
@@ -42,7 +44,9 @@ export declare class FaissStore extends SaveableVectorStore {
42
44
  * @param documents An array of Document objects corresponding to the vectors.
43
45
  * @returns A Promise that resolves with an array of document IDs when the vectors and documents have been added.
44
46
  */
45
- addVectors(vectors: number[][], documents: Document[]): Promise<string[]>;
47
+ addVectors(vectors: number[][], documents: Document[], options?: {
48
+ ids?: string[];
49
+ }): Promise<string[]>;
46
50
  /**
47
51
  * Performs a similarity search in the vector store using a query vector
48
52
  * and returns the top k results along with their scores.
@@ -57,6 +61,14 @@ export declare class FaissStore extends SaveableVectorStore {
57
61
  * @returns A Promise that resolves when the state has been saved.
58
62
  */
59
63
  save(directory: string): Promise<void>;
64
+ /**
65
+ * Method to delete documents.
66
+ * @param params Object containing the IDs of the documents to delete.
67
+ * @returns A promise that resolves when the deletion is complete.
68
+ */
69
+ delete(params: {
70
+ ids: string[];
71
+ }): Promise<void>;
60
72
  /**
61
73
  * Merges the current FaissStore with another FaissStore.
62
74
  * @param targetIndex The FaissStore to merge with.
@@ -54,9 +54,9 @@ export class FaissStore extends SaveableVectorStore {
54
54
  * @param documents An array of Document objects.
55
55
  * @returns A Promise that resolves when the documents have been added.
56
56
  */
57
- async addDocuments(documents) {
57
+ async addDocuments(documents, options) {
58
58
  const texts = documents.map(({ pageContent }) => pageContent);
59
- return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
59
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents, options);
60
60
  }
61
61
  get index() {
62
62
  if (!this._index) {
@@ -74,7 +74,7 @@ export class FaissStore extends SaveableVectorStore {
74
74
  * @param documents An array of Document objects corresponding to the vectors.
75
75
  * @returns A Promise that resolves with an array of document IDs when the vectors and documents have been added.
76
76
  */
77
- async addVectors(vectors, documents) {
77
+ async addVectors(vectors, documents, options) {
78
78
  if (vectors.length === 0) {
79
79
  return [];
80
80
  }
@@ -91,10 +91,9 @@ export class FaissStore extends SaveableVectorStore {
91
91
  throw new Error(`Vectors must have the same length as the number of dimensions (${d})`);
92
92
  }
93
93
  const docstoreSize = this.index.ntotal();
94
- const documentIds = [];
94
+ const documentIds = options?.ids ?? documents.map(() => uuid.v4());
95
95
  for (let i = 0; i < vectors.length; i += 1) {
96
- const documentId = uuid.v4();
97
- documentIds.push(documentId);
96
+ const documentId = documentIds[i];
98
97
  const id = docstoreSize + i;
99
98
  this.index.add(vectors[i]);
100
99
  this._mapping[id] = documentId;
@@ -143,6 +142,39 @@ export class FaissStore extends SaveableVectorStore {
143
142
  ])),
144
143
  ]);
145
144
  }
145
+ /**
146
+ * Method to delete documents.
147
+ * @param params Object containing the IDs of the documents to delete.
148
+ * @returns A promise that resolves when the deletion is complete.
149
+ */
150
+ async delete(params) {
151
+ const documentIds = params.ids;
152
+ if (documentIds == null) {
153
+ throw new Error("No documentIds provided to delete.");
154
+ }
155
+ const mappings = new Map(Object.entries(this._mapping).map(([key, value]) => [
156
+ parseInt(key, 10),
157
+ value,
158
+ ]));
159
+ const reversedMappings = new Map(Array.from(mappings, (entry) => [entry[1], entry[0]]));
160
+ const missingIds = new Set(documentIds.filter((id) => !reversedMappings.has(id)));
161
+ if (missingIds.size > 0) {
162
+ throw new Error(`Some specified documentIds do not exist in the current store. DocumentIds not found: ${Array.from(missingIds).join(", ")}`);
163
+ }
164
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165
+ const indexIdToDelete = documentIds.map((id) => reversedMappings.get(id));
166
+ // remove from index
167
+ this.index.removeIds(indexIdToDelete);
168
+ // remove from docstore
169
+ documentIds.forEach((id) => {
170
+ this.docstore._docs.delete(id);
171
+ });
172
+ // remove from mappings
173
+ indexIdToDelete.forEach((id) => {
174
+ mappings.delete(id);
175
+ });
176
+ this._mapping = { ...Array.from(mappings.values()) };
177
+ }
146
178
  /**
147
179
  * Merges the current FaissStore with another FaissStore.
148
180
  * @param targetIndex The FaissStore to merge with.
@@ -159,13 +159,24 @@ class WeaviateStore extends base_js_1.VectorStore {
159
159
  };
160
160
  });
161
161
  try {
162
- await this.client.batch
162
+ const responses = await this.client.batch
163
163
  .objectsBatcher()
164
164
  .withObjects(...batch)
165
165
  .do();
166
+ // if storing vectors fails, we need to know why
167
+ const errorMessages = [];
168
+ responses.forEach((response) => {
169
+ if (response?.result?.errors?.error) {
170
+ errorMessages.push(...response.result.errors.error.map((err) => err.message ??
171
+ "!! Unfortunately no error message was presented in the API response !!"));
172
+ }
173
+ });
174
+ if (errorMessages.length > 0) {
175
+ throw new Error(errorMessages.join("\n"));
176
+ }
166
177
  }
167
178
  catch (e) {
168
- throw Error(`'Error adding vectors' ${e}`);
179
+ throw Error(`Error adding vectors: ${e}`);
169
180
  }
170
181
  return documentIds;
171
182
  }
@@ -132,13 +132,24 @@ export class WeaviateStore extends VectorStore {
132
132
  };
133
133
  });
134
134
  try {
135
- await this.client.batch
135
+ const responses = await this.client.batch
136
136
  .objectsBatcher()
137
137
  .withObjects(...batch)
138
138
  .do();
139
+ // if storing vectors fails, we need to know why
140
+ const errorMessages = [];
141
+ responses.forEach((response) => {
142
+ if (response?.result?.errors?.error) {
143
+ errorMessages.push(...response.result.errors.error.map((err) => err.message ??
144
+ "!! Unfortunately no error message was presented in the API response !!"));
145
+ }
146
+ });
147
+ if (errorMessages.length > 0) {
148
+ throw new Error(errorMessages.join("\n"));
149
+ }
139
150
  }
140
151
  catch (e) {
141
- throw Error(`'Error adding vectors' ${e}`);
152
+ throw Error(`Error adding vectors: ${e}`);
142
153
  }
143
154
  return documentIds;
144
155
  }
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/experimental/chains/violation_of_expectations/index.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/experimental/chains/violation_of_expectations/index.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/experimental/chains/violation_of_expectations/index.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.168",
3
+ "version": "0.0.169",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -472,6 +472,9 @@
472
472
  "chat_models/minimax.cjs",
473
473
  "chat_models/minimax.js",
474
474
  "chat_models/minimax.d.ts",
475
+ "chat_models/yandex.cjs",
476
+ "chat_models/yandex.js",
477
+ "chat_models/yandex.d.ts",
475
478
  "chat_models/fake.cjs",
476
479
  "chat_models/fake.js",
477
480
  "chat_models/fake.d.ts",
@@ -700,6 +703,9 @@
700
703
  "experimental/hubs/makersuite/googlemakersuitehub.cjs",
701
704
  "experimental/hubs/makersuite/googlemakersuitehub.js",
702
705
  "experimental/hubs/makersuite/googlemakersuitehub.d.ts",
706
+ "experimental/chains/violation_of_expectations.cjs",
707
+ "experimental/chains/violation_of_expectations.js",
708
+ "experimental/chains/violation_of_expectations.d.ts",
703
709
  "evaluation.cjs",
704
710
  "evaluation.js",
705
711
  "evaluation.d.ts",
@@ -752,7 +758,7 @@
752
758
  "@elastic/elasticsearch": "^8.4.0",
753
759
  "@faker-js/faker": "^7.6.0",
754
760
  "@getmetal/metal-sdk": "^4.0.0",
755
- "@getzep/zep-js": "^0.7.0",
761
+ "@getzep/zep-js": "^0.8.0",
756
762
  "@gomomento/sdk": "^1.41.2",
757
763
  "@gomomento/sdk-core": "^1.41.2",
758
764
  "@google-ai/generativelanguage": "^0.2.1",
@@ -820,7 +826,7 @@
820
826
  "eslint-plugin-import": "^2.27.5",
821
827
  "eslint-plugin-no-instanceof": "^1.0.1",
822
828
  "eslint-plugin-prettier": "^4.2.1",
823
- "faiss-node": "^0.3.0",
829
+ "faiss-node": "^0.5.1",
824
830
  "fast-xml-parser": "^4.2.7",
825
831
  "firebase-admin": "^11.9.0",
826
832
  "google-auth-library": "^8.9.0",
@@ -833,7 +839,7 @@
833
839
  "jest": "^29.5.0",
834
840
  "jest-environment-node": "^29.6.4",
835
841
  "jsdom": "^22.1.0",
836
- "llmonitor": "^0.5.4",
842
+ "llmonitor": "^0.5.8",
837
843
  "lodash": "^4.17.21",
838
844
  "mammoth": "^1.5.1",
839
845
  "ml-matrix": "^6.10.4",
@@ -846,7 +852,7 @@
846
852
  "peggy": "^3.0.2",
847
853
  "pg": "^8.11.0",
848
854
  "pg-copy-streams": "^6.0.5",
849
- "pickleparser": "^0.1.0",
855
+ "pickleparser": "^0.2.1",
850
856
  "playwright": "^1.32.1",
851
857
  "portkey-ai": "^0.1.11",
852
858
  "prettier": "^2.8.3",
@@ -886,7 +892,7 @@
886
892
  "@cloudflare/ai": "^1.0.12",
887
893
  "@elastic/elasticsearch": "^8.4.0",
888
894
  "@getmetal/metal-sdk": "*",
889
- "@getzep/zep-js": "^0.7.0",
895
+ "@getzep/zep-js": "^0.8.0",
890
896
  "@gomomento/sdk": "^1.41.2",
891
897
  "@gomomento/sdk-core": "^1.41.2",
892
898
  "@gomomento/sdk-web": "^1.41.2",
@@ -927,7 +933,7 @@
927
933
  "cohere-ai": ">=6.0.0",
928
934
  "d3-dsv": "^2.0.0",
929
935
  "epub2": "^3.0.1",
930
- "faiss-node": "^0.3.0",
936
+ "faiss-node": "^0.5.1",
931
937
  "fast-xml-parser": "^4.2.7",
932
938
  "firebase-admin": "^11.9.0",
933
939
  "google-auth-library": "^8.9.0",
@@ -937,7 +943,7 @@
937
943
  "ignore": "^5.2.0",
938
944
  "ioredis": "^5.3.2",
939
945
  "jsdom": "*",
940
- "llmonitor": "*",
946
+ "llmonitor": "^0.5.8",
941
947
  "lodash": "^4.17.21",
942
948
  "mammoth": "*",
943
949
  "mongodb": "^5.2.0",
@@ -949,7 +955,7 @@
949
955
  "peggy": "^3.0.2",
950
956
  "pg": "^8.11.0",
951
957
  "pg-copy-streams": "^6.0.5",
952
- "pickleparser": "^0.1.0",
958
+ "pickleparser": "^0.2.1",
953
959
  "playwright": "^1.32.1",
954
960
  "portkey-ai": "^0.1.11",
955
961
  "puppeteer": "^19.7.2",
@@ -2063,6 +2069,11 @@
2063
2069
  "import": "./chat_models/minimax.js",
2064
2070
  "require": "./chat_models/minimax.cjs"
2065
2071
  },
2072
+ "./chat_models/yandex": {
2073
+ "types": "./chat_models/yandex.d.ts",
2074
+ "import": "./chat_models/yandex.js",
2075
+ "require": "./chat_models/yandex.cjs"
2076
+ },
2066
2077
  "./chat_models/fake": {
2067
2078
  "types": "./chat_models/fake.d.ts",
2068
2079
  "import": "./chat_models/fake.js",
@@ -2443,6 +2454,11 @@
2443
2454
  "import": "./experimental/hubs/makersuite/googlemakersuitehub.js",
2444
2455
  "require": "./experimental/hubs/makersuite/googlemakersuitehub.cjs"
2445
2456
  },
2457
+ "./experimental/chains/violation_of_expectations": {
2458
+ "types": "./experimental/chains/violation_of_expectations.d.ts",
2459
+ "import": "./experimental/chains/violation_of_expectations.js",
2460
+ "require": "./experimental/chains/violation_of_expectations.cjs"
2461
+ },
2446
2462
  "./evaluation": {
2447
2463
  "types": "./evaluation.d.ts",
2448
2464
  "import": "./evaluation.js",