langchain 0.0.195 → 0.0.196

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 (216) hide show
  1. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  2. package/dist/base_language/count_tokens.cjs +5 -70
  3. package/dist/base_language/count_tokens.d.ts +1 -10
  4. package/dist/base_language/count_tokens.js +1 -65
  5. package/dist/base_language/index.cjs +6 -196
  6. package/dist/base_language/index.d.ts +1 -111
  7. package/dist/base_language/index.js +1 -191
  8. package/dist/cache/base.cjs +15 -37
  9. package/dist/cache/base.d.ts +1 -20
  10. package/dist/cache/base.js +1 -33
  11. package/dist/cache/index.cjs +2 -46
  12. package/dist/cache/index.d.ts +1 -29
  13. package/dist/cache/index.js +1 -45
  14. package/dist/callbacks/base.cjs +3 -139
  15. package/dist/callbacks/base.d.ts +1 -266
  16. package/dist/callbacks/base.js +1 -126
  17. package/dist/callbacks/handlers/console.cjs +14 -221
  18. package/dist/callbacks/handlers/console.d.ts +1 -117
  19. package/dist/callbacks/handlers/console.js +1 -217
  20. package/dist/callbacks/handlers/initialize.cjs +15 -30
  21. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  22. package/dist/callbacks/handlers/initialize.js +1 -27
  23. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  24. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  25. package/dist/callbacks/handlers/log_stream.js +1 -289
  26. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  27. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  28. package/dist/callbacks/handlers/run_collector.js +1 -46
  29. package/dist/callbacks/handlers/tracer.cjs +15 -375
  30. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  31. package/dist/callbacks/handlers/tracer.js +1 -373
  32. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  33. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  34. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  35. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  36. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  37. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  38. package/dist/callbacks/manager.cjs +15 -676
  39. package/dist/callbacks/manager.d.ts +1 -180
  40. package/dist/callbacks/manager.js +1 -666
  41. package/dist/callbacks/promises.cjs +14 -42
  42. package/dist/callbacks/promises.d.ts +1 -11
  43. package/dist/callbacks/promises.js +1 -37
  44. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  45. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  46. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  47. package/dist/chains/openai_functions/structured_output.js +1 -1
  48. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  49. package/dist/chat_models/base.cjs +15 -296
  50. package/dist/chat_models/base.d.ts +1 -122
  51. package/dist/chat_models/base.js +1 -292
  52. package/dist/chat_models/bedrock/web.d.ts +1 -1
  53. package/dist/chat_models/fireworks.d.ts +1 -1
  54. package/dist/document.cjs +2 -24
  55. package/dist/document.d.ts +1 -12
  56. package/dist/document.js +1 -23
  57. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  58. package/dist/document_loaders/web/s3.d.ts +1 -1
  59. package/dist/embeddings/base.cjs +15 -22
  60. package/dist/embeddings/base.d.ts +1 -33
  61. package/dist/embeddings/base.js +1 -20
  62. package/dist/embeddings/cache_backed.cjs +2 -2
  63. package/dist/embeddings/cache_backed.js +1 -1
  64. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  65. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  66. package/dist/evaluation/qa/prompt.d.ts +2 -2
  67. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  68. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  69. package/dist/llms/base.cjs +15 -278
  70. package/dist/llms/base.d.ts +1 -115
  71. package/dist/llms/base.js +1 -275
  72. package/dist/llms/bedrock/web.d.ts +1 -1
  73. package/dist/llms/fireworks.d.ts +1 -1
  74. package/dist/load/import_map.cjs +2 -1
  75. package/dist/load/import_map.d.ts +1 -0
  76. package/dist/load/import_map.js +1 -0
  77. package/dist/load/index.cjs +7 -148
  78. package/dist/load/index.js +7 -148
  79. package/dist/load/map_keys.cjs +0 -24
  80. package/dist/load/map_keys.d.ts +0 -6
  81. package/dist/load/map_keys.js +1 -17
  82. package/dist/load/serializable.cjs +15 -178
  83. package/dist/load/serializable.d.ts +1 -66
  84. package/dist/load/serializable.js +1 -175
  85. package/dist/memory/base.cjs +17 -92
  86. package/dist/memory/base.d.ts +2 -68
  87. package/dist/memory/base.js +2 -87
  88. package/dist/output_parsers/openai_functions.cjs +2 -2
  89. package/dist/output_parsers/openai_functions.d.ts +1 -1
  90. package/dist/output_parsers/openai_functions.js +1 -1
  91. package/dist/output_parsers/regex.d.ts +1 -1
  92. package/dist/output_parsers/structured.d.ts +1 -1
  93. package/dist/prompts/base.cjs +8 -183
  94. package/dist/prompts/base.d.ts +3 -132
  95. package/dist/prompts/base.js +3 -178
  96. package/dist/prompts/chat.cjs +15 -477
  97. package/dist/prompts/chat.d.ts +1 -219
  98. package/dist/prompts/chat.js +1 -466
  99. package/dist/prompts/few_shot.cjs +15 -353
  100. package/dist/prompts/few_shot.d.ts +1 -192
  101. package/dist/prompts/few_shot.js +1 -350
  102. package/dist/prompts/index.cjs +3 -2
  103. package/dist/prompts/index.d.ts +2 -1
  104. package/dist/prompts/index.js +2 -1
  105. package/dist/prompts/pipeline.cjs +15 -142
  106. package/dist/prompts/pipeline.d.ts +1 -98
  107. package/dist/prompts/pipeline.js +1 -140
  108. package/dist/prompts/prompt.cjs +15 -146
  109. package/dist/prompts/prompt.d.ts +1 -92
  110. package/dist/prompts/prompt.js +1 -144
  111. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +15 -148
  112. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  113. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  114. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  115. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  116. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  117. package/dist/prompts/selectors/conditional.cjs +15 -74
  118. package/dist/prompts/selectors/conditional.d.ts +1 -63
  119. package/dist/prompts/selectors/conditional.js +1 -69
  120. package/dist/prompts/serde.cjs +15 -0
  121. package/dist/prompts/serde.d.ts +1 -43
  122. package/dist/prompts/serde.js +1 -1
  123. package/dist/prompts/template.cjs +14 -88
  124. package/dist/prompts/template.d.ts +1 -36
  125. package/dist/prompts/template.js +1 -83
  126. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  127. package/dist/runnables/index.d.ts +1 -0
  128. package/dist/runnables/index.js +1 -0
  129. package/dist/schema/document.cjs +3 -34
  130. package/dist/schema/document.d.ts +2 -29
  131. package/dist/schema/document.js +2 -32
  132. package/dist/schema/index.cjs +37 -612
  133. package/dist/schema/index.d.ts +11 -311
  134. package/dist/schema/index.js +8 -583
  135. package/dist/schema/output_parser.cjs +15 -309
  136. package/dist/schema/output_parser.d.ts +1 -173
  137. package/dist/schema/output_parser.js +1 -301
  138. package/dist/schema/retriever.cjs +15 -77
  139. package/dist/schema/retriever.d.ts +1 -43
  140. package/dist/schema/retriever.js +1 -75
  141. package/dist/schema/runnable/base.cjs +10 -1072
  142. package/dist/schema/runnable/base.d.ts +1 -356
  143. package/dist/schema/runnable/base.js +1 -1060
  144. package/dist/schema/runnable/branch.cjs +2 -131
  145. package/dist/schema/runnable/branch.d.ts +1 -94
  146. package/dist/schema/runnable/branch.js +1 -130
  147. package/dist/schema/runnable/config.cjs +0 -6
  148. package/dist/schema/runnable/config.d.ts +1 -3
  149. package/dist/schema/runnable/config.js +1 -4
  150. package/dist/schema/runnable/index.cjs +15 -16
  151. package/dist/schema/runnable/index.d.ts +1 -5
  152. package/dist/schema/runnable/index.js +1 -4
  153. package/dist/schema/runnable/passthrough.cjs +3 -113
  154. package/dist/schema/runnable/passthrough.d.ts +1 -72
  155. package/dist/schema/runnable/passthrough.js +1 -111
  156. package/dist/schema/runnable/router.cjs +2 -71
  157. package/dist/schema/runnable/router.d.ts +1 -29
  158. package/dist/schema/runnable/router.js +1 -70
  159. package/dist/schema/storage.cjs +15 -8
  160. package/dist/schema/storage.d.ts +1 -57
  161. package/dist/schema/storage.js +1 -6
  162. package/dist/tools/bingserpapi.d.ts +1 -1
  163. package/dist/tools/searchapi.d.ts +1 -1
  164. package/dist/tools/serpapi.d.ts +1 -1
  165. package/dist/tools/serper.d.ts +1 -1
  166. package/dist/util/async_caller.cjs +14 -128
  167. package/dist/util/async_caller.d.ts +1 -45
  168. package/dist/util/async_caller.js +1 -124
  169. package/package.json +11 -5
  170. package/runnables.cjs +1 -0
  171. package/runnables.d.ts +1 -0
  172. package/runnables.js +1 -0
  173. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  174. package/dist/util/@cfworker/json-schema/index.js +0 -1
  175. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  176. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  177. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  178. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  179. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  180. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  181. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  182. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  183. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  184. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  185. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  186. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  187. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  188. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  189. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  190. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  191. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  192. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  193. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  194. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  195. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  196. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  197. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  198. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  199. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  200. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  201. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  202. package/dist/util/fast-json-patch/index.cjs +0 -49
  203. package/dist/util/fast-json-patch/index.d.ts +0 -22
  204. package/dist/util/fast-json-patch/index.js +0 -16
  205. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  206. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  207. package/dist/util/fast-json-patch/src/core.js +0 -459
  208. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  209. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  210. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  211. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  212. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  213. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  214. package/dist/util/js-sha1/hash.cjs +0 -358
  215. package/dist/util/js-sha1/hash.d.ts +0 -1
  216. 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/callbacks/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/callbacks/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/callbacks/tracers/tracer_langchain_v1";