From 8e436df8b82713e6a87e3362f13ad53a05e02c1e Mon Sep 17 00:00:00 2001 From: Olmo Maldonado Date: Sat, 14 Dec 2024 19:33:53 -0800 Subject: [PATCH 1/3] Set a campaign manager, if we know there are callback managers we'd like to use This preserves existing behavior except for when library creators want handlers registered with the `registerConfigureHook` function. In that case, we want to make sure we return a callback manager. --- langchain-core/src/callbacks/manager.ts | 42 ++++++++++--------- .../src/callbacks/tests/manager.test.ts | 18 +++++++- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/langchain-core/src/callbacks/manager.ts b/langchain-core/src/callbacks/manager.ts index dcabf602b838..d20cf7fa15b9 100644 --- a/langchain-core/src/callbacks/manager.ts +++ b/langchain-core/src/callbacks/manager.ts @@ -1,31 +1,31 @@ import { v4 as uuidv4 } from "uuid"; import { AgentAction, AgentFinish } from "../agents.js"; -import type { ChainValues } from "../utils/types/index.js"; +import type { DocumentInterface } from "../documents/document.js"; +import { Serialized } from "../load/serializable.js"; +import { type BaseMessage } from "../messages/base.js"; +import { getBufferString } from "../messages/utils.js"; import { LLMResult } from "../outputs.js"; import { - BaseCallbackHandler, - CallbackHandlerMethods, - HandleLLMNewTokenCallbackFields, - isBaseCallbackHandler, - NewTokenIndices, -} from "./base.js"; + _getConfigureHooks, + getContextVariable, +} from "../singletons/async_local_storage/context.js"; +import { isBaseTracer } from "../tracers/base.js"; import { ConsoleCallbackHandler } from "../tracers/console.js"; -import { type BaseMessage } from "../messages/base.js"; -import { getBufferString } from "../messages/utils.js"; -import { getEnvironmentVariable } from "../utils/env.js"; import { LangChainTracer, LangChainTracerFields, } from "../tracers/tracer_langchain.js"; -import { consumeCallback } from "./promises.js"; -import { Serialized } from "../load/serializable.js"; -import type { DocumentInterface } from "../documents/document.js"; import { isTracingEnabled } from "../utils/callbacks.js"; -import { isBaseTracer } from "../tracers/base.js"; +import { getEnvironmentVariable } from "../utils/env.js"; +import type { ChainValues } from "../utils/types/index.js"; import { - getContextVariable, - _getConfigureHooks, -} from "../singletons/async_local_storage/context.js"; + BaseCallbackHandler, + CallbackHandlerMethods, + HandleLLMNewTokenCallbackFields, + NewTokenIndices, + isBaseCallbackHandler, +} from "./base.js"; +import { consumeCallback } from "./promises.js"; type BaseCallbackManagerMethods = { [K in keyof CallbackHandlerMethods]?: ( @@ -1276,8 +1276,12 @@ export class CallbackManager handler = new (handlerClass as any)({}); } if (handler !== undefined) { - if (!callbackManager?.handlers.some((h) => h.name === handler!.name)) { - callbackManager?.addHandler(handler, inheritable); + if (!callbackManager) { + callbackManager = new CallbackManager(); + } + + if (!callbackManager.handlers.some((h) => h.name === handler!.name)) { + callbackManager.addHandler(handler, inheritable); } } } diff --git a/langchain-core/src/callbacks/tests/manager.test.ts b/langchain-core/src/callbacks/tests/manager.test.ts index 7e947049006f..12f32890f2a7 100644 --- a/langchain-core/src/callbacks/tests/manager.test.ts +++ b/langchain-core/src/callbacks/tests/manager.test.ts @@ -1,7 +1,7 @@ /* eslint-disable no-process-env */ -import { expect, test, beforeAll, afterEach } from "@jest/globals"; +import { afterEach, beforeAll, expect, test } from "@jest/globals"; -import { setContextVariable, registerConfigureHook } from "../../context.js"; +import { registerConfigureHook, setContextVariable } from "../../context.js"; import { BaseCallbackHandler } from "../base.js"; import { CallbackManager } from "../manager.js"; @@ -26,6 +26,20 @@ test("configure with empty array", async () => { expect(manager?.handlers.length).toBe(0); }); +test("configure with no arguments", async () => { + const manager = CallbackManager.configure(); + expect(manager).toBe(undefined); +}); + +test("configure with no arguments but with handler", async () => { + setContextVariable("my_test_handler", handlerInstance); + registerConfigureHook({ + contextVar: "my_test_handler", + }); + const manager = CallbackManager.configure(); + expect(manager?.handlers[0]).toBe(handlerInstance); +}); + test("configure with one handler", async () => { const manager = CallbackManager.configure([handlerInstance]); expect(manager?.handlers[0]).toBe(handlerInstance); From 81df69618e124d0f9a7aacb199da410715ca25ca Mon Sep 17 00:00:00 2001 From: Olmo Maldonado Date: Mon, 16 Dec 2024 10:27:38 -0800 Subject: [PATCH 2/3] revert import sort --- langchain-core/src/callbacks/manager.ts | 34 ++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/langchain-core/src/callbacks/manager.ts b/langchain-core/src/callbacks/manager.ts index d20cf7fa15b9..b4ea8e266d16 100644 --- a/langchain-core/src/callbacks/manager.ts +++ b/langchain-core/src/callbacks/manager.ts @@ -1,31 +1,31 @@ import { v4 as uuidv4 } from "uuid"; import { AgentAction, AgentFinish } from "../agents.js"; -import type { DocumentInterface } from "../documents/document.js"; -import { Serialized } from "../load/serializable.js"; -import { type BaseMessage } from "../messages/base.js"; -import { getBufferString } from "../messages/utils.js"; +import type { ChainValues } from "../utils/types/index.js"; import { LLMResult } from "../outputs.js"; import { - _getConfigureHooks, - getContextVariable, -} from "../singletons/async_local_storage/context.js"; -import { isBaseTracer } from "../tracers/base.js"; + BaseCallbackHandler, + CallbackHandlerMethods, + HandleLLMNewTokenCallbackFields, + isBaseCallbackHandler, + NewTokenIndices, +} from "./base.js"; import { ConsoleCallbackHandler } from "../tracers/console.js"; +import { type BaseMessage } from "../messages/base.js"; +import { getBufferString } from "../messages/utils.js"; +import { getEnvironmentVariable } from "../utils/env.js"; import { LangChainTracer, LangChainTracerFields, } from "../tracers/tracer_langchain.js"; +import { consumeCallback } from "./promises.js"; +import { Serialized } from "../load/serializable.js"; +import type { DocumentInterface } from "../documents/document.js"; import { isTracingEnabled } from "../utils/callbacks.js"; -import { getEnvironmentVariable } from "../utils/env.js"; -import type { ChainValues } from "../utils/types/index.js"; +import { isBaseTracer } from "../tracers/base.js"; import { - BaseCallbackHandler, - CallbackHandlerMethods, - HandleLLMNewTokenCallbackFields, - NewTokenIndices, - isBaseCallbackHandler, -} from "./base.js"; -import { consumeCallback } from "./promises.js"; + getContextVariable, + _getConfigureHooks, +} from "../singletons/async_local_storage/context.js"; type BaseCallbackManagerMethods = { [K in keyof CallbackHandlerMethods]?: ( From 7de8c53eeca62fa7e26fcad8a0e87aefd0c08cc7 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 16 Dec 2024 11:07:43 -0800 Subject: [PATCH 3/3] Move below tags and metadata block --- langchain-core/src/callbacks/manager.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/langchain-core/src/callbacks/manager.ts b/langchain-core/src/callbacks/manager.ts index b4ea8e266d16..55ad0484e480 100644 --- a/langchain-core/src/callbacks/manager.ts +++ b/langchain-core/src/callbacks/manager.ts @@ -1245,18 +1245,6 @@ export class CallbackManager } } } - if (inheritableTags || localTags) { - if (callbackManager) { - callbackManager.addTags(inheritableTags ?? []); - callbackManager.addTags(localTags ?? [], false); - } - } - if (inheritableMetadata || localMetadata) { - if (callbackManager) { - callbackManager.addMetadata(inheritableMetadata ?? {}); - callbackManager.addMetadata(localMetadata ?? {}, false); - } - } for (const { contextVar, @@ -1286,6 +1274,19 @@ export class CallbackManager } } + if (inheritableTags || localTags) { + if (callbackManager) { + callbackManager.addTags(inheritableTags ?? []); + callbackManager.addTags(localTags ?? [], false); + } + } + if (inheritableMetadata || localMetadata) { + if (callbackManager) { + callbackManager.addMetadata(inheritableMetadata ?? {}); + callbackManager.addMetadata(localMetadata ?? {}, false); + } + } + return callbackManager; } }