Skip to main content

No project description provided

Project description

GraphRAG-SDK

Install

pip install graphrag_sdk

Example

Prerequisites

GraphRAG-SDK relies on FalkorDB as its graph engine and works with OpenAI.

Start FalkorDB locally:

docker run -p 6379:6379 -it --rm -v ./data:/data falkordb/falkordb:edge

Export your OpenAI API KEY:

export OPENAI_API_KEY=<YOUR_OPENAI_KEY>
from graphrag_sdk.schema import Schema
from graphrag_sdk import KnowledgeGraph, Source

# Auto generate graph schema from unstructured data
sources = [Source("./data/the_matrix.txt")]
s = Schema.auto_detect(sources)

# Create a knowledge graph based on schema
g = KnowledgeGraph("IMDB", schema=s)
g.process_sources(sources)

# Query your data
question = "Name a few actors who've played in 'The Matrix'"
answer, messages = g.ask(question)
print(f"Answer: {answer}")

# Output:
# Answer: A few actors who've played in 'The Matrix' are:
# - Keanu Reeves
# - Laurence Fishburne
# - Carrie-Anne Moss
# - Hugo Weaving

Introduction

GraphRAG-SDK provides easy-to-use tooling to get you up and running with your own Graph-RAG solution.

There are two main components:

Schema

A schema represents the types of entities and relationships within your data. For example, the main entities in your data are: Movies, Actors, and Directors. These are interconnected via ACT and DIRECTED edges.

Two approaches to schema creation are available:

Manual schema creation

Use this method when you know exactly how your data should be structured.

s = Schema()
s.add_entity('Actor').add_attribute('name', str, unique=True)
s.add_entity('Movie').add_attribute('title', str, unique=True)
s.add_relation("ACTED", 'Actor', 'Movie')

print(f"Schema: {s.to_JSON()}")

# Output:
# Schema: {"entities": [
#   {"name": "Actor",
#    "attributes": [
#       {"name": "name", "type": "str", "desc": "Actor's name", "unique": true, "mandatory": false}]},
#   {"name": "Movie",
#    "attributes": [
#       {"name": "title", "type": "str", "desc": "Movie's title", "unique": true, "mandatory": false}]}],
#   "relations": [{"name": "ACTED", "src": "Actor", "dest": "Movie"}]}

Automatic schema creation

Use this method to discover the main entities and relationships within your data. Once the schema is discovered, you can adjust it to your liking.

sources = [Source("./data/madoff.txt")]
s = Schema.auto_detect(sources)
json_schema = s.to_JSON()
print(f"Schema: {json_schema}")

# Adjust json to your liking and reload the schema from JSON
json_schema['entities'].append({"name": "Movie", "attributes": [ {"name": "Title", "type": "str", "desc": "Movie's title", "unique": true, "mandatory": true}])

# Recreate refined schema
s = Schema.from_JSON(json_schema)

# Output:
# Schema: {"entities": [
#   {"name": "Actor", "attributes": [
#       {"name": "Name", "type": "str", "desc": "Actor's Name", "unique": true, "mandatory": true}]},
#   {"name": "Critic", "attributes": [
#       {"name": "TopCritic", "type": "bool", "desc": "Critic's TopCritic", "unique": false, "mandatory": false},
#       {"name": "Name", "type": "str", "desc": "Critic's Name", "unique": true, "mandatory": true},
#       {"name": "Publication", "type": "str", "desc": "Critic's Publication", "unique": false, "mandatory": false}]}...

KnowledgeGraph

A KnowledgeGraph holds the actual entities and relationships within your data. 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.

# Create a knowledge graph based on a schema.
g = KnowledgeGraph("IMDB", host="127.0.0.1", port=6379, schema=s)
g.process_sources([Source("./data/the_matrix.txt"), Source("./data/wework.txt")])

# Query your data
question = "Name a few actors who've played in 'The Matrix'"
answer, messages = g.ask(question)
print(f"Answer: {answer}")

question = "List additional actors"
answer, messages = g.ask(question, messages)
print(f"Answer: {answer}")

# Outputs:
# Answer: A few actors who've played in 'The Matrix' are:
# - Keanu Reeves
# - Laurence Fishburne
# - Carrie-Anne Moss
# - Hugo Weaving
#
# Answer: A few additional actors who've played in 'The Matrix' are:
# - Joe Pantoliano
# - Marcus Chong
# - Paul Goddard
# - Gloria Foster

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphrag_sdk-0.1.3b0.tar.gz (20.3 kB view details)

Uploaded Source

File details

Details for the file graphrag_sdk-0.1.3b0.tar.gz.

File metadata

  • Download URL: graphrag_sdk-0.1.3b0.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1022-azure

File hashes

Hashes for graphrag_sdk-0.1.3b0.tar.gz
Algorithm Hash digest
SHA256 812ed76abd2c61cbbc7d578e494b815c5f56b7cccd27a188f0cacd20ea446605
MD5 6efb6afec2afc3944a69a70c491bf780
BLAKE2b-256 8c47a233cc42c596027b4c7d9935e6b4b46ed11387065e0ca52caeb5331c4c92

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page