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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.74",
3
+ "version": "0.0.76",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -124,6 +124,9 @@
124
124
  "vectorstores/myscale.cjs",
125
125
  "vectorstores/myscale.js",
126
126
  "vectorstores/myscale.d.ts",
127
+ "vectorstores/redis.cjs",
128
+ "vectorstores/redis.js",
129
+ "vectorstores/redis.d.ts",
127
130
  "text_splitter.cjs",
128
131
  "text_splitter.js",
129
132
  "text_splitter.d.ts",
@@ -142,6 +145,9 @@
142
145
  "document_loaders/base.cjs",
143
146
  "document_loaders/base.js",
144
147
  "document_loaders/base.d.ts",
148
+ "document_loaders/web/apify_dataset.cjs",
149
+ "document_loaders/web/apify_dataset.js",
150
+ "document_loaders/web/apify_dataset.d.ts",
145
151
  "document_loaders/web/cheerio.cjs",
146
152
  "document_loaders/web/cheerio.js",
147
153
  "document_loaders/web/cheerio.d.ts",
@@ -277,6 +283,9 @@
277
283
  "stores/message/dynamodb.cjs",
278
284
  "stores/message/dynamodb.js",
279
285
  "stores/message/dynamodb.d.ts",
286
+ "stores/message/redis.cjs",
287
+ "stores/message/redis.js",
288
+ "stores/message/redis.d.ts",
280
289
  "experimental/autogpt.cjs",
281
290
  "experimental/autogpt.js",
282
291
  "experimental/autogpt.d.ts",
@@ -286,6 +295,9 @@
286
295
  "experimental/plan_and_execute.cjs",
287
296
  "experimental/plan_and_execute.js",
288
297
  "experimental/plan_and_execute.d.ts",
298
+ "client.cjs",
299
+ "client.js",
300
+ "client.d.ts",
289
301
  "index.cjs",
290
302
  "index.js",
291
303
  "index.d.ts"
@@ -305,7 +317,7 @@
305
317
  "clean": "rm -rf dist/ && node scripts/create-entrypoints.js clean",
306
318
  "prepack": "yarn build",
307
319
  "release": "release-it --only-version --config .release-it.json",
308
- "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts",
320
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000",
309
321
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
310
322
  "test:integration": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 50000",
311
323
  "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 50000",
@@ -339,6 +351,7 @@
339
351
  "@types/uuid": "^9",
340
352
  "@typescript-eslint/eslint-plugin": "^5.58.0",
341
353
  "@typescript-eslint/parser": "^5.58.0",
354
+ "apify-client": "^2.7.1",
342
355
  "axios": "^0.26.0",
343
356
  "cheerio": "^1.0.0-rc.12",
344
357
  "chromadb": "^1.4.0",
@@ -363,7 +376,7 @@
363
376
  "playwright": "^1.32.1",
364
377
  "prettier": "^2.8.3",
365
378
  "puppeteer": "^19.7.2",
366
- "redis": "^4.6.4",
379
+ "redis": "^4.6.6",
367
380
  "release-it": "^15.10.1",
368
381
  "replicate": "^0.9.0",
369
382
  "rollup": "^3.19.1",
@@ -388,6 +401,7 @@
388
401
  "@tensorflow/tfjs-converter": "*",
389
402
  "@tensorflow/tfjs-core": "*",
390
403
  "@zilliz/milvus2-sdk-node": "^2.2.0",
404
+ "apify-client": "^2.7.1",
391
405
  "axios": "*",
392
406
  "cheerio": "^1.0.0-rc.12",
393
407
  "chromadb": "^1.4.0",
@@ -447,6 +461,9 @@
447
461
  "@zilliz/milvus2-sdk-node": {
448
462
  "optional": true
449
463
  },
464
+ "apify-client": {
465
+ "optional": true
466
+ },
450
467
  "axios": {
451
468
  "optional": true
452
469
  },
