Skip to content

Commit

Permalink
Add optional @langchain/core dependency for backwards compatibility, …
Browse files Browse the repository at this point in the history
…expose LangChain handoff functions outside
  • Loading branch information
dqbd committed May 13, 2024
1 parent f52b2bb commit 97aabcc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
4 changes: 4 additions & 0 deletions js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Chinook_Sqlite.sql
/schemas.js
/schemas.d.ts
/schemas.d.cts
/langchain.cjs
/langchain.js
/langchain.d.ts
/langchain.d.cts
/wrappers.cjs
/wrappers.js
/wrappers.d.ts
Expand Down
19 changes: 18 additions & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"schemas.js",
"schemas.d.ts",
"schemas.d.cts",
"langchain.cjs",
"langchain.js",
"langchain.d.ts",
"langchain.d.cts",
"wrappers.cjs",
"wrappers.js",
"wrappers.d.ts",
Expand Down Expand Up @@ -108,11 +112,15 @@
"typescript": "^5.4.5"
},
"peerDependencies": {
"openai": "*"
"openai": "*",
"@langchain/core": "*"
},
"peerDependenciesMeta": {
"openai": {
"optional": true
},
"@langchain/core": {
"optional": true
}
},
"resolutions": {
Expand Down Expand Up @@ -179,6 +187,15 @@
"import": "./schemas.js",
"require": "./schemas.cjs"
},
"./langchain": {
"types": {
"import": "./langchain.d.ts",
"require": "./langchain.d.cts",
"default": "./langchain.d.ts"
},
"import": "./langchain.js",
"require": "./langchain.cjs"
},
"./wrappers": {
"types": {
"import": "./wrappers.d.ts",
Expand Down
1 change: 1 addition & 0 deletions js/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const entrypoints = {
traceable: "traceable",
evaluation: "evaluation/index",
schemas: "schemas",
langchain: "langchain",
wrappers: "wrappers/index",
"wrappers/openai": "wrappers/openai",
};
Expand Down
26 changes: 22 additions & 4 deletions js/src/langchain.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
/* eslint-disable import/no-extraneous-dependencies */
import { CallbackManager } from "@langchain/core/callbacks/manager";
import { LangChainTracer } from "@langchain/core/tracers/tracer_langchain";
import { Runnable, RunnableConfig } from "@langchain/core/runnables";

import { RunTree } from "./run_trees.js";
import { Run } from "./schemas.js";
import { Runnable, RunnableConfig } from "@langchain/core/runnables";
import {
TraceableFunction,
getCurrentRunTree,
isTraceableFunction,
} from "./traceable.js";

export async function getLangchainCallbacks() {
const runTree: RunTree | undefined = getCurrentRunTree();
/**
* Converts the current run tree active within a traceable-wrapped function
* into a LangChain compatible callback manager. This is useful to handoff tracing
* from LangSmith to LangChain Runnables and LLMs.
*
* @param {RunTree | undefined} currentRunTree Current RunTree from within a traceable-wrapped function. If not provided, the current run tree will be inferred from AsyncLocalStorage.
* @returns {CallbackManager | undefined} Callback manager used by LangChain Runnable objects.
*/
export async function getLangchainCallbacks(
currentRunTree?: RunTree | undefined
) {
const runTree: RunTree | undefined = currentRunTree ?? getCurrentRunTree();
if (!runTree) return undefined;

// TODO: CallbackManager.configure() is only async due to LangChainTracer
Expand Down Expand Up @@ -70,6 +80,10 @@ export async function getLangchainCallbacks() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyTraceableFunction = TraceableFunction<(...any: any[]) => any>;

/**
* RunnableTraceable is a Runnable that wraps a traceable function.
* This allows adding Langsmith traced functions into LangChain sequences.
*/
export class RunnableTraceable<RunInput, RunOutput> extends Runnable<
RunInput,
RunOutput
Expand Down Expand Up @@ -126,4 +140,8 @@ export class RunnableTraceable<RunInput, RunOutput> extends Runnable<

return (await this.func(config, input)) as RunOutput;
}

static from(func: AnyTraceableFunction) {
return new RunnableTraceable({ func });
}
}
4 changes: 2 additions & 2 deletions js/src/tests/traceable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { FakeChatModel } from "@langchain/core/utils/testing";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { LangChainTracer } from "@langchain/core/tracers/tracer_langchain";
import { RunnableTraceable, getLangchainCallbacks } from "../langchain.js";
import { BaseMessage, HumanMessage } from "@langchain/core/messages";
import { awaitAllCallbacks } from "@langchain/core/callbacks/promises";
import { RunnableTraceable, getLangchainCallbacks } from "../langchain.js";

test("basic traceable implementation", async () => {
const { client, callSpy } = mockClient();
Expand Down Expand Up @@ -723,7 +723,7 @@ describe("langchain", () => {

const chain = prompt
.pipe(llm)
.pipe(new RunnableTraceable({ func: addValueTraceable }))
.pipe(RunnableTraceable.from(addValueTraceable))
.pipe(parser);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
1 change: 1 addition & 0 deletions js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"src/traceable.ts",
"src/evaluation/index.ts",
"src/schemas.ts",
"src/langchain.ts",
"src/wrappers/index.ts",
"src/wrappers/openai.ts"
]
Expand Down

0 comments on commit 97aabcc

Please sign in to comment.