langchain 0.0.74 → 0.0.76

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 (163) hide show
  1. package/client.cjs +1 -0
  2. package/client.d.ts +1 -0
  3. package/client.js +1 -0
  4. package/dist/base_language/count_tokens.cjs +5 -21
  5. package/dist/base_language/count_tokens.d.ts +1 -6
  6. package/dist/base_language/count_tokens.js +4 -19
  7. package/dist/base_language/index.cjs +16 -24
  8. package/dist/base_language/index.d.ts +22 -3
  9. package/dist/base_language/index.js +17 -25
  10. package/dist/cache/redis.d.ts +3 -1
  11. package/dist/callbacks/base.d.ts +17 -3
  12. package/dist/callbacks/handlers/console.cjs +15 -28
  13. package/dist/callbacks/handlers/console.d.ts +11 -20
  14. package/dist/callbacks/handlers/console.js +14 -27
  15. package/dist/callbacks/handlers/initialize.cjs +8 -3
  16. package/dist/callbacks/handlers/initialize.d.ts +4 -2
  17. package/dist/callbacks/handlers/initialize.js +6 -2
  18. package/dist/callbacks/handlers/tracer.cjs +193 -0
  19. package/dist/callbacks/handlers/tracer.d.ts +65 -0
  20. package/dist/callbacks/handlers/tracer.js +189 -0
  21. package/dist/callbacks/handlers/tracer_langchain.cjs +163 -0
  22. package/dist/callbacks/handlers/tracer_langchain.d.ts +39 -0
  23. package/dist/callbacks/handlers/tracer_langchain.js +159 -0
  24. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +202 -0
  25. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +57 -0
  26. package/dist/callbacks/handlers/tracer_langchain_v1.js +198 -0
  27. package/dist/callbacks/index.cjs +10 -5
  28. package/dist/callbacks/index.d.ts +5 -3
  29. package/dist/callbacks/index.js +5 -3
  30. package/dist/callbacks/manager.cjs +39 -9
  31. package/dist/callbacks/manager.d.ts +5 -2
  32. package/dist/callbacks/manager.js +40 -10
  33. package/dist/chains/llm_chain.cjs +17 -5
  34. package/dist/chains/llm_chain.d.ts +9 -3
  35. package/dist/chains/llm_chain.js +17 -5
  36. package/dist/chains/question_answering/map_reduce_prompts.cjs +5 -5
  37. package/dist/chains/question_answering/map_reduce_prompts.d.ts +1 -1
  38. package/dist/chains/question_answering/map_reduce_prompts.js +1 -1
  39. package/dist/chains/question_answering/refine_prompts.cjs +5 -5
  40. package/dist/chains/question_answering/refine_prompts.d.ts +1 -1
  41. package/dist/chains/question_answering/refine_prompts.js +1 -1
  42. package/dist/chains/question_answering/stuff_prompts.cjs +2 -2
  43. package/dist/chains/question_answering/stuff_prompts.d.ts +1 -1
  44. package/dist/chains/question_answering/stuff_prompts.js +1 -1
  45. package/dist/chains/sequential_chain.cjs +2 -2
  46. package/dist/chains/sequential_chain.d.ts +2 -2
  47. package/dist/chains/sequential_chain.js +2 -2
  48. package/dist/chains/sql_db/sql_db_prompt.cjs +20 -1
  49. package/dist/chains/sql_db/sql_db_prompt.d.ts +1 -0
  50. package/dist/chains/sql_db/sql_db_prompt.js +19 -0
  51. package/dist/chains/summarization/load.cjs +14 -5
  52. package/dist/chains/summarization/load.d.ts +7 -2
  53. package/dist/chains/summarization/load.js +14 -5
  54. package/dist/chat_models/anthropic.cjs +36 -9
  55. package/dist/chat_models/anthropic.d.ts +6 -3
  56. package/dist/chat_models/anthropic.js +36 -9
  57. package/dist/chat_models/base.cjs +40 -13
  58. package/dist/chat_models/base.d.ts +14 -7
  59. package/dist/chat_models/base.js +41 -14
  60. package/dist/chat_models/openai.cjs +20 -14
  61. package/dist/chat_models/openai.d.ts +2 -1
  62. package/dist/chat_models/openai.js +20 -14
  63. package/dist/client/index.cjs +5 -0
  64. package/dist/client/index.d.ts +1 -0
  65. package/dist/client/index.js +1 -0
  66. package/dist/client/langchainplus.cjs +405 -0
  67. package/dist/client/langchainplus.d.ts +65 -0
  68. package/dist/client/langchainplus.js +398 -0
  69. package/dist/document_loaders/fs/unstructured.cjs +8 -0
  70. package/dist/document_loaders/fs/unstructured.d.ts +3 -0
  71. package/dist/document_loaders/fs/unstructured.js +8 -0
  72. package/dist/document_loaders/web/apify_dataset.cjs +64 -0
  73. package/dist/document_loaders/web/apify_dataset.d.ts +28 -0
  74. package/dist/document_loaders/web/apify_dataset.js +60 -0
  75. package/dist/embeddings/openai.cjs +2 -2
  76. package/dist/embeddings/openai.js +2 -2
  77. package/dist/llms/base.cjs +43 -17
  78. package/dist/llms/base.d.ts +16 -9
  79. package/dist/llms/base.js +44 -18
  80. package/dist/llms/cohere.cjs +3 -2
  81. package/dist/llms/cohere.d.ts +1 -1
  82. package/dist/llms/cohere.js +3 -2
  83. package/dist/llms/hf.cjs +2 -2
  84. package/dist/llms/hf.d.ts +1 -1
  85. package/dist/llms/hf.js +2 -2
  86. package/dist/llms/openai-chat.cjs +13 -14
  87. package/dist/llms/openai-chat.d.ts +2 -1
  88. package/dist/llms/openai-chat.js +13 -14
  89. package/dist/llms/openai.cjs +15 -13
  90. package/dist/llms/openai.d.ts +4 -3
  91. package/dist/llms/openai.js +15 -13
  92. package/dist/llms/replicate.cjs +2 -2
  93. package/dist/llms/replicate.d.ts +1 -1
  94. package/dist/llms/replicate.js +2 -2
  95. package/dist/memory/base.cjs +9 -1
  96. package/dist/memory/base.d.ts +1 -0
  97. package/dist/memory/base.js +7 -0
  98. package/dist/memory/entity_memory.cjs +151 -0
  99. package/dist/memory/entity_memory.d.ts +35 -0
  100. package/dist/memory/entity_memory.js +147 -0
  101. package/dist/memory/index.cjs +5 -1
  102. package/dist/memory/index.d.ts +2 -0
  103. package/dist/memory/index.js +2 -0
  104. package/dist/memory/prompt.cjs +84 -1
  105. package/dist/memory/prompt.d.ts +6 -0
  106. package/dist/memory/prompt.js +83 -0
  107. package/dist/memory/stores/entity/in_memory.cjs +32 -0
  108. package/dist/memory/stores/entity/in_memory.d.ts +10 -0
  109. package/dist/memory/stores/entity/in_memory.js +28 -0
  110. package/dist/prompts/index.cjs +6 -1
  111. package/dist/prompts/index.d.ts +1 -0
  112. package/dist/prompts/index.js +1 -0
  113. package/dist/{chains/prompt_selector.d.ts → prompts/selectors/conditional.d.ts} +4 -4
  114. package/dist/retrievers/document_compressors/chain_extract.cjs +9 -11
  115. package/dist/retrievers/document_compressors/chain_extract.js +9 -11
  116. package/dist/schema/index.cjs +13 -1
  117. package/dist/schema/index.d.ts +19 -0
  118. package/dist/schema/index.js +11 -0
  119. package/dist/stores/message/dynamodb.cjs +8 -6
  120. package/dist/stores/message/dynamodb.js +8 -6
  121. package/dist/stores/message/redis.cjs +69 -0
  122. package/dist/stores/message/redis.d.ts +18 -0
  123. package/dist/stores/message/redis.js +65 -0
  124. package/dist/stores/message/utils.cjs +30 -15
  125. package/dist/stores/message/utils.d.ts +4 -2
  126. package/dist/stores/message/utils.js +28 -14
  127. package/dist/text_splitter.cjs +3 -23
  128. package/dist/text_splitter.d.ts +1 -3
  129. package/dist/text_splitter.js +3 -23
  130. package/dist/tools/webbrowser.cjs +5 -7
  131. package/dist/tools/webbrowser.js +3 -5
  132. package/dist/types/openai-types.d.ts +3 -2
  133. package/dist/util/async_caller.cjs +16 -0
  134. package/dist/util/async_caller.d.ts +4 -0
  135. package/dist/util/async_caller.js +16 -0
  136. package/dist/util/axios-fetch-adapter.cjs +6 -0
  137. package/dist/util/axios-fetch-adapter.js +6 -0
  138. package/dist/util/env.cjs +39 -7
  139. package/dist/util/env.d.ts +19 -0
  140. package/dist/util/env.js +32 -6
  141. package/dist/util/sql_utils.cjs +18 -0
  142. package/dist/util/sql_utils.js +19 -1
  143. package/dist/util/tiktoken.cjs +26 -0
  144. package/dist/util/tiktoken.d.ts +9 -0
  145. package/dist/util/tiktoken.js +21 -0
  146. package/dist/vectorstores/redis.cjs +236 -0
  147. package/dist/vectorstores/redis.d.ts +80 -0
  148. package/dist/vectorstores/redis.js +232 -0
  149. package/document_loaders/web/apify_dataset.cjs +1 -0
  150. package/document_loaders/web/apify_dataset.d.ts +1 -0
  151. package/document_loaders/web/apify_dataset.js +1 -0
  152. package/package.json +41 -5
  153. package/stores/message/redis.cjs +1 -0
  154. package/stores/message/redis.d.ts +1 -0
  155. package/stores/message/redis.js +1 -0
  156. package/vectorstores/redis.cjs +1 -0
  157. package/vectorstores/redis.d.ts +1 -0
  158. package/vectorstores/redis.js +1 -0
  159. package/dist/callbacks/handlers/tracers.cjs +0 -341
  160. package/dist/callbacks/handlers/tracers.d.ts +0 -100
  161. package/dist/callbacks/handlers/tracers.js +0 -336
  162. /package/dist/{chains/prompt_selector.cjs → prompts/selectors/conditional.cjs} +0 -0
  163. /package/dist/{chains/prompt_selector.js → prompts/selectors/conditional.js} +0 -0
