Skip to content

Commit

Permalink
Resolve circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd committed May 15, 2024
1 parent a648854 commit b5d4410
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 48 deletions.
38 changes: 37 additions & 1 deletion langchain-core/src/runnables/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import {
isAsyncIterable,
isIterator,
} from "./iter.js";
import { RunnableTraceable } from "../tracers/tracer_langchain.js";

export { type RunnableInterface, RunnableBatchOptions };

Expand Down Expand Up @@ -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<RunInput, RunOutput> 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<RunnableConfig>) {
const [config] = this._getOptionsList(options ?? {}, 1);
return (await this.func(
{ ...config, callbacks: await getCallbackManagerForConfig(config) },
input
)) as RunOutput;
}
}

/**
* A runnable that runs a callable.
*/
Expand Down
48 changes: 1 addition & 47 deletions langchain-core/src/tracers/tracer_langchain.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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<RunInput, RunOutput> 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<RunnableConfig>) {
const [config] = this._getOptionsList(options ?? {}, 1);
return (await this.func(
{ ...config, callbacks: await getCallbackManagerForConfig(config) },
input
)) as RunOutput;
}
}

0 comments on commit b5d4410

Please sign in to comment.