From e01fa011c3b646370f3e7b64902e6010e81306ed Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 29 Jan 2025 12:20:59 -0800 Subject: [PATCH 1/2] Fix Groq import --- langchain/src/hub/base.ts | 31 +++++++++++++++++++++---- langchain/src/hub/index.ts | 6 ++--- langchain/src/hub/node.ts | 4 ++-- langchain/src/hub/tests/hub.int.test.ts | 6 ++--- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/langchain/src/hub/base.ts b/langchain/src/hub/base.ts index 6b4c2de020c5..04cb32a51603 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,30 @@ 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..54a366b7687b 100644 --- a/langchain/src/hub/index.ts +++ b/langchain/src/hub/index.ts @@ -1,7 +1,7 @@ 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,8 +36,8 @@ export async function pull( const loadedPrompt = await load( JSON.stringify(promptObject.manifest), undefined, - undefined, - generateModelImportMap(options?.modelClass) + generateOptionalImportMap(options?.modelClass), + generateModelImportMap(options?.modelClass), ); return loadedPrompt; // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/langchain/src/hub/node.ts b/langchain/src/hub/node.ts index ab01777f5d2a..4df8361e5a02 100644 --- a/langchain/src/hub/node.ts +++ b/langchain/src/hub/node.ts @@ -1,5 +1,5 @@ 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 +55,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..17be63a0ea3a 100644 --- a/langchain/src/hub/tests/hub.int.test.ts +++ b/langchain/src/hub/tests/hub.int.test.ts @@ -79,12 +79,12 @@ 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); }); From 89f9f8c9cc56c27b48a129bd2c21519352738d57 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 29 Jan 2025 12:21:54 -0800 Subject: [PATCH 2/2] Format --- langchain/src/hub/base.ts | 1 - langchain/src/hub/index.ts | 9 +++++++-- langchain/src/hub/node.ts | 7 ++++++- langchain/src/hub/tests/hub.int.test.ts | 3 ++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/langchain/src/hub/base.ts b/langchain/src/hub/base.ts index 04cb32a51603..6850cd37f2bd 100644 --- a/langchain/src/hub/base.ts +++ b/langchain/src/hub/base.ts @@ -127,7 +127,6 @@ export function generateModelImportMap( return modelImportMap; } - export function generateOptionalImportMap( // eslint-disable-next-line @typescript-eslint/no-explicit-any modelClass?: new (...args: any[]) => BaseLanguageModel diff --git a/langchain/src/hub/index.ts b/langchain/src/hub/index.ts index 54a366b7687b..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, generateOptionalImportMap } from "./base.js"; +import { + basePush, + basePull, + generateModelImportMap, + generateOptionalImportMap, +} from "./base.js"; export { basePush as push }; @@ -37,7 +42,7 @@ export async function pull( JSON.stringify(promptObject.manifest), undefined, generateOptionalImportMap(options?.modelClass), - generateModelImportMap(options?.modelClass), + generateModelImportMap(options?.modelClass) ); return loadedPrompt; // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/langchain/src/hub/node.ts b/langchain/src/hub/node.ts index 4df8361e5a02..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, generateOptionalImportMap } 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 diff --git a/langchain/src/hub/tests/hub.int.test.ts b/langchain/src/hub/tests/hub.int.test.ts index 17be63a0ea3a..f1c870cd6753 100644 --- a/langchain/src/hub/tests/hub.int.test.ts +++ b/langchain/src/hub/tests/hub.int.test.ts @@ -83,7 +83,8 @@ test("Test LangChain Hub while loading model with dynamic imports", async () => includeModel: true, }); const res = await pulledPrompt.invoke({ - question: "Who is the current president of the USA as of today? You must use the provided tool for the latest info.", + 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);