-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hub entrypoint with automatic dynamic entrypoint of models
- Loading branch information
1 parent
71dd696
commit deef63c
Showing
17 changed files
with
277 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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"; | ||
|
||
export { basePush as push }; | ||
|
||
/** | ||
* Pull a prompt from the hub. | ||
* NOTE: If you are in a Node environment and want to include an instantiated model with your pulled prompt, | ||
* you can instead import this function from "langchain/hub/node" and pass "includeModel: true". | ||
* @param ownerRepoCommit The name of the repo containing the prompt, as well as an optional commit hash separated by a slash. | ||
* @param options | ||
* @returns | ||
*/ | ||
export async function pull<T extends Runnable>( | ||
ownerRepoCommit: string, | ||
options?: { | ||
apiKey?: string; | ||
apiUrl?: string; | ||
includeModel?: boolean; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
modelClass?: new (...args: any[]) => BaseLanguageModel; | ||
} | ||
) { | ||
const promptObject = await basePull(ownerRepoCommit, options); | ||
try { | ||
const loadedPrompt = await load<T>( | ||
JSON.stringify(promptObject.manifest), | ||
undefined, | ||
undefined, | ||
generateModelImportMap(options?.modelClass) | ||
); | ||
return loadedPrompt; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (e: any) { | ||
if (options?.includeModel) { | ||
throw new Error( | ||
[ | ||
e.message, | ||
"", | ||
`To load prompts with an associated non-OpenAI model, you must use the "langchain/hub/node" entrypoint, or pass a "modelClass" parameter like this:`, | ||
"", | ||
"```", | ||
`import { pull } from "langchain/hub";`, | ||
`import { ChatAnthropic } from "@langchain/anthropic";`, | ||
"", | ||
`const prompt = await pull("my-prompt", {`, | ||
` includeModel: true,`, | ||
` modelClass: ChatAnthropic,`, | ||
`});`, | ||
"```", | ||
].join("\n") | ||
); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Runnable } from "@langchain/core/runnables"; | ||
import { basePush, basePull, generateModelImportMap } from "./base.js"; | ||
import { load } from "../load/index.js"; | ||
|
||
export { basePush as push }; | ||
|
||
/** | ||
* Pull a prompt from the hub. | ||
* @param ownerRepoCommit The name of the repo containing the prompt, as well as an optional commit hash separated by a slash. | ||
* @param options | ||
* @returns | ||
*/ | ||
export async function pull<T extends Runnable>( | ||
ownerRepoCommit: string, | ||
options?: { | ||
apiKey?: string; | ||
apiUrl?: string; | ||
includeModel?: boolean; | ||
} | ||
) { | ||
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); | ||
if (modelName === "ChatAnthropic") { | ||
modelClass = (await import("@langchain/anthropic")).ChatAnthropic; | ||
} else if (modelName === "ChatAzureOpenAI") { | ||
modelClass = (await import("@langchain/openai")).AzureChatOpenAI; | ||
} else if (modelName === "ChatGoogleVertexAI") { | ||
modelClass = (await import("@langchain/google-vertexai")).ChatVertexAI; | ||
} else if (modelName === "ChatGoogleGenerativeAI") { | ||
modelClass = (await import("@langchain/google-genai")) | ||
.ChatGoogleGenerativeAI; | ||
} else if (modelName === "ChatBedrockConverse") { | ||
modelClass = (await import("@langchain/aws")).ChatBedrockConverse; | ||
} else if (modelName === "ChatMistral") { | ||
modelClass = (await import("@langchain/mistralai")).ChatMistralAI; | ||
} else if (modelName === "ChatGroq") { | ||
modelClass = (await import("@langchain/groq")).ChatGroq; | ||
} else if (modelName !== undefined) { | ||
console.warn( | ||
`Received unknown model name from prompt hub: "${modelName}"` | ||
); | ||
} | ||
} | ||
} | ||
const loadedPrompt = await load<T>( | ||
JSON.stringify(promptObject.manifest), | ||
undefined, | ||
undefined, | ||
generateModelImportMap(modelClass) | ||
); | ||
return loadedPrompt; | ||
} |
Oops, something went wrong.