@langchain/google-cloud-sql-pg 0.0.2 → 1.0.1
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/CHANGELOG.md +26 -0
- package/LICENSE +6 -6
- package/README.md +1 -1
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/chat_message_history.cjs +104 -137
- package/dist/chat_message_history.cjs.map +1 -0
- package/dist/chat_message_history.d.cts +57 -0
- package/dist/chat_message_history.d.ts +53 -43
- package/dist/chat_message_history.js +103 -133
- package/dist/chat_message_history.js.map +1 -0
- package/dist/engine.cjs +188 -242
- package/dist/engine.cjs.map +1 -0
- package/dist/engine.d.cts +138 -0
- package/dist/engine.d.ts +102 -80
- package/dist/engine.js +186 -234
- package/dist/engine.js.map +1 -0
- package/dist/index.cjs +21 -20
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +7 -4
- package/dist/indexes.cjs +96 -168
- package/dist/indexes.cjs.map +1 -0
- package/dist/indexes.d.cts +68 -0
- package/dist/indexes.d.ts +50 -47
- package/dist/indexes.js +90 -161
- package/dist/indexes.js.map +1 -0
- package/dist/loader.cjs +159 -242
- package/dist/loader.cjs.map +1 -0
- package/dist/loader.d.cts +101 -0
- package/dist/loader.d.ts +40 -26
- package/dist/loader.js +157 -237
- package/dist/loader.js.map +1 -0
- package/dist/utils/utils.cjs +36 -65
- package/dist/utils/utils.cjs.map +1 -0
- package/dist/utils/utils.js +36 -62
- package/dist/utils/utils.js.map +1 -0
- package/dist/vectorstore.cjs +438 -593
- package/dist/vectorstore.cjs.map +1 -0
- package/dist/vectorstore.d.cts +300 -0
- package/dist/vectorstore.d.ts +147 -130
- package/dist/vectorstore.js +436 -588
- package/dist/vectorstore.js.map +1 -0
- package/package.json +41 -48
- package/dist/utils/utils.d.ts +0 -22
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/dist/engine.cjs
CHANGED
|
@@ -1,252 +1,198 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_utils = require('./utils/utils.cjs');
|
|
3
|
+
const __google_cloud_cloud_sql_connector = require_rolldown_runtime.__toESM(require("@google-cloud/cloud-sql-connector"));
|
|
4
|
+
const google_auth_library = require_rolldown_runtime.__toESM(require("google-auth-library"));
|
|
5
|
+
const knex = require_rolldown_runtime.__toESM(require("knex"));
|
|
6
|
+
|
|
7
|
+
//#region src/engine.ts
|
|
8
|
+
var Column = class {
|
|
9
|
+
name;
|
|
10
|
+
dataType;
|
|
11
|
+
nullable;
|
|
12
|
+
constructor(name, dataType, nullable = true) {
|
|
13
|
+
this.name = name;
|
|
14
|
+
this.dataType = dataType;
|
|
15
|
+
this.nullable = nullable;
|
|
16
|
+
this.postInitilization();
|
|
17
|
+
}
|
|
18
|
+
postInitilization() {
|
|
19
|
+
if (typeof this.name !== "string") throw Error("Column name must be type string");
|
|
20
|
+
if (typeof this.dataType !== "string") throw Error("Column data_type must be type string");
|
|
21
|
+
}
|
|
4
22
|
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.PostgresEngine = exports.Column = void 0;
|
|
7
|
-
const cloud_sql_connector_1 = require("@google-cloud/cloud-sql-connector");
|
|
8
|
-
const google_auth_library_1 = require("google-auth-library");
|
|
9
|
-
const knex_1 = __importDefault(require("knex"));
|
|
10
|
-
const utils_js_1 = require("./utils/utils.cjs");
|
|
11
|
-
class Column {
|
|
12
|
-
constructor(name, dataType, nullable = true) {
|
|
13
|
-
Object.defineProperty(this, "name", {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
configurable: true,
|
|
16
|
-
writable: true,
|
|
17
|
-
value: void 0
|
|
18
|
-
});
|
|
19
|
-
Object.defineProperty(this, "dataType", {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
configurable: true,
|
|
22
|
-
writable: true,
|
|
23
|
-
value: void 0
|
|
24
|
-
});
|
|
25
|
-
Object.defineProperty(this, "nullable", {
|
|
26
|
-
enumerable: true,
|
|
27
|
-
configurable: true,
|
|
28
|
-
writable: true,
|
|
29
|
-
value: void 0
|
|
30
|
-
});
|
|
31
|
-
this.name = name;
|
|
32
|
-
this.dataType = dataType;
|
|
33
|
-
this.nullable = nullable;
|
|
34
|
-
this.postInitilization();
|
|
35
|
-
}
|
|
36
|
-
postInitilization() {
|
|
37
|
-
if (typeof this.name !== "string") {
|
|
38
|
-
throw Error("Column name must be type string");
|
|
39
|
-
}
|
|
40
|
-
if (typeof this.dataType !== "string") {
|
|
41
|
-
throw Error("Column data_type must be type string");
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
exports.Column = Column;
|
|
46
23
|
const USER_AGENT = "langchain-google-cloud-sql-pg-js";
|
|
47
24
|
/**
|
|
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
|
-
class PostgresEngine {
|
|
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
|
-
* Create a table for saving of vectors to be used with PostgresVectorStore.
|
|
182
|
-
*
|
|
183
|
-
* @param tableName Postgres database table name. Parameter is not escaped. Do not use with end user input.
|
|
184
|
-
* @param vectorSize Vector size for the embedding model to be used.
|
|
185
|
-
* @param schemaName The schema name to store Postgres database table. Default: "public". Parameter is not escaped. Do not use with end user input.
|
|
186
|
-
* @param contentColumn Name of the column to store document content. Default: "content".
|
|
187
|
-
* @param embeddingColumn Name of the column to store vector embeddings. Default: "embedding".
|
|
188
|
-
* @param metadataColumns Optional - A list of Columns to create for custom metadata. Default: [].
|
|
189
|
-
* @param metadataJsonColumn Optional - The column to store extra metadata in JSON format. Default: "langchain_metadata".
|
|
190
|
-
* @param idColumn Optional - Column to store ids. Default: "langchain_id" column name with data type UUID.
|
|
191
|
-
* @param overwriteExisting Whether to drop existing table. Default: False.
|
|
192
|
-
* @param storeMetadata Whether to store metadata in the table. Default: True.
|
|
193
|
-
*/
|
|
194
|
-
async initVectorstoreTable(tableName, vectorSize, { schemaName = "public", contentColumn = "content", embeddingColumn = "embedding", metadataColumns = [], metadataJsonColumn = "langchain_metadata", idColumn = "langchain_id", overwriteExisting = false, storeMetadata = true, } = {}) {
|
|
195
|
-
await this.pool.raw("CREATE EXTENSION IF NOT EXISTS vector");
|
|
196
|
-
if (overwriteExisting) {
|
|
197
|
-
await this.pool.schema
|
|
198
|
-
.withSchema(schemaName)
|
|
199
|
-
.dropTableIfExists(tableName);
|
|
200
|
-
}
|
|
201
|
-
const idDataType = typeof idColumn === "string" ? "UUID" : idColumn.dataType;
|
|
202
|
-
const idColumnName = typeof idColumn === "string" ? idColumn : idColumn.name;
|
|
203
|
-
let query = `CREATE TABLE ${schemaName}.${tableName}(
|
|
25
|
+
* Cloud SQL shared connection pool
|
|
26
|
+
*
|
|
27
|
+
* Setup:
|
|
28
|
+
* Install `@langchain/google-cloud-sql-pg`
|
|
29
|
+
*
|
|
30
|
+
* <details open>
|
|
31
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { Column, PostgresEngine, PostgresEngineArgs } from "@langchain/google-cloud-sql-pg";
|
|
35
|
+
*
|
|
36
|
+
* const pgArgs: PostgresEngineArgs = {
|
|
37
|
+
* user: "db-user",
|
|
38
|
+
* password: "password"
|
|
39
|
+
*}
|
|
40
|
+
*
|
|
41
|
+
* const engine: PostgresEngine = await PostgresEngine.fromInstance(
|
|
42
|
+
* "project-id",
|
|
43
|
+
* "region",
|
|
44
|
+
* "instance-name",
|
|
45
|
+
* "database-name",
|
|
46
|
+
* pgArgs
|
|
47
|
+
* );
|
|
48
|
+
* ```
|
|
49
|
+
* </details>
|
|
50
|
+
*
|
|
51
|
+
* <br />
|
|
52
|
+
*
|
|
53
|
+
*/
|
|
54
|
+
var PostgresEngine = class PostgresEngine {
|
|
55
|
+
static _createKey = Symbol("key");
|
|
56
|
+
pool;
|
|
57
|
+
static connector;
|
|
58
|
+
constructor(key, pool) {
|
|
59
|
+
if (key !== PostgresEngine._createKey) throw Error("Only create class through 'create' method!");
|
|
60
|
+
this.pool = pool;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* @param projectId Required - GCP Project ID
|
|
64
|
+
* @param region Required - Postgres Instance Region
|
|
65
|
+
* @param instance Required - Postgres Instance name
|
|
66
|
+
* @param database Required - Database name
|
|
67
|
+
* @param ipType Optional - IP address type. Defaults to IPAddressType.PUBLIC
|
|
68
|
+
* @param user Optional - Postgres user name. Defaults to undefined
|
|
69
|
+
* @param password Optional - Postgres user password. Defaults to undefined
|
|
70
|
+
* @param iamAccountEmail Optional - IAM service account email. Defaults to undefined
|
|
71
|
+
* @returns PostgresEngine instance
|
|
72
|
+
*/
|
|
73
|
+
static async fromInstance(projectId, region, instance, database, { ipType = __google_cloud_cloud_sql_connector.IpAddressTypes.PUBLIC, user, password, iamAccountEmail } = {}) {
|
|
74
|
+
let dbUser;
|
|
75
|
+
let enableIAMAuth;
|
|
76
|
+
if (!user && password || user && !password) throw Error("Only one of 'user' or 'password' were specified. Either both should be specified to use basic user/password authentication or neither for IAM DB authentication.");
|
|
77
|
+
if (user !== void 0 && password !== void 0) {
|
|
78
|
+
enableIAMAuth = false;
|
|
79
|
+
dbUser = user;
|
|
80
|
+
} else {
|
|
81
|
+
enableIAMAuth = true;
|
|
82
|
+
if (iamAccountEmail !== void 0) dbUser = iamAccountEmail;
|
|
83
|
+
else {
|
|
84
|
+
const auth = new google_auth_library.GoogleAuth({ scopes: "https://www.googleapis.com/auth/cloud-platform" });
|
|
85
|
+
dbUser = await require_utils.getIAMPrincipalEmail(auth);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
PostgresEngine.connector = new __google_cloud_cloud_sql_connector.Connector({ userAgent: USER_AGENT });
|
|
89
|
+
const clientOpts = await PostgresEngine.connector.getOptions({
|
|
90
|
+
instanceConnectionName: `${projectId}:${region}:${instance}`,
|
|
91
|
+
ipType,
|
|
92
|
+
authType: enableIAMAuth ? __google_cloud_cloud_sql_connector.AuthTypes.IAM : __google_cloud_cloud_sql_connector.AuthTypes.PASSWORD
|
|
93
|
+
});
|
|
94
|
+
const dbConfig = {
|
|
95
|
+
client: "pg",
|
|
96
|
+
connection: {
|
|
97
|
+
...clientOpts,
|
|
98
|
+
...password ? { password } : {},
|
|
99
|
+
user: dbUser,
|
|
100
|
+
database
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const engine = (0, knex.default)(dbConfig);
|
|
104
|
+
return new PostgresEngine(PostgresEngine._createKey, engine);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create a PostgresEngine instance from an Knex instance.
|
|
108
|
+
*
|
|
109
|
+
* @param engine knex instance
|
|
110
|
+
* @returns PostgresEngine instance from a knex instance
|
|
111
|
+
*/
|
|
112
|
+
static async fromPool(engine) {
|
|
113
|
+
return new PostgresEngine(PostgresEngine._createKey, engine);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Create a PostgresEngine instance from arguments.
|
|
117
|
+
*
|
|
118
|
+
* @param url URL use to connect to a database
|
|
119
|
+
* @param poolConfig Optional - Configuration pool to use in the Knex configuration
|
|
120
|
+
* @returns PostgresEngine instance
|
|
121
|
+
*/
|
|
122
|
+
static async fromConnectionString(url, poolConfig) {
|
|
123
|
+
const driver = "postgresql+asyncpg";
|
|
124
|
+
if (typeof url === "string" && !url.startsWith(driver)) throw Error("Driver must be type 'postgresql+asyncpg'");
|
|
125
|
+
const dbConfig = {
|
|
126
|
+
client: "pg",
|
|
127
|
+
connection: url,
|
|
128
|
+
acquireConnectionTimeout: 1e6,
|
|
129
|
+
pool: {
|
|
130
|
+
...poolConfig,
|
|
131
|
+
acquireTimeoutMillis: 6e5
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const engine = (0, knex.default)(dbConfig);
|
|
135
|
+
return new PostgresEngine(PostgresEngine._createKey, engine);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Create a table for saving of vectors to be used with PostgresVectorStore.
|
|
139
|
+
*
|
|
140
|
+
* @param tableName Postgres database table name. Parameter is not escaped. Do not use with end user input.
|
|
141
|
+
* @param vectorSize Vector size for the embedding model to be used.
|
|
142
|
+
* @param schemaName The schema name to store Postgres database table. Default: "public". Parameter is not escaped. Do not use with end user input.
|
|
143
|
+
* @param contentColumn Name of the column to store document content. Default: "content".
|
|
144
|
+
* @param embeddingColumn Name of the column to store vector embeddings. Default: "embedding".
|
|
145
|
+
* @param embeddingColumnType Type of the embedding column ("vector" | "halfvec" | "bit" | "sparsevec"). Default: "vector". More info on HNSW-supported types: https://github.com/pgvector/pgvector#hnsw
|
|
146
|
+
* @param metadataColumns Optional - A list of Columns to create for custom metadata. Default: [].
|
|
147
|
+
* @param metadataJsonColumn Optional - The column to store extra metadata in JSON format. Default: "langchain_metadata".
|
|
148
|
+
* @param idColumn Optional - Column to store ids. Default: "langchain_id" column name with data type UUID.
|
|
149
|
+
* @param overwriteExisting Whether to drop existing table. Default: False.
|
|
150
|
+
* @param storeMetadata Whether to store metadata in the table. Default: True.
|
|
151
|
+
*/
|
|
152
|
+
async initVectorstoreTable(tableName, vectorSize, { schemaName = "public", contentColumn = "content", embeddingColumn = "embedding", embeddingColumnType = "vector", metadataColumns = [], metadataJsonColumn = "langchain_metadata", idColumn = "langchain_id", overwriteExisting = false, storeMetadata = true } = {}) {
|
|
153
|
+
await this.pool.raw("CREATE EXTENSION IF NOT EXISTS vector");
|
|
154
|
+
if (overwriteExisting) await this.pool.schema.withSchema(schemaName).dropTableIfExists(tableName);
|
|
155
|
+
const idDataType = typeof idColumn === "string" ? "UUID" : idColumn.dataType;
|
|
156
|
+
const idColumnName = typeof idColumn === "string" ? idColumn : idColumn.name;
|
|
157
|
+
let query = `CREATE TABLE ${schemaName}.${tableName}(
|
|
204
158
|
${idColumnName} ${idDataType} PRIMARY KEY,
|
|
205
159
|
${contentColumn} TEXT NOT NULL,
|
|
206
|
-
${embeddingColumn}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
async initChatHistoryTable(tableName, schemaName = "public") {
|
|
224
|
-
await this.pool.raw(`CREATE TABLE IF NOT EXISTS ${schemaName}.${tableName}(
|
|
160
|
+
${embeddingColumn} ${embeddingColumnType}(${vectorSize}) NOT NULL`;
|
|
161
|
+
for (const column of metadataColumns) {
|
|
162
|
+
const nullable = !column.nullable ? "NOT NULL" : "";
|
|
163
|
+
query += `,\n ${column.name} ${column.dataType} ${nullable}`;
|
|
164
|
+
}
|
|
165
|
+
if (storeMetadata) query += `,\n${metadataJsonColumn} JSON`;
|
|
166
|
+
query += `\n);`;
|
|
167
|
+
await this.pool.raw(query);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Create a Cloud SQL table to store chat history.
|
|
171
|
+
*
|
|
172
|
+
* @param tableName Table name to store chat history
|
|
173
|
+
* @param schemaName Schema name to store chat history table
|
|
174
|
+
*/
|
|
175
|
+
async initChatHistoryTable(tableName, schemaName = "public") {
|
|
176
|
+
await this.pool.raw(`CREATE TABLE IF NOT EXISTS ${schemaName}.${tableName}(
|
|
225
177
|
id SERIAL PRIMARY KEY,
|
|
226
178
|
session_id TEXT NOT NULL,
|
|
227
179
|
data JSONB NOT NULL,
|
|
228
180
|
type TEXT NOT NULL);`);
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Dispose of connection pool
|
|
184
|
+
*/
|
|
185
|
+
async closeConnection() {
|
|
186
|
+
await this.pool.destroy();
|
|
187
|
+
if (PostgresEngine.connector !== void 0) PostgresEngine.connector.close();
|
|
188
|
+
}
|
|
189
|
+
testConnection() {
|
|
190
|
+
const now = this.pool.raw("SELECT NOW() as currentTimestamp");
|
|
191
|
+
return now;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
//#endregion
|
|
196
|
+
exports.Column = Column;
|
|
245
197
|
exports.PostgresEngine = PostgresEngine;
|
|
246
|
-
|
|
247
|
-
enumerable: true,
|
|
248
|
-
configurable: true,
|
|
249
|
-
writable: true,
|
|
250
|
-
value: Symbol("key")
|
|
251
|
-
});
|
|
252
|
-
exports.default = PostgresEngine;
|
|
198
|
+
//# sourceMappingURL=engine.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.cjs","names":["name: string","dataType: string","nullable: boolean","key: symbol","pool: knex.Knex","projectId: string","region: string","instance: string","database: string","IpAddressTypes","dbUser: string","enableIAMAuth: boolean","GoogleAuth","getIAMPrincipalEmail","Connector","AuthTypes","dbConfig: knex.Knex.Config","engine: knex.Knex","url: string | knex.Knex.StaticConnectionConfig","poolConfig?: knex.Knex.PoolConfig","tableName: string","vectorSize: number","schemaName: string"],"sources":["../src/engine.ts"],"sourcesContent":["import {\n AuthTypes,\n Connector,\n IpAddressTypes,\n} from \"@google-cloud/cloud-sql-connector\";\nimport { GoogleAuth } from \"google-auth-library\";\nimport knex from \"knex\";\nimport { getIAMPrincipalEmail } from \"./utils/utils.js\";\n\nexport interface PostgresEngineArgs {\n ipType?: IpAddressTypes;\n user?: string;\n password?: string;\n iamAccountEmail?: string;\n}\n\nexport interface VectorStoreTableArgs {\n schemaName?: string;\n contentColumn?: string;\n embeddingColumn?: string;\n embeddingColumnType?: \"vector\" | \"halfvec\" | \"bit\" | \"sparsevec\";\n metadataColumns?: Column[];\n metadataJsonColumn?: string;\n idColumn?: string | Column;\n overwriteExisting?: boolean;\n storeMetadata?: boolean;\n}\n\nexport class Column {\n name: string;\n\n dataType: string;\n\n nullable: boolean;\n\n constructor(name: string, dataType: string, nullable: boolean = true) {\n this.name = name;\n this.dataType = dataType;\n this.nullable = nullable;\n\n this.postInitilization();\n }\n\n private postInitilization() {\n if (typeof this.name !== \"string\") {\n throw Error(\"Column name must be type string\");\n }\n\n if (typeof this.dataType !== \"string\") {\n throw Error(\"Column data_type must be type string\");\n }\n }\n}\n\nconst USER_AGENT = \"langchain-google-cloud-sql-pg-js\";\n\n/**\n * Cloud SQL shared connection pool\n *\n * Setup:\n * Install `@langchain/google-cloud-sql-pg`\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { Column, PostgresEngine, PostgresEngineArgs } from \"@langchain/google-cloud-sql-pg\";\n *\n * const pgArgs: PostgresEngineArgs = {\n * user: \"db-user\",\n * password: \"password\"\n *}\n *\n * const engine: PostgresEngine = await PostgresEngine.fromInstance(\n * \"project-id\",\n * \"region\",\n * \"instance-name\",\n * \"database-name\",\n * pgArgs\n * );\n * ```\n * </details>\n *\n * <br />\n *\n */\nexport class PostgresEngine {\n private static _createKey = Symbol(\"key\");\n\n pool: knex.Knex;\n\n static connector: Connector;\n\n constructor(key: symbol, pool: knex.Knex) {\n if (key !== PostgresEngine._createKey) {\n throw Error(\"Only create class through 'create' method!\");\n }\n this.pool = pool;\n }\n\n /**\n * @param projectId Required - GCP Project ID\n * @param region Required - Postgres Instance Region\n * @param instance Required - Postgres Instance name\n * @param database Required - Database name\n * @param ipType Optional - IP address type. Defaults to IPAddressType.PUBLIC\n * @param user Optional - Postgres user name. Defaults to undefined\n * @param password Optional - Postgres user password. Defaults to undefined\n * @param iamAccountEmail Optional - IAM service account email. Defaults to undefined\n * @returns PostgresEngine instance\n */\n\n static async fromInstance(\n projectId: string,\n region: string,\n instance: string,\n database: string,\n {\n ipType = IpAddressTypes.PUBLIC,\n user,\n password,\n iamAccountEmail,\n }: PostgresEngineArgs = {}\n ): Promise<PostgresEngine> {\n let dbUser: string;\n let enableIAMAuth: boolean;\n\n if ((!user && password) || (user && !password)) {\n // XOR for strings\n throw Error(\n \"Only one of 'user' or 'password' were specified. Either \" +\n \"both should be specified to use basic user/password \" +\n \"authentication or neither for IAM DB authentication.\"\n );\n }\n\n // User and password are given so we use the basic auth\n if (user !== undefined && password !== undefined) {\n enableIAMAuth = false;\n dbUser = user!;\n } else {\n enableIAMAuth = true;\n if (iamAccountEmail !== undefined) {\n dbUser = iamAccountEmail;\n } else {\n // Get application default credentials\n const auth = new GoogleAuth({\n scopes: \"https://www.googleapis.com/auth/cloud-platform\",\n });\n // dbUser should be the iam principal email by passing the credentials obtained\n dbUser = await getIAMPrincipalEmail(auth);\n }\n }\n\n PostgresEngine.connector = new Connector({ userAgent: USER_AGENT });\n const clientOpts = await PostgresEngine.connector.getOptions({\n instanceConnectionName: `${projectId}:${region}:${instance}`,\n ipType,\n authType: enableIAMAuth ? AuthTypes.IAM : AuthTypes.PASSWORD,\n });\n\n const dbConfig: knex.Knex.Config = {\n client: \"pg\",\n connection: {\n ...clientOpts,\n ...(password ? { password } : {}),\n user: dbUser,\n database,\n },\n };\n\n const engine = knex(dbConfig);\n\n return new PostgresEngine(PostgresEngine._createKey, engine);\n }\n\n /**\n * Create a PostgresEngine instance from an Knex instance.\n *\n * @param engine knex instance\n * @returns PostgresEngine instance from a knex instance\n */\n static async fromPool(engine: knex.Knex) {\n return new PostgresEngine(PostgresEngine._createKey, engine);\n }\n\n /**\n * Create a PostgresEngine instance from arguments.\n *\n * @param url URL use to connect to a database\n * @param poolConfig Optional - Configuration pool to use in the Knex configuration\n * @returns PostgresEngine instance\n */\n static async fromConnectionString(\n url: string | knex.Knex.StaticConnectionConfig,\n poolConfig?: knex.Knex.PoolConfig\n ) {\n const driver = \"postgresql+asyncpg\";\n\n if (typeof url === \"string\" && !url.startsWith(driver)) {\n throw Error(\"Driver must be type 'postgresql+asyncpg'\");\n }\n\n const dbConfig: knex.Knex.Config = {\n client: \"pg\",\n connection: url,\n acquireConnectionTimeout: 1000000,\n pool: {\n ...poolConfig,\n acquireTimeoutMillis: 600000,\n },\n };\n\n const engine = knex(dbConfig);\n\n return new PostgresEngine(PostgresEngine._createKey, engine);\n }\n\n /**\n * Create a table for saving of vectors to be used with PostgresVectorStore.\n *\n * @param tableName Postgres database table name. Parameter is not escaped. Do not use with end user input.\n * @param vectorSize Vector size for the embedding model to be used.\n * @param schemaName The schema name to store Postgres database table. Default: \"public\". Parameter is not escaped. Do not use with end user input.\n * @param contentColumn Name of the column to store document content. Default: \"content\".\n * @param embeddingColumn Name of the column to store vector embeddings. Default: \"embedding\".\n * @param embeddingColumnType Type of the embedding column (\"vector\" | \"halfvec\" | \"bit\" | \"sparsevec\"). Default: \"vector\". More info on HNSW-supported types: https://github.com/pgvector/pgvector#hnsw\n * @param metadataColumns Optional - A list of Columns to create for custom metadata. Default: [].\n * @param metadataJsonColumn Optional - The column to store extra metadata in JSON format. Default: \"langchain_metadata\".\n * @param idColumn Optional - Column to store ids. Default: \"langchain_id\" column name with data type UUID.\n * @param overwriteExisting Whether to drop existing table. Default: False.\n * @param storeMetadata Whether to store metadata in the table. Default: True.\n */\n async initVectorstoreTable(\n tableName: string,\n vectorSize: number,\n {\n schemaName = \"public\",\n contentColumn = \"content\",\n embeddingColumn = \"embedding\",\n embeddingColumnType = \"vector\",\n metadataColumns = [],\n metadataJsonColumn = \"langchain_metadata\",\n idColumn = \"langchain_id\",\n overwriteExisting = false,\n storeMetadata = true,\n }: VectorStoreTableArgs = {}\n ): Promise<void> {\n await this.pool.raw(\"CREATE EXTENSION IF NOT EXISTS vector\");\n\n if (overwriteExisting) {\n await this.pool.schema\n .withSchema(schemaName)\n .dropTableIfExists(tableName);\n }\n\n const idDataType =\n typeof idColumn === \"string\" ? \"UUID\" : idColumn.dataType;\n const idColumnName =\n typeof idColumn === \"string\" ? idColumn : idColumn.name;\n\n let query = `CREATE TABLE ${schemaName}.${tableName}(\n ${idColumnName} ${idDataType} PRIMARY KEY,\n ${contentColumn} TEXT NOT NULL,\n ${embeddingColumn} ${embeddingColumnType}(${vectorSize}) NOT NULL`;\n\n for (const column of metadataColumns) {\n const nullable = !column.nullable ? \"NOT NULL\" : \"\";\n query += `,\\n ${column.name} ${column.dataType} ${nullable}`;\n }\n\n if (storeMetadata) {\n query += `,\\n${metadataJsonColumn} JSON`;\n }\n\n query += `\\n);`;\n\n await this.pool.raw(query);\n }\n\n /**\n * Create a Cloud SQL table to store chat history.\n *\n * @param tableName Table name to store chat history\n * @param schemaName Schema name to store chat history table\n */\n\n async initChatHistoryTable(\n tableName: string,\n schemaName: string = \"public\"\n ): Promise<void> {\n await this.pool.raw(\n `CREATE TABLE IF NOT EXISTS ${schemaName}.${tableName}(\n id SERIAL PRIMARY KEY,\n session_id TEXT NOT NULL,\n data JSONB NOT NULL,\n type TEXT NOT NULL);`\n );\n }\n\n /**\n * Dispose of connection pool\n */\n async closeConnection(): Promise<void> {\n await this.pool.destroy();\n if (PostgresEngine.connector !== undefined) {\n PostgresEngine.connector.close();\n }\n }\n\n // Just to test the connection to the database\n testConnection() {\n const now = this.pool.raw(\"SELECT NOW() as currentTimestamp\");\n return now;\n }\n}\n\nexport default PostgresEngine;\n"],"mappings":";;;;;;;AA4BA,IAAa,SAAb,MAAoB;CAClB;CAEA;CAEA;CAEA,YAAYA,MAAcC,UAAkBC,WAAoB,MAAM;EACp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,MAAI,eACF,SAAS,CAAC,GAAG,EAAE,mBAAmB,KAAK,CAAC;EAG1C,SAAS,CAAC,IAAI,CAAC;EAEf,MAAM,KAAK,KAAK,IAAI,MAAM;CAC3B;;;;;;;CASD,MAAM,qBACJD,WACAE,aAAqB,UACN;EACf,MAAM,KAAK,KAAK,IACd,CAAC,2BAA2B,EAAE,WAAW,CAAC,EAAE,UAAU;;;;0BAIlC,CAAC,CACtB;CACF;;;;CAKD,MAAM,kBAAiC;EACrC,MAAM,KAAK,KAAK,SAAS;AACzB,MAAI,eAAe,cAAc,QAC/B,eAAe,UAAU,OAAO;CAEnC;CAGD,iBAAiB;EACf,MAAM,MAAM,KAAK,KAAK,IAAI,mCAAmC;AAC7D,SAAO;CACR;AACF"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Connector, IpAddressTypes } from "@google-cloud/cloud-sql-connector";
|
|
2
|
+
import knex from "knex";
|
|
3
|
+
|
|
4
|
+
//#region src/engine.d.ts
|
|
5
|
+
interface PostgresEngineArgs {
|
|
6
|
+
ipType?: IpAddressTypes;
|
|
7
|
+
user?: string;
|
|
8
|
+
password?: string;
|
|
9
|
+
iamAccountEmail?: string;
|
|
10
|
+
}
|
|
11
|
+
interface VectorStoreTableArgs {
|
|
12
|
+
schemaName?: string;
|
|
13
|
+
contentColumn?: string;
|
|
14
|
+
embeddingColumn?: string;
|
|
15
|
+
embeddingColumnType?: "vector" | "halfvec" | "bit" | "sparsevec";
|
|
16
|
+
metadataColumns?: Column[];
|
|
17
|
+
metadataJsonColumn?: string;
|
|
18
|
+
idColumn?: string | Column;
|
|
19
|
+
overwriteExisting?: boolean;
|
|
20
|
+
storeMetadata?: boolean;
|
|
21
|
+
}
|
|
22
|
+
declare class Column {
|
|
23
|
+
name: string;
|
|
24
|
+
dataType: string;
|
|
25
|
+
nullable: boolean;
|
|
26
|
+
constructor(name: string, dataType: string, nullable?: boolean);
|
|
27
|
+
private postInitilization;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Cloud SQL shared connection pool
|
|
31
|
+
*
|
|
32
|
+
* Setup:
|
|
33
|
+
* Install `@langchain/google-cloud-sql-pg`
|
|
34
|
+
*
|
|
35
|
+
* <details open>
|
|
36
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
37
|
+
*
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import { Column, PostgresEngine, PostgresEngineArgs } from "@langchain/google-cloud-sql-pg";
|
|
40
|
+
*
|
|
41
|
+
* const pgArgs: PostgresEngineArgs = {
|
|
42
|
+
* user: "db-user",
|
|
43
|
+
* password: "password"
|
|
44
|
+
*}
|
|
45
|
+
*
|
|
46
|
+
* const engine: PostgresEngine = await PostgresEngine.fromInstance(
|
|
47
|
+
* "project-id",
|
|
48
|
+
* "region",
|
|
49
|
+
* "instance-name",
|
|
50
|
+
* "database-name",
|
|
51
|
+
* pgArgs
|
|
52
|
+
* );
|
|
53
|
+
* ```
|
|
54
|
+
* </details>
|
|
55
|
+
*
|
|
56
|
+
* <br />
|
|
57
|
+
*
|
|
58
|
+
*/
|
|
59
|
+
declare class PostgresEngine {
|
|
60
|
+
private static _createKey;
|
|
61
|
+
pool: knex.Knex;
|
|
62
|
+
static connector: Connector;
|
|
63
|
+
constructor(key: symbol, pool: knex.Knex);
|
|
64
|
+
/**
|
|
65
|
+
* @param projectId Required - GCP Project ID
|
|
66
|
+
* @param region Required - Postgres Instance Region
|
|
67
|
+
* @param instance Required - Postgres Instance name
|
|
68
|
+
* @param database Required - Database name
|
|
69
|
+
* @param ipType Optional - IP address type. Defaults to IPAddressType.PUBLIC
|
|
70
|
+
* @param user Optional - Postgres user name. Defaults to undefined
|
|
71
|
+
* @param password Optional - Postgres user password. Defaults to undefined
|
|
72
|
+
* @param iamAccountEmail Optional - IAM service account email. Defaults to undefined
|
|
73
|
+
* @returns PostgresEngine instance
|
|
74
|
+
*/
|
|
75
|
+
static fromInstance(projectId: string, region: string, instance: string, database: string, {
|
|
76
|
+
ipType,
|
|
77
|
+
user,
|
|
78
|
+
password,
|
|
79
|
+
iamAccountEmail
|
|
80
|
+
}?: PostgresEngineArgs): Promise<PostgresEngine>;
|
|
81
|
+
/**
|
|
82
|
+
* Create a PostgresEngine instance from an Knex instance.
|
|
83
|
+
*
|
|
84
|
+
* @param engine knex instance
|
|
85
|
+
* @returns PostgresEngine instance from a knex instance
|
|
86
|
+
*/
|
|
87
|
+
static fromPool(engine: knex.Knex): Promise<PostgresEngine>;
|
|
88
|
+
/**
|
|
89
|
+
* Create a PostgresEngine instance from arguments.
|
|
90
|
+
*
|
|
91
|
+
* @param url URL use to connect to a database
|
|
92
|
+
* @param poolConfig Optional - Configuration pool to use in the Knex configuration
|
|
93
|
+
* @returns PostgresEngine instance
|
|
94
|
+
*/
|
|
95
|
+
static fromConnectionString(url: string | knex.Knex.StaticConnectionConfig, poolConfig?: knex.Knex.PoolConfig): Promise<PostgresEngine>;
|
|
96
|
+
/**
|
|
97
|
+
* Create a table for saving of vectors to be used with PostgresVectorStore.
|
|
98
|
+
*
|
|
99
|
+
* @param tableName Postgres database table name. Parameter is not escaped. Do not use with end user input.
|
|
100
|
+
* @param vectorSize Vector size for the embedding model to be used.
|
|
101
|
+
* @param schemaName The schema name to store Postgres database table. Default: "public". Parameter is not escaped. Do not use with end user input.
|
|
102
|
+
* @param contentColumn Name of the column to store document content. Default: "content".
|
|
103
|
+
* @param embeddingColumn Name of the column to store vector embeddings. Default: "embedding".
|
|
104
|
+
* @param embeddingColumnType Type of the embedding column ("vector" | "halfvec" | "bit" | "sparsevec"). Default: "vector". More info on HNSW-supported types: https://github.com/pgvector/pgvector#hnsw
|
|
105
|
+
* @param metadataColumns Optional - A list of Columns to create for custom metadata. Default: [].
|
|
106
|
+
* @param metadataJsonColumn Optional - The column to store extra metadata in JSON format. Default: "langchain_metadata".
|
|
107
|
+
* @param idColumn Optional - Column to store ids. Default: "langchain_id" column name with data type UUID.
|
|
108
|
+
* @param overwriteExisting Whether to drop existing table. Default: False.
|
|
109
|
+
* @param storeMetadata Whether to store metadata in the table. Default: True.
|
|
110
|
+
*/
|
|
111
|
+
initVectorstoreTable(tableName: string, vectorSize: number, {
|
|
112
|
+
schemaName,
|
|
113
|
+
contentColumn,
|
|
114
|
+
embeddingColumn,
|
|
115
|
+
embeddingColumnType,
|
|
116
|
+
metadataColumns,
|
|
117
|
+
metadataJsonColumn,
|
|
118
|
+
idColumn,
|
|
119
|
+
overwriteExisting,
|
|
120
|
+
storeMetadata
|
|
121
|
+
}?: VectorStoreTableArgs): Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Create a Cloud SQL table to store chat history.
|
|
124
|
+
*
|
|
125
|
+
* @param tableName Table name to store chat history
|
|
126
|
+
* @param schemaName Schema name to store chat history table
|
|
127
|
+
*/
|
|
128
|
+
initChatHistoryTable(tableName: string, schemaName?: string): Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Dispose of connection pool
|
|
131
|
+
*/
|
|
132
|
+
closeConnection(): Promise<void>;
|
|
133
|
+
// Just to test the connection to the database
|
|
134
|
+
testConnection(): knex.Knex.Raw<any>;
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
export { Column, PostgresEngine, PostgresEngineArgs, VectorStoreTableArgs };
|
|
138
|
+
//# sourceMappingURL=engine.d.cts.map
|