langchain 0.0.195 → 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 (236) 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 +5 -70
  6. package/dist/base_language/count_tokens.d.ts +1 -10
  7. package/dist/base_language/count_tokens.js +1 -65
  8. package/dist/base_language/index.cjs +6 -196
  9. package/dist/base_language/index.d.ts +1 -111
  10. package/dist/base_language/index.js +1 -191
  11. package/dist/cache/base.cjs +15 -37
  12. package/dist/cache/base.d.ts +1 -20
  13. package/dist/cache/base.js +1 -33
  14. package/dist/cache/index.cjs +2 -46
  15. package/dist/cache/index.d.ts +1 -29
  16. package/dist/cache/index.js +1 -45
  17. package/dist/callbacks/base.cjs +3 -139
  18. package/dist/callbacks/base.d.ts +1 -266
  19. package/dist/callbacks/base.js +1 -126
  20. package/dist/callbacks/handlers/console.cjs +14 -221
  21. package/dist/callbacks/handlers/console.d.ts +1 -117
  22. package/dist/callbacks/handlers/console.js +1 -217
  23. package/dist/callbacks/handlers/initialize.cjs +15 -30
  24. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  25. package/dist/callbacks/handlers/initialize.js +1 -27
  26. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  27. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  28. package/dist/callbacks/handlers/log_stream.js +1 -289
  29. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  30. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  31. package/dist/callbacks/handlers/run_collector.js +1 -46
  32. package/dist/callbacks/handlers/tracer.cjs +15 -375
  33. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  34. package/dist/callbacks/handlers/tracer.js +1 -373
  35. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  36. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  37. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  38. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  39. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  40. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  41. package/dist/callbacks/manager.cjs +15 -676
  42. package/dist/callbacks/manager.d.ts +1 -180
  43. package/dist/callbacks/manager.js +1 -666
  44. package/dist/callbacks/promises.cjs +14 -42
  45. package/dist/callbacks/promises.d.ts +1 -11
  46. package/dist/callbacks/promises.js +1 -37
  47. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  48. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  49. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  50. package/dist/chains/openai_functions/structured_output.js +1 -1
  51. package/dist/chat_models/anthropic.cjs +15 -348
  52. package/dist/chat_models/anthropic.d.ts +1 -156
  53. package/dist/chat_models/anthropic.js +1 -346
  54. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  55. package/dist/chat_models/base.cjs +15 -296
  56. package/dist/chat_models/base.d.ts +1 -122
  57. package/dist/chat_models/base.js +1 -292
  58. package/dist/chat_models/bedrock/web.cjs +21 -1
  59. package/dist/chat_models/bedrock/web.d.ts +2 -2
  60. package/dist/chat_models/bedrock/web.js +21 -1
  61. package/dist/chat_models/fireworks.d.ts +1 -1
  62. package/dist/document.cjs +2 -24
  63. package/dist/document.d.ts +1 -12
  64. package/dist/document.js +1 -23
  65. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  66. package/dist/document_loaders/web/github.cjs +105 -0
  67. package/dist/document_loaders/web/github.d.ts +26 -0
  68. package/dist/document_loaders/web/github.js +105 -0
  69. package/dist/document_loaders/web/s3.d.ts +1 -1
  70. package/dist/embeddings/base.cjs +15 -22
  71. package/dist/embeddings/base.d.ts +1 -33
  72. package/dist/embeddings/base.js +1 -20
  73. package/dist/embeddings/cache_backed.cjs +2 -2
  74. package/dist/embeddings/cache_backed.js +1 -1
  75. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  76. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  77. package/dist/evaluation/qa/prompt.d.ts +2 -2
  78. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  79. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  80. package/dist/llms/base.cjs +15 -278
  81. package/dist/llms/base.d.ts +1 -115
  82. package/dist/llms/base.js +1 -275
  83. package/dist/llms/bedrock/web.cjs +21 -1
  84. package/dist/llms/bedrock/web.d.ts +2 -2
  85. package/dist/llms/bedrock/web.js +21 -1
  86. package/dist/llms/fireworks.d.ts +1 -1
  87. package/dist/load/import_map.cjs +2 -1
  88. package/dist/load/import_map.d.ts +1 -0
  89. package/dist/load/import_map.js +1 -0
  90. package/dist/load/index.cjs +7 -148
  91. package/dist/load/index.js +7 -148
  92. package/dist/load/map_keys.cjs +0 -24
  93. package/dist/load/map_keys.d.ts +0 -6
  94. package/dist/load/map_keys.js +1 -17
  95. package/dist/load/serializable.cjs +15 -178
  96. package/dist/load/serializable.d.ts +1 -66
  97. package/dist/load/serializable.js +1 -175
  98. package/dist/memory/base.cjs +17 -92
  99. package/dist/memory/base.d.ts +2 -68
  100. package/dist/memory/base.js +2 -87
  101. package/dist/output_parsers/list.cjs +4 -122
  102. package/dist/output_parsers/list.d.ts +1 -57
  103. package/dist/output_parsers/list.js +1 -119
  104. package/dist/output_parsers/openai_functions.cjs +2 -2
  105. package/dist/output_parsers/openai_functions.d.ts +1 -1
  106. package/dist/output_parsers/openai_functions.js +1 -1
  107. package/dist/output_parsers/regex.d.ts +1 -1
  108. package/dist/output_parsers/structured.d.ts +1 -1
  109. package/dist/prompts/base.cjs +8 -183
  110. package/dist/prompts/base.d.ts +3 -132
  111. package/dist/prompts/base.js +3 -178
  112. package/dist/prompts/chat.cjs +13 -477
  113. package/dist/prompts/chat.d.ts +2 -219
  114. package/dist/prompts/chat.js +2 -466
  115. package/dist/prompts/few_shot.cjs +3 -352
  116. package/dist/prompts/few_shot.d.ts +1 -192
  117. package/dist/prompts/few_shot.js +1 -350
  118. package/dist/prompts/index.cjs +3 -2
  119. package/dist/prompts/index.d.ts +2 -1
  120. package/dist/prompts/index.js +2 -1
  121. package/dist/prompts/pipeline.cjs +2 -141
  122. package/dist/prompts/pipeline.d.ts +1 -98
  123. package/dist/prompts/pipeline.js +1 -140
  124. package/dist/prompts/prompt.cjs +2 -145
  125. package/dist/prompts/prompt.d.ts +1 -92
  126. package/dist/prompts/prompt.js +1 -144
  127. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +2 -147
  128. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  129. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  130. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  131. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  132. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  133. package/dist/prompts/selectors/conditional.cjs +5 -73
  134. package/dist/prompts/selectors/conditional.d.ts +1 -63
  135. package/dist/prompts/selectors/conditional.js +1 -69
  136. package/dist/prompts/serde.d.ts +1 -43
  137. package/dist/prompts/template.cjs +8 -88
  138. package/dist/prompts/template.d.ts +1 -36
  139. package/dist/prompts/template.js +1 -83
  140. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  141. package/dist/runnables/index.d.ts +1 -0
  142. package/dist/runnables/index.js +1 -0
  143. package/dist/schema/document.cjs +3 -34
  144. package/dist/schema/document.d.ts +2 -29
  145. package/dist/schema/document.js +2 -32
  146. package/dist/schema/index.cjs +37 -612
  147. package/dist/schema/index.d.ts +11 -311
  148. package/dist/schema/index.js +8 -583
  149. package/dist/schema/output_parser.cjs +15 -309
  150. package/dist/schema/output_parser.d.ts +1 -173
  151. package/dist/schema/output_parser.js +1 -301
  152. package/dist/schema/retriever.cjs +15 -77
  153. package/dist/schema/retriever.d.ts +1 -43
  154. package/dist/schema/retriever.js +1 -75
  155. package/dist/schema/runnable/base.cjs +10 -1072
  156. package/dist/schema/runnable/base.d.ts +1 -356
  157. package/dist/schema/runnable/base.js +1 -1060
  158. package/dist/schema/runnable/branch.cjs +2 -131
  159. package/dist/schema/runnable/branch.d.ts +1 -94
  160. package/dist/schema/runnable/branch.js +1 -130
  161. package/dist/schema/runnable/config.cjs +0 -6
  162. package/dist/schema/runnable/config.d.ts +1 -3
  163. package/dist/schema/runnable/config.js +1 -4
  164. package/dist/schema/runnable/index.cjs +15 -16
  165. package/dist/schema/runnable/index.d.ts +1 -5
  166. package/dist/schema/runnable/index.js +1 -4
  167. package/dist/schema/runnable/passthrough.cjs +3 -113
  168. package/dist/schema/runnable/passthrough.d.ts +1 -72
  169. package/dist/schema/runnable/passthrough.js +1 -111
  170. package/dist/schema/runnable/router.cjs +2 -71
  171. package/dist/schema/runnable/router.d.ts +1 -29
  172. package/dist/schema/runnable/router.js +1 -70
  173. package/dist/schema/storage.cjs +15 -8
  174. package/dist/schema/storage.d.ts +1 -57
  175. package/dist/schema/storage.js +1 -6
  176. package/dist/tools/bingserpapi.d.ts +1 -1
  177. package/dist/tools/searchapi.d.ts +1 -1
  178. package/dist/tools/serpapi.d.ts +1 -1
  179. package/dist/tools/serper.d.ts +1 -1
  180. package/dist/util/async_caller.cjs +14 -128
  181. package/dist/util/async_caller.d.ts +1 -45
  182. package/dist/util/async_caller.js +1 -124
  183. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  184. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  185. package/dist/vectorstores/momento_vector_index.js +40 -1
  186. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  187. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  188. package/dist/vectorstores/mongodb_atlas.js +22 -2
  189. package/package.json +18 -11
  190. package/runnables.cjs +1 -0
  191. package/runnables.d.ts +1 -0
  192. package/runnables.js +1 -0
  193. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  194. package/dist/util/@cfworker/json-schema/index.js +0 -1
  195. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  196. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  197. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  198. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  199. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  200. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  201. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  202. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  203. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  204. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  205. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  206. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  207. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  208. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  209. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  210. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  211. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  212. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  213. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  214. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  215. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  216. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  217. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  218. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  219. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  220. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  221. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  222. package/dist/util/fast-json-patch/index.cjs +0 -49
  223. package/dist/util/fast-json-patch/index.d.ts +0 -22
  224. package/dist/util/fast-json-patch/index.js +0 -16
  225. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  226. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  227. package/dist/util/fast-json-patch/src/core.js +0 -459
  228. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  229. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  230. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  231. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  232. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  233. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  234. package/dist/util/js-sha1/hash.cjs +0 -358
  235. package/dist/util/js-sha1/hash.d.ts +0 -1
  236. package/dist/util/js-sha1/hash.js +0 -355
