@langchain/neo4j 0.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/LICENSE +21 -0
- package/README.md +281 -0
- package/dist/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/chains/graph_qa/cypher.cjs +117 -0
- package/dist/chains/graph_qa/cypher.cjs.map +1 -0
- package/dist/chains/graph_qa/cypher.d.cts +73 -0
- package/dist/chains/graph_qa/cypher.d.cts.map +1 -0
- package/dist/chains/graph_qa/cypher.d.ts +73 -0
- package/dist/chains/graph_qa/cypher.d.ts.map +1 -0
- package/dist/chains/graph_qa/cypher.js +115 -0
- package/dist/chains/graph_qa/cypher.js.map +1 -0
- package/dist/chains/graph_qa/index.cjs +2 -0
- package/dist/chains/graph_qa/index.d.ts +2 -0
- package/dist/chains/graph_qa/index.js +3 -0
- package/dist/chains/graph_qa/prompts.cjs +42 -0
- package/dist/chains/graph_qa/prompts.cjs.map +1 -0
- package/dist/chains/graph_qa/prompts.d.cts +14 -0
- package/dist/chains/graph_qa/prompts.d.cts.map +1 -0
- package/dist/chains/graph_qa/prompts.d.ts +14 -0
- package/dist/chains/graph_qa/prompts.d.ts.map +1 -0
- package/dist/chains/graph_qa/prompts.js +40 -0
- package/dist/chains/graph_qa/prompts.js.map +1 -0
- package/dist/graphs/document.cjs +63 -0
- package/dist/graphs/document.cjs.map +1 -0
- package/dist/graphs/document.d.cts +56 -0
- package/dist/graphs/document.d.cts.map +1 -0
- package/dist/graphs/document.d.ts +56 -0
- package/dist/graphs/document.d.ts.map +1 -0
- package/dist/graphs/document.js +60 -0
- package/dist/graphs/document.js.map +1 -0
- package/dist/graphs/index.cjs +3 -0
- package/dist/graphs/index.d.ts +3 -0
- package/dist/graphs/index.js +4 -0
- package/dist/graphs/memgraph_graph.cjs +74 -0
- package/dist/graphs/memgraph_graph.cjs.map +1 -0
- package/dist/graphs/memgraph_graph.d.cts +36 -0
- package/dist/graphs/memgraph_graph.d.cts.map +1 -0
- package/dist/graphs/memgraph_graph.d.ts +36 -0
- package/dist/graphs/memgraph_graph.d.ts.map +1 -0
- package/dist/graphs/memgraph_graph.js +74 -0
- package/dist/graphs/memgraph_graph.js.map +1 -0
- package/dist/graphs/neo4j_graph.cjs +429 -0
- package/dist/graphs/neo4j_graph.cjs.map +1 -0
- package/dist/graphs/neo4j_graph.d.cts +96 -0
- package/dist/graphs/neo4j_graph.d.cts.map +1 -0
- package/dist/graphs/neo4j_graph.d.ts +96 -0
- package/dist/graphs/neo4j_graph.d.ts.map +1 -0
- package/dist/graphs/neo4j_graph.js +425 -0
- package/dist/graphs/neo4j_graph.js.map +1 -0
- package/dist/index.cjs +28 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +12 -0
- package/dist/stores/message/index.cjs +1 -0
- package/dist/stores/message/index.d.ts +1 -0
- package/dist/stores/message/index.js +2 -0
- package/dist/stores/message/neo4j.cjs +114 -0
- package/dist/stores/message/neo4j.cjs.map +1 -0
- package/dist/stores/message/neo4j.d.cts +40 -0
- package/dist/stores/message/neo4j.d.cts.map +1 -0
- package/dist/stores/message/neo4j.d.ts +40 -0
- package/dist/stores/message/neo4j.d.ts.map +1 -0
- package/dist/stores/message/neo4j.js +112 -0
- package/dist/stores/message/neo4j.js.map +1 -0
- package/dist/vectorstores/index.cjs +1 -0
- package/dist/vectorstores/index.d.ts +1 -0
- package/dist/vectorstores/index.js +2 -0
- package/dist/vectorstores/neo4j_vector.cjs +622 -0
- package/dist/vectorstores/neo4j_vector.cjs.map +1 -0
- package/dist/vectorstores/neo4j_vector.d.cts +96 -0
- package/dist/vectorstores/neo4j_vector.d.cts.map +1 -0
- package/dist/vectorstores/neo4j_vector.d.ts +96 -0
- package/dist/vectorstores/neo4j_vector.d.ts.map +1 -0
- package/dist/vectorstores/neo4j_vector.js +616 -0
- package/dist/vectorstores/neo4j_vector.js.map +1 -0
- package/package.json +98 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const require_runtime = require("../../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
let neo4j_driver = require("neo4j-driver");
|
|
3
|
+
neo4j_driver = require_runtime.__toESM(neo4j_driver);
|
|
4
|
+
let uuid = require("uuid");
|
|
5
|
+
let _langchain_core_chat_history = require("@langchain/core/chat_history");
|
|
6
|
+
let _langchain_core_messages = require("@langchain/core/messages");
|
|
7
|
+
//#region src/stores/message/neo4j.ts
|
|
8
|
+
const defaultConfig = {
|
|
9
|
+
sessionNodeLabel: "ChatSession",
|
|
10
|
+
messageNodeLabel: "ChatMessage",
|
|
11
|
+
windowSize: 3
|
|
12
|
+
};
|
|
13
|
+
var Neo4jChatMessageHistory = class Neo4jChatMessageHistory extends _langchain_core_chat_history.BaseListChatMessageHistory {
|
|
14
|
+
lc_namespace = [
|
|
15
|
+
"langchain",
|
|
16
|
+
"stores",
|
|
17
|
+
"message",
|
|
18
|
+
"neo4j"
|
|
19
|
+
];
|
|
20
|
+
sessionId;
|
|
21
|
+
sessionNodeLabel;
|
|
22
|
+
messageNodeLabel;
|
|
23
|
+
windowSize;
|
|
24
|
+
driver;
|
|
25
|
+
constructor({ sessionId = (0, uuid.v4)(), sessionNodeLabel = defaultConfig.sessionNodeLabel, messageNodeLabel = defaultConfig.messageNodeLabel, url, username, password, windowSize = defaultConfig.windowSize }) {
|
|
26
|
+
super();
|
|
27
|
+
this.sessionId = sessionId;
|
|
28
|
+
this.sessionNodeLabel = sessionNodeLabel;
|
|
29
|
+
this.messageNodeLabel = messageNodeLabel;
|
|
30
|
+
this.windowSize = windowSize;
|
|
31
|
+
if (url && username && password) try {
|
|
32
|
+
this.driver = neo4j_driver.default.driver(url, neo4j_driver.auth.basic(username, password));
|
|
33
|
+
} catch (e) {
|
|
34
|
+
const error = e;
|
|
35
|
+
throw new Error(`Could not create a Neo4j driver instance. Please check the connection details.\nCause: ${error.message}`);
|
|
36
|
+
}
|
|
37
|
+
else throw new Error("Neo4j connection details not provided.");
|
|
38
|
+
}
|
|
39
|
+
static async initialize(props) {
|
|
40
|
+
const instance = new Neo4jChatMessageHistory(props);
|
|
41
|
+
try {
|
|
42
|
+
await instance.verifyConnectivity();
|
|
43
|
+
} catch (e) {
|
|
44
|
+
const error = e;
|
|
45
|
+
throw new Error(`Could not verify connection to the Neo4j database.\nCause: ${error.message}`);
|
|
46
|
+
}
|
|
47
|
+
return instance;
|
|
48
|
+
}
|
|
49
|
+
async verifyConnectivity() {
|
|
50
|
+
return await this.driver.getServerInfo();
|
|
51
|
+
}
|
|
52
|
+
async getMessages() {
|
|
53
|
+
const getMessagesCypherQuery = `
|
|
54
|
+
MERGE (chatSession:${this.sessionNodeLabel} {id: $sessionId})
|
|
55
|
+
WITH chatSession
|
|
56
|
+
MATCH (chatSession)-[:LAST_MESSAGE]->(lastMessage)
|
|
57
|
+
MATCH p=(lastMessage)<-[:NEXT*0..${this.windowSize * 2 - 1}]-()
|
|
58
|
+
WITH p, length(p) AS length
|
|
59
|
+
ORDER BY length DESC LIMIT 1
|
|
60
|
+
UNWIND reverse(nodes(p)) AS node
|
|
61
|
+
RETURN {data:{content: node.content}, type:node.type} AS result
|
|
62
|
+
`;
|
|
63
|
+
try {
|
|
64
|
+
const { records } = await this.driver.executeQuery(getMessagesCypherQuery, { sessionId: this.sessionId });
|
|
65
|
+
return (0, _langchain_core_messages.mapStoredMessagesToChatMessages)(records.map((record) => record.get("result")));
|
|
66
|
+
} catch (e) {
|
|
67
|
+
const error = e;
|
|
68
|
+
throw new Error(`Ohno! Couldn't get messages.\nCause: ${error.message}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async addMessage(message) {
|
|
72
|
+
const addMessageCypherQuery = `
|
|
73
|
+
MERGE (chatSession:${this.sessionNodeLabel} {id: $sessionId})
|
|
74
|
+
WITH chatSession
|
|
75
|
+
OPTIONAL MATCH (chatSession)-[lastMessageRel:LAST_MESSAGE]->(lastMessage)
|
|
76
|
+
CREATE (chatSession)-[:LAST_MESSAGE]->(newLastMessage:${this.messageNodeLabel})
|
|
77
|
+
SET newLastMessage += {type:$type, content:$content}
|
|
78
|
+
WITH newLastMessage, lastMessageRel, lastMessage
|
|
79
|
+
WHERE lastMessage IS NOT NULL
|
|
80
|
+
CREATE (lastMessage)-[:NEXT]->(newLastMessage)
|
|
81
|
+
DELETE lastMessageRel
|
|
82
|
+
`;
|
|
83
|
+
try {
|
|
84
|
+
await this.driver.executeQuery(addMessageCypherQuery, {
|
|
85
|
+
sessionId: this.sessionId,
|
|
86
|
+
type: message.getType(),
|
|
87
|
+
content: message.content
|
|
88
|
+
});
|
|
89
|
+
} catch (e) {
|
|
90
|
+
const error = e;
|
|
91
|
+
throw new Error(`Ohno! Couldn't add message.\nCause: ${error.message}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async clear() {
|
|
95
|
+
const clearMessagesCypherQuery = `
|
|
96
|
+
MATCH p=(chatSession:${this.sessionNodeLabel} {id: $sessionId})-[:LAST_MESSAGE]->(lastMessage)<-[:NEXT*0..]-()
|
|
97
|
+
UNWIND nodes(p) as node
|
|
98
|
+
DETACH DELETE node
|
|
99
|
+
`;
|
|
100
|
+
try {
|
|
101
|
+
await this.driver.executeQuery(clearMessagesCypherQuery, { sessionId: this.sessionId });
|
|
102
|
+
} catch (e) {
|
|
103
|
+
const error = e;
|
|
104
|
+
throw new Error(`Ohno! Couldn't clear chat history.\nCause: ${error.message}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async close() {
|
|
108
|
+
await this.driver.close();
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
//#endregion
|
|
112
|
+
exports.Neo4jChatMessageHistory = Neo4jChatMessageHistory;
|
|
113
|
+
|
|
114
|
+
//# sourceMappingURL=neo4j.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"neo4j.cjs","names":["BaseListChatMessageHistory","neo4j","auth"],"sources":["../../../src/stores/message/neo4j.ts"],"sourcesContent":["import neo4j, { Driver, Record, auth, type ServerInfo } from \"neo4j-driver\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport { BaseListChatMessageHistory } from \"@langchain/core/chat_history\";\nimport {\n BaseMessage,\n mapStoredMessagesToChatMessages,\n} from \"@langchain/core/messages\";\n\nexport type Neo4jChatMessageHistoryConfigInput = {\n sessionId?: string | number;\n sessionNodeLabel?: string;\n messageNodeLabel?: string;\n url: string;\n username: string;\n password: string;\n windowSize?: number;\n};\n\nconst defaultConfig = {\n sessionNodeLabel: \"ChatSession\",\n messageNodeLabel: \"ChatMessage\",\n windowSize: 3,\n};\n\nexport class Neo4jChatMessageHistory extends BaseListChatMessageHistory {\n lc_namespace: string[] = [\"langchain\", \"stores\", \"message\", \"neo4j\"];\n\n sessionId: string | number;\n\n sessionNodeLabel: string;\n\n messageNodeLabel: string;\n\n windowSize: number;\n\n private driver: Driver;\n\n constructor({\n sessionId = uuidv4(),\n sessionNodeLabel = defaultConfig.sessionNodeLabel,\n messageNodeLabel = defaultConfig.messageNodeLabel,\n url,\n username,\n password,\n windowSize = defaultConfig.windowSize,\n }: Neo4jChatMessageHistoryConfigInput) {\n super();\n\n this.sessionId = sessionId;\n this.sessionNodeLabel = sessionNodeLabel;\n this.messageNodeLabel = messageNodeLabel;\n this.windowSize = windowSize;\n\n if (url && username && password) {\n try {\n this.driver = neo4j.driver(url, auth.basic(username, password));\n } catch (e) {\n const error = e as Error;\n throw new Error(\n `Could not create a Neo4j driver instance. Please check the connection details.\\nCause: ${error.message}`\n );\n }\n } else {\n throw new Error(\"Neo4j connection details not provided.\");\n }\n }\n\n static async initialize(\n props: Neo4jChatMessageHistoryConfigInput\n ): Promise<Neo4jChatMessageHistory> {\n const instance = new Neo4jChatMessageHistory(props);\n\n try {\n await instance.verifyConnectivity();\n } catch (e) {\n const error = e as Error;\n throw new Error(\n `Could not verify connection to the Neo4j database.\\nCause: ${error.message}`\n );\n }\n\n return instance;\n }\n\n async verifyConnectivity(): Promise<ServerInfo> {\n const connectivity = await this.driver.getServerInfo();\n return connectivity;\n }\n\n async getMessages(): Promise<BaseMessage[]> {\n const getMessagesCypherQuery = `\n MERGE (chatSession:${this.sessionNodeLabel} {id: $sessionId})\n WITH chatSession\n MATCH (chatSession)-[:LAST_MESSAGE]->(lastMessage)\n MATCH p=(lastMessage)<-[:NEXT*0..${this.windowSize * 2 - 1}]-()\n WITH p, length(p) AS length\n ORDER BY length DESC LIMIT 1\n UNWIND reverse(nodes(p)) AS node\n RETURN {data:{content: node.content}, type:node.type} AS result\n `;\n\n try {\n const { records } = await this.driver.executeQuery(\n getMessagesCypherQuery,\n {\n sessionId: this.sessionId,\n }\n );\n const results = records.map((record: Record) => record.get(\"result\"));\n\n return mapStoredMessagesToChatMessages(results);\n } catch (e) {\n const error = e as Error;\n throw new Error(`Ohno! Couldn't get messages.\\nCause: ${error.message}`);\n }\n }\n\n async addMessage(message: BaseMessage): Promise<void> {\n const addMessageCypherQuery = `\n MERGE (chatSession:${this.sessionNodeLabel} {id: $sessionId})\n WITH chatSession\n OPTIONAL MATCH (chatSession)-[lastMessageRel:LAST_MESSAGE]->(lastMessage)\n CREATE (chatSession)-[:LAST_MESSAGE]->(newLastMessage:${this.messageNodeLabel})\n SET newLastMessage += {type:$type, content:$content}\n WITH newLastMessage, lastMessageRel, lastMessage\n WHERE lastMessage IS NOT NULL\n CREATE (lastMessage)-[:NEXT]->(newLastMessage)\n DELETE lastMessageRel\n `;\n\n try {\n await this.driver.executeQuery(addMessageCypherQuery, {\n sessionId: this.sessionId,\n type: message.getType(),\n content: message.content,\n });\n } catch (e) {\n const error = e as Error;\n throw new Error(`Ohno! Couldn't add message.\\nCause: ${error.message}`);\n }\n }\n\n async clear() {\n const clearMessagesCypherQuery = `\n MATCH p=(chatSession:${this.sessionNodeLabel} {id: $sessionId})-[:LAST_MESSAGE]->(lastMessage)<-[:NEXT*0..]-()\n UNWIND nodes(p) as node\n DETACH DELETE node\n `;\n\n try {\n await this.driver.executeQuery(clearMessagesCypherQuery, {\n sessionId: this.sessionId,\n });\n } catch (e) {\n const error = e as Error;\n throw new Error(\n `Ohno! Couldn't clear chat history.\\nCause: ${error.message}`\n );\n }\n }\n\n async close() {\n await this.driver.close();\n }\n}\n"],"mappings":";;;;;;;AAkBA,MAAM,gBAAgB;CACpB,kBAAkB;CAClB,kBAAkB;CAClB,YAAY;CACb;AAED,IAAa,0BAAb,MAAa,gCAAgCA,6BAAAA,2BAA2B;CACtE,eAAyB;EAAC;EAAa;EAAU;EAAW;EAAQ;CAEpE;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,EACV,aAAA,GAAA,KAAA,KAAoB,EACpB,mBAAmB,cAAc,kBACjC,mBAAmB,cAAc,kBACjC,KACA,UACA,UACA,aAAa,cAAc,cACU;AACrC,SAAO;AAEP,OAAK,YAAY;AACjB,OAAK,mBAAmB;AACxB,OAAK,mBAAmB;AACxB,OAAK,aAAa;AAElB,MAAI,OAAO,YAAY,SACrB,KAAI;AACF,QAAK,SAASC,aAAAA,QAAM,OAAO,KAAKC,aAAAA,KAAK,MAAM,UAAU,SAAS,CAAC;WACxD,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MACR,0FAA0F,MAAM,UACjG;;MAGH,OAAM,IAAI,MAAM,yCAAyC;;CAI7D,aAAa,WACX,OACkC;EAClC,MAAM,WAAW,IAAI,wBAAwB,MAAM;AAEnD,MAAI;AACF,SAAM,SAAS,oBAAoB;WAC5B,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MACR,8DAA8D,MAAM,UACrE;;AAGH,SAAO;;CAGT,MAAM,qBAA0C;AAE9C,SADqB,MAAM,KAAK,OAAO,eAAe;;CAIxD,MAAM,cAAsC;EAC1C,MAAM,yBAAyB;2BACR,KAAK,iBAAiB;;;yCAGR,KAAK,aAAa,IAAI,EAAE;;;;;;AAO7D,MAAI;GACF,MAAM,EAAE,YAAY,MAAM,KAAK,OAAO,aACpC,wBACA,EACE,WAAW,KAAK,WACjB,CACF;AAGD,WAAA,GAAA,yBAAA,iCAFgB,QAAQ,KAAK,WAAmB,OAAO,IAAI,SAAS,CAAC,CAEtB;WACxC,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MAAM,wCAAwC,MAAM,UAAU;;;CAI5E,MAAM,WAAW,SAAqC;EACpD,MAAM,wBAAwB;2BACP,KAAK,iBAAiB;;;8DAGa,KAAK,iBAAiB;;;;;;;AAQhF,MAAI;AACF,SAAM,KAAK,OAAO,aAAa,uBAAuB;IACpD,WAAW,KAAK;IAChB,MAAM,QAAQ,SAAS;IACvB,SAAS,QAAQ;IAClB,CAAC;WACK,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MAAM,uCAAuC,MAAM,UAAU;;;CAI3E,MAAM,QAAQ;EACZ,MAAM,2BAA2B;6BACR,KAAK,iBAAiB;;;;AAK/C,MAAI;AACF,SAAM,KAAK,OAAO,aAAa,0BAA0B,EACvD,WAAW,KAAK,WACjB,CAAC;WACK,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MACR,8CAA8C,MAAM,UACrD;;;CAIL,MAAM,QAAQ;AACZ,QAAM,KAAK,OAAO,OAAO"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ServerInfo } from "neo4j-driver";
|
|
2
|
+
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
|
|
3
|
+
import { BaseMessage } from "@langchain/core/messages";
|
|
4
|
+
|
|
5
|
+
//#region src/stores/message/neo4j.d.ts
|
|
6
|
+
type Neo4jChatMessageHistoryConfigInput = {
|
|
7
|
+
sessionId?: string | number;
|
|
8
|
+
sessionNodeLabel?: string;
|
|
9
|
+
messageNodeLabel?: string;
|
|
10
|
+
url: string;
|
|
11
|
+
username: string;
|
|
12
|
+
password: string;
|
|
13
|
+
windowSize?: number;
|
|
14
|
+
};
|
|
15
|
+
declare class Neo4jChatMessageHistory extends BaseListChatMessageHistory {
|
|
16
|
+
lc_namespace: string[];
|
|
17
|
+
sessionId: string | number;
|
|
18
|
+
sessionNodeLabel: string;
|
|
19
|
+
messageNodeLabel: string;
|
|
20
|
+
windowSize: number;
|
|
21
|
+
private driver;
|
|
22
|
+
constructor({
|
|
23
|
+
sessionId,
|
|
24
|
+
sessionNodeLabel,
|
|
25
|
+
messageNodeLabel,
|
|
26
|
+
url,
|
|
27
|
+
username,
|
|
28
|
+
password,
|
|
29
|
+
windowSize
|
|
30
|
+
}: Neo4jChatMessageHistoryConfigInput);
|
|
31
|
+
static initialize(props: Neo4jChatMessageHistoryConfigInput): Promise<Neo4jChatMessageHistory>;
|
|
32
|
+
verifyConnectivity(): Promise<ServerInfo>;
|
|
33
|
+
getMessages(): Promise<BaseMessage[]>;
|
|
34
|
+
addMessage(message: BaseMessage): Promise<void>;
|
|
35
|
+
clear(): Promise<void>;
|
|
36
|
+
close(): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { Neo4jChatMessageHistory, Neo4jChatMessageHistoryConfigInput };
|
|
40
|
+
//# sourceMappingURL=neo4j.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"neo4j.d.cts","names":[],"sources":["../../../src/stores/message/neo4j.ts"],"mappings":";;;;;KAQY,kCAAA;EACV,SAAA;EACA,gBAAA;EACA,gBAAA;EACA,GAAA;EACA,QAAA;EACA,QAAA;EACA,UAAA;AAAA;AAAA,cASW,uBAAA,SAAgC,0BAAA;EAC3C,YAAA;EAEA,SAAA;EAEA,gBAAA;EAEA,gBAAA;EAEA,UAAA;EAAA,QAEQ,MAAA;EAER,WAAA,CAAA;IACE,SAAA;IACA,gBAAA;IACA,gBAAA;IACA,GAAA;IACA,QAAA;IACA,QAAA;IACA;EAAA,GACC,kCAAA;EAAA,OAsBU,UAAA,CACX,KAAA,EAAO,kCAAA,GACN,OAAA,CAAQ,uBAAA;EAeL,kBAAA,CAAA,GAAsB,OAAA,CAAQ,UAAA;EAK9B,WAAA,CAAA,GAAe,OAAA,CAAQ,WAAA;EA4BvB,UAAA,CAAW,OAAA,EAAS,WAAA,GAAc,OAAA;EAyBlC,KAAA,CAAA,GAAK,OAAA;EAmBL,KAAA,CAAA,GAAK,OAAA;AAAA"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ServerInfo } from "neo4j-driver";
|
|
2
|
+
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
|
|
3
|
+
import { BaseMessage } from "@langchain/core/messages";
|
|
4
|
+
|
|
5
|
+
//#region src/stores/message/neo4j.d.ts
|
|
6
|
+
type Neo4jChatMessageHistoryConfigInput = {
|
|
7
|
+
sessionId?: string | number;
|
|
8
|
+
sessionNodeLabel?: string;
|
|
9
|
+
messageNodeLabel?: string;
|
|
10
|
+
url: string;
|
|
11
|
+
username: string;
|
|
12
|
+
password: string;
|
|
13
|
+
windowSize?: number;
|
|
14
|
+
};
|
|
15
|
+
declare class Neo4jChatMessageHistory extends BaseListChatMessageHistory {
|
|
16
|
+
lc_namespace: string[];
|
|
17
|
+
sessionId: string | number;
|
|
18
|
+
sessionNodeLabel: string;
|
|
19
|
+
messageNodeLabel: string;
|
|
20
|
+
windowSize: number;
|
|
21
|
+
private driver;
|
|
22
|
+
constructor({
|
|
23
|
+
sessionId,
|
|
24
|
+
sessionNodeLabel,
|
|
25
|
+
messageNodeLabel,
|
|
26
|
+
url,
|
|
27
|
+
username,
|
|
28
|
+
password,
|
|
29
|
+
windowSize
|
|
30
|
+
}: Neo4jChatMessageHistoryConfigInput);
|
|
31
|
+
static initialize(props: Neo4jChatMessageHistoryConfigInput): Promise<Neo4jChatMessageHistory>;
|
|
32
|
+
verifyConnectivity(): Promise<ServerInfo>;
|
|
33
|
+
getMessages(): Promise<BaseMessage[]>;
|
|
34
|
+
addMessage(message: BaseMessage): Promise<void>;
|
|
35
|
+
clear(): Promise<void>;
|
|
36
|
+
close(): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { Neo4jChatMessageHistory, Neo4jChatMessageHistoryConfigInput };
|
|
40
|
+
//# sourceMappingURL=neo4j.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"neo4j.d.ts","names":[],"sources":["../../../src/stores/message/neo4j.ts"],"mappings":";;;;;KAQY,kCAAA;EACV,SAAA;EACA,gBAAA;EACA,gBAAA;EACA,GAAA;EACA,QAAA;EACA,QAAA;EACA,UAAA;AAAA;AAAA,cASW,uBAAA,SAAgC,0BAAA;EAC3C,YAAA;EAEA,SAAA;EAEA,gBAAA;EAEA,gBAAA;EAEA,UAAA;EAAA,QAEQ,MAAA;EAER,WAAA,CAAA;IACE,SAAA;IACA,gBAAA;IACA,gBAAA;IACA,GAAA;IACA,QAAA;IACA,QAAA;IACA;EAAA,GACC,kCAAA;EAAA,OAsBU,UAAA,CACX,KAAA,EAAO,kCAAA,GACN,OAAA,CAAQ,uBAAA;EAeL,kBAAA,CAAA,GAAsB,OAAA,CAAQ,UAAA;EAK9B,WAAA,CAAA,GAAe,OAAA,CAAQ,WAAA;EA4BvB,UAAA,CAAW,OAAA,EAAS,WAAA,GAAc,OAAA;EAyBlC,KAAA,CAAA,GAAK,OAAA;EAmBL,KAAA,CAAA,GAAK,OAAA;AAAA"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import neo4j, { auth } from "neo4j-driver";
|
|
2
|
+
import { v4 } from "uuid";
|
|
3
|
+
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
|
|
4
|
+
import { mapStoredMessagesToChatMessages } from "@langchain/core/messages";
|
|
5
|
+
//#region src/stores/message/neo4j.ts
|
|
6
|
+
const defaultConfig = {
|
|
7
|
+
sessionNodeLabel: "ChatSession",
|
|
8
|
+
messageNodeLabel: "ChatMessage",
|
|
9
|
+
windowSize: 3
|
|
10
|
+
};
|
|
11
|
+
var Neo4jChatMessageHistory = class Neo4jChatMessageHistory extends BaseListChatMessageHistory {
|
|
12
|
+
lc_namespace = [
|
|
13
|
+
"langchain",
|
|
14
|
+
"stores",
|
|
15
|
+
"message",
|
|
16
|
+
"neo4j"
|
|
17
|
+
];
|
|
18
|
+
sessionId;
|
|
19
|
+
sessionNodeLabel;
|
|
20
|
+
messageNodeLabel;
|
|
21
|
+
windowSize;
|
|
22
|
+
driver;
|
|
23
|
+
constructor({ sessionId = v4(), sessionNodeLabel = defaultConfig.sessionNodeLabel, messageNodeLabel = defaultConfig.messageNodeLabel, url, username, password, windowSize = defaultConfig.windowSize }) {
|
|
24
|
+
super();
|
|
25
|
+
this.sessionId = sessionId;
|
|
26
|
+
this.sessionNodeLabel = sessionNodeLabel;
|
|
27
|
+
this.messageNodeLabel = messageNodeLabel;
|
|
28
|
+
this.windowSize = windowSize;
|
|
29
|
+
if (url && username && password) try {
|
|
30
|
+
this.driver = neo4j.driver(url, auth.basic(username, password));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
const error = e;
|
|
33
|
+
throw new Error(`Could not create a Neo4j driver instance. Please check the connection details.\nCause: ${error.message}`);
|
|
34
|
+
}
|
|
35
|
+
else throw new Error("Neo4j connection details not provided.");
|
|
36
|
+
}
|
|
37
|
+
static async initialize(props) {
|
|
38
|
+
const instance = new Neo4jChatMessageHistory(props);
|
|
39
|
+
try {
|
|
40
|
+
await instance.verifyConnectivity();
|
|
41
|
+
} catch (e) {
|
|
42
|
+
const error = e;
|
|
43
|
+
throw new Error(`Could not verify connection to the Neo4j database.\nCause: ${error.message}`);
|
|
44
|
+
}
|
|
45
|
+
return instance;
|
|
46
|
+
}
|
|
47
|
+
async verifyConnectivity() {
|
|
48
|
+
return await this.driver.getServerInfo();
|
|
49
|
+
}
|
|
50
|
+
async getMessages() {
|
|
51
|
+
const getMessagesCypherQuery = `
|
|
52
|
+
MERGE (chatSession:${this.sessionNodeLabel} {id: $sessionId})
|
|
53
|
+
WITH chatSession
|
|
54
|
+
MATCH (chatSession)-[:LAST_MESSAGE]->(lastMessage)
|
|
55
|
+
MATCH p=(lastMessage)<-[:NEXT*0..${this.windowSize * 2 - 1}]-()
|
|
56
|
+
WITH p, length(p) AS length
|
|
57
|
+
ORDER BY length DESC LIMIT 1
|
|
58
|
+
UNWIND reverse(nodes(p)) AS node
|
|
59
|
+
RETURN {data:{content: node.content}, type:node.type} AS result
|
|
60
|
+
`;
|
|
61
|
+
try {
|
|
62
|
+
const { records } = await this.driver.executeQuery(getMessagesCypherQuery, { sessionId: this.sessionId });
|
|
63
|
+
return mapStoredMessagesToChatMessages(records.map((record) => record.get("result")));
|
|
64
|
+
} catch (e) {
|
|
65
|
+
const error = e;
|
|
66
|
+
throw new Error(`Ohno! Couldn't get messages.\nCause: ${error.message}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async addMessage(message) {
|
|
70
|
+
const addMessageCypherQuery = `
|
|
71
|
+
MERGE (chatSession:${this.sessionNodeLabel} {id: $sessionId})
|
|
72
|
+
WITH chatSession
|
|
73
|
+
OPTIONAL MATCH (chatSession)-[lastMessageRel:LAST_MESSAGE]->(lastMessage)
|
|
74
|
+
CREATE (chatSession)-[:LAST_MESSAGE]->(newLastMessage:${this.messageNodeLabel})
|
|
75
|
+
SET newLastMessage += {type:$type, content:$content}
|
|
76
|
+
WITH newLastMessage, lastMessageRel, lastMessage
|
|
77
|
+
WHERE lastMessage IS NOT NULL
|
|
78
|
+
CREATE (lastMessage)-[:NEXT]->(newLastMessage)
|
|
79
|
+
DELETE lastMessageRel
|
|
80
|
+
`;
|
|
81
|
+
try {
|
|
82
|
+
await this.driver.executeQuery(addMessageCypherQuery, {
|
|
83
|
+
sessionId: this.sessionId,
|
|
84
|
+
type: message.getType(),
|
|
85
|
+
content: message.content
|
|
86
|
+
});
|
|
87
|
+
} catch (e) {
|
|
88
|
+
const error = e;
|
|
89
|
+
throw new Error(`Ohno! Couldn't add message.\nCause: ${error.message}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async clear() {
|
|
93
|
+
const clearMessagesCypherQuery = `
|
|
94
|
+
MATCH p=(chatSession:${this.sessionNodeLabel} {id: $sessionId})-[:LAST_MESSAGE]->(lastMessage)<-[:NEXT*0..]-()
|
|
95
|
+
UNWIND nodes(p) as node
|
|
96
|
+
DETACH DELETE node
|
|
97
|
+
`;
|
|
98
|
+
try {
|
|
99
|
+
await this.driver.executeQuery(clearMessagesCypherQuery, { sessionId: this.sessionId });
|
|
100
|
+
} catch (e) {
|
|
101
|
+
const error = e;
|
|
102
|
+
throw new Error(`Ohno! Couldn't clear chat history.\nCause: ${error.message}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async close() {
|
|
106
|
+
await this.driver.close();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
//#endregion
|
|
110
|
+
export { Neo4jChatMessageHistory };
|
|
111
|
+
|
|
112
|
+
//# sourceMappingURL=neo4j.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"neo4j.js","names":["uuidv4"],"sources":["../../../src/stores/message/neo4j.ts"],"sourcesContent":["import neo4j, { Driver, Record, auth, type ServerInfo } from \"neo4j-driver\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport { BaseListChatMessageHistory } from \"@langchain/core/chat_history\";\nimport {\n BaseMessage,\n mapStoredMessagesToChatMessages,\n} from \"@langchain/core/messages\";\n\nexport type Neo4jChatMessageHistoryConfigInput = {\n sessionId?: string | number;\n sessionNodeLabel?: string;\n messageNodeLabel?: string;\n url: string;\n username: string;\n password: string;\n windowSize?: number;\n};\n\nconst defaultConfig = {\n sessionNodeLabel: \"ChatSession\",\n messageNodeLabel: \"ChatMessage\",\n windowSize: 3,\n};\n\nexport class Neo4jChatMessageHistory extends BaseListChatMessageHistory {\n lc_namespace: string[] = [\"langchain\", \"stores\", \"message\", \"neo4j\"];\n\n sessionId: string | number;\n\n sessionNodeLabel: string;\n\n messageNodeLabel: string;\n\n windowSize: number;\n\n private driver: Driver;\n\n constructor({\n sessionId = uuidv4(),\n sessionNodeLabel = defaultConfig.sessionNodeLabel,\n messageNodeLabel = defaultConfig.messageNodeLabel,\n url,\n username,\n password,\n windowSize = defaultConfig.windowSize,\n }: Neo4jChatMessageHistoryConfigInput) {\n super();\n\n this.sessionId = sessionId;\n this.sessionNodeLabel = sessionNodeLabel;\n this.messageNodeLabel = messageNodeLabel;\n this.windowSize = windowSize;\n\n if (url && username && password) {\n try {\n this.driver = neo4j.driver(url, auth.basic(username, password));\n } catch (e) {\n const error = e as Error;\n throw new Error(\n `Could not create a Neo4j driver instance. Please check the connection details.\\nCause: ${error.message}`\n );\n }\n } else {\n throw new Error(\"Neo4j connection details not provided.\");\n }\n }\n\n static async initialize(\n props: Neo4jChatMessageHistoryConfigInput\n ): Promise<Neo4jChatMessageHistory> {\n const instance = new Neo4jChatMessageHistory(props);\n\n try {\n await instance.verifyConnectivity();\n } catch (e) {\n const error = e as Error;\n throw new Error(\n `Could not verify connection to the Neo4j database.\\nCause: ${error.message}`\n );\n }\n\n return instance;\n }\n\n async verifyConnectivity(): Promise<ServerInfo> {\n const connectivity = await this.driver.getServerInfo();\n return connectivity;\n }\n\n async getMessages(): Promise<BaseMessage[]> {\n const getMessagesCypherQuery = `\n MERGE (chatSession:${this.sessionNodeLabel} {id: $sessionId})\n WITH chatSession\n MATCH (chatSession)-[:LAST_MESSAGE]->(lastMessage)\n MATCH p=(lastMessage)<-[:NEXT*0..${this.windowSize * 2 - 1}]-()\n WITH p, length(p) AS length\n ORDER BY length DESC LIMIT 1\n UNWIND reverse(nodes(p)) AS node\n RETURN {data:{content: node.content}, type:node.type} AS result\n `;\n\n try {\n const { records } = await this.driver.executeQuery(\n getMessagesCypherQuery,\n {\n sessionId: this.sessionId,\n }\n );\n const results = records.map((record: Record) => record.get(\"result\"));\n\n return mapStoredMessagesToChatMessages(results);\n } catch (e) {\n const error = e as Error;\n throw new Error(`Ohno! Couldn't get messages.\\nCause: ${error.message}`);\n }\n }\n\n async addMessage(message: BaseMessage): Promise<void> {\n const addMessageCypherQuery = `\n MERGE (chatSession:${this.sessionNodeLabel} {id: $sessionId})\n WITH chatSession\n OPTIONAL MATCH (chatSession)-[lastMessageRel:LAST_MESSAGE]->(lastMessage)\n CREATE (chatSession)-[:LAST_MESSAGE]->(newLastMessage:${this.messageNodeLabel})\n SET newLastMessage += {type:$type, content:$content}\n WITH newLastMessage, lastMessageRel, lastMessage\n WHERE lastMessage IS NOT NULL\n CREATE (lastMessage)-[:NEXT]->(newLastMessage)\n DELETE lastMessageRel\n `;\n\n try {\n await this.driver.executeQuery(addMessageCypherQuery, {\n sessionId: this.sessionId,\n type: message.getType(),\n content: message.content,\n });\n } catch (e) {\n const error = e as Error;\n throw new Error(`Ohno! Couldn't add message.\\nCause: ${error.message}`);\n }\n }\n\n async clear() {\n const clearMessagesCypherQuery = `\n MATCH p=(chatSession:${this.sessionNodeLabel} {id: $sessionId})-[:LAST_MESSAGE]->(lastMessage)<-[:NEXT*0..]-()\n UNWIND nodes(p) as node\n DETACH DELETE node\n `;\n\n try {\n await this.driver.executeQuery(clearMessagesCypherQuery, {\n sessionId: this.sessionId,\n });\n } catch (e) {\n const error = e as Error;\n throw new Error(\n `Ohno! Couldn't clear chat history.\\nCause: ${error.message}`\n );\n }\n }\n\n async close() {\n await this.driver.close();\n }\n}\n"],"mappings":";;;;;AAkBA,MAAM,gBAAgB;CACpB,kBAAkB;CAClB,kBAAkB;CAClB,YAAY;CACb;AAED,IAAa,0BAAb,MAAa,gCAAgC,2BAA2B;CACtE,eAAyB;EAAC;EAAa;EAAU;EAAW;EAAQ;CAEpE;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,EACV,YAAYA,IAAQ,EACpB,mBAAmB,cAAc,kBACjC,mBAAmB,cAAc,kBACjC,KACA,UACA,UACA,aAAa,cAAc,cACU;AACrC,SAAO;AAEP,OAAK,YAAY;AACjB,OAAK,mBAAmB;AACxB,OAAK,mBAAmB;AACxB,OAAK,aAAa;AAElB,MAAI,OAAO,YAAY,SACrB,KAAI;AACF,QAAK,SAAS,MAAM,OAAO,KAAK,KAAK,MAAM,UAAU,SAAS,CAAC;WACxD,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MACR,0FAA0F,MAAM,UACjG;;MAGH,OAAM,IAAI,MAAM,yCAAyC;;CAI7D,aAAa,WACX,OACkC;EAClC,MAAM,WAAW,IAAI,wBAAwB,MAAM;AAEnD,MAAI;AACF,SAAM,SAAS,oBAAoB;WAC5B,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MACR,8DAA8D,MAAM,UACrE;;AAGH,SAAO;;CAGT,MAAM,qBAA0C;AAE9C,SADqB,MAAM,KAAK,OAAO,eAAe;;CAIxD,MAAM,cAAsC;EAC1C,MAAM,yBAAyB;2BACR,KAAK,iBAAiB;;;yCAGR,KAAK,aAAa,IAAI,EAAE;;;;;;AAO7D,MAAI;GACF,MAAM,EAAE,YAAY,MAAM,KAAK,OAAO,aACpC,wBACA,EACE,WAAW,KAAK,WACjB,CACF;AAGD,UAAO,gCAFS,QAAQ,KAAK,WAAmB,OAAO,IAAI,SAAS,CAAC,CAEtB;WACxC,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MAAM,wCAAwC,MAAM,UAAU;;;CAI5E,MAAM,WAAW,SAAqC;EACpD,MAAM,wBAAwB;2BACP,KAAK,iBAAiB;;;8DAGa,KAAK,iBAAiB;;;;;;;AAQhF,MAAI;AACF,SAAM,KAAK,OAAO,aAAa,uBAAuB;IACpD,WAAW,KAAK;IAChB,MAAM,QAAQ,SAAS;IACvB,SAAS,QAAQ;IAClB,CAAC;WACK,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MAAM,uCAAuC,MAAM,UAAU;;;CAI3E,MAAM,QAAQ;EACZ,MAAM,2BAA2B;6BACR,KAAK,iBAAiB;;;;AAK/C,MAAI;AACF,SAAM,KAAK,OAAO,aAAa,0BAA0B,EACvD,WAAW,KAAK,WACjB,CAAC;WACK,GAAG;GACV,MAAM,QAAQ;AACd,SAAM,IAAI,MACR,8CAA8C,MAAM,UACrD;;;CAIL,MAAM,QAAQ;AACZ,QAAM,KAAK,OAAO,OAAO"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require("./neo4j_vector.cjs");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { DistanceStrategy, IndexType, Metadata, Neo4jVectorStore, Neo4jVectorStoreArgs, SearchType, constructMetadataFilter, isVersionLessThan, removeLuceneChars } from "./neo4j_vector.js";
|