@langchain/google-cloud-sql-pg 0.0.1 → 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.
- package/CHANGELOG.md +17 -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.cts.map +1 -0
- package/dist/chat_message_history.d.ts +53 -43
- package/dist/chat_message_history.d.ts.map +1 -0
- 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.cts.map +1 -0
- package/dist/engine.d.ts +102 -80
- package/dist/engine.d.ts.map +1 -0
- 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.cts.map +1 -0
- package/dist/indexes.d.ts +50 -47
- package/dist/indexes.d.ts.map +1 -0
- 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.cts.map +1 -0
- package/dist/loader.d.ts +40 -26
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +157 -237
- package/dist/loader.js.map +1 -0
- package/dist/utils/utils.cjs +35 -64
- 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 -586
- package/dist/vectorstore.cjs.map +1 -0
- package/dist/vectorstore.d.cts +300 -0
- package/dist/vectorstore.d.cts.map +1 -0
- package/dist/vectorstore.d.ts +147 -130
- package/dist/vectorstore.d.ts.map +1 -0
- package/dist/vectorstore.js +436 -581
- package/dist/vectorstore.js.map +1 -0
- package/package.json +39 -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
|
@@ -1,69 +1,46 @@
|
|
|
1
1
|
import { BaseChatMessageHistory } from "@langchain/core/chat_history";
|
|
2
|
-
import { AIMessage, HumanMessage, mapStoredMessagesToChatMessages
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
this.schemaName = schemaName;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Create a new PostgresChatMessageHistory instance.
|
|
48
|
-
*
|
|
49
|
-
* @param {PostgresEngine} engine Postgres engine instance to use.
|
|
50
|
-
* @param {string} sessionId Retrieve the table content with this session ID.
|
|
51
|
-
* @param {string} tableName Table name that stores that chat message history. Parameter is not escaped. Do not use with end user input.
|
|
52
|
-
* @param {string} schemaName Schema name for the chat message history table. Default: "public". Parameter is not escaped. Do not use with end user input.
|
|
53
|
-
* @returns PostgresChatMessageHistory instance.
|
|
54
|
-
*/
|
|
55
|
-
static async initialize(engine, sessionId, tableName, schemaName = "public") {
|
|
56
|
-
const query = `SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '${tableName}' AND table_schema = '${schemaName}'`;
|
|
57
|
-
const { rows } = await engine.pool.raw(query);
|
|
58
|
-
const columnNames = [];
|
|
59
|
-
for (const index in rows) {
|
|
60
|
-
if (Object.prototype.hasOwnProperty.call(rows, index)) {
|
|
61
|
-
columnNames.push(rows[index].column_name);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const requiredColumns = ["id", "session_id", "data", "type"];
|
|
65
|
-
if (!requiredColumns.every((x) => columnNames.includes(x))) {
|
|
66
|
-
throw new Error(`Table '${schemaName}'.'${tableName}' has incorrect schema.
|
|
2
|
+
import { AIMessage, HumanMessage, mapStoredMessagesToChatMessages } from "@langchain/core/messages";
|
|
3
|
+
|
|
4
|
+
//#region src/chat_message_history.ts
|
|
5
|
+
var PostgresChatMessageHistory = class PostgresChatMessageHistory extends BaseChatMessageHistory {
|
|
6
|
+
lc_namespace = [
|
|
7
|
+
"langchain",
|
|
8
|
+
"stores",
|
|
9
|
+
"message",
|
|
10
|
+
"google-cloud-sql-pg"
|
|
11
|
+
];
|
|
12
|
+
engine;
|
|
13
|
+
sessionId;
|
|
14
|
+
tableName;
|
|
15
|
+
schemaName;
|
|
16
|
+
constructor({ engine, sessionId, tableName, schemaName = "public" }) {
|
|
17
|
+
super();
|
|
18
|
+
this.engine = engine;
|
|
19
|
+
this.sessionId = sessionId;
|
|
20
|
+
this.tableName = tableName;
|
|
21
|
+
this.schemaName = schemaName;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create a new PostgresChatMessageHistory instance.
|
|
25
|
+
*
|
|
26
|
+
* @param {PostgresEngine} engine Postgres engine instance to use.
|
|
27
|
+
* @param {string} sessionId Retrieve the table content with this session ID.
|
|
28
|
+
* @param {string} tableName Table name that stores that chat message history. Parameter is not escaped. Do not use with end user input.
|
|
29
|
+
* @param {string} schemaName Schema name for the chat message history table. Default: "public". Parameter is not escaped. Do not use with end user input.
|
|
30
|
+
* @returns PostgresChatMessageHistory instance.
|
|
31
|
+
*/
|
|
32
|
+
static async initialize(engine, sessionId, tableName, schemaName = "public") {
|
|
33
|
+
const query = `SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '${tableName}' AND table_schema = '${schemaName}'`;
|
|
34
|
+
const { rows } = await engine.pool.raw(query);
|
|
35
|
+
const columnNames = [];
|
|
36
|
+
for (const index in rows) if (Object.prototype.hasOwnProperty.call(rows, index)) columnNames.push(rows[index].column_name);
|
|
37
|
+
const requiredColumns = [
|
|
38
|
+
"id",
|
|
39
|
+
"session_id",
|
|
40
|
+
"data",
|
|
41
|
+
"type"
|
|
42
|
+
];
|
|
43
|
+
if (!requiredColumns.every((x) => columnNames.includes(x))) throw new Error(`Table '${schemaName}'.'${tableName}' has incorrect schema.
|
|
67
44
|
Got column names ${columnNames} but required column names ${requiredColumns}.
|
|
68
45
|
Please create table with following schema: CREATE TABLE '${schemaName}'.'${tableName}' (
|
|
69
46
|
id SERIAL AUTO_INCREMENT PRIMARY KEY,
|
|
@@ -72,71 +49,64 @@ export class PostgresChatMessageHistory extends BaseChatMessageHistory {
|
|
|
72
49
|
type TEXT NOT NULL
|
|
73
50
|
);
|
|
74
51
|
`);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const query = `DELETE FROM "${this.schemaName}"."${this.tableName}" WHERE session_id = :session_id;`;
|
|
137
|
-
const values = {
|
|
138
|
-
session_id: this.sessionId,
|
|
139
|
-
};
|
|
140
|
-
await this.engine.pool.raw(query, values);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
52
|
+
return new PostgresChatMessageHistory({
|
|
53
|
+
engine,
|
|
54
|
+
sessionId,
|
|
55
|
+
tableName,
|
|
56
|
+
schemaName
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
addUserMessage(message) {
|
|
60
|
+
return this.addMessage(new HumanMessage(message));
|
|
61
|
+
}
|
|
62
|
+
addAIChatMessage(message) {
|
|
63
|
+
return this.addMessage(new AIMessage(message));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Returns a list of messages stored in the store.
|
|
67
|
+
*/
|
|
68
|
+
async getMessages() {
|
|
69
|
+
const query = `SELECT data, type FROM "${this.schemaName}"."${this.tableName}" WHERE session_id = :session_id ORDER BY id;`;
|
|
70
|
+
const values = { session_id: this.sessionId };
|
|
71
|
+
const items = [];
|
|
72
|
+
const { rows } = await this.engine.pool.raw(query, values);
|
|
73
|
+
if (rows.length === 0) return [];
|
|
74
|
+
for (const row of rows) items.push({
|
|
75
|
+
data: row.data.data,
|
|
76
|
+
type: row.type
|
|
77
|
+
});
|
|
78
|
+
return mapStoredMessagesToChatMessages(items);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Add a message object to the store.
|
|
82
|
+
* @param {BaseMessage} message Message to be added to the store
|
|
83
|
+
*/
|
|
84
|
+
async addMessage(message) {
|
|
85
|
+
const query = `INSERT INTO "${this.schemaName}"."${this.tableName}"("session_id", "data", "type") VALUES (:session_id, :data, :type)`;
|
|
86
|
+
const values = {
|
|
87
|
+
session_id: this.sessionId,
|
|
88
|
+
data: JSON.stringify(message.toDict()),
|
|
89
|
+
type: message.getType()
|
|
90
|
+
};
|
|
91
|
+
await this.engine.pool.raw(query, values);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Add a list of messages object to the store.
|
|
95
|
+
* @param {Array<BaseMessage>} messages List of messages to be added to the store
|
|
96
|
+
*/
|
|
97
|
+
async addMessages(messages) {
|
|
98
|
+
for (const msg of messages) await this.addMessage(msg);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Remove all messages from the store.
|
|
102
|
+
*/
|
|
103
|
+
async clear() {
|
|
104
|
+
const query = `DELETE FROM "${this.schemaName}"."${this.tableName}" WHERE session_id = :session_id;`;
|
|
105
|
+
const values = { session_id: this.sessionId };
|
|
106
|
+
await this.engine.pool.raw(query, values);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
//#endregion
|
|
111
|
+
export { PostgresChatMessageHistory };
|
|
112
|
+
//# sourceMappingURL=chat_message_history.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat_message_history.js","names":["engine: PostgresEngine","sessionId: string","tableName: string","schemaName: string","columnNames: string[]","message: string","values: { [key: string]: string }","items: StoredMessage[]","message: BaseMessage","messages: BaseMessage[]"],"sources":["../src/chat_message_history.ts"],"sourcesContent":["import { BaseChatMessageHistory } from \"@langchain/core/chat_history\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n StoredMessage,\n mapStoredMessagesToChatMessages,\n} from \"@langchain/core/messages\";\nimport PostgresEngine from \"./engine.js\";\n\nexport interface PostgresChatMessageHistoryInput {\n engine: PostgresEngine;\n sessionId: string;\n tableName: string;\n schemaName: string;\n}\n\nexport class PostgresChatMessageHistory extends BaseChatMessageHistory {\n lc_namespace: string[] = [\n \"langchain\",\n \"stores\",\n \"message\",\n \"google-cloud-sql-pg\",\n ];\n\n engine: PostgresEngine;\n\n sessionId: string;\n\n tableName: string;\n\n schemaName: string;\n\n constructor({\n engine,\n sessionId,\n tableName,\n schemaName = \"public\",\n }: PostgresChatMessageHistoryInput) {\n super();\n this.engine = engine;\n this.sessionId = sessionId;\n this.tableName = tableName;\n this.schemaName = schemaName;\n }\n\n /**\n * Create a new PostgresChatMessageHistory instance.\n *\n * @param {PostgresEngine} engine Postgres engine instance to use.\n * @param {string} sessionId Retrieve the table content with this session ID.\n * @param {string} tableName Table name that stores that chat message history. Parameter is not escaped. Do not use with end user input.\n * @param {string} schemaName Schema name for the chat message history table. Default: \"public\". Parameter is not escaped. Do not use with end user input.\n * @returns PostgresChatMessageHistory instance.\n */\n static async initialize(\n engine: PostgresEngine,\n sessionId: string,\n tableName: string,\n schemaName: string = \"public\"\n ) {\n const query = `SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '${tableName}' AND table_schema = '${schemaName}'`;\n const { rows } = await engine.pool.raw(query);\n const columnNames: string[] = [];\n\n for (const index in rows) {\n if (Object.prototype.hasOwnProperty.call(rows, index)) {\n columnNames.push(rows[index].column_name);\n }\n }\n\n const requiredColumns = [\"id\", \"session_id\", \"data\", \"type\"];\n\n if (!requiredColumns.every((x) => columnNames.includes(x))) {\n throw new Error(\n `Table '${schemaName}'.'${tableName}' has incorrect schema.\n Got column names ${columnNames} but required column names ${requiredColumns}.\n Please create table with following schema: CREATE TABLE '${schemaName}'.'${tableName}' (\n id SERIAL AUTO_INCREMENT PRIMARY KEY,\n session_id TEXT NOT NULL,\n data JSONB NOT NULL,\n type TEXT NOT NULL\n );\n `\n );\n }\n\n return new PostgresChatMessageHistory({\n engine,\n sessionId,\n tableName,\n schemaName,\n });\n }\n\n addUserMessage(message: string): Promise<void> {\n return this.addMessage(new HumanMessage(message));\n }\n\n addAIChatMessage(message: string): Promise<void> {\n return this.addMessage(new AIMessage(message));\n }\n\n /**\n * Returns a list of messages stored in the store.\n */\n async getMessages(): Promise<BaseMessage[]> {\n const query = `SELECT data, type FROM \"${this.schemaName}\".\"${this.tableName}\" WHERE session_id = :session_id ORDER BY id;`;\n const values: { [key: string]: string } = {\n session_id: this.sessionId,\n };\n const items: StoredMessage[] = [];\n const { rows } = await this.engine.pool.raw(query, values);\n\n if (rows.length === 0) {\n return [];\n }\n\n for (const row of rows) {\n items.push({\n data: row.data.data,\n type: row.type,\n });\n }\n\n return mapStoredMessagesToChatMessages(items);\n }\n\n /**\n * Add a message object to the store.\n * @param {BaseMessage} message Message to be added to the store\n */\n async addMessage(message: BaseMessage): Promise<void> {\n const query = `INSERT INTO \"${this.schemaName}\".\"${this.tableName}\"(\"session_id\", \"data\", \"type\") VALUES (:session_id, :data, :type)`;\n const values: { [key: string]: string } = {\n session_id: this.sessionId,\n data: JSON.stringify(message.toDict()),\n type: message.getType(),\n };\n\n await this.engine.pool.raw(query, values);\n }\n\n /**\n * Add a list of messages object to the store.\n * @param {Array<BaseMessage>} messages List of messages to be added to the store\n */\n async addMessages(messages: BaseMessage[]): Promise<void> {\n for (const msg of messages) {\n await this.addMessage(msg);\n }\n }\n\n /**\n * Remove all messages from the store.\n */\n async clear(): Promise<void> {\n const query = `DELETE FROM \"${this.schemaName}\".\"${this.tableName}\" WHERE session_id = :session_id;`;\n const values: { [key: string]: string } = {\n session_id: this.sessionId,\n };\n\n await this.engine.pool.raw(query, values);\n }\n}\n"],"mappings":";;;;AAiBA,IAAa,6BAAb,MAAa,mCAAmC,uBAAuB;CACrE,eAAyB;EACvB;EACA;EACA;EACA;CACD;CAED;CAEA;CAEA;CAEA;CAEA,YAAY,EACV,QACA,WACA,WACA,aAAa,UACmB,EAAE;EAClC,OAAO;EACP,KAAK,SAAS;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