-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
load.ts
38 lines (35 loc) · 1.13 KB
/
load.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { BasePromptTemplate } from "@langchain/core/prompts";
import { loadFromHub } from "../util/hub.js";
import { FileLoader, loadFromFile } from "../util/load.js";
import { parseFileConfig } from "../util/parse.js";
const loadPromptFromFile: FileLoader<BasePromptTemplate> = (text, path) =>
BasePromptTemplate.deserialize(parseFileConfig(text, path));
/**
* Load a prompt from {@link https://github.com/hwchase17/langchain-hub | LangchainHub} or local filesystem.
*
* @example
* Loading from LangchainHub:
* ```ts
* import { loadPrompt } from "langchain/prompts/load";
* const prompt = await loadPrompt("lc://prompts/hello-world/prompt.yaml");
* ```
*
* @example
* Loading from local filesystem:
* ```ts
* import { loadPrompt } from "langchain/prompts/load";
* const prompt = await loadPrompt("/path/to/prompt.json");
* ```
*/
export const loadPrompt = async (uri: string): Promise<BasePromptTemplate> => {
const hubResult = await loadFromHub(
uri,
loadPromptFromFile,
"prompts",
new Set(["py", "json", "yaml"])
);
if (hubResult) {
return hubResult;
}
return loadFromFile(uri, loadPromptFromFile);
};