Skip to content

Commit

Permalink
Order.
Browse files Browse the repository at this point in the history
  • Loading branch information
antejavor committed Dec 4, 2024
1 parent 038d48a commit 6d169f9
Showing 1 changed file with 82 additions and 110 deletions.
192 changes: 82 additions & 110 deletions memgraph-graphRAG/graphRAG.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,46 @@
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate queries\n",
"\n",
"Now that GPT has provided us with the structured data for the nodes and\n",
"relationships, the next step is to generate the Cypher queries that we will use\n",
"to execute in Memgraph."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def generate_cypher_queries(nodes, relationships):\n",
" queries = []\n",
"\n",
" # Create nodes\n",
" for node in nodes:\n",
" query = f\"\"\"\n",
" MERGE (n:{node['type']}:Entity {{name: '{node['name']}'}}) \n",
" ON CREATE SET n.id={node['id']} \n",
" ON MATCH SET n.id={node['id']}\n",
" \"\"\"\n",
" queries.append(query)\n",
"\n",
" # Create relationships\n",
" for rel in relationships:\n",
" query = f\"MATCH (a {{id: {rel['source']}}}), (b {{id: {rel['target']}}}) \" \\\n",
" f\"CREATE (a)-[:{rel['relationship']}]->(b)\"\n",
" queries.append(query)\n",
"\n",
" return queries\n",
"\n",
"cypher_queries = generate_cypher_queries(nodes, relationships)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -864,63 +904,60 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error executing query: \n",
"Executed query: \n",
" MERGE (n:Character:Entity {name: 'Viserys Targaryen'}) \n",
" ON CREATE SET n.id=n1 \n",
" ON MATCH SET n.id=n1\n",
" . Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n1.}\n",
"Error executing query: \n",
" ON CREATE SET n.id=1 \n",
" ON MATCH SET n.id=1\n",
" \n",
"Executed query: \n",
" MERGE (n:Character:Entity {name: 'Aerys II Targaryen'}) \n",
" ON CREATE SET n.id=n2 \n",
" ON MATCH SET n.id=n2\n",
" . Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n2.}\n",
"Error executing query: \n",
" MERGE (n:Title:Entity {name: 'Targaryen'}) \n",
" ON CREATE SET n.id=n3 \n",
" ON MATCH SET n.id=n3\n",
" . Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n3.}\n",
"Error executing query: \n",
" MERGE (n:Title:Entity {name: 'the Iron Throne'}) \n",
" ON CREATE SET n.id=n4 \n",
" ON MATCH SET n.id=n4\n",
" . Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n4.}\n",
"Error executing query: \n",
" ON CREATE SET n.id=2 \n",
" ON MATCH SET n.id=2\n",
" \n",
"Executed query: \n",
" MERGE (n:House:Entity {name: 'Targaryen'}) \n",
" ON CREATE SET n.id=3 \n",
" ON MATCH SET n.id=3\n",
" \n",
"Executed query: \n",
" MERGE (n:Title:Entity {name: 'Iron Throne'}) \n",
" ON CREATE SET n.id=4 \n",
" ON MATCH SET n.id=4\n",
" \n",
"Executed query: \n",
" MERGE (n:Location:Entity {name: 'Westeros'}) \n",
" ON CREATE SET n.id=n5 \n",
" ON MATCH SET n.id=n5\n",
" . Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n5.}\n",
"Error executing query: \n",
" ON CREATE SET n.id=5 \n",
" ON MATCH SET n.id=5\n",
" \n",
"Executed query: \n",
" MERGE (n:Character:Entity {name: 'Daenerys Targaryen'}) \n",
" ON CREATE SET n.id=n6 \n",
" ON MATCH SET n.id=n6\n",
" . Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n6.}\n",
"Error executing query: \n",
" ON CREATE SET n.id=6 \n",
" ON MATCH SET n.id=6\n",
" \n",
"Executed query: \n",
" MERGE (n:Character:Entity {name: 'Khal Drogo'}) \n",
" ON CREATE SET n.id=n7 \n",
" ON MATCH SET n.id=n7\n",
" . Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n7.}\n",
"Error executing query: \n",
" ON CREATE SET n.id=7 \n",
" ON MATCH SET n.id=7\n",
" \n",
"Executed query: \n",
" MERGE (n:House:Entity {name: 'Dothraki'}) \n",
" ON CREATE SET n.id=n8 \n",
" ON MATCH SET n.id=n8\n",
" . Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n8.}\n",
"Error executing query: MATCH (a {id: n1}), (b {id: n2}) CREATE (a)-[:PARENT_OF]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n1.}\n",
"Error executing query: MATCH (a {id: n1}), (b {id: n3}) CREATE (a)-[:SIBLING_OF]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n1.}\n",
"Error executing query: MATCH (a {id: n1}), (b {id: n4}) CREATE (a)-[:HAPPENED_IN]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n1.}\n",
"Error executing query: MATCH (a {id: n1}), (b {id: n5}) CREATE (a)-[:RULES]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n1.}\n",
"Error executing query: MATCH (a {id: n1}), (b {id: n6}) CREATE (a)-[:SIBLING_OF]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n1.}\n",
"Error executing query: MATCH (a {id: n6}), (b {id: n7}) CREATE (a)-[:MARRIED_TO]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n6.}\n",
"Error executing query: MATCH (a {id: n7}), (b {id: n8}) CREATE (a)-[:LOYAL_TO]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n7.}\n",
"Error executing query: MATCH (a {id: n7}), (b {id: n8}) CREATE (a)-[:RULES]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n7.}\n",
"Error executing query: MATCH (a {id: n7}), (b {id: n1}) CREATE (a)-[:BETRAYED_BY]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n7.}\n",
"Error executing query: MATCH (a {id: n7}), (b {id: n1}) CREATE (a)-[:KILLED]->(b). Error: {code: Memgraph.ClientError.MemgraphError.MemgraphError} {message: Unbound variable: n7.}\n"
" ON CREATE SET n.id=8 \n",
" ON MATCH SET n.id=8\n",
" \n",
"Executed query: MATCH (a {id: 1}), (b {id: 2}) CREATE (a)-[:PARENT_OF]->(b)\n",
"Executed query: MATCH (a {id: 1}), (b {id: 4}) CREATE (a)-[:RULES]->(b)\n",
"Executed query: MATCH (a {id: 1}), (b {id: 5}) CREATE (a)-[:RULES]->(b)\n",
"Executed query: MATCH (a {id: 1}), (b {id: 6}) CREATE (a)-[:SIBLING_OF]->(b)\n",
"Executed query: MATCH (a {id: 1}), (b {id: 7}) CREATE (a)-[:MARRIED_TO]->(b)\n",
"Executed query: MATCH (a {id: 1}), (b {id: 7}) CREATE (a)-[:KILLED]->(b)\n",
"Executed query: MATCH (a {id: 7}), (b {id: 8}) CREATE (a)-[:LOYAL_TO]->(b)\n"
]
}
],
Expand Down Expand Up @@ -992,71 +1029,6 @@
"driver.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate queries\n",
"\n",
"Now that GPT has provided us with the structured data for the nodes and\n",
"relationships, the next step is to generate the Cypher queries that we will use\n",
"to execute in Memgraph."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def generate_cypher_queries(nodes, relationships):\n",
" queries = []\n",
"\n",
" # Create nodes\n",
" for node in nodes:\n",
" query = f\"\"\"\n",
" MERGE (n:{node['type']}:Entity {{name: '{node['name']}'}}) \n",
" ON CREATE SET n.id={node['id']} \n",
" ON MATCH SET n.id={node['id']}\n",
" \"\"\"\n",
" queries.append(query)\n",
"\n",
" # Create relationships\n",
" for rel in relationships:\n",
" query = f\"MATCH (a {{id: {rel['source']}}}), (b {{id: {rel['target']}}}) \" \\\n",
" f\"CREATE (a)-[:{rel['relationship']}]->(b)\"\n",
" queries.append(query)\n",
"\n",
" return queries\n",
"\n",
"cypher_queries = generate_cypher_queries(nodes, relationships)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Execute queries\n",
"\n",
"The final step is to execute those queries in Memgraph, enriching your graph\n",
"with the newly created context. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with driver.session() as session:\n",
" for query in cypher_queries:\n",
" try:\n",
" session.run(query)\n",
" print(f\"Executed query: {query}\")\n",
" except Exception as e:\n",
" print(f\"Error executing query: {query}. Error: {e}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 6d169f9

Please sign in to comment.