manager.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. import { v4 as uuidv4 } from "uuid";
  2. import { BaseCallbackHandler, isBaseCallbackHandler, } from "./base.js";
  3. import { ConsoleCallbackHandler } from "../tracers/console.js";
  4. import { getBufferString } from "../messages/utils.js";
  5. import { getEnvironmentVariable } from "../utils/env.js";
  6. import { LangChainTracer, } from "../tracers/tracer_langchain.js";
  7. import { consumeCallback } from "./promises.js";
  8. import { isTracingEnabled } from "../utils/callbacks.js";
  9. import { isBaseTracer } from "../tracers/base.js";
  10. import { getContextVariable, _getConfigureHooks, } from "../singletons/async_local_storage/context.js";
  11. export function parseCallbackConfigArg(arg) {
  12. if (!arg) {
  13. return {};
  14. }
  15. else if (Array.isArray(arg) || "name" in arg) {
  16. return { callbacks: arg };
  17. }
  18. else {
  19. return arg;
  20. }
  21. }
  22. /**
  23. * Manage callbacks from different components of LangChain.
  24. */
  25. export class BaseCallbackManager {
  26. setHandler(handler) {
  27. return this.setHandlers([handler]);
  28. }
  29. }
  30. /**
  31. * Base class for run manager in LangChain.
  32. */
  33. export class BaseRunManager {
  34. constructor(runId, handlers, inheritableHandlers, tags, inheritableTags, metadata, inheritableMetadata, _parentRunId) {
  35. Object.defineProperty(this, "runId", {
  36. enumerable: true,
  37. configurable: true,
  38. writable: true,
  39. value: runId
  40. });
  41. Object.defineProperty(this, "handlers", {
  42. enumerable: true,
  43. configurable: true,
  44. writable: true,
  45. value: handlers
  46. });
  47. Object.defineProperty(this, "inheritableHandlers", {
  48. enumerable: true,
  49. configurable: true,
  50. writable: true,
  51. value: inheritableHandlers
  52. });
  53. Object.defineProperty(this, "tags", {
  54. enumerable: true,
  55. configurable: true,
  56. writable: true,
  57. value: tags
  58. });
  59. Object.defineProperty(this, "inheritableTags", {
  60. enumerable: true,
  61. configurable: true,
  62. writable: true,
  63. value: inheritableTags
  64. });
  65. Object.defineProperty(this, "metadata", {
  66. enumerable: true,
  67. configurable: true,
  68. writable: true,
  69. value: metadata
  70. });
  71. Object.defineProperty(this, "inheritableMetadata", {
  72. enumerable: true,
  73. configurable: true,
  74. writable: true,
  75. value: inheritableMetadata
  76. });
  77. Object.defineProperty(this, "_parentRunId", {
  78. enumerable: true,
  79. configurable: true,
  80. writable: true,
  81. value: _parentRunId
  82. });
  83. }
  84. get parentRunId() {
  85. return this._parentRunId;
  86. }
  87. async handleText(text) {
  88. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  89. try {
  90. await handler.handleText?.(text, this.runId, this._parentRunId, this.tags);
  91. }
  92. catch (err) {
  93. const logFunction = handler.raiseError
  94. ? console.error
  95. : console.warn;
  96. logFunction(`Error in handler ${handler.constructor.name}, handleText: ${err}`);
  97. if (handler.raiseError) {
  98. throw err;
  99. }
  100. }
  101. }, handler.awaitHandlers)));
  102. }
  103. async handleCustomEvent(eventName,
  104. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  105. data, _runId, _tags,
  106. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  107. _metadata) {
  108. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  109. try {
  110. await handler.handleCustomEvent?.(eventName, data, this.runId, this.tags, this.metadata);
  111. }
  112. catch (err) {
  113. const logFunction = handler.raiseError
  114. ? console.error
  115. : console.warn;
  116. logFunction(`Error in handler ${handler.constructor.name}, handleCustomEvent: ${err}`);
  117. if (handler.raiseError) {
  118. throw err;
  119. }
  120. }
  121. }, handler.awaitHandlers)));
  122. }
  123. }
  124. /**
  125. * Manages callbacks for retriever runs.
  126. */
  127. export class CallbackManagerForRetrieverRun extends BaseRunManager {
  128. getChild(tag) {
  129. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  130. const manager = new CallbackManager(this.runId);
  131. manager.setHandlers(this.inheritableHandlers);
  132. manager.addTags(this.inheritableTags);
  133. manager.addMetadata(this.inheritableMetadata);
  134. if (tag) {
  135. manager.addTags([tag], false);
  136. }
  137. return manager;
  138. }
  139. async handleRetrieverEnd(documents) {
  140. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  141. if (!handler.ignoreRetriever) {
  142. try {
  143. await handler.handleRetrieverEnd?.(documents, this.runId, this._parentRunId, this.tags);
  144. }
  145. catch (err) {
  146. const logFunction = handler.raiseError
  147. ? console.error
  148. : console.warn;
  149. logFunction(`Error in handler ${handler.constructor.name}, handleRetriever`);
  150. if (handler.raiseError) {
  151. throw err;
  152. }
  153. }
  154. }
  155. }, handler.awaitHandlers)));
  156. }
  157. async handleRetrieverError(err) {
  158. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  159. if (!handler.ignoreRetriever) {
  160. try {
  161. await handler.handleRetrieverError?.(err, this.runId, this._parentRunId, this.tags);
  162. }
  163. catch (error) {
  164. const logFunction = handler.raiseError
  165. ? console.error
  166. : console.warn;
  167. logFunction(`Error in handler ${handler.constructor.name}, handleRetrieverError: ${error}`);
  168. if (handler.raiseError) {
  169. throw err;
  170. }
  171. }
  172. }
  173. }, handler.awaitHandlers)));
  174. }
  175. }
  176. export class CallbackManagerForLLMRun extends BaseRunManager {
  177. async handleLLMNewToken(token, idx, _runId, _parentRunId, _tags, fields) {
  178. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  179. if (!handler.ignoreLLM) {
  180. try {
  181. await handler.handleLLMNewToken?.(token, idx ?? { prompt: 0, completion: 0 }, this.runId, this._parentRunId, this.tags, fields);
  182. }
  183. catch (err) {
  184. const logFunction = handler.raiseError
  185. ? console.error
  186. : console.warn;
  187. logFunction(`Error in handler ${handler.constructor.name}, handleLLMNewToken: ${err}`);
  188. if (handler.raiseError) {
  189. throw err;
  190. }
  191. }
  192. }
  193. }, handler.awaitHandlers)));
  194. }
  195. async handleLLMError(err, _runId, _parentRunId, _tags, extraParams) {
  196. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  197. if (!handler.ignoreLLM) {
  198. try {
  199. await handler.handleLLMError?.(err, this.runId, this._parentRunId, this.tags, extraParams);
  200. }
  201. catch (err) {
  202. const logFunction = handler.raiseError
  203. ? console.error
  204. : console.warn;
  205. logFunction(`Error in handler ${handler.constructor.name}, handleLLMError: ${err}`);
  206. if (handler.raiseError) {
  207. throw err;
  208. }
  209. }
  210. }
  211. }, handler.awaitHandlers)));
  212. }
  213. async handleLLMEnd(output, _runId, _parentRunId, _tags, extraParams) {
  214. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  215. if (!handler.ignoreLLM) {
  216. try {
  217. await handler.handleLLMEnd?.(output, this.runId, this._parentRunId, this.tags, extraParams);
  218. }
  219. catch (err) {
  220. const logFunction = handler.raiseError
  221. ? console.error
  222. : console.warn;
  223. logFunction(`Error in handler ${handler.constructor.name}, handleLLMEnd: ${err}`);
  224. if (handler.raiseError) {
  225. throw err;
  226. }
  227. }
  228. }
  229. }, handler.awaitHandlers)));
  230. }
  231. }
  232. export class CallbackManagerForChainRun extends BaseRunManager {
  233. getChild(tag) {
  234. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  235. const manager = new CallbackManager(this.runId);
  236. manager.setHandlers(this.inheritableHandlers);
  237. manager.addTags(this.inheritableTags);
  238. manager.addMetadata(this.inheritableMetadata);
  239. if (tag) {
  240. manager.addTags([tag], false);
  241. }
  242. return manager;
  243. }
  244. async handleChainError(err, _runId, _parentRunId, _tags, kwargs) {
  245. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  246. if (!handler.ignoreChain) {
  247. try {
  248. await handler.handleChainError?.(err, this.runId, this._parentRunId, this.tags, kwargs);
  249. }
  250. catch (err) {
  251. const logFunction = handler.raiseError
  252. ? console.error
  253. : console.warn;
  254. logFunction(`Error in handler ${handler.constructor.name}, handleChainError: ${err}`);
  255. if (handler.raiseError) {
  256. throw err;
  257. }
  258. }
  259. }
  260. }, handler.awaitHandlers)));
  261. }
  262. async handleChainEnd(output, _runId, _parentRunId, _tags, kwargs) {
  263. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  264. if (!handler.ignoreChain) {
  265. try {
  266. await handler.handleChainEnd?.(output, this.runId, this._parentRunId, this.tags, kwargs);
  267. }
  268. catch (err) {
  269. const logFunction = handler.raiseError
  270. ? console.error
  271. : console.warn;
  272. logFunction(`Error in handler ${handler.constructor.name}, handleChainEnd: ${err}`);
  273. if (handler.raiseError) {
  274. throw err;
  275. }
  276. }
  277. }
  278. }, handler.awaitHandlers)));
  279. }
  280. async handleAgentAction(action) {
  281. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  282. if (!handler.ignoreAgent) {
  283. try {
  284. await handler.handleAgentAction?.(action, this.runId, this._parentRunId, this.tags);
  285. }
  286. catch (err) {
  287. const logFunction = handler.raiseError
  288. ? console.error
  289. : console.warn;
  290. logFunction(`Error in handler ${handler.constructor.name}, handleAgentAction: ${err}`);
  291. if (handler.raiseError) {
  292. throw err;
  293. }
  294. }
  295. }
  296. }, handler.awaitHandlers)));
  297. }
  298. async handleAgentEnd(action) {
  299. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  300. if (!handler.ignoreAgent) {
  301. try {
  302. await handler.handleAgentEnd?.(action, this.runId, this._parentRunId, this.tags);
  303. }
  304. catch (err) {
  305. const logFunction = handler.raiseError
  306. ? console.error
  307. : console.warn;
  308. logFunction(`Error in handler ${handler.constructor.name}, handleAgentEnd: ${err}`);
  309. if (handler.raiseError) {
  310. throw err;
  311. }
  312. }
  313. }
  314. }, handler.awaitHandlers)));
  315. }
  316. }
  317. export class CallbackManagerForToolRun extends BaseRunManager {
  318. getChild(tag) {
  319. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  320. const manager = new CallbackManager(this.runId);
  321. manager.setHandlers(this.inheritableHandlers);
  322. manager.addTags(this.inheritableTags);
  323. manager.addMetadata(this.inheritableMetadata);
  324. if (tag) {
  325. manager.addTags([tag], false);
  326. }
  327. return manager;
  328. }
  329. async handleToolError(err) {
  330. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  331. if (!handler.ignoreAgent) {
  332. try {
  333. await handler.handleToolError?.(err, this.runId, this._parentRunId, this.tags);
  334. }
  335. catch (err) {
  336. const logFunction = handler.raiseError
  337. ? console.error
  338. : console.warn;
  339. logFunction(`Error in handler ${handler.constructor.name}, handleToolError: ${err}`);
  340. if (handler.raiseError) {
  341. throw err;
  342. }
  343. }
  344. }
  345. }, handler.awaitHandlers)));
  346. }
  347. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  348. async handleToolEnd(output) {
  349. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  350. if (!handler.ignoreAgent) {
  351. try {
  352. await handler.handleToolEnd?.(output, this.runId, this._parentRunId, this.tags);
  353. }
  354. catch (err) {
  355. const logFunction = handler.raiseError
  356. ? console.error
  357. : console.warn;
  358. logFunction(`Error in handler ${handler.constructor.name}, handleToolEnd: ${err}`);
  359. if (handler.raiseError) {
  360. throw err;
  361. }
  362. }
  363. }
  364. }, handler.awaitHandlers)));
  365. }
  366. }
  367. /**
  368. * @example
  369. * ```typescript
  370. * const prompt = PromptTemplate.fromTemplate("What is the answer to {question}?");
  371. *
  372. * // Example of using LLMChain with OpenAI and a simple prompt
  373. * const chain = new LLMChain({
  374. * llm: new ChatOpenAI({ temperature: 0.9 }),
  375. * prompt,
  376. * });
  377. *
  378. * // Running the chain with a single question
  379. * const result = await chain.call({
  380. * question: "What is the airspeed velocity of an unladen swallow?",
  381. * });
  382. * console.log("The answer is:", result);
  383. * ```
  384. */
  385. export class CallbackManager extends BaseCallbackManager {
  386. constructor(parentRunId, options) {
  387. super();
  388. Object.defineProperty(this, "handlers", {
  389. enumerable: true,
  390. configurable: true,
  391. writable: true,
  392. value: []
  393. });
  394. Object.defineProperty(this, "inheritableHandlers", {
  395. enumerable: true,
  396. configurable: true,
  397. writable: true,
  398. value: []
  399. });
  400. Object.defineProperty(this, "tags", {
  401. enumerable: true,
  402. configurable: true,
  403. writable: true,
  404. value: []
  405. });
  406. Object.defineProperty(this, "inheritableTags", {
  407. enumerable: true,
  408. configurable: true,
  409. writable: true,
  410. value: []
  411. });
  412. Object.defineProperty(this, "metadata", {
  413. enumerable: true,
  414. configurable: true,
  415. writable: true,
  416. value: {}
  417. });
  418. Object.defineProperty(this, "inheritableMetadata", {
  419. enumerable: true,
  420. configurable: true,
  421. writable: true,
  422. value: {}
  423. });
  424. Object.defineProperty(this, "name", {
  425. enumerable: true,
  426. configurable: true,
  427. writable: true,
  428. value: "callback_manager"
  429. });
  430. Object.defineProperty(this, "_parentRunId", {
  431. enumerable: true,
  432. configurable: true,
  433. writable: true,
  434. value: void 0
  435. });
  436. this.handlers = options?.handlers ?? this.handlers;
  437. this.inheritableHandlers =
  438. options?.inheritableHandlers ?? this.inheritableHandlers;
  439. this.tags = options?.tags ?? this.tags;
  440. this.inheritableTags = options?.inheritableTags ?? this.inheritableTags;
  441. this.metadata = options?.metadata ?? this.metadata;
  442. this.inheritableMetadata =
  443. options?.inheritableMetadata ?? this.inheritableMetadata;
  444. this._parentRunId = parentRunId;
  445. }
  446. /**
  447. * Gets the parent run ID, if any.
  448. *
  449. * @returns The parent run ID.
  450. */
  451. getParentRunId() {
  452. return this._parentRunId;
  453. }
  454. async handleLLMStart(llm, prompts, runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
  455. return Promise.all(prompts.map(async (prompt, idx) => {
  456. // Can't have duplicate runs with the same run ID (if provided)
  457. const runId_ = idx === 0 && runId ? runId : uuidv4();
  458. await Promise.all(this.handlers.map((handler) => {
  459. if (handler.ignoreLLM) {
  460. return;
  461. }
  462. if (isBaseTracer(handler)) {
  463. // Create and add run to the run map.
  464. // We do this synchronously to avoid race conditions
  465. // when callbacks are backgrounded.
  466. handler._createRunForLLMStart(llm, [prompt], runId_, this._parentRunId, extraParams, this.tags, this.metadata, runName);
  467. }
  468. return consumeCallback(async () => {
  469. try {
  470. await handler.handleLLMStart?.(llm, [prompt], runId_, this._parentRunId, extraParams, this.tags, this.metadata, runName);
  471. }
  472. catch (err) {
  473. const logFunction = handler.raiseError
  474. ? console.error
  475. : console.warn;
  476. logFunction(`Error in handler ${handler.constructor.name}, handleLLMStart: ${err}`);
  477. if (handler.raiseError) {
  478. throw err;
  479. }
  480. }
  481. }, handler.awaitHandlers);
  482. }));
  483. return new CallbackManagerForLLMRun(runId_, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
  484. }));
  485. }
  486. async handleChatModelStart(llm, messages, runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
  487. return Promise.all(messages.map(async (messageGroup, idx) => {
  488. // Can't have duplicate runs with the same run ID (if provided)
  489. const runId_ = idx === 0 && runId ? runId : uuidv4();
  490. await Promise.all(this.handlers.map((handler) => {
  491. if (handler.ignoreLLM) {
  492. return;
  493. }
  494. if (isBaseTracer(handler)) {
  495. // Create and add run to the run map.
  496. // We do this synchronously to avoid race conditions
  497. // when callbacks are backgrounded.
  498. handler._createRunForChatModelStart(llm, [messageGroup], runId_, this._parentRunId, extraParams, this.tags, this.metadata, runName);
  499. }
  500. return consumeCallback(async () => {
  501. try {
  502. if (handler.handleChatModelStart) {
  503. await handler.handleChatModelStart?.(llm, [messageGroup], runId_, this._parentRunId, extraParams, this.tags, this.metadata, runName);
  504. }
  505. else if (handler.handleLLMStart) {
  506. const messageString = getBufferString(messageGroup);
  507. await handler.handleLLMStart?.(llm, [messageString], runId_, this._parentRunId, extraParams, this.tags, this.metadata, runName);
  508. }
  509. }
  510. catch (err) {
  511. const logFunction = handler.raiseError
  512. ? console.error
  513. : console.warn;
  514. logFunction(`Error in handler ${handler.constructor.name}, handleLLMStart: ${err}`);
  515. if (handler.raiseError) {
  516. throw err;
  517. }
  518. }
  519. }, handler.awaitHandlers);
  520. }));
  521. return new CallbackManagerForLLMRun(runId_, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
  522. }));
  523. }
  524. async handleChainStart(chain, inputs, runId = uuidv4(), runType = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
  525. await Promise.all(this.handlers.map((handler) => {
  526. if (handler.ignoreChain) {
  527. return;
  528. }
  529. if (isBaseTracer(handler)) {
  530. // Create and add run to the run map.
  531. // We do this synchronously to avoid race conditions
  532. // when callbacks are backgrounded.
  533. handler._createRunForChainStart(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType, runName);
  534. }
  535. return consumeCallback(async () => {
  536. try {
  537. await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType, runName);
  538. }
  539. catch (err) {
  540. const logFunction = handler.raiseError
  541. ? console.error
  542. : console.warn;
  543. logFunction(`Error in handler ${handler.constructor.name}, handleChainStart: ${err}`);
  544. if (handler.raiseError) {
  545. throw err;
  546. }
  547. }
  548. }, handler.awaitHandlers);
  549. }));
  550. return new CallbackManagerForChainRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
  551. }
  552. async handleToolStart(tool, input, runId = uuidv4(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
  553. await Promise.all(this.handlers.map((handler) => {
  554. if (handler.ignoreAgent) {
  555. return;
  556. }
  557. if (isBaseTracer(handler)) {
  558. // Create and add run to the run map.
  559. // We do this synchronously to avoid race conditions
  560. // when callbacks are backgrounded.
  561. handler._createRunForToolStart(tool, input, runId, this._parentRunId, this.tags, this.metadata, runName);
  562. }
  563. return consumeCallback(async () => {
  564. try {
  565. await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata, runName);
  566. }
  567. catch (err) {
  568. const logFunction = handler.raiseError
  569. ? console.error
  570. : console.warn;
  571. logFunction(`Error in handler ${handler.constructor.name}, handleToolStart: ${err}`);
  572. if (handler.raiseError) {
  573. throw err;
  574. }
  575. }
  576. }, handler.awaitHandlers);
  577. }));
  578. return new CallbackManagerForToolRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
  579. }
  580. async handleRetrieverStart(retriever, query, runId = uuidv4(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
  581. await Promise.all(this.handlers.map((handler) => {
  582. if (handler.ignoreRetriever) {
  583. return;
  584. }
  585. if (isBaseTracer(handler)) {
  586. // Create and add run to the run map.
  587. // We do this synchronously to avoid race conditions
  588. // when callbacks are backgrounded.
  589. handler._createRunForRetrieverStart(retriever, query, runId, this._parentRunId, this.tags, this.metadata, runName);
  590. }
  591. return consumeCallback(async () => {
  592. try {
  593. await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata, runName);
  594. }
  595. catch (err) {
  596. const logFunction = handler.raiseError
  597. ? console.error
  598. : console.warn;
  599. logFunction(`Error in handler ${handler.constructor.name}, handleRetrieverStart: ${err}`);
  600. if (handler.raiseError) {
  601. throw err;
  602. }
  603. }
  604. }, handler.awaitHandlers);
  605. }));
  606. return new CallbackManagerForRetrieverRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
  607. }
  608. async handleCustomEvent(eventName,
  609. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  610. data, runId, _tags,
  611. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  612. _metadata) {
  613. await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
  614. if (!handler.ignoreCustomEvent) {
  615. try {
  616. await handler.handleCustomEvent?.(eventName, data, runId, this.tags, this.metadata);
  617. }
  618. catch (err) {
  619. const logFunction = handler.raiseError
  620. ? console.error
  621. : console.warn;
  622. logFunction(`Error in handler ${handler.constructor.name}, handleCustomEvent: ${err}`);
  623. if (handler.raiseError) {
  624. throw err;
  625. }
  626. }
  627. }
  628. }, handler.awaitHandlers)));
  629. }
  630. addHandler(handler, inherit = true) {
  631. this.handlers.push(handler);
  632. if (inherit) {
  633. this.inheritableHandlers.push(handler);
  634. }
  635. }
  636. removeHandler(handler) {
  637. this.handlers = this.handlers.filter((_handler) => _handler !== handler);
  638. this.inheritableHandlers = this.inheritableHandlers.filter((_handler) => _handler !== handler);
  639. }
  640. setHandlers(handlers, inherit = true) {
  641. this.handlers = [];
  642. this.inheritableHandlers = [];
  643. for (const handler of handlers) {
  644. this.addHandler(handler, inherit);
  645. }
  646. }
  647. addTags(tags, inherit = true) {
  648. this.removeTags(tags); // Remove duplicates
  649. this.tags.push(...tags);
  650. if (inherit) {
  651. this.inheritableTags.push(...tags);
  652. }
  653. }
  654. removeTags(tags) {
  655. this.tags = this.tags.filter((tag) => !tags.includes(tag));
  656. this.inheritableTags = this.inheritableTags.filter((tag) => !tags.includes(tag));
  657. }
  658. addMetadata(metadata, inherit = true) {
  659. this.metadata = { ...this.metadata, ...metadata };
  660. if (inherit) {
  661. this.inheritableMetadata = { ...this.inheritableMetadata, ...metadata };
  662. }
  663. }
  664. removeMetadata(metadata) {
  665. for (const key of Object.keys(metadata)) {
  666. delete this.metadata[key];
  667. delete this.inheritableMetadata[key];
  668. }
  669. }
  670. copy(additionalHandlers = [], inherit = true) {
  671. const manager = new CallbackManager(this._parentRunId);
  672. for (const handler of this.handlers) {
  673. const inheritable = this.inheritableHandlers.includes(handler);
  674. manager.addHandler(handler, inheritable);
  675. }
  676. for (const tag of this.tags) {
  677. const inheritable = this.inheritableTags.includes(tag);
  678. manager.addTags([tag], inheritable);
  679. }
  680. for (const key of Object.keys(this.metadata)) {
  681. const inheritable = Object.keys(this.inheritableMetadata).includes(key);
  682. manager.addMetadata({ [key]: this.metadata[key] }, inheritable);
  683. }
  684. for (const handler of additionalHandlers) {
  685. if (
  686. // Prevent multiple copies of console_callback_handler
  687. manager.handlers
  688. .filter((h) => h.name === "console_callback_handler")
  689. .some((h) => h.name === handler.name)) {
  690. continue;
  691. }
  692. manager.addHandler(handler, inherit);
  693. }
  694. return manager;
  695. }
  696. static fromHandlers(handlers) {
  697. class Handler extends BaseCallbackHandler {
  698. constructor() {
  699. super();
  700. Object.defineProperty(this, "name", {
  701. enumerable: true,
  702. configurable: true,
  703. writable: true,
  704. value: uuidv4()
  705. });
  706. Object.assign(this, handlers);
  707. }
  708. }
  709. const manager = new this();
  710. manager.addHandler(new Handler());
  711. return manager;
  712. }
  713. static configure(inheritableHandlers, localHandlers, inheritableTags, localTags, inheritableMetadata, localMetadata, options) {
  714. return this._configureSync(inheritableHandlers, localHandlers, inheritableTags, localTags, inheritableMetadata, localMetadata, options);
  715. }
  716. // TODO: Deprecate async method in favor of this one.
  717. static _configureSync(inheritableHandlers, localHandlers, inheritableTags, localTags, inheritableMetadata, localMetadata, options) {
  718. let callbackManager;
  719. if (inheritableHandlers || localHandlers) {
  720. if (Array.isArray(inheritableHandlers) || !inheritableHandlers) {
  721. callbackManager = new CallbackManager();
  722. callbackManager.setHandlers(inheritableHandlers?.map(ensureHandler) ?? [], true);
  723. }
  724. else {
  725. callbackManager = inheritableHandlers;
  726. }
  727. callbackManager = callbackManager.copy(Array.isArray(localHandlers)
  728. ? localHandlers.map(ensureHandler)
  729. : localHandlers?.handlers, false);
  730. }
  731. const verboseEnabled = getEnvironmentVariable("LANGCHAIN_VERBOSE") === "true" ||
  732. options?.verbose;
  733. const tracingV2Enabled = LangChainTracer.getTraceableRunTree()?.tracingEnabled ||
  734. isTracingEnabled();
  735. const tracingEnabled = tracingV2Enabled ||
  736. (getEnvironmentVariable("LANGCHAIN_TRACING") ?? false);
  737. if (verboseEnabled || tracingEnabled) {
  738. if (!callbackManager) {
  739. callbackManager = new CallbackManager();
  740. }
  741. if (verboseEnabled &&
  742. !callbackManager.handlers.some((handler) => handler.name === ConsoleCallbackHandler.prototype.name)) {
  743. const consoleHandler = new ConsoleCallbackHandler();
  744. callbackManager.addHandler(consoleHandler, true);
  745. }
  746. if (tracingEnabled &&
  747. !callbackManager.handlers.some((handler) => handler.name === "langchain_tracer")) {
  748. if (tracingV2Enabled) {
  749. const tracerV2 = new LangChainTracer();
  750. callbackManager.addHandler(tracerV2, true);
  751. // handoff between langchain and langsmith/traceable
  752. // override the parent run ID
  753. callbackManager._parentRunId =
  754. LangChainTracer.getTraceableRunTree()?.id ??
  755. callbackManager._parentRunId;
  756. }
  757. }
  758. }
  759. for (const { contextVar, inheritable = true, handlerClass, envVar, } of _getConfigureHooks()) {
  760. const createIfNotInContext = envVar && getEnvironmentVariable(envVar) === "true" && handlerClass;
  761. let handler;
  762. const contextVarValue = contextVar !== undefined ? getContextVariable(contextVar) : undefined;
  763. if (contextVarValue && isBaseCallbackHandler(contextVarValue)) {
  764. handler = contextVarValue;
  765. }
  766. else if (createIfNotInContext) {
  767. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  768. handler = new handlerClass({});
  769. }
  770. if (handler !== undefined) {
  771. if (!callbackManager) {
  772. callbackManager = new CallbackManager();
  773. }
  774. if (!callbackManager.handlers.some((h) => h.name === handler.name)) {
  775. callbackManager.addHandler(handler, inheritable);
  776. }
  777. }
  778. }
  779. if (inheritableTags || localTags) {
  780. if (callbackManager) {
  781. callbackManager.addTags(inheritableTags ?? []);
  782. callbackManager.addTags(localTags ?? [], false);
  783. }
  784. }
  785. if (inheritableMetadata || localMetadata) {
  786. if (callbackManager) {
  787. callbackManager.addMetadata(inheritableMetadata ?? {});
  788. callbackManager.addMetadata(localMetadata ?? {}, false);
  789. }
  790. }
  791. return callbackManager;
  792. }
  793. }
  794. export function ensureHandler(handler) {
  795. if ("name" in handler) {
  796. return handler;
  797. }
  798. return BaseCallbackHandler.fromMethods(handler);
  799. }
  800. /**
  801. * @deprecated Use [`traceable`](https://docs.smith.langchain.com/observability/how_to_guides/tracing/annotate_code)
  802. * from "langsmith" instead.
  803. */
  804. export class TraceGroup {
  805. constructor(groupName, options) {
  806. Object.defineProperty(this, "groupName", {
  807. enumerable: true,
  808. configurable: true,
  809. writable: true,
  810. value: groupName
  811. });
  812. Object.defineProperty(this, "options", {
  813. enumerable: true,
  814. configurable: true,
  815. writable: true,
  816. value: options
  817. });
  818. Object.defineProperty(this, "runManager", {
  819. enumerable: true,
  820. configurable: true,
  821. writable: true,
  822. value: void 0
  823. });
  824. }
  825. async getTraceGroupCallbackManager(group_name, inputs, options) {
  826. const cb = new LangChainTracer(options);
  827. const cm = await CallbackManager.configure([cb]);
  828. const runManager = await cm?.handleChainStart({
  829. lc: 1,
  830. type: "not_implemented",
  831. id: ["langchain", "callbacks", "groups", group_name],
  832. }, inputs ?? {});
  833. if (!runManager) {
  834. throw new Error("Failed to create run group callback manager.");
  835. }
  836. return runManager;
  837. }
  838. async start(inputs) {
  839. if (!this.runManager) {
  840. this.runManager = await this.getTraceGroupCallbackManager(this.groupName, inputs, this.options);
  841. }
  842. return this.runManager.getChild();
  843. }
  844. async error(err) {
  845. if (this.runManager) {
  846. await this.runManager.handleChainError(err);
  847. this.runManager = undefined;
  848. }
  849. }
  850. async end(output) {
  851. if (this.runManager) {
  852. await this.runManager.handleChainEnd(output ?? {});
  853. this.runManager = undefined;
  854. }
  855. }
  856. }
  857. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  858. function _coerceToDict(value, defaultKey) {
  859. return value && !Array.isArray(value) && typeof value === "object"
  860. ? value
  861. : { [defaultKey]: value };
  862. }
  863. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  864. export async function traceAsGroup(groupOptions, enclosedCode, ...args) {
  865. const traceGroup = new TraceGroup(groupOptions.name, groupOptions);
  866. const callbackManager = await traceGroup.start({ ...args });
  867. try {
  868. const result = await enclosedCode(callbackManager, ...args);
  869. await traceGroup.end(_coerceToDict(result, "output"));
  870. return result;
  871. }
  872. catch (err) {
  873. await traceGroup.error(err);
  874. throw err;
  875. }
  876. }