langchain 0.0.197 → 0.0.199
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/index.d.ts +1 -1
- package/dist/chains/conversational_retrieval_chain.cjs +16 -2
- package/dist/chains/conversational_retrieval_chain.d.ts +2 -0
- package/dist/chains/conversational_retrieval_chain.js +16 -2
- package/dist/chat_models/llama_cpp.cjs +45 -0
- package/dist/chat_models/llama_cpp.d.ts +4 -1
- package/dist/chat_models/llama_cpp.js +45 -0
- package/dist/document_loaders/fs/chatgpt.cjs +85 -0
- package/dist/document_loaders/fs/chatgpt.d.ts +8 -0
- package/dist/document_loaders/fs/chatgpt.js +81 -0
- package/dist/document_loaders/fs/pptx.cjs +39 -0
- package/dist/document_loaders/fs/pptx.d.ts +23 -0
- package/dist/document_loaders/fs/pptx.js +35 -0
- package/dist/document_loaders/web/confluence.cjs +31 -7
- package/dist/document_loaders/web/confluence.d.ts +12 -5
- package/dist/document_loaders/web/confluence.js +31 -7
- package/dist/experimental/openai_assistant/index.cjs +32 -0
- package/dist/experimental/openai_assistant/index.d.ts +26 -0
- package/dist/experimental/openai_assistant/index.js +32 -0
- package/dist/experimental/tools/pyinterpreter.cjs +248 -0
- package/dist/experimental/tools/pyinterpreter.d.ts +18 -0
- package/dist/experimental/tools/pyinterpreter.js +244 -0
- package/dist/graphs/neo4j_graph.cjs +49 -14
- package/dist/graphs/neo4j_graph.d.ts +30 -0
- package/dist/graphs/neo4j_graph.js +49 -14
- package/dist/llms/gradient_ai.cjs +98 -0
- package/dist/llms/gradient_ai.d.ts +50 -0
- package/dist/llms/gradient_ai.js +94 -0
- package/dist/llms/hf.cjs +13 -2
- package/dist/llms/hf.d.ts +5 -0
- package/dist/llms/hf.js +13 -2
- package/dist/llms/llama_cpp.cjs +17 -3
- package/dist/llms/llama_cpp.d.ts +4 -1
- package/dist/llms/llama_cpp.js +17 -3
- package/dist/llms/watsonx_ai.cjs +154 -0
- package/dist/llms/watsonx_ai.d.ts +72 -0
- package/dist/llms/watsonx_ai.js +150 -0
- package/dist/load/import_constants.cjs +6 -0
- package/dist/load/import_constants.js +6 -0
- package/dist/load/import_map.cjs +4 -3
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/output_parsers/json.cjs +4 -0
- package/dist/output_parsers/json.js +4 -0
- package/dist/tools/google_places.cjs +81 -0
- package/dist/tools/google_places.d.ts +21 -0
- package/dist/tools/google_places.js +77 -0
- package/dist/vectorstores/clickhouse.cjs +286 -0
- package/dist/vectorstores/clickhouse.d.ts +126 -0
- package/dist/vectorstores/clickhouse.js +259 -0
- package/dist/vectorstores/elasticsearch.cjs +16 -3
- package/dist/vectorstores/elasticsearch.d.ts +6 -2
- package/dist/vectorstores/elasticsearch.js +16 -3
- package/dist/vectorstores/pgvector.cjs +142 -18
- package/dist/vectorstores/pgvector.d.ts +21 -0
- package/dist/vectorstores/pgvector.js +142 -18
- package/dist/vectorstores/prisma.cjs +1 -1
- package/dist/vectorstores/prisma.js +1 -1
- package/dist/vectorstores/weaviate.cjs +45 -2
- package/dist/vectorstores/weaviate.d.ts +27 -1
- package/dist/vectorstores/weaviate.js +45 -2
- package/dist/vectorstores/xata.cjs +3 -2
- package/dist/vectorstores/xata.js +3 -2
- package/document_loaders/fs/chatgpt.cjs +1 -0
- package/document_loaders/fs/chatgpt.d.ts +1 -0
- package/document_loaders/fs/chatgpt.js +1 -0
- package/document_loaders/fs/pptx.cjs +1 -0
- package/document_loaders/fs/pptx.d.ts +1 -0
- package/document_loaders/fs/pptx.js +1 -0
- package/experimental/tools/pyinterpreter.cjs +1 -0
- package/experimental/tools/pyinterpreter.d.ts +1 -0
- package/experimental/tools/pyinterpreter.js +1 -0
- package/llms/gradient_ai.cjs +1 -0
- package/llms/gradient_ai.d.ts +1 -0
- package/llms/gradient_ai.js +1 -0
- package/llms/watsonx_ai.cjs +1 -0
- package/llms/watsonx_ai.d.ts +1 -0
- package/llms/watsonx_ai.js +1 -0
- package/package.json +87 -13
- package/tools/google_places.cjs +1 -0
- package/tools/google_places.d.ts +1 -0
- package/tools/google_places.js +1 -0
- package/vectorstores/clickhouse.cjs +1 -0
- package/vectorstores/clickhouse.d.ts +1 -0
- package/vectorstores/clickhouse.js +1 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { getEnvironmentVariable } from "../util/env.js";
|
|
2
|
+
import { Tool } from "./base.js";
|
|
3
|
+
/**
|
|
4
|
+
* Tool that queries the Google Places API
|
|
5
|
+
*/
|
|
6
|
+
export class GooglePlacesAPI extends Tool {
|
|
7
|
+
static lc_name() {
|
|
8
|
+
return "GooglePlacesAPI";
|
|
9
|
+
}
|
|
10
|
+
get lc_secrets() {
|
|
11
|
+
return {
|
|
12
|
+
apiKey: "GOOGLE_PLACES_API_KEY",
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
constructor(fields) {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
Object.defineProperty(this, "name", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: "google_places"
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "apiKey", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "description", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: `A wrapper around Google Places API. Useful for when you need to validate or
|
|
34
|
+
discover addresses from ambiguous text. Input should be a search query.`
|
|
35
|
+
});
|
|
36
|
+
const apiKey = fields?.apiKey ?? getEnvironmentVariable("GOOGLE_PLACES_API_KEY");
|
|
37
|
+
if (apiKey === undefined) {
|
|
38
|
+
throw new Error(`Google Places API key not set. You can set it as "GOOGLE_PLACES_API_KEY" in your environment variables.`);
|
|
39
|
+
}
|
|
40
|
+
this.apiKey = apiKey;
|
|
41
|
+
}
|
|
42
|
+
async _call(input) {
|
|
43
|
+
const res = await fetch(`https://places.googleapis.com/v1/places:searchText`, {
|
|
44
|
+
method: "POST",
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
textQuery: input,
|
|
47
|
+
languageCode: "en",
|
|
48
|
+
}),
|
|
49
|
+
headers: {
|
|
50
|
+
"X-Goog-Api-Key": this.apiKey,
|
|
51
|
+
"X-Goog-FieldMask": "places.displayName,places.formattedAddress,places.id,places.internationalPhoneNumber,places.websiteUri",
|
|
52
|
+
"Content-Type": "application/json",
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
if (!res.ok) {
|
|
56
|
+
let message;
|
|
57
|
+
try {
|
|
58
|
+
const json = await res.json();
|
|
59
|
+
message = json.error.message;
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
message =
|
|
63
|
+
"Unable to parse error message: Google did not return a JSON response.";
|
|
64
|
+
}
|
|
65
|
+
throw new Error(`Got ${res.status}: ${res.statusText} error from Google Places API: ${message}`);
|
|
66
|
+
}
|
|
67
|
+
const json = await res.json();
|
|
68
|
+
const results = json?.places?.map((place) => ({
|
|
69
|
+
name: place.displayName?.text,
|
|
70
|
+
id: place.id,
|
|
71
|
+
address: place.formattedAddress,
|
|
72
|
+
phoneNumber: place.internationalPhoneNumber,
|
|
73
|
+
website: place.websiteUri,
|
|
74
|
+
})) ?? [];
|
|
75
|
+
return JSON.stringify(results);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.ClickHouseStore = void 0;
|
|
27
|
+
const uuid = __importStar(require("uuid"));
|
|
28
|
+
const client_1 = require("@clickhouse/client");
|
|
29
|
+
const mysql2_1 = require("mysql2");
|
|
30
|
+
const base_js_1 = require("./base.cjs");
|
|
31
|
+
const document_js_1 = require("../document.cjs");
|
|
32
|
+
/**
|
|
33
|
+
* Class for interacting with the ClickHouse database. It extends the
|
|
34
|
+
* VectorStore class and provides methods for adding vectors and
|
|
35
|
+
* documents, searching for similar vectors, and creating instances from
|
|
36
|
+
* texts or documents.
|
|
37
|
+
*/
|
|
38
|
+
class ClickHouseStore extends base_js_1.VectorStore {
|
|
39
|
+
_vectorstoreType() {
|
|
40
|
+
return "clickhouse";
|
|
41
|
+
}
|
|
42
|
+
constructor(embeddings, args) {
|
|
43
|
+
super(embeddings, args);
|
|
44
|
+
Object.defineProperty(this, "client", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "indexType", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(this, "indexParam", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: void 0
|
|
61
|
+
});
|
|
62
|
+
Object.defineProperty(this, "indexQueryParams", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(this, "columnMap", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(this, "database", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: void 0
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(this, "table", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: void 0
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(this, "isInitialized", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: false
|
|
91
|
+
});
|
|
92
|
+
this.indexType = args.indexType || "annoy";
|
|
93
|
+
this.indexParam = args.indexParam || { L2Distance: 100 };
|
|
94
|
+
this.indexQueryParams = args.indexQueryParams || {};
|
|
95
|
+
this.columnMap = args.columnMap || {
|
|
96
|
+
id: "id",
|
|
97
|
+
document: "document",
|
|
98
|
+
embedding: "embedding",
|
|
99
|
+
metadata: "metadata",
|
|
100
|
+
uuid: "uuid",
|
|
101
|
+
};
|
|
102
|
+
this.database = args.database || "default";
|
|
103
|
+
this.table = args.table || "vector_table";
|
|
104
|
+
this.client = (0, client_1.createClient)({
|
|
105
|
+
host: `${args.protocol ?? "https://"}${args.host}:${args.port}`,
|
|
106
|
+
username: args.username,
|
|
107
|
+
password: args.password,
|
|
108
|
+
session_id: uuid.v4(),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Method to add vectors to the ClickHouse database.
|
|
113
|
+
* @param vectors The vectors to add.
|
|
114
|
+
* @param documents The documents associated with the vectors.
|
|
115
|
+
* @returns Promise that resolves when the vectors have been added.
|
|
116
|
+
*/
|
|
117
|
+
async addVectors(vectors, documents) {
|
|
118
|
+
if (vectors.length === 0) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (!this.isInitialized) {
|
|
122
|
+
await this.initialize(vectors[0].length);
|
|
123
|
+
}
|
|
124
|
+
const queryStr = this.buildInsertQuery(vectors, documents);
|
|
125
|
+
await this.client.exec({ query: queryStr });
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Method to add documents to the ClickHouse database.
|
|
129
|
+
* @param documents The documents to add.
|
|
130
|
+
* @returns Promise that resolves when the documents have been added.
|
|
131
|
+
*/
|
|
132
|
+
async addDocuments(documents) {
|
|
133
|
+
return this.addVectors(await this.embeddings.embedDocuments(documents.map((d) => d.pageContent)), documents);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Method to search for vectors that are similar to a given query vector.
|
|
137
|
+
* @param query The query vector.
|
|
138
|
+
* @param k The number of similar vectors to return.
|
|
139
|
+
* @param filter Optional filter for the search results.
|
|
140
|
+
* @returns Promise that resolves with an array of tuples, each containing a Document and a score.
|
|
141
|
+
*/
|
|
142
|
+
async similaritySearchVectorWithScore(query, k, filter) {
|
|
143
|
+
if (!this.isInitialized) {
|
|
144
|
+
await this.initialize(query.length);
|
|
145
|
+
}
|
|
146
|
+
const queryStr = this.buildSearchQuery(query, k, filter);
|
|
147
|
+
const queryResultSet = await this.client.query({ query: queryStr });
|
|
148
|
+
const queryResult = await queryResultSet.json();
|
|
149
|
+
const result = queryResult.data.map((item) => [
|
|
150
|
+
new document_js_1.Document({ pageContent: item.document, metadata: item.metadata }),
|
|
151
|
+
item.dist,
|
|
152
|
+
]);
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Static method to create an instance of ClickHouseStore from texts.
|
|
157
|
+
* @param texts The texts to use.
|
|
158
|
+
* @param metadatas The metadata associated with the texts.
|
|
159
|
+
* @param embeddings The embeddings to use.
|
|
160
|
+
* @param args The arguments for the ClickHouseStore.
|
|
161
|
+
* @returns Promise that resolves with a new instance of ClickHouseStore.
|
|
162
|
+
*/
|
|
163
|
+
static async fromTexts(texts, metadatas, embeddings, args) {
|
|
164
|
+
const docs = [];
|
|
165
|
+
for (let i = 0; i < texts.length; i += 1) {
|
|
166
|
+
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
|
|
167
|
+
const newDoc = new document_js_1.Document({
|
|
168
|
+
pageContent: texts[i],
|
|
169
|
+
metadata,
|
|
170
|
+
});
|
|
171
|
+
docs.push(newDoc);
|
|
172
|
+
}
|
|
173
|
+
return ClickHouseStore.fromDocuments(docs, embeddings, args);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Static method to create an instance of ClickHouseStore from documents.
|
|
177
|
+
* @param docs The documents to use.
|
|
178
|
+
* @param embeddings The embeddings to use.
|
|
179
|
+
* @param args The arguments for the ClickHouseStore.
|
|
180
|
+
* @returns Promise that resolves with a new instance of ClickHouseStore.
|
|
181
|
+
*/
|
|
182
|
+
static async fromDocuments(docs, embeddings, args) {
|
|
183
|
+
const instance = new this(embeddings, args);
|
|
184
|
+
await instance.addDocuments(docs);
|
|
185
|
+
return instance;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Static method to create an instance of ClickHouseStore from an existing
|
|
189
|
+
* index.
|
|
190
|
+
* @param embeddings The embeddings to use.
|
|
191
|
+
* @param args The arguments for the ClickHouseStore.
|
|
192
|
+
* @returns Promise that resolves with a new instance of ClickHouseStore.
|
|
193
|
+
*/
|
|
194
|
+
static async fromExistingIndex(embeddings, args) {
|
|
195
|
+
const instance = new this(embeddings, args);
|
|
196
|
+
await instance.initialize();
|
|
197
|
+
return instance;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Method to initialize the ClickHouse database.
|
|
201
|
+
* @param dimension Optional dimension of the vectors.
|
|
202
|
+
* @returns Promise that resolves when the database has been initialized.
|
|
203
|
+
*/
|
|
204
|
+
async initialize(dimension) {
|
|
205
|
+
const dim = dimension ?? (await this.embeddings.embedQuery("test")).length;
|
|
206
|
+
const indexParamStr = this.indexParam
|
|
207
|
+
? Object.entries(this.indexParam)
|
|
208
|
+
.map(([key, value]) => `'${key}', ${value}`)
|
|
209
|
+
.join(", ")
|
|
210
|
+
: "";
|
|
211
|
+
const query = `
|
|
212
|
+
CREATE TABLE IF NOT EXISTS ${this.database}.${this.table}(
|
|
213
|
+
${this.columnMap.id} Nullable(String),
|
|
214
|
+
${this.columnMap.document} Nullable(String),
|
|
215
|
+
${this.columnMap.embedding} Array(Float32),
|
|
216
|
+
${this.columnMap.metadata} JSON,
|
|
217
|
+
${this.columnMap.uuid} UUID DEFAULT generateUUIDv4(),
|
|
218
|
+
CONSTRAINT cons_vec_len CHECK length(${this.columnMap.embedding}) = ${dim},
|
|
219
|
+
INDEX vec_idx ${this.columnMap.embedding} TYPE ${this.indexType}(${indexParamStr}) GRANULARITY 1000
|
|
220
|
+
) ENGINE = MergeTree ORDER BY ${this.columnMap.uuid} SETTINGS index_granularity = 8192;`;
|
|
221
|
+
await this.client.exec({
|
|
222
|
+
query,
|
|
223
|
+
clickhouse_settings: {
|
|
224
|
+
allow_experimental_object_type: 1,
|
|
225
|
+
allow_experimental_annoy_index: 1,
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
this.isInitialized = true;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Method to build an SQL query for inserting vectors and documents into
|
|
232
|
+
* the ClickHouse database.
|
|
233
|
+
* @param vectors The vectors to insert.
|
|
234
|
+
* @param documents The documents to insert.
|
|
235
|
+
* @returns The SQL query string.
|
|
236
|
+
*/
|
|
237
|
+
buildInsertQuery(vectors, documents) {
|
|
238
|
+
const columnsStr = Object.values(Object.fromEntries(Object.entries(this.columnMap).filter(([key]) => key !== this.columnMap.uuid))).join(", ");
|
|
239
|
+
const placeholders = vectors.map(() => "(?, ?, ?, ?)").join(", ");
|
|
240
|
+
const values = [];
|
|
241
|
+
for (let i = 0; i < vectors.length; i += 1) {
|
|
242
|
+
const vector = vectors[i];
|
|
243
|
+
const document = documents[i];
|
|
244
|
+
values.push(uuid.v4(), this.escapeString(document.pageContent), JSON.stringify(vector), JSON.stringify(document.metadata));
|
|
245
|
+
}
|
|
246
|
+
const insertQueryStr = `
|
|
247
|
+
INSERT INTO TABLE ${this.database}.${this.table}(${columnsStr})
|
|
248
|
+
VALUES ${placeholders}
|
|
249
|
+
`;
|
|
250
|
+
const insertQuery = (0, mysql2_1.format)(insertQueryStr, values);
|
|
251
|
+
return insertQuery;
|
|
252
|
+
}
|
|
253
|
+
escapeString(str) {
|
|
254
|
+
return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Method to build an SQL query for searching for similar vectors in the
|
|
258
|
+
* ClickHouse database.
|
|
259
|
+
* @param query The query vector.
|
|
260
|
+
* @param k The number of similar vectors to return.
|
|
261
|
+
* @param filter Optional filter for the search results.
|
|
262
|
+
* @returns The SQL query string.
|
|
263
|
+
*/
|
|
264
|
+
buildSearchQuery(query, k, filter) {
|
|
265
|
+
const order = "ASC";
|
|
266
|
+
const whereStr = filter ? `PREWHERE ${filter.whereStr}` : "";
|
|
267
|
+
const placeholders = query.map(() => "?").join(", ");
|
|
268
|
+
const settingStrings = [];
|
|
269
|
+
if (this.indexQueryParams) {
|
|
270
|
+
for (const [key, value] of Object.entries(this.indexQueryParams)) {
|
|
271
|
+
settingStrings.push(`SETTING ${key}=${value}`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const searchQueryStr = `
|
|
275
|
+
SELECT ${this.columnMap.document} AS document, ${this.columnMap.metadata} AS metadata, dist
|
|
276
|
+
FROM ${this.database}.${this.table}
|
|
277
|
+
${whereStr}
|
|
278
|
+
ORDER BY L2Distance(${this.columnMap.embedding}, [${placeholders}]) AS dist ${order}
|
|
279
|
+
LIMIT ${k} ${settingStrings.join(" ")}
|
|
280
|
+
`;
|
|
281
|
+
// Format the query with actual values
|
|
282
|
+
const searchQuery = (0, mysql2_1.format)(searchQueryStr, query);
|
|
283
|
+
return searchQuery;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
exports.ClickHouseStore = ClickHouseStore;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Embeddings } from "../embeddings/base.js";
|
|
2
|
+
import { VectorStore } from "./base.js";
|
|
3
|
+
import { Document } from "../document.js";
|
|
4
|
+
/**
|
|
5
|
+
* Arguments for the ClickHouseStore class, which include the host, port,
|
|
6
|
+
* protocol, username, password, index type, index parameters,
|
|
7
|
+
* index query params, column map, database, table.
|
|
8
|
+
*/
|
|
9
|
+
export interface ClickHouseLibArgs {
|
|
10
|
+
host: string;
|
|
11
|
+
port: string | number;
|
|
12
|
+
protocol?: string;
|
|
13
|
+
username: string;
|
|
14
|
+
password: string;
|
|
15
|
+
indexType?: string;
|
|
16
|
+
indexParam?: Record<string, number>;
|
|
17
|
+
indexQueryParams?: Record<string, string>;
|
|
18
|
+
columnMap?: ColumnMap;
|
|
19
|
+
database?: string;
|
|
20
|
+
table?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Mapping of columns in the ClickHouse database.
|
|
24
|
+
*/
|
|
25
|
+
export interface ColumnMap {
|
|
26
|
+
id: string;
|
|
27
|
+
uuid: string;
|
|
28
|
+
document: string;
|
|
29
|
+
embedding: string;
|
|
30
|
+
metadata: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Type for filtering search results in the ClickHouse database.
|
|
34
|
+
*/
|
|
35
|
+
export interface ClickHouseFilter {
|
|
36
|
+
whereStr: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Class for interacting with the ClickHouse database. It extends the
|
|
40
|
+
* VectorStore class and provides methods for adding vectors and
|
|
41
|
+
* documents, searching for similar vectors, and creating instances from
|
|
42
|
+
* texts or documents.
|
|
43
|
+
*/
|
|
44
|
+
export declare class ClickHouseStore extends VectorStore {
|
|
45
|
+
FilterType: ClickHouseFilter;
|
|
46
|
+
private client;
|
|
47
|
+
private indexType;
|
|
48
|
+
private indexParam;
|
|
49
|
+
private indexQueryParams;
|
|
50
|
+
private columnMap;
|
|
51
|
+
private database;
|
|
52
|
+
private table;
|
|
53
|
+
private isInitialized;
|
|
54
|
+
_vectorstoreType(): string;
|
|
55
|
+
constructor(embeddings: Embeddings, args: ClickHouseLibArgs);
|
|
56
|
+
/**
|
|
57
|
+
* Method to add vectors to the ClickHouse database.
|
|
58
|
+
* @param vectors The vectors to add.
|
|
59
|
+
* @param documents The documents associated with the vectors.
|
|
60
|
+
* @returns Promise that resolves when the vectors have been added.
|
|
61
|
+
*/
|
|
62
|
+
addVectors(vectors: number[][], documents: Document[]): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Method to add documents to the ClickHouse database.
|
|
65
|
+
* @param documents The documents to add.
|
|
66
|
+
* @returns Promise that resolves when the documents have been added.
|
|
67
|
+
*/
|
|
68
|
+
addDocuments(documents: Document[]): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Method to search for vectors that are similar to a given query vector.
|
|
71
|
+
* @param query The query vector.
|
|
72
|
+
* @param k The number of similar vectors to return.
|
|
73
|
+
* @param filter Optional filter for the search results.
|
|
74
|
+
* @returns Promise that resolves with an array of tuples, each containing a Document and a score.
|
|
75
|
+
*/
|
|
76
|
+
similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[Document, number][]>;
|
|
77
|
+
/**
|
|
78
|
+
* Static method to create an instance of ClickHouseStore from texts.
|
|
79
|
+
* @param texts The texts to use.
|
|
80
|
+
* @param metadatas The metadata associated with the texts.
|
|
81
|
+
* @param embeddings The embeddings to use.
|
|
82
|
+
* @param args The arguments for the ClickHouseStore.
|
|
83
|
+
* @returns Promise that resolves with a new instance of ClickHouseStore.
|
|
84
|
+
*/
|
|
85
|
+
static fromTexts(texts: string[], metadatas: object | object[], embeddings: Embeddings, args: ClickHouseLibArgs): Promise<ClickHouseStore>;
|
|
86
|
+
/**
|
|
87
|
+
* Static method to create an instance of ClickHouseStore from documents.
|
|
88
|
+
* @param docs The documents to use.
|
|
89
|
+
* @param embeddings The embeddings to use.
|
|
90
|
+
* @param args The arguments for the ClickHouseStore.
|
|
91
|
+
* @returns Promise that resolves with a new instance of ClickHouseStore.
|
|
92
|
+
*/
|
|
93
|
+
static fromDocuments(docs: Document[], embeddings: Embeddings, args: ClickHouseLibArgs): Promise<ClickHouseStore>;
|
|
94
|
+
/**
|
|
95
|
+
* Static method to create an instance of ClickHouseStore from an existing
|
|
96
|
+
* index.
|
|
97
|
+
* @param embeddings The embeddings to use.
|
|
98
|
+
* @param args The arguments for the ClickHouseStore.
|
|
99
|
+
* @returns Promise that resolves with a new instance of ClickHouseStore.
|
|
100
|
+
*/
|
|
101
|
+
static fromExistingIndex(embeddings: Embeddings, args: ClickHouseLibArgs): Promise<ClickHouseStore>;
|
|
102
|
+
/**
|
|
103
|
+
* Method to initialize the ClickHouse database.
|
|
104
|
+
* @param dimension Optional dimension of the vectors.
|
|
105
|
+
* @returns Promise that resolves when the database has been initialized.
|
|
106
|
+
*/
|
|
107
|
+
private initialize;
|
|
108
|
+
/**
|
|
109
|
+
* Method to build an SQL query for inserting vectors and documents into
|
|
110
|
+
* the ClickHouse database.
|
|
111
|
+
* @param vectors The vectors to insert.
|
|
112
|
+
* @param documents The documents to insert.
|
|
113
|
+
* @returns The SQL query string.
|
|
114
|
+
*/
|
|
115
|
+
private buildInsertQuery;
|
|
116
|
+
private escapeString;
|
|
117
|
+
/**
|
|
118
|
+
* Method to build an SQL query for searching for similar vectors in the
|
|
119
|
+
* ClickHouse database.
|
|
120
|
+
* @param query The query vector.
|
|
121
|
+
* @param k The number of similar vectors to return.
|
|
122
|
+
* @param filter Optional filter for the search results.
|
|
123
|
+
* @returns The SQL query string.
|
|
124
|
+
*/
|
|
125
|
+
private buildSearchQuery;
|
|
126
|
+
}
|