-
Notifications
You must be signed in to change notification settings - Fork 582
/
Copy pathdata_insert.py
78 lines (69 loc) · 2.39 KB
/
data_insert.py
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
import os
from dotenv import load_dotenv
from nodes_relationships import links, nodes
from pymongo import MongoClient
def build_lookup_map():
quick_lookup = {}
for key in links.keys():
relationship = links[key]
source_node = relationship.source
lookup_key = str(source_node.id) + ":" + str(source_node.type)
lookup_content = quick_lookup.get(lookup_key, "empty")
if lookup_content != "empty":
quick_lookup.get(lookup_key).append(relationship)
else:
quick_lookup[lookup_key] = [relationship]
return quick_lookup
def create_mongo_documents():
mongo_documents = []
quick_lookup = build_lookup_map()
for key in nodes.keys():
node = nodes[key]
id = str(node.id) + ":" + str(node.type)
type = node.type
rel = quick_lookup.get(id, None)
relationships = set()
targets = {}
if rel is not None:
for relationship in rel:
target_id = (
str(relationship.target.id) + ":" + str(relationship.target.type)
)
relationships.add(target_id)
target_type = targets.get(target_id, None)
if target_type is not None:
targets[target_id].append(relationship.type)
else:
targets[target_id] = [relationship.type]
mongo_documents.append(
{
"_id": id,
"type": type,
"relationships": list(relationships),
"targets": targets,
}
)
else:
mongo_documents.append(
{"_id": id, "type": type, "relationships": [], "targets": {}}
)
return mongo_documents
def mongo_insert():
mongo_documents = create_mongo_documents()
try:
uri = os.getenv("ATLAS_CONNECTION_STRING")
print(uri)
client = MongoClient(uri, appname="devrel.showcase.graph_rag_app")
database = client["langchain_db"]
collection = database["nodes_relationships"]
for doc in mongo_documents:
collection.insert_one(doc)
except Exception as e:
print(e)
finally:
client.close()
if __name__ == "__main__":
load_dotenv()
print("Inserting Documents")
mongo_insert()
print("Successfully Inserted Documents")