-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
load.ts
166 lines (157 loc) Β· 4.55 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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 { QA_PROMPT_SELECTOR } from "./stuff_prompts.js";
import {
COMBINE_PROMPT_SELECTOR,
COMBINE_QA_PROMPT_SELECTOR,
} from "./map_reduce_prompts.js";
import {
QUESTION_PROMPT_SELECTOR,
REFINE_PROMPT_SELECTOR,
} from "./refine_prompts.js";
/**
* Represents the parameters for creating a QAChain. It can be of three
* types: "stuff", "map_reduce", or "refine".
*/
export type QAChainParams =
| ({
type?: "stuff";
} & StuffQAChainParams)
| ({
type?: "map_reduce";
} & MapReduceQAChainParams)
| ({
type?: "refine";
} & RefineQAChainParams);
export const loadQAChain = (
llm: BaseLanguageModelInterface,
params: QAChainParams = { type: "stuff" }
) => {
const { type } = params;
if (type === "stuff") {
return loadQAStuffChain(llm, params);
}
if (type === "map_reduce") {
return loadQAMapReduceChain(llm, params);
}
if (type === "refine") {
return loadQARefineChain(llm, params);
}
throw new Error(`Invalid _type: ${type}`);
};
/**
* Represents the parameters for creating a StuffQAChain.
*/
export interface StuffQAChainParams {
prompt?: BasePromptTemplate;
verbose?: boolean;
}
/**
* Loads a StuffQAChain based on the provided parameters. It takes an LLM
* instance and StuffQAChainParams as parameters.
* @param llm An instance of BaseLanguageModel.
* @param params Parameters for creating a StuffQAChain.
* @returns A StuffQAChain instance.
*/
export function loadQAStuffChain(
llm: BaseLanguageModelInterface,
params: StuffQAChainParams = {}
) {
const { prompt = QA_PROMPT_SELECTOR.getPrompt(llm), verbose } = params;
const llmChain = new LLMChain({ prompt, llm, verbose });
const chain = new StuffDocumentsChain({ llmChain, verbose });
return chain;
}
/**
* Represents the parameters for creating a MapReduceQAChain.
*/
export interface MapReduceQAChainParams {
returnIntermediateSteps?: MapReduceDocumentsChainInput["returnIntermediateSteps"];
combineMapPrompt?: BasePromptTemplate;
combinePrompt?: BasePromptTemplate;
combineLLM?: BaseLanguageModelInterface;
verbose?: boolean;
}
/**
* Loads a MapReduceQAChain based on the provided parameters. It takes an
* LLM instance and MapReduceQAChainParams as parameters.
* @param llm An instance of BaseLanguageModel.
* @param params Parameters for creating a MapReduceQAChain.
* @returns A MapReduceQAChain instance.
*/
export function loadQAMapReduceChain(
llm: BaseLanguageModelInterface,
params: MapReduceQAChainParams = {}
) {
const {
combineMapPrompt = COMBINE_QA_PROMPT_SELECTOR.getPrompt(llm),
combinePrompt = COMBINE_PROMPT_SELECTOR.getPrompt(llm),
verbose,
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: "summaries",
verbose,
});
const chain = new MapReduceDocumentsChain({
llmChain,
combineDocumentChain,
returnIntermediateSteps,
verbose,
});
return chain;
}
/**
* Represents the parameters for creating a RefineQAChain.
*/
export interface RefineQAChainParams {
questionPrompt?: BasePromptTemplate;
refinePrompt?: BasePromptTemplate;
refineLLM?: BaseLanguageModelInterface;
verbose?: boolean;
}
/**
* Loads a RefineQAChain based on the provided parameters. It takes an LLM
* instance and RefineQAChainParams as parameters.
* @param llm An instance of BaseLanguageModel.
* @param params Parameters for creating a RefineQAChain.
* @returns A RefineQAChain instance.
*/
export function loadQARefineChain(
llm: BaseLanguageModelInterface,
params: RefineQAChainParams = {}
) {
const {
questionPrompt = QUESTION_PROMPT_SELECTOR.getPrompt(llm),
refinePrompt = REFINE_PROMPT_SELECTOR.getPrompt(llm),
refineLLM,
verbose,
} = 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,
verbose,
});
return chain;
}