-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathload.ts
105 lines (102 loc) · 2.89 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import { BasePromptTemplate } from "@langchain/core/prompts";
import { LLMChain } from "../llm_chain.js";
import {
StuffDocumentsChain,
MapReduceDocumentsChain,
RefineDocumentsChain,
MapReduceDocumentsChainInput,
} from "../combine_docs_chain.js";
import { DEFAULT_PROMPT } from "./stuff_prompts.js";
import { REFINE_PROMPT } from "./refine_prompts.js";
/**
* Type for the base parameters that can be used to configure a
* summarization chain.
*/
type BaseParams = {
verbose?: boolean;
};
/** @interface */
export type SummarizationChainParams = BaseParams &
(
| {
type?: "stuff";
prompt?: BasePromptTemplate;
}
| ({
type?: "map_reduce";
combineMapPrompt?: BasePromptTemplate;
combinePrompt?: BasePromptTemplate;
combineLLM?: BaseLanguageModelInterface;
} & Pick<MapReduceDocumentsChainInput, "returnIntermediateSteps">)
| {
type?: "refine";
refinePrompt?: BasePromptTemplate;
refineLLM?: BaseLanguageModelInterface;
questionPrompt?: BasePromptTemplate;
}
);
export const loadSummarizationChain = (
llm: BaseLanguageModelInterface,
params: SummarizationChainParams = { type: "map_reduce" }
) => {
const { verbose } = params;
if (params.type === "stuff") {
const { prompt = DEFAULT_PROMPT } = params;
const llmChain = new LLMChain({ prompt, llm, verbose });
const chain = new StuffDocumentsChain({
llmChain,
documentVariableName: "text",
verbose,
});
return chain;
}
if (params.type === "map_reduce") {
const {
combineMapPrompt = DEFAULT_PROMPT,
combinePrompt = DEFAULT_PROMPT,
combineLLM,
returnIntermediateSteps,
} = params;
const llmChain = new LLMChain({ prompt: combineMapPrompt, llm, verbose });
const combineLLMChain = new LLMChain({
prompt: combinePrompt,
llm: combineLLM ?? llm,
verbose,
});
const combineDocumentChain = new StuffDocumentsChain({
llmChain: combineLLMChain,
documentVariableName: "text",
verbose,
});
const chain = new MapReduceDocumentsChain({
llmChain,
combineDocumentChain,
documentVariableName: "text",
returnIntermediateSteps,
verbose,
});
return chain;
}
if (params.type === "refine") {
const {
refinePrompt = REFINE_PROMPT,
refineLLM,
questionPrompt = DEFAULT_PROMPT,
} = params;
const llmChain = new LLMChain({ prompt: questionPrompt, llm, verbose });
const refineLLMChain = new LLMChain({
prompt: refinePrompt,
llm: refineLLM ?? llm,
verbose,
});
const chain = new RefineDocumentsChain({
llmChain,
refineLLMChain,
documentVariableName: "text",
verbose,
});
return chain;
}
throw new Error(`Invalid _type: ${params.type}`);
};