-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
119 lines (95 loc) · 2.84 KB
/
index.js
File metadata and controls
119 lines (95 loc) · 2.84 KB
1
2
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
45
46
47
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
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
require('dotenv').config();
const { OpenAI } = require('openai');
const neo4j = require('neo4j-driver');
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const driver = neo4j.driver(
process.env.NEO4J_URI,
neo4j.auth.basic(process.env.NEO4J_USER, process.env.NEO4J_PASSWORD)
);
// Here goes your unstructured text
const inputText = ``;
const prompt = `
You are an assistant that extracts structured knowledge from my journal data.
Analyze the following text. Each paragraph represents a separate entry.
Extract the following in **strict JSON format** for graph database insertion:
- people
- emotions
- places
- entities
- relationships (with "source", "type", and "target" fields)
Each node must include:
- id (a unique string)
- name (human-readable label)
- type (e.g., "Person", "Place", "Emotion", etc.)
Each relationship(edge) must include:
- from (refers to a node's id)
- to (refers to another node's id)
- type (e.g., "FEELS", "LOCATED_IN", etc.)
Return a strict JSON object:
{
"nodes": [...],
"edges": [...]
}
Example:
Input:
"A cat and a dog. I am sad because I forgot to feed my dog."
Output:
{
"nodes": [
{"id": "1", "type": "Animal", "name": "cat"},
{"id": "2", "type": "Animal", "name": "dog"},
{"id": "3", "type": "Emotion", "name": "sadness"},
{"id": "4", "type": "Action", "name": "feeding"}
],
"edges": [
{"from": "3", "to": "1", "type": "feeling_towards"},
{"from": "3", "to": "2", "type": "feeling_towards"},
{"from": "4", "to": "2", "type": "performed_on"}
]
}
‼️ Rules:
- Output ONLY the JSON (no prose).
- All strings must use double quotes (").
- No trailing commas.
- Escape internal quotes correctly.
- Do not add comments or markdown.
- Ensure it's complete and parsable.
Here is the text: """${inputText}"""
`
async function extractGraph() {
const completion = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: prompt }],
temperature: 0
});
const graph = JSON.parse(completion.choices[0].message.content);
return graph;
}
async function insertIntoNeo4j({ nodes, edges }) {
const session = driver.session();
try {
for (const node of nodes) {
await session.run(
`MERGE (n:${node.label} {id: $id}) SET n.name = $name`,
{ id: node.id, name: node.name }
);
}
for (const edge of edges) {
const relType = edge.type.toUpperCase().replace(/\s+/g, "_");
const query = `
MATCH (a {id: $from}), (b {id: $to})
MERGE (a)-[r:${relType}]->(b)
`;
await session.run(query, { from: edge.from, to: edge.to });
}
} finally {
await session.close();
}
}
async function main() {
const graph = await extractGraph();
await insertIntoNeo4j(graph);
await driver.close();
console.log("Graph inserted! Open http://localhost:7474 to view it.");
}
main();