-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
load.ts
51 lines (48 loc) · 1.4 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
39
40
41
42
43
44
45
46
47
48
49
50
51
import { BaseChain } from "./base.js";
import { loadFromHub } from "../util/hub.js";
import { FileLoader, LoadValues, loadFromFile } from "../util/load.js";
import { parseFileConfig } from "../util/parse.js";
const loadChainFromFile: FileLoader<BaseChain> = async (
file: string,
path: string,
values: LoadValues = {}
) => {
const serialized = parseFileConfig(file, path);
return BaseChain.deserialize(serialized, values);
};
/**
* Load a chain from {@link https://github.com/hwchase17/langchain-hub | LangchainHub} or local filesystem.
*
* @example
* Loading from LangchainHub:
* ```ts
* import { loadChain } from "langchain/chains/load";
* const chain = await loadChain("lc://chains/hello-world/chain.json");
* const res = await chain.call({ topic: "my favorite color" });
* ```
*
* @example
* Loading from local filesystem:
* ```ts
* import { loadChain } from "langchain/chains/load";
* const chain = await loadChain("/path/to/chain.json");
* ```
*
* @deprecated Use newer {@link https://api.js.langchain.com/functions/langchain_load.load.html | load method}.
*/
export const loadChain = async (
uri: string,
values: LoadValues = {}
): Promise<BaseChain> => {
const hubResult = await loadFromHub(
uri,
loadChainFromFile,
"chains",
new Set(["json", "yaml"]),
values
);
if (hubResult) {
return hubResult;
}
return loadFromFile(uri, loadChainFromFile, values);
};