aboutsummaryrefslogtreecommitdiff
path: root/R2R/r2r/examples/scripts/basic_kg_cookbook.py
blob: 52db9cffe9f6b0d5f4a42d367a3a938dd7f753fd (about) (plain)
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
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)