-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
singlestore.ts
297 lines (268 loc) Β· 8.51 KB
/
singlestore.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
import type {
Pool,
RowDataPacket,
OkPacket,
ResultSetHeader,
FieldPacket,
PoolOptions,
} from "mysql2/promise";
import { format } from "mysql2";
import { createPool } from "mysql2/promise";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { VectorStore } from "@langchain/core/vectorstores";
import { Document } from "@langchain/core/documents";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Metadata = Record<string, any>;
export type DistanceMetrics = "DOT_PRODUCT" | "EUCLIDEAN_DISTANCE";
const OrderingDirective: Record<DistanceMetrics, string> = {
DOT_PRODUCT: "DESC",
EUCLIDEAN_DISTANCE: "",
};
export interface ConnectionOptions extends PoolOptions {}
type ConnectionWithUri = {
connectionOptions?: never;
connectionURI: string;
};
type ConnectionWithOptions = {
connectionURI?: never;
connectionOptions: ConnectionOptions;
};
type ConnectionConfig = ConnectionWithUri | ConnectionWithOptions;
export type SingleStoreVectorStoreConfig = ConnectionConfig & {
tableName?: string;
contentColumnName?: string;
vectorColumnName?: string;
metadataColumnName?: string;
distanceMetric?: DistanceMetrics;
};
function withConnectAttributes(
config: SingleStoreVectorStoreConfig
): ConnectionOptions {
let newOptions: ConnectionOptions = {};
if (config.connectionURI) {
newOptions = {
uri: config.connectionURI,
};
} else if (config.connectionOptions) {
newOptions = {
...config.connectionOptions,
};
}
const result: ConnectionOptions = {
...newOptions,
connectAttributes: {
...newOptions.connectAttributes,
},
};
if (!result.connectAttributes) {
result.connectAttributes = {};
}
result.connectAttributes = {
...result.connectAttributes,
_connector_name: "langchain js sdk",
_connector_version: "1.0.0",
_driver_name: "Node-MySQL-2",
};
return result;
}
/**
* Class for interacting with SingleStoreDB, a high-performance
* distributed SQL database. It provides vector storage and vector
* functions.
*/
export class SingleStoreVectorStore extends VectorStore {
connectionPool: Pool;
tableName: string;
contentColumnName: string;
vectorColumnName: string;
metadataColumnName: string;
distanceMetric: DistanceMetrics;
_vectorstoreType(): string {
return "singlestore";
}
constructor(
embeddings: EmbeddingsInterface,
config: SingleStoreVectorStoreConfig
) {
super(embeddings, config);
this.connectionPool = createPool(withConnectAttributes(config));
this.tableName = config.tableName ?? "embeddings";
this.contentColumnName = config.contentColumnName ?? "content";
this.vectorColumnName = config.vectorColumnName ?? "vector";
this.metadataColumnName = config.metadataColumnName ?? "metadata";
this.distanceMetric = config.distanceMetric ?? "DOT_PRODUCT";
}
/**
* Creates a new table in the SingleStoreDB database if it does not
* already exist.
*/
async createTableIfNotExists(): Promise<void> {
await this.connectionPool
.execute(`CREATE TABLE IF NOT EXISTS ${this.tableName} (
${this.contentColumnName} TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
${this.vectorColumnName} BLOB,
${this.metadataColumnName} JSON);`);
}
/**
* Ends the connection to the SingleStoreDB database.
*/
async end(): Promise<void> {
return this.connectionPool.end();
}
/**
* Adds new documents to the SingleStoreDB database.
* @param documents An array of Document objects.
*/
async addDocuments(documents: Document[]): Promise<void> {
const texts = documents.map(({ pageContent }) => pageContent);
const vectors = await this.embeddings.embedDocuments(texts);
return this.addVectors(vectors, documents);
}
/**
* Adds new vectors to the SingleStoreDB database.
* @param vectors An array of vectors.
* @param documents An array of Document objects.
*/
async addVectors(vectors: number[][], documents: Document[]): Promise<void> {
await this.createTableIfNotExists();
const { tableName } = this;
await Promise.all(
vectors.map(async (vector, idx) => {
try {
await this.connectionPool.execute(
format(
`INSERT INTO ${tableName} VALUES (?, JSON_ARRAY_PACK('[?]'), ?);`,
[
documents[idx].pageContent,
vector,
JSON.stringify(documents[idx].metadata),
]
)
);
} catch (error) {
console.error(`Error adding vector at index ${idx}:`, error);
}
})
);
}
/**
* Performs a similarity search on the vectors stored in the SingleStoreDB
* database.
* @param query An array of numbers representing the query vector.
* @param k The number of nearest neighbors to return.
* @param filter Optional metadata to filter the vectors by.
* @returns Top matching vectors with score
*/
async similaritySearchVectorWithScore(
query: number[],
k: number,
filter?: Metadata
): Promise<[Document, number][]> {
// build the where clause from filter
const whereArgs: string[] = [];
const buildWhereClause = (record: Metadata, argList: string[]): string => {
const whereTokens: string[] = [];
for (const key in record)
if (record[key] !== undefined) {
if (
typeof record[key] === "object" &&
record[key] != null &&
!Array.isArray(record[key])
) {
whereTokens.push(
buildWhereClause(record[key], argList.concat([key]))
);
} else {
whereTokens.push(
`JSON_EXTRACT_JSON(${this.metadataColumnName}, `.concat(
Array.from({ length: argList.length + 1 }, () => "?").join(
", "
),
") = ?"
)
);
whereArgs.push(...argList, key, JSON.stringify(record[key]));
}
}
return whereTokens.join(" AND ");
};
const whereClause = filter
? "WHERE ".concat(buildWhereClause(filter, []))
: "";
const [rows]: [
(
| RowDataPacket[]
| RowDataPacket[][]
| OkPacket
| OkPacket[]
| ResultSetHeader
),
FieldPacket[]
] = await this.connectionPool.query(
format(
`SELECT ${this.contentColumnName},
${this.metadataColumnName},
${this.distanceMetric}(${
this.vectorColumnName
}, JSON_ARRAY_PACK('[?]')) as __score FROM ${
this.tableName
} ${whereClause}
ORDER BY __score ${OrderingDirective[this.distanceMetric]} LIMIT ?;`,
[query, ...whereArgs, k]
)
);
const result: [Document, number][] = [];
for (const row of rows as RowDataPacket[]) {
const rowData = row as unknown as Record<string, unknown>;
result.push([
new Document({
pageContent: rowData[this.contentColumnName] as string,
metadata: rowData[this.metadataColumnName] as Record<string, unknown>,
}),
Number(rowData.score),
]);
}
return result;
}
/**
* Creates a new instance of the SingleStoreVectorStore class from a list
* of texts.
* @param texts An array of strings.
* @param metadatas An array of metadata objects.
* @param embeddings An Embeddings object.
* @param dbConfig A SingleStoreVectorStoreConfig object.
* @returns A new SingleStoreVectorStore instance
*/
static async fromTexts(
texts: string[],
metadatas: object[],
embeddings: EmbeddingsInterface,
dbConfig: SingleStoreVectorStoreConfig
): Promise<SingleStoreVectorStore> {
const docs = texts.map((text, idx) => {
const metadata = Array.isArray(metadatas) ? metadatas[idx] : metadatas;
return new Document({
pageContent: text,
metadata,
});
});
return SingleStoreVectorStore.fromDocuments(docs, embeddings, dbConfig);
}
/**
* Creates a new instance of the SingleStoreVectorStore class from a list
* of Document objects.
* @param docs An array of Document objects.
* @param embeddings An Embeddings object.
* @param dbConfig A SingleStoreVectorStoreConfig object.
* @returns A new SingleStoreVectorStore instance
*/
static async fromDocuments(
docs: Document[],
embeddings: EmbeddingsInterface,
dbConfig: SingleStoreVectorStoreConfig
): Promise<SingleStoreVectorStore> {
const instance = new this(embeddings, dbConfig);
await instance.addDocuments(docs);
return instance;
}
}