Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul authored Jul 7, 2024
2 parents 9826402 + 484191a commit b2e7995
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 88 deletions.
4 changes: 0 additions & 4 deletions langchain-core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ utils/async_caller.cjs
utils/async_caller.js
utils/async_caller.d.ts
utils/async_caller.d.cts
utils/beta_warning.cjs
utils/beta_warning.js
utils/beta_warning.d.ts
utils/beta_warning.d.cts
utils/chunk_array.cjs
utils/chunk_array.js
utils/chunk_array.d.ts
Expand Down
1 change: 0 additions & 1 deletion langchain-core/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export const config = {
"tracers/tracer_langchain": "tracers/tracer_langchain",
"tracers/tracer_langchain_v1": "tracers/tracer_langchain_v1",
"utils/async_caller": "utils/async_caller",
"utils/beta_warning": "utils/beta_warning",
"utils/chunk_array": "utils/chunk_array",
"utils/env": "utils/env",
"utils/event_source_parse": "utils/event_source_parse",
Expand Down
13 changes: 0 additions & 13 deletions langchain-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,6 @@
"import": "./utils/async_caller.js",
"require": "./utils/async_caller.cjs"
},
"./utils/beta_warning": {
"types": {
"import": "./utils/beta_warning.d.ts",
"require": "./utils/beta_warning.d.cts",
"default": "./utils/beta_warning.d.ts"
},
"import": "./utils/beta_warning.js",
"require": "./utils/beta_warning.cjs"
},
"./utils/chunk_array": {
"types": {
"import": "./utils/chunk_array.d.ts",
Expand Down Expand Up @@ -771,10 +762,6 @@
"utils/async_caller.js",
"utils/async_caller.d.ts",
"utils/async_caller.d.cts",
"utils/beta_warning.cjs",
"utils/beta_warning.js",
"utils/beta_warning.d.ts",
"utils/beta_warning.d.cts",
"utils/chunk_array.cjs",
"utils/chunk_array.js",
"utils/chunk_array.d.ts",
Expand Down
35 changes: 29 additions & 6 deletions langchain-core/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
NewTokenIndices,
} from "./base.js";
import { ConsoleCallbackHandler } from "../tracers/console.js";
import { getTracingV2CallbackHandler } from "../tracers/initialize.js";
import { type BaseMessage } from "../messages/base.js";
import { getBufferString } from "../messages/utils.js";
import { getEnvironmentVariable } from "../utils/env.js";
Expand All @@ -20,6 +19,7 @@ import {
import { consumeCallback } from "./promises.js";
import { Serialized } from "../load/serializable.js";
import type { DocumentInterface } from "../documents/document.js";
import { isTracingEnabled } from "../utils/callbacks.js";

if (
/* #__PURE__ */ getEnvironmentVariable("LANGCHAIN_TRACING_V2") === "true" &&
Expand Down Expand Up @@ -111,7 +111,7 @@ export abstract class BaseCallbackManager {
/**
* Base class for run manager in LangChain.
*/
class BaseRunManager {
export class BaseRunManager {
constructor(
public readonly runId: string,
public readonly handlers: BaseCallbackHandler[],
Expand All @@ -123,6 +123,10 @@ class BaseRunManager {
protected readonly _parentRunId?: string
) {}

get parentRunId() {
return this._parentRunId;
}

async handleText(text: string): Promise<void> {
await Promise.all(
this.handlers.map((handler) =>
Expand Down Expand Up @@ -962,6 +966,27 @@ export class CallbackManager
localMetadata?: Record<string, unknown>,
options?: CallbackManagerOptions
): Promise<CallbackManager | undefined> {
return this._configureSync(
inheritableHandlers,
localHandlers,
inheritableTags,
localTags,
inheritableMetadata,
localMetadata,
options
);
}

// TODO: Deprecate async method in favor of this one.
static _configureSync(
inheritableHandlers?: Callbacks,
localHandlers?: Callbacks,
inheritableTags?: string[],
localTags?: string[],
inheritableMetadata?: Record<string, unknown>,
localMetadata?: Record<string, unknown>,
options?: CallbackManagerOptions
) {
let callbackManager: CallbackManager | undefined;
if (inheritableHandlers || localHandlers) {
if (Array.isArray(inheritableHandlers) || !inheritableHandlers) {
Expand All @@ -984,9 +1009,7 @@ export class CallbackManager
const verboseEnabled =
getEnvironmentVariable("LANGCHAIN_VERBOSE") === "true" ||
options?.verbose;
const tracingV2Enabled =
getEnvironmentVariable("LANGCHAIN_TRACING_V2") === "true" ||
getEnvironmentVariable("LANGSMITH_TRACING") === "true";
const tracingV2Enabled = isTracingEnabled();

const tracingEnabled =
tracingV2Enabled ||
Expand All @@ -1011,7 +1034,7 @@ export class CallbackManager
)
) {
if (tracingV2Enabled) {
const tracerV2 = await getTracingV2CallbackHandler();
const tracerV2 = new LangChainTracer();
callbackManager.addHandler(tracerV2, true);

// handoff between langchain and langsmith/traceable
Expand Down
56 changes: 20 additions & 36 deletions langchain-core/src/runnables/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import {
isTraceableFunction,
} from "langsmith/singletons/traceable";
import type { RunnableInterface, RunnableBatchOptions } from "./types.js";
import {
CallbackManager,
CallbackManagerForChainRun,
} from "../callbacks/manager.js";
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
import {
LogStreamCallbackHandler,
LogStreamCallbackHandlerInput,
Expand Down Expand Up @@ -2318,15 +2315,19 @@ export class RunnableLambda<RunInput, RunOutput> extends Runnable<
}
}
}
const childConfig = patchConfig(config, {
callbacks: runManager?.getChild(),
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
});
const output = await new Promise<RunOutput | Runnable>(
(resolve, reject) => {
void AsyncLocalStorageProviderSingleton.getInstance().run(
config,
childConfig,
async () => {
try {
const res = await this.func(finalChunk as RunInput, {
...config,
config,
...childConfig,
config: childConfig,
});
resolve(res);
} catch (e) {
Expand All @@ -2340,23 +2341,19 @@ export class RunnableLambda<RunInput, RunOutput> extends Runnable<
if (config?.recursionLimit === 0) {
throw new Error("Recursion limit reached.");
}
const stream = await output.stream(
finalChunk as RunInput,
patchConfig(config, {
callbacks: runManager?.getChild(),
recursionLimit:
(config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
})
);
const stream = await output.stream(finalChunk as RunInput, childConfig);
for await (const chunk of stream) {
yield chunk;
}
} else if (isAsyncIterable(output)) {
for await (const chunk of consumeAsyncIterableInContext(config, output)) {
for await (const chunk of consumeAsyncIterableInContext(
childConfig,
output
)) {
yield chunk as RunOutput;
}
} else if (isIterableIterator(output)) {
for (const chunk of consumeIteratorInContext(config, output)) {
for (const chunk of consumeIteratorInContext(childConfig, output)) {
yield chunk as RunOutput;
}
} else {
Expand Down Expand Up @@ -2433,29 +2430,24 @@ export class RunnableWithFallbacks<RunInput, RunOutput> extends Runnable<
input: RunInput,
options?: Partial<RunnableConfig>
): Promise<RunOutput> {
const callbackManager_ = await CallbackManager.configure(
options?.callbacks,
undefined,
options?.tags,
undefined,
options?.metadata
);
const { runId, ...otherOptions } = options ?? {};
const config = ensureConfig(options);
const callbackManager_ = await getCallbackManagerForConfig(options);
const { runId, ...otherConfigFields } = config;
const runManager = await callbackManager_?.handleChainStart(
this.toJSON(),
_coerceToDict(input, "input"),
runId,
undefined,
undefined,
undefined,
otherOptions?.runName
otherConfigFields?.runName
);
let firstError;
for (const runnable of this.runnables()) {
try {
const output = await runnable.invoke(
input,
patchConfig(otherOptions, { callbacks: runManager?.getChild() })
patchConfig(otherConfigFields, { callbacks: runManager?.getChild() })
);
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
return output;
Expand Down Expand Up @@ -2500,15 +2492,7 @@ export class RunnableWithFallbacks<RunInput, RunOutput> extends Runnable<
}
const configList = this._getOptionsList(options ?? {}, inputs.length);
const callbackManagers = await Promise.all(
configList.map((config) =>
CallbackManager.configure(
config?.callbacks,
undefined,
config?.tags,
undefined,
config?.metadata
)
)
configList.map((config) => getCallbackManagerForConfig(config))
);
const runManagers = await Promise.all(
callbackManagers.map(async (callbackManager, i) => {
Expand Down
Loading

0 comments on commit b2e7995

Please sign in to comment.