about summary refs log tree commit diff
path: root/R2R/r2r/examples/scripts/basic_kg_cookbook.py
diff options
context:
space:
mode:
authorS. Solomon Darnell2025-03-28 21:52:21 -0500
committerS. Solomon Darnell2025-03-28 21:52:21 -0500
commit4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch)
treeee3dc5af3b6313e921cd920906356f5d4febc4ed /R2R/r2r/examples/scripts/basic_kg_cookbook.py
parentcc961e04ba734dd72309fb548a2f97d67d578813 (diff)
downloadgn-ai-master.tar.gz
two version of R2R are here HEAD master
Diffstat (limited to 'R2R/r2r/examples/scripts/basic_kg_cookbook.py')
-rwxr-xr-xR2R/r2r/examples/scripts/basic_kg_cookbook.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/R2R/r2r/examples/scripts/basic_kg_cookbook.py b/R2R/r2r/examples/scripts/basic_kg_cookbook.py
new file mode 100755
index 00000000..52db9cff
--- /dev/null
+++ b/R2R/r2r/examples/scripts/basic_kg_cookbook.py
@@ -0,0 +1,67 @@
+from r2r import R2RClient
+
+if __name__ == "__main__":
+    client = R2RClient(base_url="http://localhost:8000")
+
+    with open("john.txt", "w") as f:
+        f.write("John is a person that works at Google.")
+    with open("paul.txt", "w") as f:
+        f.write("Paul is a person that works at Microsoft that knows John.")
+
+    client.ingest_files(
+        ["john.txt", "paul.txt"],
+        metadatas=[
+            {
+                "title": "KG Document 1",
+                "user_id": "063edaf8-3e63-4cb9-a4d6-a855f36376c3",
+            },
+            {
+                "title": "KG Document 2",
+                "user_id": "063edaf8-3e63-4cb9-a4d6-a855f36376c3",
+            },
+        ],
+    )
+
+    # Get the KG provider
+    # neo4j_kg = app.providers.kg
+
+    # # The expected entities
+    # entity_names = ["John", "Paul", "Google", "Microsoft"]
+
+    # print("\nEntities:")
+    # for entity in entity_names:
+    #     print(
+    #         f"Locating {entity}:\n", neo4j_kg.get(properties={"name": entity})
+    #     )
+
+    # relationships = neo4j_kg.get_triplets(entity_names=entity_names)
+
+    # print("\nRelationships:")
+    # for triplet in relationships:
+    #     source, relation, target = triplet
+    #     print(f"{source} -[{relation.label}]-> {target} ")
+
+    # # Search the vector database
+    # search_results = app.search(query="Who is john")
+    # print("\nSearch Results:\n", search_results)
+
+    # # Semantic search over the knowledge graph
+    # from r2r.base import VectorStoreQuery
+
+    # node_result = neo4j_kg.vector_query(
+    #     VectorStoreQuery(
+    #         query_embedding=app.providers.embedding.get_embedding("A person"),
+    #     )
+    # )
+    # print("\nNode Result:", node_result)
+
+    # # Structured query
+    # structured_query = """
+    # MATCH (p1:person)-[:KNOWS]->(p2:person)
+    # RETURN p1.name AS Person1, p2.name AS Person2
+    # ORDER BY p1.name
+    # LIMIT 10;
+    # """
+    # print("Executing query:\n", structured_query)
+    # structured_result = neo4j_kg.structured_query(structured_query)
+    # print("Structured Results:\n", structured_result)