-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
supabase.ts
313 lines (278 loc) Β· 10 KB
/
supabase.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import type { SupabaseClient } from "@supabase/supabase-js";
import type { PostgrestFilterBuilder } from "@supabase/postgrest-js";
import {
MaxMarginalRelevanceSearchOptions,
VectorStore,
} from "@langchain/core/vectorstores";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { Document } from "@langchain/core/documents";
import { maximalMarginalRelevance } from "@langchain/core/utils/math";
/**
* Interface for the parameters required for searching embeddings.
*/
interface SearchEmbeddingsParams {
query_embedding: number[];
match_count: number; // int
filter?: SupabaseMetadata | SupabaseFilterRPCCall;
}
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any
export type SupabaseMetadata = Record<string, any>;
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any
export type SupabaseFilter = PostgrestFilterBuilder<any, any, any>;
export type SupabaseFilterRPCCall = (rpcCall: SupabaseFilter) => SupabaseFilter;
/**
* Interface for the response returned when searching embeddings.
*/
interface SearchEmbeddingsResponse {
id: number;
content: string;
metadata: object;
embedding: number[];
similarity: number;
}
/**
* Interface for the arguments required to initialize a Supabase library.
*/
export interface SupabaseLibArgs {
client: SupabaseClient;
tableName?: string;
queryName?: string;
filter?: SupabaseMetadata | SupabaseFilterRPCCall;
upsertBatchSize?: number;
}
/**
* Class for interacting with a Supabase database to store and manage
* vectors.
*/
export class SupabaseVectorStore extends VectorStore {
declare FilterType: SupabaseMetadata | SupabaseFilterRPCCall;
client: SupabaseClient;
tableName: string;
queryName: string;
filter?: SupabaseMetadata | SupabaseFilterRPCCall;
upsertBatchSize = 500;
_vectorstoreType(): string {
return "supabase";
}
constructor(embeddings: EmbeddingsInterface, args: SupabaseLibArgs) {
super(embeddings, args);
this.client = args.client;
this.tableName = args.tableName || "documents";
this.queryName = args.queryName || "match_documents";
this.filter = args.filter;
this.upsertBatchSize = args.upsertBatchSize ?? this.upsertBatchSize;
}
/**
* Adds documents to the vector store.
* @param documents The documents to add.
* @param options Optional parameters for adding the documents.
* @returns A promise that resolves when the documents have been added.
*/
async addDocuments(
documents: Document[],
options?: { ids?: string[] | number[] }
) {
const texts = documents.map(({ pageContent }) => pageContent);
return this.addVectors(
await this.embeddings.embedDocuments(texts),
documents,
options
);
}
/**
* Adds vectors to the vector store.
* @param vectors The vectors to add.
* @param documents The documents associated with the vectors.
* @param options Optional parameters for adding the vectors.
* @returns A promise that resolves with the IDs of the added vectors when the vectors have been added.
*/
async addVectors(
vectors: number[][],
documents: Document[],
options?: { ids?: string[] | number[] }
) {
const rows = vectors.map((embedding, idx) => ({
content: documents[idx].pageContent,
embedding,
metadata: documents[idx].metadata,
}));
// upsert returns 500/502/504 (yes really any of them) if given too many rows/characters
// ~2000 trips it, but my data is probably smaller than average pageContent and metadata
let returnedIds: string[] = [];
for (let i = 0; i < rows.length; i += this.upsertBatchSize) {
const chunk = rows.slice(i, i + this.upsertBatchSize).map((row, j) => {
if (options?.ids) {
return { id: options.ids[i + j], ...row };
}
return row;
});
const res = await this.client.from(this.tableName).upsert(chunk).select();
if (res.error) {
throw new Error(
`Error inserting: ${res.error.message} ${res.status} ${res.statusText}`
);
}
if (res.data) {
returnedIds = returnedIds.concat(res.data.map((row) => row.id));
}
}
return returnedIds;
}
/**
* Deletes vectors from the vector store.
* @param params The parameters for deleting vectors.
* @returns A promise that resolves when the vectors have been deleted.
*/
async delete(params: { ids: string[] | number[] }): Promise<void> {
const { ids } = params;
for (const id of ids) {
await this.client.from(this.tableName).delete().eq("id", id);
}
}
protected async _searchSupabase(
query: number[],
k: number,
filter?: this["FilterType"]
): Promise<SearchEmbeddingsResponse[]> {
if (filter && this.filter) {
throw new Error("cannot provide both `filter` and `this.filter`");
}
const _filter = filter ?? this.filter ?? {};
const matchDocumentsParams: Partial<SearchEmbeddingsParams> = {
query_embedding: query,
};
let filterFunction: SupabaseFilterRPCCall;
if (typeof _filter === "function") {
filterFunction = (rpcCall) => _filter(rpcCall).limit(k);
} else if (typeof _filter === "object") {
matchDocumentsParams.filter = _filter;
matchDocumentsParams.match_count = k;
filterFunction = (rpcCall) => rpcCall;
} else {
throw new Error("invalid filter type");
}
const rpcCall = this.client.rpc(this.queryName, matchDocumentsParams);
const { data: searches, error } = await filterFunction(rpcCall);
if (error) {
throw new Error(
`Error searching for documents: ${error.code} ${error.message} ${error.details}`
);
}
return searches;
}
/**
* Performs a similarity search on the vector store.
* @param query The query vector.
* @param k The number of results to return.
* @param filter Optional filter to apply to the search.
* @returns A promise that resolves with the search results when the search is complete.
*/
async similaritySearchVectorWithScore(
query: number[],
k: number,
filter?: this["FilterType"]
): Promise<[Document, number][]> {
const searches = await this._searchSupabase(query, k, filter);
const result: [Document, number][] = searches.map((resp) => [
new Document({
metadata: resp.metadata,
pageContent: resp.content,
}),
resp.similarity,
]);
return result;
}
/**
* Return documents selected using the maximal marginal relevance.
* Maximal marginal relevance optimizes for similarity to the query AND diversity
* among selected documents.
*
* @param {string} query - Text to look up documents similar to.
* @param {number} options.k - Number of documents to return.
* @param {number} options.fetchK=20- Number of documents to fetch before passing to the MMR algorithm.
* @param {number} options.lambda=0.5 - Number between 0 and 1 that determines the degree of diversity among the results,
* where 0 corresponds to maximum diversity and 1 to minimum diversity.
* @param {SupabaseLibArgs} options.filter - Optional filter to apply to the search.
*
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
*/
async maxMarginalRelevanceSearch(
query: string,
options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>
): Promise<Document[]> {
const queryEmbedding = await this.embeddings.embedQuery(query);
const searches = await this._searchSupabase(
queryEmbedding,
options.fetchK ?? 20,
options.filter
);
const embeddingList = searches.map((searchResp) => searchResp.embedding);
const mmrIndexes = maximalMarginalRelevance(
queryEmbedding,
embeddingList,
options.lambda,
options.k
);
return mmrIndexes.map(
(idx) =>
new Document({
metadata: searches[idx].metadata,
pageContent: searches[idx].content,
})
);
}
/**
* Creates a new SupabaseVectorStore instance from an array of texts.
* @param texts The texts to create documents from.
* @param metadatas The metadata for the documents.
* @param embeddings The embeddings to use.
* @param dbConfig The configuration for the Supabase database.
* @returns A promise that resolves with a new SupabaseVectorStore instance when the instance has been created.
*/
static async fromTexts(
texts: string[],
metadatas: object[] | object,
embeddings: EmbeddingsInterface,
dbConfig: SupabaseLibArgs
): Promise<SupabaseVectorStore> {
const docs: Document[] = [];
for (let i = 0; i < texts.length; i += 1) {
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
const newDoc = new Document({
pageContent: texts[i],
metadata,
});
docs.push(newDoc);
}
return SupabaseVectorStore.fromDocuments(docs, embeddings, dbConfig);
}
/**
* Creates a new SupabaseVectorStore instance from an array of documents.
* @param docs The documents to create the instance from.
* @param embeddings The embeddings to use.
* @param dbConfig The configuration for the Supabase database.
* @returns A promise that resolves with a new SupabaseVectorStore instance when the instance has been created.
*/
static async fromDocuments(
docs: Document[],
embeddings: EmbeddingsInterface,
dbConfig: SupabaseLibArgs
): Promise<SupabaseVectorStore> {
const instance = new this(embeddings, dbConfig);
await instance.addDocuments(docs);
return instance;
}
/**
* Creates a new SupabaseVectorStore instance from an existing index.
* @param embeddings The embeddings to use.
* @param dbConfig The configuration for the Supabase database.
* @returns A promise that resolves with a new SupabaseVectorStore instance when the instance has been created.
*/
static async fromExistingIndex(
embeddings: EmbeddingsInterface,
dbConfig: SupabaseLibArgs
): Promise<SupabaseVectorStore> {
const instance = new this(embeddings, dbConfig);
return instance;
}
}