Skip to content

Commit 2ae56aa

Browse files
committed
fix(langchain): handle prompts created from runs
1 parent d84fbb9 commit 2ae56aa

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

langchain/src/hub/node.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
bindOutputSchema,
88
} from "./base.js";
99
import { load } from "../load/index.js";
10+
import { arraysEqual } from "../util/misc.js";
1011

1112
// TODO: Make this the default, add web entrypoint in next breaking release
1213

@@ -33,9 +34,15 @@ export async function pull<T extends Runnable>(
3334
const promptObject = await basePull(ownerRepoCommit, options);
3435
let modelClass;
3536
if (options?.includeModel) {
36-
if (Array.isArray(promptObject.manifest.kwargs?.last?.kwargs?.bound?.id)) {
37-
const modelName =
38-
promptObject.manifest.kwargs?.last?.kwargs?.bound?.id.at(-1);
37+
const chatModelObject = arraysEqual(
38+
promptObject.manifest.kwargs?.last?.id,
39+
["langchain_core", "runnables", "RunnableBinding"]
40+
)
41+
? promptObject.manifest.kwargs?.last?.kwargs?.bound
42+
: promptObject.manifest.kwargs?.last;
43+
44+
if (Array.isArray(chatModelObject?.id)) {
45+
const modelName = chatModelObject?.id.at(-1);
3946
if (modelName === "ChatOpenAI") {
4047
modelClass = (await import("@langchain/openai")).ChatOpenAI;
4148
} else if (modelName === "ChatAnthropic") {

langchain/src/hub/tests/hub.int.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,14 @@ test("Test LangChain Hub while loading model with dynamic imports and structured
112112
expect(res).not.toBeInstanceOf(AIMessage);
113113
expect(typeof res.correctness).toBe("boolean");
114114
});
115+
116+
test("Test LangChain Hub while loading model not defined in a RunnableBinding", async () => {
117+
const promptA = await nodePull("hntrl/binding-manifest", {
118+
includeModel: true,
119+
});
120+
const resA = await promptA.invoke({
121+
question: "What's the capital of the USA?",
122+
});
123+
expect(resA).toBeInstanceOf(AIMessage);
124+
expect(resA.content).toBeDefined();
125+
});

langchain/src/util/misc.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Helper function to check if two arrays are deeply equal
3+
*/
4+
export function arraysEqual<T>(a: T[], b: T[]): boolean {
5+
if (!Array.isArray(a) || !Array.isArray(b)) {
6+
return false;
7+
}
8+
if (a.length !== b.length) {
9+
return false;
10+
}
11+
for (let i = 0; i < a.length; i++) {
12+
if (a[i] !== b[i]) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}

0 commit comments

Comments
 (0)