@@ -0,0 +1,398 @@
1
+ import { LangChainTracer } from "../callbacks/handlers/tracer_langchain.js";
2
+ import { mapStoredMessagesToChatMessages } from "../stores/message/utils.js";
3
+ // utility functions
4
+ const isLocalhost = (url) => {
5
+ const strippedUrl = url.replace("http://", "").replace("https://", "");
6
+ const hostname = strippedUrl.split("/")[0].split(":")[0];
7
+ return (hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1");
8
+ };
9
+ const getSeededTenantId = async (apiUrl, apiKey) => {
10
+ // Get the tenant ID from the seeded tenant
11
+ const url = `${apiUrl}/tenants`;
12
+ let response;
13
+ try {
14
+ response = await fetch(url, {
15
+ method: "GET",
16
+ headers: apiKey ? { authorization: `Bearer ${apiKey}` } : undefined,
17
+ });
18
+ }
19
+ catch (err) {
20
+ throw new Error("Unable to get seeded tenant ID. Please manually provide.");
21
+ }
22
+ if (!response.ok) {
23
+ throw new Error(`Failed to fetch seeded tenant ID: ${response.status} ${response.statusText}`);
24
+ }
25
+ const tenants = await response.json();
26
+ if (!Array.isArray(tenants)) {
27
+ throw new Error(`Expected tenants GET request to return an array, but got ${tenants}`);
28
+ }
29
+ if (tenants.length === 0) {
30
+ throw new Error("No seeded tenant found");
31
+ }
32
+ return tenants[0].id;
33
+ };
34
+ const stringifyError = (err) => {
35
+ let result;
36
+ if (err == null) {
37
+ result = "Error null or undefined";
38
+ }
39
+ else {
40
+ const error = err;
41
+ result = `Error: ${error?.name}: ${error?.message}`;
42
+ }
43
+ return result;
44
+ };
45
+ export function isLLM(llm) {
46
+ const blm = llm;
47
+ return (typeof blm?._modelType === "function" && blm?._modelType() === "base_llm");
48
+ }
49
+ export function isChatModel(llm) {
50
+ const blm = llm;
51
+ return (typeof blm?._modelType === "function" &&
52
+ blm?._modelType() === "base_chat_model");
53
+ }
54
+ export async function isChain(llm) {
55
+ if (isLLM(llm)) {
56
+ return false;
57
+ }
58
+ const bchFactory = llm;
59
+ const bch = await bchFactory();
60
+ return (typeof bch?._chainType === "function" && bch?._chainType() !== undefined);
61
+ }
62
+ async function getModelOrFactoryType(llm) {
63
+ if (isLLM(llm)) {
64
+ return "llm";
65
+ }
66
+ if (isChatModel(llm)) {
67
+ return "chatModel";
68
+ }
69
+ const bchFactory = llm;
70
+ const bch = await bchFactory();
71
+ if (typeof bch?._chainType === "function") {
72
+ return "chainFactory";
73
+ }
74
+ throw new Error("Unknown model or factory type");
75
+ }
76
+ export class LangChainPlusClient {
77
+ constructor(apiUrl, tenantId, apiKey) {
78
+ Object.defineProperty(this, "apiKey", {
79
+ enumerable: true,
80
+ configurable: true,
81
+ writable: true,
82
+ value: void 0
83
+ });
84
+ Object.defineProperty(this, "apiUrl", {
85
+ enumerable: true,
86
+ configurable: true,
87
+ writable: true,
88
+ value: void 0
89
+ });
90
+ Object.defineProperty(this, "tenantId", {
91
+ enumerable: true,
92
+ configurable: true,
93
+ writable: true,
94
+ value: void 0
95
+ });
96
+ this.apiUrl = apiUrl;
97
+ this.apiKey = apiKey;
98
+ this.tenantId = tenantId;
99
+ this.validateApiKeyIfHosted();
100
+ }
101
+ static async create(apiUrl, apiKey = undefined, tenantId = undefined) {
102
+ let tenantId_ = tenantId;
103
+ if (!tenantId_) {
104
+ tenantId_ = await getSeededTenantId(apiUrl, apiKey);
105
+ }
106
+ return new LangChainPlusClient(apiUrl, tenantId_, apiKey);
107
+ }
108
+ validateApiKeyIfHosted() {
109
+ const isLocal = isLocalhost(this.apiUrl);
110
+ if (!isLocal && !this.apiKey) {
111
+ throw new Error("API key must be provided when using hosted LangChain+ API");
112
+ }
113
+ }
114
+ get headers() {
115
+ const headers = {};
116
+ if (this.apiKey) {
117
+ headers.authorization = `Bearer ${this.apiKey}`;
118
+ }
119
+ return headers;
120
+ }
121
+ get queryParams() {
122
+ return { tenant_id: this.tenantId };
123
+ }
124
+ async _get(path, queryParams = {}) {
125
+ const params = { ...this.queryParams, ...queryParams };
126
+ let queryString = "";
127
+ for (const key in params) {
128
+ if (Object.prototype.hasOwnProperty.call(params, key)) {
129
+ queryString = queryString
130
+ ? `${queryString}&${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
131
+ : `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`;
132
+ }
133
+ }
134
+ const url = `${this.apiUrl}${path}${queryString ? `?${queryString}` : ""}`;
135
+ const response = await fetch(url, {
136
+ method: "GET",
137
+ headers: this.headers,
138
+ });
139
+ if (!response.ok) {
140
+ throw new Error(`Failed to fetch ${path}: ${response.status} ${response.statusText}`);
141
+ }
142
+ return response.json();
143
+ }
144
+ async uploadCsv(csvFile, fileName, description, inputKeys, outputKeys) {
145
+ const url = `${this.apiUrl}/datasets/upload`;
146
+ const formData = new FormData();
147
+ formData.append("file", csvFile, fileName);
148
+ formData.append("input_keys", inputKeys.join(","));
149
+ formData.append("output_keys", outputKeys.join(","));
150
+ formData.append("description", description);
151
+ formData.append("tenant_id", this.tenantId);
152
+ const response = await fetch(url, {
153
+ method: "POST",
154
+ headers: this.headers,
155
+ body: formData,
156
+ });
157
+ if (!response.ok) {
158
+ const result = await response.json();
159
+ if (result.detail && result.detail.includes("already exists")) {
160
+ throw new Error(`Dataset ${fileName} already exists`);
161
+ }
162
+ throw new Error(`Failed to upload CSV: ${response.status} ${response.statusText}`);
163
+ }
164
+ const result = await response.json();
165
+ return result;
166
+ }
167
+ async createDataset(name, description) {
168
+ const response = await fetch(`${this.apiUrl}/datasets`, {
169
+ method: "POST",
170
+ headers: { ...this.headers, "Content-Type": "application/json" },
171
+ body: JSON.stringify({
172
+ name,
173
+ description,
174
+ tenant_id: this.tenantId,
175
+ }),
176
+ });
177
+ if (!response.ok) {
178
+ const result = await response.json();
179
+ if (result.detail && result.detail.includes("already exists")) {
180
+ throw new Error(`Dataset ${name} already exists`);
181
+ }
182
+ throw new Error(`Failed to create dataset ${response.status} ${response.statusText}`);
183
+ }
184
+ const result = await response.json();
185
+ return result;
186
+ }
187
+ async readDataset(datasetId, datasetName) {
188
+ let path = "/datasets";
189
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
190
+ const params = { limit: 1 };
191
+ if (datasetId !== undefined && datasetName !== undefined) {
192
+ throw new Error("Must provide either datasetName or datasetId, not both");
193
+ }
194
+ else if (datasetId !== undefined) {
195
+ path += `/${datasetId}`;
196
+ }
197
+ else if (datasetName !== undefined) {
198
+ params.name = datasetName;
199
+ }
200
+ else {
201
+ throw new Error("Must provide datasetName or datasetId");
202
+ }
203
+ const response = await this._get(path, params);
204
+ let result;
205
+ if (Array.isArray(response)) {
206
+ if (response.length === 0) {
207
+ throw new Error(`Dataset[id=${datasetId}, name=${datasetName}] not found`);
208
+ }
209
+ result = response[0];
210
+ }
211
+ else {
212
+ result = response;
213
+ }
214
+ return result;
215
+ }
216
+ async listDatasets(limit = 100) {
217
+ const path = "/datasets";
218
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
219
+ const params = { limit };
220
+ const response = await this._get(path, params);
221
+ if (!Array.isArray(response)) {
222
+ throw new Error(`Expected ${path} to return an array, but got ${response}`);
223
+ }
224
+ return response;
225
+ }
226
+ async deleteDataset(datasetId, datasetName) {
227
+ let path = "/datasets";
228
+ let datasetId_ = datasetId;
229
+ if (datasetId !== undefined && datasetName !== undefined) {
230
+ throw new Error("Must provide either datasetName or datasetId, not both");
231
+ }
232
+ else if (datasetName !== undefined) {
233
+ const dataset = await this.readDataset(undefined, datasetName);
234
+ datasetId_ = dataset.id;
235
+ }
236
+ if (datasetId_ !== undefined) {
237
+ path += `/${datasetId_}`;
238
+ }
239
+ else {
240
+ throw new Error("Must provide datasetName or datasetId");
241
+ }
242
+ const response = await fetch(this.apiUrl + path, {
243
+ method: "DELETE",
244
+ headers: this.headers,
245
+ });
246
+ if (!response.ok) {
247
+ throw new Error(`Failed to delete ${path}: ${response.status} ${response.statusText}`);
248
+ }
249
+ const results = await response.json();
250
+ return results;
251
+ }
252
+ async createExample(inputs, outputs = {}, datasetId = undefined, datasetName = undefined, createdAt = undefined) {
253
+ let datasetId_ = datasetId;
254
+ if (datasetId_ === undefined && datasetName === undefined) {
255
+ throw new Error("Must provide either datasetName or datasetId");
256
+ }
257
+ else if (datasetId_ !== undefined && datasetName !== undefined) {
258
+ throw new Error("Must provide either datasetName or datasetId, not both");
259
+ }
260
+ else if (datasetId_ === undefined) {
261
+ const dataset = await this.readDataset(undefined, datasetName);
262
+ datasetId_ = dataset.id;
263
+ }
264
+ const createdAt_ = createdAt || new Date();
265
+ const data = {
266
+ dataset_id: datasetId_,
267
+ inputs,
268
+ outputs,
269
+ created_at: createdAt_.toISOString(),
270
+ };
271
+ const response = await fetch(`${this.apiUrl}/examples`, {
272
+ method: "POST",
273
+ headers: { ...this.headers, "Content-Type": "application/json" },
274
+ body: JSON.stringify(data),
275
+ });
276
+ if (!response.ok) {
277
+ throw new Error(`Failed to create example: ${response.status} ${response.statusText}`);
278
+ }
279
+ const result = await response.json();
280
+ return result;
281
+ }
282
+ async readExample(exampleId) {
283
+ const path = `/examples/${exampleId}`;
284
+ return await this._get(path);
285
+ }
286
+ async listExamples(datasetId = undefined, datasetName = undefined) {
287
+ let datasetId_;
288
+ if (datasetId !== undefined && datasetName !== undefined) {
289
+ throw new Error("Must provide either datasetName or datasetId, not both");
290
+ }
291
+ else if (datasetId !== undefined) {
292
+ datasetId_ = datasetId;
293
+ }
294
+ else if (datasetName !== undefined) {
295
+ const dataset = await this.readDataset(undefined, datasetName);
296
+ datasetId_ = dataset.id;
297
+ }
298
+ else {
299
+ throw new Error("Must provide a datasetName or datasetId");
300
+ }
301
+ const response = await this._get("/examples", {
302
+ dataset: datasetId_,
303
+ });
304
+ if (!Array.isArray(response)) {
305
+ throw new Error(`Expected /examples to return an array, but got ${response}`);
306
+ }
307
+ return response;
308
+ }
309
+ async deleteExample(exampleId) {
310
+ const path = `/examples/${exampleId}`;
311
+ const response = await fetch(this.apiUrl + path, {
312
+ method: "DELETE",
313
+ headers: this.headers,
314
+ });
315
+ if (!response.ok) {
316
+ throw new Error(`Failed to delete ${path}: ${response.status} ${response.statusText}`);
317
+ }
318
+ const result = await response.json();
319
+ return result;
320
+ }
321
+ async runLLM(example, tracer, llm, numRepetitions = 1) {
322
+ const results = await Promise.all(Array.from({ length: numRepetitions }).map(async () => {
323
+ try {
324
+ const prompt = example.inputs.prompt;
325
+ return llm.generate([prompt], undefined, [tracer]);
326
+ }
327
+ catch (e) {
328
+ console.error(e);
329
+ return stringifyError(e);
330
+ }
331
+ }));
332
+ return results;
333
+ }
334
+ async runChain(example, tracer, chainFactory, numRepetitions = 1) {
335
+ const results = await Promise.all(Array.from({ length: numRepetitions }).map(async () => {
336
+ try {
337
+ const chain = await chainFactory();
338
+ return chain.call(example.inputs, [tracer]);
339
+ }
340
+ catch (e) {
341
+ console.error(e);
342
+ return stringifyError(e);
343
+ }
344
+ }));
345
+ return results;
346
+ }
347
+ async runChatModel(example, tracer, chatModel, numRepetitions = 1) {
348
+ const results = await Promise.all(Array.from({ length: numRepetitions }).map(async () => {
349
+ try {
350
+ const messages = example.inputs.messages;
351
+ return chatModel.generate([mapStoredMessagesToChatMessages(messages)], undefined, [tracer]);
352
+ }
353
+ catch (e) {
354
+ console.error(e);
355
+ return stringifyError(e);
356
+ }
357
+ }));
358
+ return results;
359
+ }
360
+ async runOnDataset(datasetName, llmOrChainFactory, numRepetitions = 1, sessionName = undefined) {
361
+ const examples = await this.listExamples(undefined, datasetName);
362
+ let sessionName_;
363
+ if (sessionName === undefined) {
364
+ const currentTime = new Date().toISOString();
365
+ sessionName_ = `${datasetName}-${llmOrChainFactory.constructor.name}-${currentTime}`;
366
+ }
367
+ else {
368
+ sessionName_ = sessionName;
369
+ }
370
+ const results = {};
371
+ const modelOrFactoryType = await getModelOrFactoryType(llmOrChainFactory);
372
+ await Promise.all(examples.map(async (example) => {
373
+ const tracer = new LangChainTracer({
374
+ exampleId: example.id,
375
+ sessionName: sessionName_,
376
+ });
377
+ if (modelOrFactoryType === "llm") {
378
+ const llm = llmOrChainFactory;
379
+ const llmResult = await this.runLLM(example, tracer, llm, numRepetitions);
380
+ results[example.id] = llmResult;
381
+ }
382
+ else if (modelOrFactoryType === "chainFactory") {
383
+ const chainFactory = llmOrChainFactory;
384
+ const chainResult = await this.runChain(example, tracer, chainFactory, numRepetitions);
385
+ results[example.id] = chainResult;
386
+ }
387
+ else if (modelOrFactoryType === "chatModel") {
388
+ const chatModel = llmOrChainFactory;
389
+ const chatModelResult = await this.runChatModel(example, tracer, chatModel, numRepetitions);
390
+ results[example.id] = chatModelResult;
391
+ }
392
+ else {
393
+ throw new Error(` llm or chain type: ${llmOrChainFactory}`);
394
+ }
395
+ }));
396
+ return results;
397
+ }
398
+ }
@@ -42,6 +42,12 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
42
42
  writable: true,
43
43
  value: void 0
44
44
  });
45
+ Object.defineProperty(this, "strategy", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
45
51
  // Temporary shim to avoid breaking existing users
46
52
  // Remove when API keys are enforced by Unstructured and existing code will break anyway
47
53
  const isLegacySyntax = typeof optionsOrLegacyFilePath === "string";
@@ -53,6 +59,7 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
53
59
  this.filePath = filePathOrLegacyApiUrl;
54
60
  this.apiKey = optionsOrLegacyFilePath.apiKey;
55
61
  this.apiUrl = optionsOrLegacyFilePath.apiUrl ?? this.apiUrl;
62
+ this.strategy = optionsOrLegacyFilePath.strategy ?? "hi_res";
56
63
  }
57
64
  }
58
65
  async _partition() {
@@ -66,6 +73,7 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
66
73
  formData.append("files", new Blob([buffer]), fileName);
67
74
  const headers = {
68
75
  "UNSTRUCTURED-API-KEY": this.apiKey ?? "",
76
+ strategy: this.strategy,
69
77
  };
70
78
  const response = await fetch(this.apiUrl, {
71
79
  method: "POST",
@@ -15,15 +15,18 @@ type Element = {
15
15
  type UnstructuredLoaderOptions = {
16
16
  apiKey?: string;
17
17
  apiUrl?: string;
18
+ strategy?: string;
18
19
  };
19
20
  type UnstructuredDirectoryLoaderOptions = UnstructuredLoaderOptions & {
20
21
  recursive?: boolean;
21
22
  unknown?: UnknownHandling;
23
+ strategy?: string;
22
24
  };
23
25
  export declare class UnstructuredLoader extends BaseDocumentLoader {
24
26
  filePath: string;
25
27
  private apiUrl;
26
28
  private apiKey?;
29
+ private strategy;
27
30
  constructor(filePathOrLegacyApiUrl: string, optionsOrLegacyFilePath?: UnstructuredLoaderOptions | string);
28
31
  _partition(): Promise<Element[]>;
29
32
  load(): Promise<Document[]>;
@@ -38,6 +38,12 @@ export class UnstructuredLoader extends BaseDocumentLoader {
38
38
  writable: true,
39
39
  value: void 0
40
40
  });
41
+ Object.defineProperty(this, "strategy", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: void 0
46
+ });
41
47
  // Temporary shim to avoid breaking existing users
42
48
  // Remove when API keys are enforced by Unstructured and existing code will break anyway
43
49
  const isLegacySyntax = typeof optionsOrLegacyFilePath === "string";
@@ -49,6 +55,7 @@ export class UnstructuredLoader extends BaseDocumentLoader {
49
55
  this.filePath = filePathOrLegacyApiUrl;
50
56
  this.apiKey = optionsOrLegacyFilePath.apiKey;
51
57
  this.apiUrl = optionsOrLegacyFilePath.apiUrl ?? this.apiUrl;
58
+ this.strategy = optionsOrLegacyFilePath.strategy ?? "hi_res";
52
59
  }
53
60
  }
54
61
  async _partition() {
@@ -62,6 +69,7 @@ export class UnstructuredLoader extends BaseDocumentLoader {
62
69
  formData.append("files", new Blob([buffer]), fileName);
63
70
  const headers = {
64
71
  "UNSTRUCTURED-API-KEY": this.apiKey ?? "",
72
+ strategy: this.strategy,
65
73
  };
66
74
  const response = await fetch(this.apiUrl, {
67
75
  method: "POST",
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApifyDatasetLoader = void 0;
4
+ const apify_client_1 = require("apify-client");
5
+ const base_js_1 = require("../base.cjs");
6
+ class ApifyDatasetLoader extends base_js_1.BaseDocumentLoader {
7
+ constructor(datasetId, config) {
8
+ super();
9
+ Object.defineProperty(this, "apifyClient", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: void 0
14
+ });
15
+ Object.defineProperty(this, "datasetId", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "datasetMappingFunction", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ const apifyApiToken = ApifyDatasetLoader._getApifyApiToken(config.clientOptions);
28
+ this.apifyClient = new apify_client_1.ApifyClient({
29
+ ...config.clientOptions,
30
+ token: apifyApiToken,
31
+ });
32
+ this.datasetId = datasetId;
33
+ this.datasetMappingFunction = config.datasetMappingFunction;
34
+ }
35
+ static _getApifyApiToken(config) {
36
+ return (config?.token ??
37
+ // eslint-disable-next-line no-process-env
38
+ (typeof process !== "undefined" ? process.env.APIFY_API_TOKEN : undefined));
39
+ }
40
+ async load() {
41
+ const datasetItems = (await this.apifyClient.dataset(this.datasetId).listItems({ clean: true })).items;
42
+ return datasetItems.map(this.datasetMappingFunction);
43
+ }
44
+ /**
45
+ * Create an ApifyDatasetLoader by calling an Actor on the Apify platform and waiting for its results to be ready.
46
+ * @param actorId The ID or name of the Actor on the Apify platform.
47
+ * @param input The input object of the Actor that you're trying to run.
48
+ * @param options Options specifying settings for the Actor run.
49
+ * @param options.datasetMappingFunction A function that takes a single object (an Apify dataset item) and converts it to an instance of the Document class.
50
+ * @returns An instance of `ApifyDatasetLoader` with the results from the Actor run.
51
+ */
52
+ static async fromActorCall(actorId, input, config) {
53
+ const apifyApiToken = ApifyDatasetLoader._getApifyApiToken(config.clientOptions);
54
+ const apifyClient = new apify_client_1.ApifyClient({ token: apifyApiToken });
55
+ const actorCall = await apifyClient
56
+ .actor(actorId)
57
+ .call(input, config.callOptions ?? {});
58
+ return new ApifyDatasetLoader(actorCall.defaultDatasetId, {
59
+ datasetMappingFunction: config.datasetMappingFunction,
60
+ clientOptions: { ...config.clientOptions, token: apifyApiToken },
61
+ });
62
+ }
63
+ }
64
+ exports.ApifyDatasetLoader = ApifyDatasetLoader;
@@ -0,0 +1,28 @@
1
+ import { ApifyClient, ApifyClientOptions, ActorCallOptions } from "apify-client";
2
+ import { Document } from "../../document.js";
3
+ import { BaseDocumentLoader, DocumentLoader } from "../base.js";
4
+ export type ApifyDatasetMappingFunction = (item: Record<string | number, unknown>) => Document;
5
+ export declare class ApifyDatasetLoader extends BaseDocumentLoader implements DocumentLoader {
6
+ protected apifyClient: ApifyClient;
7
+ protected datasetId: string;
8
+ protected datasetMappingFunction: (item: Record<string | number, unknown>) => Document;
9
+ constructor(datasetId: string, config: {
10
+ datasetMappingFunction: ApifyDatasetMappingFunction;
11
+ clientOptions?: ApifyClientOptions;
12
+ });
13
+ private static _getApifyApiToken;
14
+ load(): Promise<Document[]>;
15
+ /**
16
+ * Create an ApifyDatasetLoader by calling an Actor on the Apify platform and waiting for its results to be ready.
17
+ * @param actorId The ID or name of the Actor on the Apify platform.
18
+ * @param input The input object of the Actor that you're trying to run.
19
+ * @param options Options specifying settings for the Actor run.
20
+ * @param options.datasetMappingFunction A function that takes a single object (an Apify dataset item) and converts it to an instance of the Document class.
21
+ * @returns An instance of `ApifyDatasetLoader` with the results from the Actor run.
22
+ */
23
+ static fromActorCall(actorId: string, input: Record<string | number, unknown>, config: {
24
+ callOptions?: ActorCallOptions;
25
+ clientOptions?: ApifyClientOptions;
26
+ datasetMappingFunction: ApifyDatasetMappingFunction;
27
+ }): Promise<ApifyDatasetLoader>;
28
+ }
@@ -0,0 +1,60 @@
1
+ import { ApifyClient, } from "apify-client";
2
+ import { BaseDocumentLoader } from "../base.js";
3
+ export class ApifyDatasetLoader extends BaseDocumentLoader {
4
+ constructor(datasetId, config) {
5
+ super();
6
+ Object.defineProperty(this, "apifyClient", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: void 0
11
+ });
12
+ Object.defineProperty(this, "datasetId", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ Object.defineProperty(this, "datasetMappingFunction", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ const apifyApiToken = ApifyDatasetLoader._getApifyApiToken(config.clientOptions);
25
+ this.apifyClient = new ApifyClient({
26
+ ...config.clientOptions,
27
+ token: apifyApiToken,
28
+ });
29
+ this.datasetId = datasetId;
30
+ this.datasetMappingFunction = config.datasetMappingFunction;
31
+ }
32
+ static _getApifyApiToken(config) {
33
+ return (config?.token ??
34
+ // eslint-disable-next-line no-process-env
35
+ (typeof process !== "undefined" ? process.env.APIFY_API_TOKEN : undefined));
36
+ }
37
+ async load() {
38
+ const datasetItems = (await this.apifyClient.dataset(this.datasetId).listItems({ clean: true })).items;
39
+ return datasetItems.map(this.datasetMappingFunction);
40
+ }
41
+ /**
42
+ * Create an ApifyDatasetLoader by calling an Actor on the Apify platform and waiting for its results to be ready.
43
+ * @param actorId The ID or name of the Actor on the Apify platform.
44
+ * @param input The input object of the Actor that you're trying to run.
45
+ * @param options Options specifying settings for the Actor run.
46
+ * @param options.datasetMappingFunction A function that takes a single object (an Apify dataset item) and converts it to an instance of the Document class.
47
+ * @returns An instance of `ApifyDatasetLoader` with the results from the Actor run.
48
+ */
49
+ static async fromActorCall(actorId, input, config) {
50
+ const apifyApiToken = ApifyDatasetLoader._getApifyApiToken(config.clientOptions);
51
+ const apifyClient = new ApifyClient({ token: apifyApiToken });
52
+ const actorCall = await apifyClient
53
+ .actor(actorId)
54
+ .call(input, config.callOptions ?? {});
55
+ return new ApifyDatasetLoader(actorCall.defaultDatasetId, {
56
+ datasetMappingFunction: config.datasetMappingFunction,
57
+ clientOptions: { ...config.clientOptions, token: apifyApiToken },
58
+ });
59
+ }
60
+ }
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.OpenAIEmbeddings = void 0;
7
7
  const openai_1 = require("openai");
8
- const browser_or_node_1 = require("browser-or-node");
8
+ const env_js_1 = require("../util/env.cjs");
9
9
  const axios_fetch_adapter_js_1 = __importDefault(require("../util/axios-fetch-adapter.cjs"));
10
10
  const chunk_js_1 = require("../util/chunk.cjs");
11
11
  const base_js_1 = require("./base.cjs");
@@ -159,7 +159,7 @@ class OpenAIEmbeddings extends base_js_1.Embeddings {
159
159
  basePath: endpoint,
160
160
  baseOptions: {
161
161
  timeout: this.timeout,
162
- adapter: browser_or_node_1.isNode ? undefined : axios_fetch_adapter_js_1.default,
162
+ adapter: (0, env_js_1.isNode)() ? undefined : axios_fetch_adapter_js_1.default,
163
163
  ...this.clientConfig.baseOptions,
164
164
  },
165
165
  });
@@ -1,5 +1,5 @@
1
1
  import { Configuration, OpenAIApi, } from "openai";
2
- import { isNode } from "browser-or-node";
2
+ import { isNode } from "../util/env.js";
3
3
  import fetchAdapter from "../util/axios-fetch-adapter.js";
4
4
  import { chunkArray } from "../util/chunk.js";
5
5
  import { Embeddings } from "./base.js";
@@ -153,7 +153,7 @@ export class OpenAIEmbeddings extends Embeddings {
153
153
  basePath: endpoint,
154
154
  baseOptions: {
155
155
  timeout: this.timeout,
156
- adapter: isNode ? undefined : fetchAdapter,
156
+ adapter: isNode() ? undefined : fetchAdapter,
157
157
  ...this.clientConfig.baseOptions,
158
158
  },
159
159
  });