From b5d44105edd63ff8a825547bedaf39ce1fe67bc7 Mon Sep 17 00:00:00 2001 From: Tat Dat Duong Date: Wed, 15 May 2024 17:49:19 +0200 Subject: [PATCH] Resolve circular dependency --- langchain-core/src/runnables/base.ts | 38 ++++++++++++++- .../src/tracers/tracer_langchain.ts | 48 +------------------ 2 files changed, 38 insertions(+), 48 deletions(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index f4991408ee3f..4fb54a59f959 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -48,7 +48,6 @@ import { isAsyncIterable, isIterator, } from "./iter.js"; -import { RunnableTraceable } from "../tracers/tracer_langchain.js"; export { type RunnableInterface, RunnableBatchOptions }; @@ -1950,6 +1949,43 @@ export class RunnableMap< } } +// eslint-disable-next-line @typescript-eslint/no-explicit-any +type AnyTraceableFunction = TraceableFunction<(...any: any[]) => any>; + +/** + * A runnabble that runs a traced function from LangSmith + */ +export class RunnableTraceable extends Runnable< + RunInput, + RunOutput +> { + lc_serializable = false; + + lc_namespace = ["langchain_core", "runnables"]; + + protected func: AnyTraceableFunction; + + constructor(fields: { func: AnyTraceableFunction }) { + super(fields); + + if (!isTraceableFunction(fields.func)) { + throw new Error( + "RunnableTraceable requires a function that is wrapped in traceable higher-order function" + ); + } + + this.func = fields.func; + } + + async invoke(input: RunInput, options?: Partial) { + const [config] = this._getOptionsList(options ?? {}, 1); + return (await this.func( + { ...config, callbacks: await getCallbackManagerForConfig(config) }, + input + )) as RunOutput; + } +} + /** * A runnable that runs a callable. */ diff --git a/langchain-core/src/tracers/tracer_langchain.ts b/langchain-core/src/tracers/tracer_langchain.ts index be43323d55ed..e4a366b7150c 100644 --- a/langchain-core/src/tracers/tracer_langchain.ts +++ b/langchain-core/src/tracers/tracer_langchain.ts @@ -1,10 +1,6 @@ import { Client } from "langsmith"; import { RunTree } from "langsmith/run_trees"; -import { - type TraceableFunction, - isTraceableFunction, - getCurrentRunTree, -} from "langsmith/singletons/traceable"; +import { getCurrentRunTree } from "langsmith/singletons/traceable"; import { BaseRun, @@ -15,11 +11,6 @@ import { import { getEnvironmentVariable, getRuntimeEnvironment } from "../utils/env.js"; import { BaseTracer } from "./base.js"; import { BaseCallbackHandlerInput } from "../callbacks/base.js"; -import { Runnable } from "../runnables/base.js"; -import { - RunnableConfig, - getCallbackManagerForConfig, -} from "../runnables/config.js"; export interface Run extends BaseRun { id: string; @@ -157,40 +148,3 @@ export class LangChainTracer } } } - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -type AnyTraceableFunction = TraceableFunction<(...any: any[]) => any>; - -/** - * A runnabble that runs a traced function from LangSmith - */ -export class RunnableTraceable extends Runnable< - RunInput, - RunOutput -> { - lc_serializable = false; - - lc_namespace = ["langchain_core", "runnables"]; - - protected func: AnyTraceableFunction; - - constructor(fields: { func: AnyTraceableFunction }) { - super(fields); - - if (!isTraceableFunction(fields.func)) { - throw new Error( - "RunnableTraceable requires a function that is wrapped in traceable higher-order function" - ); - } - - this.func = fields.func; - } - - async invoke(input: RunInput, options?: Partial) { - const [config] = this._getOptionsList(options ?? {}, 1); - return (await this.func( - { ...config, callbacks: await getCallbackManagerForConfig(config) }, - input - )) as RunOutput; - } -}