@langchain/google-cloud-sql-pg 0.0.2 → 1.0.0

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