-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
179 lines (160 loc) Β· 5.39 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
import {
BaseRetriever,
type BaseRetrieverInput,
} from "@langchain/core/retrievers";
import { Document } from "@langchain/core/documents";
import { VectorStore } from "@langchain/core/vectorstores";
import { CallbackManagerForRetrieverRun } from "@langchain/core/callbacks/manager";
import { LLMChain } from "../../chains/llm_chain.js";
import {
QueryConstructorChainOptions,
loadQueryConstructorChain,
} from "../../chains/query_constructor/index.js";
import { StructuredQuery } from "../../chains/query_constructor/ir.js";
import { FunctionalTranslator } from "./functional.js";
import { BaseTranslator, BasicTranslator } from "./base.js";
export { BaseTranslator, BasicTranslator, FunctionalTranslator };
/**
* Interface for the arguments required to create a SelfQueryRetriever
* instance. It extends the BaseRetrieverInput interface.
*/
export interface SelfQueryRetrieverArgs<T extends VectorStore>
extends BaseRetrieverInput {
vectorStore: T;
structuredQueryTranslator: BaseTranslator<T>;
llmChain: LLMChain;
verbose?: boolean;
useOriginalQuery?: boolean;
searchParams?: {
k?: number;
filter?: T["FilterType"];
mergeFiltersOperator?: "or" | "and" | "replace";
forceDefaultFilter?: boolean;
};
}
/**
* Class for question answering over an index. It retrieves relevant
* documents based on a query. It extends the BaseRetriever class and
* implements the SelfQueryRetrieverArgs interface.
* @example
* ```typescript
* const selfQueryRetriever = await SelfQueryRetriever.fromLLM({
* llm: new ChatOpenAI(),
* vectorStore: await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings()),
* documentContents: "Brief summary of a movie",
* attributeInfo: attributeInfo,
* structuredQueryTranslator: new FunctionalTranslator(),
* });
* const relevantDocuments = await selfQueryRetriever.getRelevantDocuments(
* "Which movies are directed by Greta Gerwig?",
* );
* ```
*/
export class SelfQueryRetriever<T extends VectorStore>
extends BaseRetriever
implements SelfQueryRetrieverArgs<T>
{
static lc_name() {
return "SelfQueryRetriever";
}
get lc_namespace() {
return ["langchain", "retrievers", "self_query"];
}
vectorStore: T;
llmChain: LLMChain;
verbose?: boolean;
structuredQueryTranslator: BaseTranslator<T>;
useOriginalQuery = false;
searchParams?: {
k?: number;
filter?: T["FilterType"];
mergeFiltersOperator?: "or" | "and" | "replace";
forceDefaultFilter?: boolean;
} = { k: 4, forceDefaultFilter: false };
constructor(options: SelfQueryRetrieverArgs<T>) {
super(options);
this.vectorStore = options.vectorStore;
this.llmChain = options.llmChain;
this.verbose = options.verbose ?? false;
this.searchParams = options.searchParams ?? this.searchParams;
this.useOriginalQuery = options.useOriginalQuery ?? this.useOriginalQuery;
this.structuredQueryTranslator = options.structuredQueryTranslator;
}
async _getRelevantDocuments(
query: string,
runManager?: CallbackManagerForRetrieverRun
): Promise<Document<Record<string, unknown>>[]> {
const { [this.llmChain.outputKey]: output } = await this.llmChain.call(
{
[this.llmChain.inputKeys[0]]: query,
},
runManager?.getChild("llm_chain")
);
const generatedStructuredQuery = output as StructuredQuery;
const nextArg = this.structuredQueryTranslator.visitStructuredQuery(
generatedStructuredQuery
);
const filter = this.structuredQueryTranslator.mergeFilters(
this.searchParams?.filter,
nextArg.filter,
this.searchParams?.mergeFiltersOperator,
this.searchParams?.forceDefaultFilter
);
const generatedQuery = generatedStructuredQuery.query;
let myQuery = query;
if (!this.useOriginalQuery && generatedQuery && generatedQuery.length > 0) {
myQuery = generatedQuery;
}
if (!filter) {
return [];
} else {
return this.vectorStore.similaritySearch(
myQuery,
this.searchParams?.k,
filter,
runManager?.getChild("vectorstore")
);
}
}
/**
* Static method to create a new SelfQueryRetriever instance from a
* BaseLanguageModel and a VectorStore. It first loads a query constructor
* chain using the loadQueryConstructorChain function, then creates a new
* SelfQueryRetriever instance with the loaded chain and the provided
* options.
* @param options The options used to create the SelfQueryRetriever instance. It includes the QueryConstructorChainOptions and all the SelfQueryRetrieverArgs except 'llmChain'.
* @returns A new instance of SelfQueryRetriever.
*/
static fromLLM<T extends VectorStore>(
options: QueryConstructorChainOptions &
Omit<SelfQueryRetrieverArgs<T>, "llmChain">
): SelfQueryRetriever<T> {
const {
structuredQueryTranslator,
allowedComparators,
allowedOperators,
llm,
documentContents,
attributeInfo,
examples,
vectorStore,
...rest
} = options;
const llmChain = loadQueryConstructorChain({
llm,
documentContents,
attributeInfo,
examples,
allowedComparators:
allowedComparators ?? structuredQueryTranslator.allowedComparators,
allowedOperators:
allowedOperators ?? structuredQueryTranslator.allowedOperators,
});
return new SelfQueryRetriever<T>({
...rest,
llmChain,
vectorStore,
structuredQueryTranslator,
});
}
}