| 1 | # GraphRAG-SDK |
| 2 | |
| 3 | ## Install |
| 4 | ```sh |
| 5 | pip install graphrag_sdk |
| 6 | ``` |
| 7 | |
| 8 | ## Example |
| 9 | |
| 10 | ### Prerequisites |
| 11 | GraphRAG-SDK relies on [FalkorDB](http://falkordb.com) as its graph engine and works with OpenAI. |
| 12 | |
| 13 | Start FalkorDB locally: |
| 14 | ```sh |
| 15 | docker run -p 6379:6379 -it --rm -v ./data:/data falkordb/falkordb:edge |
| 16 | ``` |
| 17 | |
| 18 | Export your OpenAI API KEY: |
| 19 | ```sh |
| 20 | export OPENAI_API_KEY=<YOUR_OPENAI_KEY> |
| 21 | ``` |
| 22 | |
| 23 | ```python |
| 24 | from graphrag_sdk.schema import Schema |
| 25 | from graphrag_sdk import KnowledgeGraph, Source |
| 26 | |
| 27 | # Auto generate graph schema from unstructured data |
| 28 | sources = [Source("./data/the_matrix.txt")] |
| 29 | s = Schema.auto_detect(sources) |
| 30 | |
| 31 | # Create a knowledge graph based on schema |
| 32 | g = KnowledgeGraph("IMDB", schema=s) |
| 33 | g.process_sources(sources) |
| 34 | |
| 35 | # Query your data |
| 36 | question = "Name a few actors who've played in 'The Matrix'" |
| 37 | answer, messages = g.ask(question) |
| 38 | print(f"Answer: {answer}") |
| 39 | |
| 40 | # Output: |
| 41 | # Answer: A few actors who've played in 'The Matrix' are: |
| 42 | # - Keanu Reeves |
| 43 | # - Laurence Fishburne |
| 44 | # - Carrie-Anne Moss |
| 45 | # - Hugo Weaving |
| 46 | ``` |
| 47 | |
| 48 | ## Introduction |
| 49 | GraphRAG-SDK provides easy-to-use tooling to get you up and running with your own |
| 50 | Graph-RAG solution. |
| 51 | |
| 52 | There are two main components: |
| 53 | ### Schema |
| 54 | A `schema` represents the types of entities and relationships within your data. |
| 55 | For example, the main entities in your data are: Movies, Actors, and Directors. |
| 56 | These are interconnected via `ACT` and `DIRECTED` edges. |
| 57 | |
| 58 | Two approaches to schema creation are available: |
| 59 | |
| 60 | #### Manual schema creation |
| 61 | Use this method when you know exactly how your data should be structured. |
| 62 | |
| 63 | ```python |
| 64 | s = Schema() |
| 65 | s.add_entity('Actor').add_attribute('name', str, unique=True) |
| 66 | s.add_entity('Movie').add_attribute('title', str, unique=True) |
| 67 | s.add_relation("ACTED", 'Actor', 'Movie') |
| 68 | |
| 69 | print(f"Schema: {s.to_JSON()}") |
| 70 | |
| 71 | # Output: |
| 72 | # Schema: {"entities": [ |
| 73 | # {"name": "Actor", |
| 74 | # "attributes": [ |
| 75 | # {"name": "name", "type": "str", "desc": "Actor's name", "unique": true, "mandatory": false}]}, |
| 76 | # {"name": "Movie", |
| 77 | # "attributes": [ |
| 78 | # {"name": "title", "type": "str", "desc": "Movie's title", "unique": true, "mandatory": false}]}], |
| 79 | # "relations": [{"name": "ACTED", "src": "Actor", "dest": "Movie"}]} |
| 80 | ``` |
| 81 | |
| 82 | #### Automatic schema creation |
| 83 | Use this method to discover the main entities and relationships within your data. |
| 84 | Once the schema is discovered, you can adjust it to your liking. |
| 85 | |
| 86 | ```python |
| 87 | sources = [Source("./data/madoff.txt")] |
| 88 | s = Schema.auto_detect(sources) |
| 89 | json_schema = s.to_JSON() |
| 90 | print(f"Schema: {json_schema}") |
| 91 | |
| 92 | # Adjust json to your liking and reload the schema from JSON |
| 93 | json_schema['entities'].append({"name": "Movie", "attributes": [ {"name": "Title", "type": "str", "desc": "Movie's title", "unique": true, "mandatory": true}]) |
| 94 | |
| 95 | # Recreate refined schema |
| 96 | s = Schema.from_JSON(json_schema) |
| 97 | |
| 98 | # Output: |
| 99 | # Schema: {"entities": [ |
| 100 | # {"name": "Actor", "attributes": [ |
| 101 | # {"name": "Name", "type": "str", "desc": "Actor's Name", "unique": true, "mandatory": true}]}, |
| 102 | # {"name": "Critic", "attributes": [ |
| 103 | # {"name": "TopCritic", "type": "bool", "desc": "Critic's TopCritic", "unique": false, "mandatory": false}, |
| 104 | # {"name": "Name", "type": "str", "desc": "Critic's Name", "unique": true, "mandatory": true}, |
| 105 | # {"name": "Publication", "type": "str", "desc": "Critic's Publication", "unique": false, "mandatory": false}]}... |
| 106 | ``` |
| 107 | |
| 108 | ### KnowledgeGraph |
| 109 | A `KnowledgeGraph` holds the actual entities and relationships within your data. |
| 110 | Once constructed, it serves as the backbone of your RAG solution. A Large Language Model will query your knowledge-graph to build a precise context on which its answer will be based. |
| 111 | |
| 112 | ```python |
| 113 | # Create a knowledge graph based on a schema. |
| 114 | g = KnowledgeGraph("IMDB", host="127.0.0.1", port=6379, schema=s) |
| 115 | g.process_sources([Source("./data/the_matrix.txt"), Source("./data/wework.txt")]) |
| 116 | |
| 117 | # Query your data |
| 118 | question = "Name a few actors who've played in 'The Matrix'" |
| 119 | answer, messages = g.ask(question) |
| 120 | print(f"Answer: {answer}") |
| 121 | |
| 122 | question = "List additional actors" |
| 123 | answer, messages = g.ask(question, messages) |
| 124 | print(f"Answer: {answer}") |
| 125 | |
| 126 | # Outputs: |
| 127 | # Answer: A few actors who've played in 'The Matrix' are: |
| 128 | # - Keanu Reeves |
| 129 | # - Laurence Fishburne |
| 130 | # - Carrie-Anne Moss |
| 131 | # - Hugo Weaving |
| 132 | # |
| 133 | # Answer: A few additional actors who've played in 'The Matrix' are: |
| 134 | # - Joe Pantoliano |
| 135 | # - Marcus Chong |
| 136 | # - Paul Goddard |
| 137 | # - Gloria Foster |
| 138 | ``` |