Elffuss BraidHub

kikocis/GraphRAG-SDK public

ca14f368bce9 · 4.4 KiBRenderedRawPermalink
1# GraphRAG-SDK
2
3## Install
4```sh
5pip install graphrag_sdk
6```
7
8## Example
9
10### Prerequisites
11GraphRAG-SDK relies on [FalkorDB](http://falkordb.com) as its graph engine and works with OpenAI.
12
13Start FalkorDB locally:
14```sh
15docker run -p 6379:6379 -it --rm -v ./data:/data falkordb/falkordb:edge
16```
17
18Export your OpenAI API KEY:
19```sh
20export OPENAI_API_KEY=<YOUR_OPENAI_KEY>
21```
22
23```python
24from graphrag_sdk.schema import Schema
25from graphrag_sdk import KnowledgeGraph, Source
26
27# Auto generate graph schema from unstructured data
28sources = [Source("./data/the_matrix.txt")]
29s = Schema.auto_detect(sources)
30
31# Create a knowledge graph based on schema
32g = KnowledgeGraph("IMDB", schema=s)
33g.process_sources(sources)
34
35# Query your data
36question = "Name a few actors who've played in 'The Matrix'"
37answer, messages = g.ask(question)
38print(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
49GraphRAG-SDK provides easy-to-use tooling to get you up and running with your own
50Graph-RAG solution.
51
52There are two main components:
53### Schema
54A `schema` represents the types of entities and relationships within your data.
55For example, the main entities in your data are: Movies, Actors, and Directors.
56These are interconnected via `ACT` and `DIRECTED` edges.
57
58Two approaches to schema creation are available:
59
60#### Manual schema creation
61Use this method when you know exactly how your data should be structured.
62
63```python
64s = Schema()
65s.add_entity('Actor').add_attribute('name', str, unique=True)
66s.add_entity('Movie').add_attribute('title', str, unique=True)
67s.add_relation("ACTED", 'Actor', 'Movie')
68
69print(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
83Use this method to discover the main entities and relationships within your data.
84Once the schema is discovered, you can adjust it to your liking.
85
86```python
87sources = [Source("./data/madoff.txt")]
88s = Schema.auto_detect(sources)
89json_schema = s.to_JSON()
90print(f"Schema: {json_schema}")
91
92# Adjust json to your liking and reload the schema from JSON
93json_schema['entities'].append({"name": "Movie", "attributes": [ {"name": "Title", "type": "str", "desc": "Movie's title", "unique": true, "mandatory": true}])
94
95# Recreate refined schema
96s = 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
109A `KnowledgeGraph` holds the actual entities and relationships within your data.
110Once 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.
114g = KnowledgeGraph("IMDB", host="127.0.0.1", port=6379, schema=s)
115g.process_sources([Source("./data/the_matrix.txt"), Source("./data/wework.txt")])
116
117# Query your data
118question = "Name a few actors who've played in 'The Matrix'"
119answer, messages = g.ask(question)
120print(f"Answer: {answer}")
121
122question = "List additional actors"
123answer, messages = g.ask(question, messages)
124print(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```