@@ -504,12 +521,11 @@
504
521
  },
505
522
  "dependencies": {
506
523
  "@anthropic-ai/sdk": "^0.4.3",
507
- "@dqbd/tiktoken": "^1.0.7",
508
524
  "ansi-styles": "^5.0.0",
509
525
  "binary-extensions": "^2.2.0",
510
- "browser-or-node": "^2.1.1",
511
526
  "expr-eval": "^2.0.2",
512
527
  "flat": "^5.0.2",
528
+ "js-tiktoken": "^1.0.6",
513
529
  "jsonpointer": "^5.0.1",
514
530
  "ml-distance": "^4.0.0",
515
531
  "object-hash": "^3.0.0",
@@ -740,6 +756,11 @@
740
756
  "import": "./vectorstores/myscale.js",
741
757
  "require": "./vectorstores/myscale.cjs"
742
758
  },
759
+ "./vectorstores/redis": {
760
+ "types": "./vectorstores/redis.d.ts",
761
+ "import": "./vectorstores/redis.js",
762
+ "require": "./vectorstores/redis.cjs"
763
+ },
743
764
  "./text_splitter": {
744
765
  "types": "./text_splitter.d.ts",
745
766
  "import": "./text_splitter.js",
@@ -772,6 +793,11 @@
772
793
  "import": "./document_loaders/base.js",
773
794
  "require": "./document_loaders/base.cjs"
774
795
  },
796
+ "./document_loaders/web/apify_dataset": {
797
+ "types": "./document_loaders/web/apify_dataset.d.ts",
798
+ "import": "./document_loaders/web/apify_dataset.js",
799
+ "require": "./document_loaders/web/apify_dataset.cjs"
800
+ },
775
801
  "./document_loaders/web/cheerio": {
776
802
  "types": "./document_loaders/web/cheerio.d.ts",
777
803
  "import": "./document_loaders/web/cheerio.js",
@@ -1001,6 +1027,11 @@
1001
1027
  "import": "./stores/message/dynamodb.js",
1002
1028
  "require": "./stores/message/dynamodb.cjs"
1003
1029
  },
1030
+ "./stores/message/redis": {
1031
+ "types": "./stores/message/redis.d.ts",
1032
+ "import": "./stores/message/redis.js",
1033
+ "require": "./stores/message/redis.cjs"
1034
+ },
1004
1035
  "./experimental/autogpt": {
1005
1036
  "types": "./experimental/autogpt.d.ts",
1006
1037
  "import": "./experimental/autogpt.js",
@@ -1016,6 +1047,11 @@
1016
1047
  "import": "./experimental/plan_and_execute.js",
1017
1048
  "require": "./experimental/plan_and_execute.cjs"
1018
1049
  },
1050
+ "./client": {
1051
+ "types": "./client.d.ts",
1052
+ "import": "./client.js",
1053
+ "require": "./client.cjs"
1054
+ },
1019
1055
  "./package.json": "./package.json"
1020
1056
  }
