diff --git a/langchain/src/hub/base.ts b/langchain/src/hub/base.ts index 6b4c2de020c5..6850cd37f2bd 100644 --- a/langchain/src/hub/base.ts +++ b/langchain/src/hub/base.ts @@ -96,10 +96,6 @@ export function generateModelImportMap( ) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const modelImportMap: Record = {}; - // TODO: Fix in 0.4.0. We can't get lc_id without instantiating the class, so we - // must put them inline here. In the future, make this less hacky - // This should probably use dynamic imports and have a web-only entrypoint - // in a future breaking release if (modelClass !== undefined) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const modelLcName = (modelClass as any)?.lc_name(); @@ -130,3 +126,29 @@ export function generateModelImportMap( } return modelImportMap; } + +export function generateOptionalImportMap( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + modelClass?: new (...args: any[]) => BaseLanguageModel +) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const optionalImportMap: Record = {}; + if (modelClass !== undefined) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const modelLcName = (modelClass as any)?.lc_name(); + let optionalImportMapKey; + if (modelLcName === "ChatGoogleGenerativeAI") { + optionalImportMapKey = "langchain_google_genai/chat_models"; + } else if (modelLcName === "ChatBedrockConverse") { + optionalImportMapKey = "langchain_aws/chat_models"; + } else if (modelLcName === "ChatGroq") { + optionalImportMapKey = "langchain_groq/chat_models"; + } + if (optionalImportMapKey !== undefined) { + optionalImportMap[optionalImportMapKey] = { + [modelLcName]: modelClass, + }; + } + } + return optionalImportMap; +} diff --git a/langchain/src/hub/index.ts b/langchain/src/hub/index.ts index 71377ba0f11c..ba7ae918ee22 100644 --- a/langchain/src/hub/index.ts +++ b/langchain/src/hub/index.ts @@ -1,7 +1,12 @@ import { Runnable } from "@langchain/core/runnables"; import type { BaseLanguageModel } from "@langchain/core/language_models/base"; import { load } from "../load/index.js"; -import { basePush, basePull, generateModelImportMap } from "./base.js"; +import { + basePush, + basePull, + generateModelImportMap, + generateOptionalImportMap, +} from "./base.js"; export { basePush as push }; @@ -36,7 +41,7 @@ export async function pull( const loadedPrompt = await load( JSON.stringify(promptObject.manifest), undefined, - undefined, + generateOptionalImportMap(options?.modelClass), generateModelImportMap(options?.modelClass) ); return loadedPrompt; diff --git a/langchain/src/hub/node.ts b/langchain/src/hub/node.ts index ab01777f5d2a..10e0f8940bec 100644 --- a/langchain/src/hub/node.ts +++ b/langchain/src/hub/node.ts @@ -1,5 +1,10 @@ import { Runnable } from "@langchain/core/runnables"; -import { basePush, basePull, generateModelImportMap } from "./base.js"; +import { + basePush, + basePull, + generateModelImportMap, + generateOptionalImportMap, +} from "./base.js"; import { load } from "../load/index.js"; // TODO: Make this the default, add web entrypoint in next breaking release @@ -55,7 +60,7 @@ export async function pull( const loadedPrompt = await load( JSON.stringify(promptObject.manifest), undefined, - undefined, + generateOptionalImportMap(modelClass), generateModelImportMap(modelClass) ); return loadedPrompt; diff --git a/langchain/src/hub/tests/hub.int.test.ts b/langchain/src/hub/tests/hub.int.test.ts index 130e3d3e9bb9..f1c870cd6753 100644 --- a/langchain/src/hub/tests/hub.int.test.ts +++ b/langchain/src/hub/tests/hub.int.test.ts @@ -79,12 +79,13 @@ test("Test LangChain Hub while loading model", async () => { }); test("Test LangChain Hub while loading model with dynamic imports", async () => { - const pulledPrompt = await nodePull("jacob/lahzo-testing", { + const pulledPrompt = await nodePull("jacob/groq-test", { includeModel: true, }); const res = await pulledPrompt.invoke({ - agent: { name: "testing" }, - messages: [new AIMessage("foo")], + question: + "Who is the current president of the USA as of today? You must use the provided tool for the latest info.", }); expect(res).toBeInstanceOf(AIMessage); + expect(res.tool_calls?.length).toEqual(1); });