-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathvector_db_qa.ts
164 lines (147 loc) Β· 4.76 KB
/
vector_db_qa.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
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { VectorStoreInterface } from "@langchain/core/vectorstores";
import { CallbackManagerForChainRun } from "@langchain/core/callbacks/manager";
import { ChainValues } from "@langchain/core/utils/types";
import { BaseChain, ChainInputs } from "./base.js";
import { SerializedVectorDBQAChain } from "./serde.js";
import { loadQAStuffChain } from "./question_answering/load.js";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type LoadValues = Record<string, any>;
/**
* Interface that extends the `ChainInputs` interface and defines the
* input fields required for a VectorDBQAChain. It includes properties
* such as `vectorstore`, `combineDocumentsChain`,
* `returnSourceDocuments`, `k`, and `inputKey`.
*
* @deprecated
* Switch to {@link https://js.langchain.com/docs/modules/chains/ | createRetrievalChain}
* Will be removed in 0.2.0
*/
export interface VectorDBQAChainInput extends Omit<ChainInputs, "memory"> {
vectorstore: VectorStoreInterface;
combineDocumentsChain: BaseChain;
returnSourceDocuments?: boolean;
k?: number;
inputKey?: string;
}
/**
* Class that represents a VectorDBQAChain. It extends the `BaseChain`
* class and implements the `VectorDBQAChainInput` interface. It performs
* a similarity search using a vector store and combines the search
* results using a specified combine documents chain.
*
* @deprecated
* Switch to {@link https://js.langchain.com/docs/modules/chains/ | createRetrievalChain}
* Will be removed in 0.2.0
*/
export class VectorDBQAChain extends BaseChain implements VectorDBQAChainInput {
static lc_name() {
return "VectorDBQAChain";
}
k = 4;
inputKey = "query";
get inputKeys() {
return [this.inputKey];
}
get outputKeys() {
return this.combineDocumentsChain.outputKeys.concat(
this.returnSourceDocuments ? ["sourceDocuments"] : []
);
}
vectorstore: VectorStoreInterface;
combineDocumentsChain: BaseChain;
returnSourceDocuments = false;
constructor(fields: VectorDBQAChainInput) {
super(fields);
this.vectorstore = fields.vectorstore;
this.combineDocumentsChain = fields.combineDocumentsChain;
this.inputKey = fields.inputKey ?? this.inputKey;
this.k = fields.k ?? this.k;
this.returnSourceDocuments =
fields.returnSourceDocuments ?? this.returnSourceDocuments;
}
/** @ignore */
async _call(
values: ChainValues,
runManager?: CallbackManagerForChainRun
): Promise<ChainValues> {
if (!(this.inputKey in values)) {
throw new Error(`Question key ${this.inputKey} not found.`);
}
const question: string = values[this.inputKey];
const docs = await this.vectorstore.similaritySearch(
question,
this.k,
values.filter,
runManager?.getChild("vectorstore")
);
const inputs = { question, input_documents: docs };
const result = await this.combineDocumentsChain.call(
inputs,
runManager?.getChild("combine_documents")
);
if (this.returnSourceDocuments) {
return {
...result,
sourceDocuments: docs,
};
}
return result;
}
_chainType() {
return "vector_db_qa" as const;
}
static async deserialize(
data: SerializedVectorDBQAChain,
values: LoadValues
) {
if (!("vectorstore" in values)) {
throw new Error(
`Need to pass in a vectorstore to deserialize VectorDBQAChain`
);
}
const { vectorstore } = values;
if (!data.combine_documents_chain) {
throw new Error(
`VectorDBQAChain must have combine_documents_chain in serialized data`
);
}
return new VectorDBQAChain({
combineDocumentsChain: await BaseChain.deserialize(
data.combine_documents_chain
),
k: data.k,
vectorstore,
});
}
serialize(): SerializedVectorDBQAChain {
return {
_type: this._chainType(),
combine_documents_chain: this.combineDocumentsChain.serialize(),
k: this.k,
};
}
/**
* Static method that creates a VectorDBQAChain instance from a
* BaseLanguageModel and a vector store. It also accepts optional options
* to customize the chain.
* @param llm The BaseLanguageModel instance.
* @param vectorstore The vector store used for similarity search.
* @param options Optional options to customize the chain.
* @returns A new instance of VectorDBQAChain.
*/
static fromLLM(
llm: BaseLanguageModelInterface,
vectorstore: VectorStoreInterface,
options?: Partial<
Omit<VectorDBQAChainInput, "combineDocumentsChain" | "vectorstore">
>
): VectorDBQAChain {
const qaChain = loadQAStuffChain(llm);
return new this({
vectorstore,
combineDocumentsChain: qaChain,
...options,
});
}
}