1021
1057
  }
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/stores/message/redis.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/stores/message/redis.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/stores/message/redis.js'
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/vectorstores/redis.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/vectorstores/redis.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/vectorstores/redis.js'
@@ -1,341 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LangChainTracer = exports.BaseTracer = void 0;
4
- const base_js_1 = require("../base.cjs");
5
- class BaseTracer extends base_js_1.BaseCallbackHandler {
6
- constructor() {
7
- super();
8
- Object.defineProperty(this, "session", {
9
- enumerable: true,
10
- configurable: true,
11
- writable: true,
12
- value: void 0
13
- });
14
- Object.defineProperty(this, "runMap", {
15
- enumerable: true,
16
- configurable: true,
17
- writable: true,
18
- value: new Map()
19
- });
20
- }
21
- copy() {
22
- return this;
23
- }
24
- async newSession(sessionName) {
25
- const sessionCreate = {
26
- start_time: Date.now(),
27
- name: sessionName,
28
- };
29
- const session = await this.persistSession(sessionCreate);
30
- this.session = session;
31
- return session;
32
- }
33
- _addChildRun(parentRun, childRun) {
34
- if (childRun.type === "llm") {
35
- parentRun.child_llm_runs.push(childRun);
36
- }
37
- else if (childRun.type === "chain") {
38
- parentRun.child_chain_runs.push(childRun);
39
- }
40
- else if (childRun.type === "tool") {
41
- parentRun.child_tool_runs.push(childRun);
42
- }
43
- else {
44
- throw new Error("Invalid run type");
45
- }
46
- }
47
- _startTrace(run) {
48
- if (run.parent_uuid) {
49
- const parentRun = this.runMap.get(run.parent_uuid);
50
- if (parentRun) {
51
- if (!(parentRun.type === "tool" || parentRun.type === "chain")) {
52
- throw new Error("Caller run can only be a tool or chain");
53
- }
54
- else {
55
- this._addChildRun(parentRun, run);
56
- }
57
- }
58
- else {
59
- throw new Error(`Caller run ${run.parent_uuid} not found`);
60
- }
61
- }
62
- this.runMap.set(run.uuid, run);
63
- }
64
- async _endTrace(run) {
65
- if (!run.parent_uuid) {
66
- await this.persistRun(run);
67
- }
68
- else {
69
- const parentRun = this.runMap.get(run.parent_uuid);
70
- if (parentRun === undefined) {
71
- throw new Error(`Parent run ${run.parent_uuid} not found`);
72
- }
73
- parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
74
- }
75
- this.runMap.delete(run.uuid);
76
- }
77
- _getExecutionOrder(parentRunId) {
78
- // If a run has no parent then execution order is 1
79
- if (parentRunId === undefined) {
80
- return 1;
81
- }
82
- const parentRun = this.runMap.get(parentRunId);
83
- if (parentRun === undefined) {
84
- throw new Error(`Parent run ${parentRunId} not found`);
85
- }
86
- return parentRun.child_execution_order + 1;
87
- }
88
- async handleLLMStart(llm, prompts, runId, parentRunId) {
89
- if (this.session === undefined) {
90
- this.session = await this.loadDefaultSession();
91
- }
92
- const execution_order = this._getExecutionOrder(parentRunId);
93
- const run = {
94
- uuid: runId,
95
- parent_uuid: parentRunId,
96
- start_time: Date.now(),
97
- end_time: 0,
98
- serialized: llm,
99
- prompts,
100
- session_id: this.session.id,
101
- execution_order,
102
- child_execution_order: execution_order,
103
- type: "llm",
104
- };
105
- this._startTrace(run);
106
- await this.onLLMStart?.(run);
107
- }
108
- async handleLLMEnd(output, runId) {
109
- const run = this.runMap.get(runId);
110
- if (!run || run?.type !== "llm") {
111
- throw new Error("No LLM run to end.");
112
- }
113
- const llmRun = run;
114
- llmRun.end_time = Date.now();
115
- llmRun.response = output;
116
- await this.onLLMEnd?.(llmRun);
117
- await this._endTrace(llmRun);
118
- }
119
- async handleLLMError(error, runId) {
120
- const run = this.runMap.get(runId);
121
- if (!run || run?.type !== "llm") {
122
- throw new Error("No LLM run to end.");
123
- }
124
- const llmRun = run;
125
- llmRun.end_time = Date.now();
126
- llmRun.error = error.message;
127
- await this.onLLMError?.(llmRun);
128
- await this._endTrace(llmRun);
129
- }
130
- async handleChainStart(chain, inputs, runId, parentRunId) {
131
- if (this.session === undefined) {
132
- this.session = await this.loadDefaultSession();
133
- }
134
- const execution_order = this._getExecutionOrder(parentRunId);
135
- const run = {
136
- uuid: runId,
137
- parent_uuid: parentRunId,
138
- start_time: Date.now(),
139
- end_time: 0,
140
- serialized: chain,
141
- inputs,
142
- session_id: this.session.id,
143
- execution_order,
144
- child_execution_order: execution_order,
145
- type: "chain",
146
- child_llm_runs: [],
147
- child_chain_runs: [],
148
- child_tool_runs: [],
149
- };
150
- this._startTrace(run);
151
- await this.onChainStart?.(run);
152
- }
153
- async handleChainEnd(outputs, runId) {
154
- const run = this.runMap.get(runId);
155
- if (!run || run?.type !== "chain") {
156
- throw new Error("No chain run to end.");
157
- }
158
- const chainRun = run;
159
- chainRun.end_time = Date.now();
160
- chainRun.outputs = outputs;
161
- await this.onChainEnd?.(chainRun);
162
- await this._endTrace(chainRun);
163
- }
164
- async handleChainError(error, runId) {
165
- const run = this.runMap.get(runId);
166
- if (!run || run?.type !== "chain") {
167
- throw new Error("No chain run to end.");
168
- }
169
- const chainRun = run;
170
- chainRun.end_time = Date.now();
171
- chainRun.error = error.message;
172
- await this.onChainError?.(chainRun);
173
- await this._endTrace(chainRun);
174
- }
175
- async handleToolStart(tool, input, runId, parentRunId) {
176
- if (this.session === undefined) {
177
- this.session = await this.loadDefaultSession();
178
- }
179
- const execution_order = this._getExecutionOrder(parentRunId);
180
- const run = {
181
- uuid: runId,
182
- parent_uuid: parentRunId,
183
- start_time: Date.now(),
184
- end_time: 0,
185
- serialized: tool,
186
- tool_input: input,
187
- session_id: this.session.id,
188
- execution_order,
189
- child_execution_order: execution_order,
190
- type: "tool",
191
- action: JSON.stringify(tool),
192
- child_llm_runs: [],
193
- child_chain_runs: [],
194
- child_tool_runs: [],
195
- };
196
- this._startTrace(run);
197
- await this.onToolStart?.(run);
198
- }
199
- async handleToolEnd(output, runId) {
200
- const run = this.runMap.get(runId);
201
- if (!run || run?.type !== "tool") {
202
- throw new Error("No tool run to end");
203
- }
204
- const toolRun = run;
205
- toolRun.end_time = Date.now();
206
- toolRun.output = output;
207
- await this.onToolEnd?.(toolRun);
208
- await this._endTrace(toolRun);
209
- }
210
- async handleToolError(error, runId) {
211
- const run = this.runMap.get(runId);
212
- if (!run || run?.type !== "tool") {
213
- throw new Error("No tool run to end");
214
- }
215
- const toolRun = run;
216
- toolRun.end_time = Date.now();
217
- toolRun.error = error.message;
218
- await this.onToolError?.(toolRun);
219
- await this._endTrace(toolRun);
220
- }
221
- async handleAgentAction(action, runId) {
222
- const run = this.runMap.get(runId);
223
- if (!run || run?.type !== "chain") {
224
- return;
225
- }
226
- const agentRun = run;
227
- agentRun.actions = agentRun.actions || [];
228
- agentRun.actions.push(action);
229
- await this.onAgentAction?.(run);
230
- }
231
- }
232
- exports.BaseTracer = BaseTracer;
233
- class LangChainTracer extends BaseTracer {
234
- constructor() {
235
- super();
236
- Object.defineProperty(this, "name", {
237
- enumerable: true,
238
- configurable: true,
239
- writable: true,
240
- value: "langchain_tracer"
241
- });
242
- Object.defineProperty(this, "endpoint", {
243
- enumerable: true,
244
- configurable: true,
245
- writable: true,
246
- value: (typeof process !== "undefined"
247
- ? // eslint-disable-next-line no-process-env
248
- process.env?.LANGCHAIN_ENDPOINT
249
- : undefined) || "http://localhost:8000"
250
- });
251
- Object.defineProperty(this, "headers", {
252
- enumerable: true,
253
- configurable: true,
254
- writable: true,
255
- value: {
256
- "Content-Type": "application/json",
257
- }
258
- });
259
- // eslint-disable-next-line no-process-env
260
- if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
261
- // eslint-disable-next-line no-process-env
262
- this.headers["x-api-key"] = process.env?.LANGCHAIN_API_KEY;
263
- }
264
- }
265
- async persistRun(run) {
266
- let endpoint;
267
- if (run.type === "llm") {
268
- endpoint = `${this.endpoint}/llm-runs`;
269
- }
270
- else if (run.type === "chain") {
271
- endpoint = `${this.endpoint}/chain-runs`;
272
- }
273
- else {
274
- endpoint = `${this.endpoint}/tool-runs`;
275
- }
276
- const response = await fetch(endpoint, {
277
- method: "POST",
278
- headers: this.headers,
279
- body: JSON.stringify(run),
280
- });
281
- if (!response.ok) {
282
- console.error(`Failed to persist run: ${response.status} ${response.statusText}`);
283
- }
284
- }
285
- async persistSession(sessionCreate) {
286
- const endpoint = `${this.endpoint}/sessions`;
287
- const response = await fetch(endpoint, {
288
- method: "POST",
289
- headers: this.headers,
290
- body: JSON.stringify(sessionCreate),
291
- });
292
- if (!response.ok) {
293
- console.error(`Failed to persist session: ${response.status} ${response.statusText}, using default session.`);
294
- return {
295
- id: 1,
296
- ...sessionCreate,
297
- };
298
- }
299
- return {
300
- id: (await response.json()).id,
301
- ...sessionCreate,
302
- };
303
- }
304
- async loadSession(sessionName) {
305
- const endpoint = `${this.endpoint}/sessions?name=${sessionName}`;
306
- return this._handleSessionResponse(endpoint);
307
- }
308
- async loadDefaultSession() {
309
- const endpoint = `${this.endpoint}/sessions?name=default`;
310
- return this._handleSessionResponse(endpoint);
311
- }
312
- async _handleSessionResponse(endpoint) {
313
- const response = await fetch(endpoint, {
314
- method: "GET",
315
- headers: this.headers,
316
- });
317
- let tracerSession;
318
- if (!response.ok) {
319
- console.error(`Failed to load session: ${response.status} ${response.statusText}`);
320
- tracerSession = {
321
- id: 1,
322
- start_time: Date.now(),
323
- };
324
- this.session = tracerSession;
325
- return tracerSession;
326
- }
327
- const resp = (await response.json());
328
- if (resp.length === 0) {
329
- tracerSession = {
330
- id: 1,
331
- start_time: Date.now(),
332
- };
333
- this.session = tracerSession;
334
- return tracerSession;
335
- }
336
- [tracerSession] = resp;
337
- this.session = tracerSession;
338
- return tracerSession;
339
- }
340
- }
341
- exports.LangChainTracer = LangChainTracer;
@@ -1,100 +0,0 @@
1
- import { AgentAction, ChainValues, LLMResult } from "../../schema/index.js";
2
- import { BaseCallbackHandler } from "../base.js";
3
- export type RunType = "llm" | "chain" | "tool";
4
- export interface BaseTracerSession {
5
- start_time: number;
6
- name?: string;
7
- }
8
- export type TracerSessionCreate = BaseTracerSession;
9
- export interface TracerSession extends BaseTracerSession {
10
- id: number;
11
- }
12
- export interface BaseRun {
13
- uuid: string;
14
- parent_uuid?: string;
15
- start_time: number;
16
- end_time: number;
17
- execution_order: number;
18
- child_execution_order: number;
19
- serialized: {
20
- name: string;
21
- };
22
- session_id: number;
23
- error?: string;
24
- type: RunType;
25
- }
26
- export interface LLMRun extends BaseRun {
27
- prompts: string[];
28
- response?: LLMResult;
29
- }
30
- export interface ChainRun extends BaseRun {
31
- inputs: ChainValues;
32
- outputs?: ChainValues;
33
- child_llm_runs: LLMRun[];
34
- child_chain_runs: ChainRun[];
35
- child_tool_runs: ToolRun[];
36
- }
37
- export interface AgentRun extends ChainRun {
38
- actions: AgentAction[];
39
- }
40
- export interface ToolRun extends BaseRun {
41
- tool_input: string;
42
- output?: string;
43
- action: string;
44
- child_llm_runs: LLMRun[];
45
- child_chain_runs: ChainRun[];
46
- child_tool_runs: ToolRun[];
47
- }
48
- export type Run = LLMRun | ChainRun | ToolRun;
49
- export declare abstract class BaseTracer extends BaseCallbackHandler {
50
- protected session?: TracerSession;
51
- protected runMap: Map<string, Run>;
52
- protected constructor();
53
- copy(): this;
54
- abstract loadSession(sessionName: string): Promise<TracerSession>;
55
- abstract loadDefaultSession(): Promise<TracerSession>;
56
- protected abstract persistRun(run: Run): Promise<void>;
57
- protected abstract persistSession(session: TracerSessionCreate): Promise<TracerSession>;
58
- newSession(sessionName?: string): Promise<TracerSession>;
59
- protected _addChildRun(parentRun: ChainRun | ToolRun, childRun: Run): void;
60
- protected _startTrace(run: Run): void;
61
- protected _endTrace(run: Run): Promise<void>;
62
- protected _getExecutionOrder(parentRunId: string | undefined): number;
63
- handleLLMStart(llm: {
64
- name: string;
65
- }, prompts: string[], runId: string, parentRunId?: string): Promise<void>;
66
- handleLLMEnd(output: LLMResult, runId: string): Promise<void>;
67
- handleLLMError(error: Error, runId: string): Promise<void>;
68
- handleChainStart(chain: {
69
- name: string;
70
- }, inputs: ChainValues, runId: string, parentRunId?: string): Promise<void>;
71
- handleChainEnd(outputs: ChainValues, runId: string): Promise<void>;
72
- handleChainError(error: Error, runId: string): Promise<void>;
73
- handleToolStart(tool: {
74
- name: string;
75
- }, input: string, runId: string, parentRunId?: string): Promise<void>;
76
- handleToolEnd(output: string, runId: string): Promise<void>;
77
- handleToolError(error: Error, runId: string): Promise<void>;
78
- handleAgentAction(action: AgentAction, runId: string): Promise<void>;
79
- onLLMStart?(run: LLMRun): void | Promise<void>;
80
- onLLMEnd?(run: LLMRun): void | Promise<void>;
81
- onLLMError?(run: LLMRun): void | Promise<void>;
82
- onChainStart?(run: ChainRun): void | Promise<void>;
83
- onChainEnd?(run: ChainRun): void | Promise<void>;
84
- onChainError?(run: ChainRun): void | Promise<void>;
85
- onToolStart?(run: ToolRun): void | Promise<void>;
86
- onToolEnd?(run: ToolRun): void | Promise<void>;
87
- onToolError?(run: ToolRun): void | Promise<void>;
88
- onAgentAction?(run: AgentRun): void | Promise<void>;
89
- }
90
- export declare class LangChainTracer extends BaseTracer {
91
- name: string;
92
- protected endpoint: string;
93
- protected headers: Record<string, string>;
94
- constructor();
95
- protected persistRun(run: LLMRun | ChainRun | ToolRun): Promise<void>;
96
- protected persistSession(sessionCreate: TracerSessionCreate): Promise<TracerSession>;
97
- loadSession(sessionName: string): Promise<TracerSession>;
98
- loadDefaultSession(): Promise<TracerSession>;
99
- private _handleSessionResponse;
100
- }