@@ -1,199 +1,17 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LangChainTracerV1 = void 0;
4
- const base_js_1 = require("../../memory/base.cjs");
5
- const env_js_1 = require("../../util/env.cjs");
6
- const tracer_js_1 = require("./tracer.cjs");
7
- class LangChainTracerV1 extends tracer_js_1.BaseTracer {
8
- constructor() {
9
- super();
10
- Object.defineProperty(this, "name", {
11
- enumerable: true,
12
- configurable: true,
13
- writable: true,
14
- value: "langchain_tracer"
15
- });
16
- Object.defineProperty(this, "endpoint", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
21
- });
22
- Object.defineProperty(this, "headers", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: {
27
- "Content-Type": "application/json",
28
- }
29
- });
30
- Object.defineProperty(this, "session", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: void 0
35
- });
36
- const apiKey = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_API_KEY");
37
- if (apiKey) {
38
- this.headers["x-api-key"] = apiKey;
39
- }
40
- }
41
- async newSession(sessionName) {
42
- const sessionCreate = {
43
- start_time: Date.now(),
44
- name: sessionName,
45
- };
46
- const session = await this.persistSession(sessionCreate);
47
- this.session = session;
48
- return session;
49
- }
50
- async loadSession(sessionName) {
51
- const endpoint = `${this.endpoint}/sessions?name=${sessionName}`;
52
- return this._handleSessionResponse(endpoint);
53
- }
54
- async loadDefaultSession() {
55
- const endpoint = `${this.endpoint}/sessions?name=default`;
56
- return this._handleSessionResponse(endpoint);
57
- }
58
- async convertV2RunToRun(run) {
59
- const session = this.session ?? (await this.loadDefaultSession());
60
- const serialized = run.serialized;
61
- let runResult;
62
- if (run.run_type === "llm") {
63
- const prompts = run.inputs.prompts
64
- ? run.inputs.prompts
65
- : run.inputs.messages.map((x) => (0, base_js_1.getBufferString)(x));
66
- const llmRun = {
67
- uuid: run.id,
68
- start_time: run.start_time,
69
- end_time: run.end_time,
70
- execution_order: run.execution_order,
71
- child_execution_order: run.child_execution_order,
72
- serialized,
73
- type: run.run_type,
74
- session_id: session.id,
75
- prompts,
76
- response: run.outputs,
77
- };
78
- runResult = llmRun;
79
- }
80
- else if (run.run_type === "chain") {
81
- const child_runs = await Promise.all(run.child_runs.map((child_run) => this.convertV2RunToRun(child_run)));
82
- const chainRun = {
83
- uuid: run.id,
84
- start_time: run.start_time,
85
- end_time: run.end_time,
86
- execution_order: run.execution_order,
87
- child_execution_order: run.child_execution_order,
88
- serialized,
89
- type: run.run_type,
90
- session_id: session.id,
91
- inputs: run.inputs,
92
- outputs: run.outputs,
93
- child_llm_runs: child_runs.filter((child_run) => child_run.type === "llm"),
94
- child_chain_runs: child_runs.filter((child_run) => child_run.type === "chain"),
95
- child_tool_runs: child_runs.filter((child_run) => child_run.type === "tool"),
96
- };
97
- runResult = chainRun;
98
- }
99
- else if (run.run_type === "tool") {
100
- const child_runs = await Promise.all(run.child_runs.map((child_run) => this.convertV2RunToRun(child_run)));
101
- const toolRun = {
102
- uuid: run.id,
103
- start_time: run.start_time,
104
- end_time: run.end_time,
105
- execution_order: run.execution_order,
106
- child_execution_order: run.child_execution_order,
107
- serialized,
108
- type: run.run_type,
109
- session_id: session.id,
110
- tool_input: run.inputs.input,
111
- output: run.outputs?.output,
112
- action: JSON.stringify(serialized),
113
- child_llm_runs: child_runs.filter((child_run) => child_run.type === "llm"),
114
- child_chain_runs: child_runs.filter((child_run) => child_run.type === "chain"),
115
- child_tool_runs: child_runs.filter((child_run) => child_run.type === "tool"),
116
- };
117
- runResult = toolRun;
118
- }
119
- else {
120
- throw new Error(`Unknown run type: ${run.run_type}`);
121
- }
122
- return runResult;
123
- }
124
- async persistRun(run) {
125
- let endpoint;
126
- let v1Run;
127
- if (run.run_type !== undefined) {
128
- v1Run = await this.convertV2RunToRun(run);
129
- }
130
- else {
131
- v1Run = run;
132
- }
133
- if (v1Run.type === "llm") {
134
- endpoint = `${this.endpoint}/llm-runs`;
135
- }
136
- else if (v1Run.type === "chain") {
137
- endpoint = `${this.endpoint}/chain-runs`;
138
- }
139
- else {
140
- endpoint = `${this.endpoint}/tool-runs`;
141
- }
142
- const response = await fetch(endpoint, {
143
- method: "POST",
144
- headers: this.headers,
145
- body: JSON.stringify(v1Run),
146
- });
147
- if (!response.ok) {
148
- console.error(`Failed to persist run: ${response.status} ${response.statusText}`);
149
- }
150
- }
151
- async persistSession(sessionCreate) {
152
- const endpoint = `${this.endpoint}/sessions`;
153
- const response = await fetch(endpoint, {
154
- method: "POST",
155
- headers: this.headers,
156
- body: JSON.stringify(sessionCreate),
157
- });
158
- if (!response.ok) {
159
- console.error(`Failed to persist session: ${response.status} ${response.statusText}, using default session.`);
160
- return {
161
- id: 1,
162
- ...sessionCreate,
163
- };
164
- }
165
- return {
166
- id: (await response.json()).id,
167
- ...sessionCreate,
168
- };
169
- }
170
- async _handleSessionResponse(endpoint) {
171
- const response = await fetch(endpoint, {
172
- method: "GET",
173
- headers: this.headers,
174
- });
175
- let tracerSession;
176
- if (!response.ok) {
177
- console.error(`Failed to load session: ${response.status} ${response.statusText}`);
178
- tracerSession = {
179
- id: 1,
180
- start_time: Date.now(),
181
- };
182
- this.session = tracerSession;
183
- return tracerSession;
184
- }
185
- const resp = (await response.json());
186
- if (resp.length === 0) {
187
- tracerSession = {
188
- id: 1,
189
- start_time: Date.now(),
190
- };
191
- this.session = tracerSession;
192
- return tracerSession;
193
- }
194
- [tracerSession] = resp;
195
- this.session = tracerSession;
196
- return tracerSession;
197
- }
198
- }
199
- exports.LangChainTracerV1 = LangChainTracerV1;
17
+ __exportStar(require("langchain-core/tracers/tracer_langchain_v1"), exports);
@@ -1,57 +1 @@
1
- import { ChainValues, LLMResult } from "../../schema/index.js";
2
- import { BaseTracer, RunType, Run } from "./tracer.js";
3
- export interface BaseRunV1 {
4
- uuid: string;
5
- parent_uuid?: string;
6
- start_time: number;
7
- end_time?: number;
8
- execution_order: number;
9
- child_execution_order: number;
10
- serialized: {
11
- name: string;
12
- };
13
- session_id: number;
14
- error?: string;
15
- type: RunType;
16
- }
17
- export interface LLMRun extends BaseRunV1 {
18
- prompts: string[];
19
- response?: LLMResult;
20
- }
21
- export interface ChainRun extends BaseRunV1 {
22
- inputs: ChainValues;
23
- outputs?: ChainValues;
24
- child_llm_runs: LLMRun[];
25
- child_chain_runs: ChainRun[];
26
- child_tool_runs: ToolRun[];
27
- }
28
- export interface ToolRun extends BaseRunV1 {
29
- tool_input: string;
30
- output?: string;
31
- action: string;
32
- child_llm_runs: LLMRun[];
33
- child_chain_runs: ChainRun[];
34
- child_tool_runs: ToolRun[];
35
- }
36
- export interface BaseTracerSession {
37
- start_time: number;
38
- name?: string;
39
- }
40
- export type TracerSessionCreate = BaseTracerSession;
41
- export interface TracerSessionV1 extends BaseTracerSession {
42
- id: number;
43
- }
44
- export declare class LangChainTracerV1 extends BaseTracer {
45
- name: string;
46
- protected endpoint: string;
47
- protected headers: Record<string, string>;
48
- protected session: TracerSessionV1;
49
- constructor();
50
- newSession(sessionName?: string): Promise<TracerSessionV1>;
51
- loadSession(sessionName: string): Promise<TracerSessionV1>;
52
- loadDefaultSession(): Promise<TracerSessionV1>;
53
- protected convertV2RunToRun(run: Run): Promise<LLMRun | ChainRun | ToolRun>;
54
- protected persistRun(run: Run | LLMRun | ChainRun | ToolRun): Promise<void>;
55
- protected persistSession(sessionCreate: BaseTracerSession): Promise<TracerSessionV1>;
56
- protected _handleSessionResponse(endpoint: string): Promise<TracerSessionV1>;
57
- }
1
+ export * from "langchain-core/tracers/tracer_langchain_v1";
@@ -1,195 +1 @@
1
- import { getBufferString } from "../../memory/base.js";
2
- import { getEnvironmentVariable } from "../../util/env.js";
3
- import { BaseTracer } from "./tracer.js";
4
- export class LangChainTracerV1 extends BaseTracer {
5
- constructor() {
6
- super();
7
- Object.defineProperty(this, "name", {
8
- enumerable: true,
9
- configurable: true,
10
- writable: true,
11
- value: "langchain_tracer"
12
- });
13
- Object.defineProperty(this, "endpoint", {
14
- enumerable: true,
15
- configurable: true,
16
- writable: true,
17
- value: getEnvironmentVariable("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
18
- });
19
- Object.defineProperty(this, "headers", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: {
24
- "Content-Type": "application/json",
25
- }
26
- });
27
- Object.defineProperty(this, "session", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: void 0
32
- });
33
- const apiKey = getEnvironmentVariable("LANGCHAIN_API_KEY");
34
- if (apiKey) {
35
- this.headers["x-api-key"] = apiKey;
36
- }
37
- }
38
- async newSession(sessionName) {
39
- const sessionCreate = {
40
- start_time: Date.now(),
41
- name: sessionName,
42
- };
43
- const session = await this.persistSession(sessionCreate);
44
- this.session = session;
45
- return session;
46
- }
47
- async loadSession(sessionName) {
48
- const endpoint = `${this.endpoint}/sessions?name=${sessionName}`;
49
- return this._handleSessionResponse(endpoint);
50
- }
51
- async loadDefaultSession() {
52
- const endpoint = `${this.endpoint}/sessions?name=default`;
53
- return this._handleSessionResponse(endpoint);
54
- }
55
- async convertV2RunToRun(run) {
56
- const session = this.session ?? (await this.loadDefaultSession());
57
- const serialized = run.serialized;
58
- let runResult;
59
- if (run.run_type === "llm") {
60
- const prompts = run.inputs.prompts
61
- ? run.inputs.prompts
62
- : run.inputs.messages.map((x) => getBufferString(x));
63
- const llmRun = {
64
- uuid: run.id,
65
- start_time: run.start_time,
66
- end_time: run.end_time,
67
- execution_order: run.execution_order,
68
- child_execution_order: run.child_execution_order,
69
- serialized,
70
- type: run.run_type,
71
- session_id: session.id,
72
- prompts,
73
- response: run.outputs,
74
- };
75
- runResult = llmRun;
76
- }
77
- else if (run.run_type === "chain") {
78
- const child_runs = await Promise.all(run.child_runs.map((child_run) => this.convertV2RunToRun(child_run)));
79
- const chainRun = {
80
- uuid: run.id,
81
- start_time: run.start_time,
82
- end_time: run.end_time,
83
- execution_order: run.execution_order,
84
- child_execution_order: run.child_execution_order,
85
- serialized,
86
- type: run.run_type,
87
- session_id: session.id,
88
- inputs: run.inputs,
89
- outputs: run.outputs,
90
- child_llm_runs: child_runs.filter((child_run) => child_run.type === "llm"),
91
- child_chain_runs: child_runs.filter((child_run) => child_run.type === "chain"),
92
- child_tool_runs: child_runs.filter((child_run) => child_run.type === "tool"),
93
- };
94
- runResult = chainRun;
95
- }
96
- else if (run.run_type === "tool") {
97
- const child_runs = await Promise.all(run.child_runs.map((child_run) => this.convertV2RunToRun(child_run)));
98
- const toolRun = {
99
- uuid: run.id,
100
- start_time: run.start_time,
101
- end_time: run.end_time,
102
- execution_order: run.execution_order,
103
- child_execution_order: run.child_execution_order,
104
- serialized,
105
- type: run.run_type,
106
- session_id: session.id,
107
- tool_input: run.inputs.input,
108
- output: run.outputs?.output,
109
- action: JSON.stringify(serialized),
110
- child_llm_runs: child_runs.filter((child_run) => child_run.type === "llm"),
111
- child_chain_runs: child_runs.filter((child_run) => child_run.type === "chain"),
112
- child_tool_runs: child_runs.filter((child_run) => child_run.type === "tool"),
113
- };
114
- runResult = toolRun;
115
- }
116
- else {
117
- throw new Error(`Unknown run type: ${run.run_type}`);
118
- }
119
- return runResult;
120
- }
121
- async persistRun(run) {
122
- let endpoint;
123
- let v1Run;
124
- if (run.run_type !== undefined) {
125
- v1Run = await this.convertV2RunToRun(run);
126
- }
127
- else {
128
- v1Run = run;
129
- }
130
- if (v1Run.type === "llm") {
131
- endpoint = `${this.endpoint}/llm-runs`;
132
- }
133
- else if (v1Run.type === "chain") {
134
- endpoint = `${this.endpoint}/chain-runs`;
135
- }
136
- else {
137
- endpoint = `${this.endpoint}/tool-runs`;
138
- }
139
- const response = await fetch(endpoint, {
140
- method: "POST",
141
- headers: this.headers,
142
- body: JSON.stringify(v1Run),
143
- });
144
- if (!response.ok) {
145
- console.error(`Failed to persist run: ${response.status} ${response.statusText}`);
146
- }
147
- }
148
- async persistSession(sessionCreate) {
149
- const endpoint = `${this.endpoint}/sessions`;
150
- const response = await fetch(endpoint, {
151
- method: "POST",
152
- headers: this.headers,
153
- body: JSON.stringify(sessionCreate),
154
- });
155
- if (!response.ok) {
156
- console.error(`Failed to persist session: ${response.status} ${response.statusText}, using default session.`);
157
- return {
158
- id: 1,
159
- ...sessionCreate,
160
- };
161
- }
162
- return {
163
- id: (await response.json()).id,
164
- ...sessionCreate,
165
- };
166
- }
167
- async _handleSessionResponse(endpoint) {
168
- const response = await fetch(endpoint, {
169
- method: "GET",
170
- headers: this.headers,
171
- });
172
- let tracerSession;
173
- if (!response.ok) {
174
- console.error(`Failed to load session: ${response.status} ${response.statusText}`);
175
- tracerSession = {
176
- id: 1,
177
- start_time: Date.now(),
178
- };
179
- this.session = tracerSession;
180
- return tracerSession;
181
- }
182
- const resp = (await response.json());
183
- if (resp.length === 0) {
184
- tracerSession = {
185
- id: 1,
186
- start_time: Date.now(),
187
- };
188
- this.session = tracerSession;
189
- return tracerSession;
190
- }
191
- [tracerSession] = resp;
192
- this.session = tracerSession;
193
- return tracerSession;
194
- }
195
- }
1
+ export * from "langchain-core/tracers/tracer_langchain_v1";