Skip to content

Commit

Permalink
Implement FalkorGraphQueryEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
randombet committed Sep 9, 2024
1 parent f10e5cf commit 2c59430
Show file tree
Hide file tree
Showing 5 changed files with 566 additions and 0 deletions.
100 changes: 100 additions & 0 deletions .github/workflows/contrib-graph-rag-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: ContribGraphRagTests

on:
pull_request:
branches: ["main"]
paths:
- "autogen/agentchat/contrib/graph_rag/**"
- "test/agentchat/contrib/graph_rag/**"
- ".github/workflows/contrib-tests.yml"
- "setup.py"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
permissions:
{}
# actions: read
# checks: read
# contents: read
# deployments: read
jobs:
GraphRagUnitTest:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-2019]
python-version: ["3.9", "3.10", "3.11"]
exclude:
- os: macos-latest
python-version: "3.9"
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install packages and dependencies for all tests
run: |
python -m pip install --upgrade pip wheel
pip install pytest-cov>=5
- name: Install Falkor DB SDK when python-version is 3.10
if: matrix.python-version == '3.10'
run: |
pip install -e .[graph_rag_falkor_db]
- name: Set AUTOGEN_USE_DOCKER based on OS
shell: bash
run: |
if [[ ${{ matrix.os }} != ubuntu-latest ]]; then
echo "AUTOGEN_USE_DOCKER=False" >> $GITHUB_ENV
fi
- name: Coverage
run: |
pytest test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py --skip-openai
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests

GraphRagIntegrationTest-FalkorDB-Ubuntu:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11"]
services:
falkordb:
image: falkordb/falkordb:edge
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install packages and dependencies for all tests
run: |
python -m pip install --upgrade pip wheel
pip install pytest
- name: Install Falkor DB SDK when on linux
run: |
pip install -e .[graph_rag_falkor_db]
- name: Set AUTOGEN_USE_DOCKER based on OS
shell: bash
run: |
echo "AUTOGEN_USE_DOCKER=False" >> $GITHUB_ENV
- name: Coverage
run: |
pip install pytest-cov>=5
pytest test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py --skip-openai
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
46 changes: 46 additions & 0 deletions autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
from dataclasses import field
from typing import List

from graphrag_sdk import KnowledgeGraph, Source
from graphrag_sdk.schema import Schema

from .document import Document
from .graph_query_engine import GraphQueryEngine, GraphStoreQueryResult


class FalkorGraphQueryResult(GraphStoreQueryResult):
messages: list = field(default_factory=list)


class FalkorGraphQueryEngine:

def __init__(
self,
name: str,
host: str = "127.0.0.1",
port: int = 6379,
username: str | None = None,
password: str | None = None,
model: str = "gpt-4-1106-preview",
schema: Schema | None = None,
):

self.knowledge_graph = KnowledgeGraph(name, host, port, username, password, model, schema)

def init_db(self, input_doc: List[Document] | None):
sources = []
for doc in input_doc:
if os.path.exists(doc.path_or_url):
sources.append(Source(doc.path_or_url))

if sources:
self.knowledge_graph.process_sources(sources)

def add_records(self, new_records: List) -> bool:
raise NotImplementedError("This method is not supported by Falkor DB SDK yet.")

def query(self, question: str, n_results: int = 1, **kwargs) -> FalkorGraphQueryResult:
messages = kwargs.pop("messages", [])
answer, messages = self.knowledge_graph.ask(question, messages)
return FalkorGraphQueryResult(answer=answer, results=[], messages=messages)
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@

retrieve_chat_pgvector = [*retrieve_chat, "pgvector>=0.2.5"]

graph_rag_falkor_db = [
"graphrag_sdk",
]

if current_os in ["Windows", "Darwin"]:
retrieve_chat_pgvector.extend(["psycopg[binary]>=3.1.18"])
elif current_os == "Linux":
Expand All @@ -81,6 +85,7 @@
"retrievechat-pgvector": retrieve_chat_pgvector,
"retrievechat-mongodb": [*retrieve_chat, "pymongo>=4.0.0"],
"retrievechat-qdrant": [*retrieve_chat, "qdrant_client", "fastembed>=0.3.1"],
"graph_rag_falkor_db": graph_rag_falkor_db,
"autobuild": ["chromadb", "sentence-transformers", "huggingface-hub", "pysqlite3"],
"teachable": ["chromadb"],
"lmm": ["replicate", "pillow"],
Expand Down
Empty file.
Loading

0 comments on commit 2c59430

Please sign in to comment.