Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions langchain/src/hub/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
bindOutputSchema,
} from "./base.js";
import { load } from "../load/index.js";
import { arraysEqual } from "../util/misc.js";

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

Expand All @@ -33,9 +34,15 @@ export async function pull<T extends Runnable>(
const promptObject = await basePull(ownerRepoCommit, options);
let modelClass;
if (options?.includeModel) {
if (Array.isArray(promptObject.manifest.kwargs?.last?.kwargs?.bound?.id)) {
const modelName =
promptObject.manifest.kwargs?.last?.kwargs?.bound?.id.at(-1);
const chatModelObject = arraysEqual(
promptObject.manifest.kwargs?.last?.id,
["langchain_core", "runnables", "RunnableBinding"]
)
? promptObject.manifest.kwargs?.last?.kwargs?.bound
: promptObject.manifest.kwargs?.last;

if (Array.isArray(chatModelObject?.id)) {
const modelName = chatModelObject?.id.at(-1);
if (modelName === "ChatOpenAI") {
modelClass = (await import("@langchain/openai")).ChatOpenAI;
} else if (modelName === "ChatAnthropic") {
Expand Down
11 changes: 11 additions & 0 deletions langchain/src/hub/tests/hub.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,14 @@ test("Test LangChain Hub while loading model with dynamic imports and structured
expect(res).not.toBeInstanceOf(AIMessage);
expect(typeof res.correctness).toBe("boolean");
});

test("Test LangChain Hub while loading model not defined in a RunnableBinding", async () => {
const promptA = await nodePull("hntrl/binding-manifest", {
includeModel: true,
});
const resA = await promptA.invoke({
question: "What's the capital of the USA?",
});
expect(resA).toBeInstanceOf(AIMessage);
expect(resA.content).toBeDefined();
});
17 changes: 17 additions & 0 deletions langchain/src/util/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Helper function to check if two arrays are deeply equal
*/
export function arraysEqual<T>(a: T[], b: T[]): boolean {
if (!Array.isArray(a) || !Array.isArray(b)) {
return false;
}
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
Loading