-
Notifications
You must be signed in to change notification settings - Fork 577
/
Copy pathdo_graphlookup.py
40 lines (36 loc) · 1.18 KB
/
do_graphlookup.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
import os
from pprint import pprint
from dotenv import load_dotenv
from pymongo import MongoClient
load_dotenv()
def graph_lookup(node_name, max_depth):
graph_lookup_docs = []
try:
uri = os.getenv("ATLAS_CONNECTION_STRING")
client = MongoClient(uri, appname="devrel.showcase.apps.graph_rag_demo")
database = client["langchain_db"]
collection = database["nodes_relationships"]
pipeline = [
{"$match": {"_id": node_name}},
{
"$graphLookup": {
"from": "nodes_relationships",
"startWith": "$_id",
"connectFromField": "relationships",
"connectToField": "_id",
"as": "relates_to",
"maxDepth": max_depth,
"depthField": "distance",
"restrictSearchWithMatch": {},
}
},
]
cursor = collection.aggregate(pipeline)
for doc in cursor:
graph_lookup_docs.append(doc)
except Exception as e:
print(e)
finally:
client.close()
pprint(graph_lookup_docs)
return graph_lookup_docs