From 2c594306d41b6dcdf2cb0f210d7e26a6c2abca58 Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Fri, 6 Sep 2024 22:16:31 +0000 Subject: [PATCH 1/7] Implement FalkorGraphQueryEngine --- .github/workflows/contrib-graph-rag-tests.yml | 100 +++++ .../graph_rag/falkor_graph_query_engine.py | 46 ++ setup.py | 5 + .../graph_rag/test_falkor_graph_rag.py | 0 .../contrib/graph_rag/the_matrix.txt | 415 ++++++++++++++++++ 5 files changed, 566 insertions(+) create mode 100644 .github/workflows/contrib-graph-rag-tests.yml create mode 100644 autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py create mode 100644 test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py create mode 100644 test/agentchat/contrib/graph_rag/the_matrix.txt diff --git a/.github/workflows/contrib-graph-rag-tests.yml b/.github/workflows/contrib-graph-rag-tests.yml new file mode 100644 index 000000000000..d5cb89ed5e6b --- /dev/null +++ b/.github/workflows/contrib-graph-rag-tests.yml @@ -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 diff --git a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py new file mode 100644 index 000000000000..c10a6b40a566 --- /dev/null +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -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) diff --git a/setup.py b/setup.py index b8e6a4616481..63c212598914 100644 --- a/setup.py +++ b/setup.py @@ -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": @@ -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"], diff --git a/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py b/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test/agentchat/contrib/graph_rag/the_matrix.txt b/test/agentchat/contrib/graph_rag/the_matrix.txt new file mode 100644 index 000000000000..3f155218af3c --- /dev/null +++ b/test/agentchat/contrib/graph_rag/the_matrix.txt @@ -0,0 +1,415 @@ +Signed in +Skip to Main Content + Rotten Tomatoes + +What's the Tomatometer®? +Critics +MOVIES TV SHOWS SHOP NEW NEWS SHOWTIMES +TRENDING ON RT +Furiosa First Reviews +Most Anticipated 2025 Movies +Best Movies of All Time +TV Premiere Dates +Main image for The Matrix +The Matrix +R, Now Playing, 2h 16m, Sci-Fi/ Action + + LIST +Videos +Rotten Tomatoes is Wrong About... The Matrix Sequels +51:31 +Rotten Tomatoes is Wrong About... The Matrix Sequels +Content collapsed. +‘The Matrix’ Government Lobby Scene | Rotten Tomatoes’ 21 Most Memorable Moments +2:31 +‘The Matrix’ Government Lobby Scene | Rotten Tomatoes’ 21 Most Memorable Moments +Content collapsed. +View more videos +movie poster +Tomatometer +207 Reviews +Audience Score +250,000+ Ratings +Neo (Keanu Reeves) believes that Morpheus (Laurence Fishburne), an elusive figure considered to be the most dangerous man alive, can answer his question -- What is the Matrix? Neo is contacted by Trinity (Carrie-Anne Moss), a beautiful stranger who leads him into an underworld where he meets Morpheus. They fight a brutal battle for their lives against a cadre of viciously intelligent secret agents. It is a truth that could cost Neo something more precious than his life. +Content collapsed. +Read More +fandango +Now in Theaters +Now Playing +Buy tickets for The Matrix +Where to Watch +What to Know +Reviews +Cast & Crew +More Like This +Related News +Videos +Photos +Media Info + +Next +Where To Watch +The Matrix +fandango +In Theaters +vudu +Fandango at Home +amazon-prime-video-us +Prime Video +netflix +Netflix +Watch The Matrix with a subscription on Netflix, rent on Fandango at Home, Prime Video, or buy on Fandango at Home, Prime Video. +The Matrix +What To Know + Critics Consensus +Thanks to the Wachowskis' imaginative vision, The Matrix is a smartly crafted combination of spectacular action and groundbreaking special effects. + +Read Critics Reviews +Critics Reviews +View All (207) Critics Reviews + Critic's profile +Critic's Name +Mike Clark +Publication +USA Today +TOP CRITIC +The review +Though The Matrix ultimately overdoses on gloom-and-doom grunge, over-elaborate cool and especially running time, it's just clever enough to enable [the Wachowskis] to nix the sophomore jinx following 1996's Bound. +Content collapsed. +Fresh score. +Rated: 2.5/4 • +Date +Jul 13, 2023 +Full Review + Critic's profile +Critic's Name +Joe Morgenstern +Publication +Wall Street Journal +TOP CRITIC +The review +I know almost nothing of the intricate techniques that make The Matrix as eye-filling as it is brain-numbing, but I can tell you that... [its visuals] are wondrous to behold. +Content collapsed. +Fresh score. +Date +Jul 13, 2023 +Full Review + Critic's profile +Critic's Name +Manohla Dargis +Publication +L.A. Weekly +TOP CRITIC +The review +There's more form than content in The Matrix, but Bill Pope's swooping, noir-inflected cinematography is wonderfully complemented by Owen Paterson's inventive production design, a great soundtrack and the best fight choreography this side of Hong Kong. +Content collapsed. +Fresh score. +Date +Jul 13, 2023 +Full Review + Critic's profile +Critic's Name +Sean Axmaker +Publication +Stream on Demand +The review +The Wachowskis proceed to turn the world as we know it into a virtual reality landscape with a 20th century tech-noir look (lit with a sickly green hue, like the glow of an old IBM computer screen) and the physics of a video game. +Content collapsed. +Fresh score. +Date +Sep 9, 2023 +Full Review + Critic's profile +Critic's Name +Dan DiNicola +Publication +The Daily Gazette (Schenectady, NY) +The review +A $60 million, high-tech assault on the senses and a pretentious insult to the mind. +Content collapsed. +Rotten score. +Date +Jul 15, 2023 +Full Review + Critic's profile +Critic's Name +Christopher Brandon +Publication +TNT's Rough Cut +The review +A cyberpunk thriller that's so much fun to watch, it could single-handedly reboot the genre. +Content collapsed. +Fresh score. +Date +Jul 13, 2023 +Full Review +Read all reviews + +Next +Audience Reviews +View All (1000+) audience reviews +Critic's Name +casearmitage +The review +Twenty five years later, it still holds up. You have to see it to believe it. +Content collapsed. +Rated 5/5 Stars • Rated 5 out of 5 stars +Date +04/04/24 +Full Review +Critic's Name +Nathanael +The review +Of course The Matrix is absolutely amazing, but the 4DX thing was pretty cool too! +Content collapsed. +Rated 5/5 Stars • Rated 5 out of 5 stars +Date +12/14/21 +Full Review +Critic's Name +Average P +The review +So good that you entirely forget the nutty premise that we're all being kept in suspended animation wired into some kind of giant mainframe which creates more energy than it requires. Oh well, whatever. Great adventure story. +Content collapsed. +Rated 5/5 Stars • Rated 5 out of 5 stars +Date +05/23/24 +Full Review +Critic's Name +First name L +The review +Event cinema in at it's best, it's often mocked, equally celebrated, for a reason. +Content collapsed. +Rated 4/5 Stars • Rated 4 out of 5 stars +Date +05/14/24 +Full Review +Critic's Name +Jace E +The review +The Matrix is one of the most ambitious and captivating Sci-Fi films of all time. The action is electric, the characters are strong and there are legendary moments peppered throughout. Where the film is lacking is in the actual human aspect; the romance is underdeveloped and Neo's life in the Matrix is superficial (why do we need a scene of his boss disciplining him?). That being said, the fight sequences are incredibly entertaining and the entire idea of the Matrix plays with your mind. I honestly fell asleep multiple times watching the film and had to go back and watch it again. From the slow motion to the muted colors, it was easy to doze off. However, once the climactic moments hit, I was enthralled. The helicopter scene is great and the final fight sequences hit hard. I'm curious where the franchise will go from here, but this film is a brilliant start. Best Character: Tank Best Quote: "There's a difference between knowing the path and walking the path." - Morpheus Best Scene: The Bullet Dodge Best Piece of Score: "He's the One Alright" +Content collapsed. +Rated 4/5 Stars • Rated 4 out of 5 stars +Date +05/13/24 +Full Review +Critic's Name +Antonio M +The review +Ha fatto la storia del cinema cyberpunk. Un cult intramontabile. +Content collapsed. +Rated 5/5 Stars • Rated 5 out of 5 stars +Date +05/09/24 +Full Review +Read all reviews + +Next +Cast & Crew +Lilly Wachowski thumbnail image +Lilly Wachowski +Director + Lana Wachowski thumbnail image +Lana Wachowski +Director + Keanu Reeves thumbnail image +Keanu Reeves +Thomas A. Anderson + Laurence Fishburne thumbnail image +Laurence Fishburne +Morpheus + Carrie-Anne Moss thumbnail image +Carrie-Anne Moss +Trinity + Hugo Weaving thumbnail image +Hugo Weaving +Agent Smith +Content collapsed. +Show More Cast & Crew +More Like This +View All Movies in Theaters + The Matrix Reloaded poster +Certified fresh score. +74% +Fresh audience score. +72% +The Matrix Reloaded + + WATCHLIST +The Matrix Revolutions poster +Rotten score. +34% +Fresh audience score. +60% +The Matrix Revolutions + + WATCHLIST +Demolition Man poster +Fresh score. +63% +Fresh audience score. +67% +Demolition Man + + WATCHLIST +Universal Soldier: The Return poster +Rotten score. +5% +Rotten audience score. +24% +Universal Soldier: The Return + + WATCHLIST +Terminator 3: Rise of the Machines poster +Fresh score. +70% +Rotten audience score. +46% +Terminator 3: Rise of the Machines + + WATCHLIST + +Discover more movies and TV shows. +View More + +Next +Related Movie News +View All Related Movie News + +25 Memorable Movie Lines of the Last 25 Years +Content collapsed. + +Sterling K. Brown’s Five Favorite Films +Content collapsed. + +The Matrix vs. John Wick +Content collapsed. + +Next +Videos +View All videos +The Matrix +Rotten Tomatoes is Wrong About... The Matrix Sequels +51:31 +Rotten Tomatoes is Wrong About... The Matrix Sequels +Content collapsed. +‘The Matrix’ Government Lobby Scene | Rotten Tomatoes’ 21 Most Memorable Moments +2:31 +‘The Matrix’ Government Lobby Scene | Rotten Tomatoes’ 21 Most Memorable Moments +Content collapsed. +View more videos + +Next +Photos +View All The Matrix photos +The Matrix + +The Matrix photo 1 + +The Matrix photo 2 + +The Matrix photo 3 + +The Matrix photo 4 + +Anthony Ray Parker as Dozer, Keanu Reeves as Thomas A. Anderson/Neo, Carrie-Anne Moss as Trinity, and Laurence Fishburne as Morpheus in Nebuchadnezzer's cockpit. + +Keanu Reeves and Carrie-Anne Moss in Warner Brothers' The Matrix. + +Laurence Fishburne in Warner Brothers' The Matrix + +Carrie-Anne Moss and Keanu Reeves in Warner Brothers' The Matrix + +Carrie-Anne Moss in Warner Brothers' The Matrix + +Keanu Reeves in Warner Brothers' The Matrix +View more photos + +Next +Movie Info +Synopsis +Neo (Keanu Reeves) believes that Morpheus (Laurence Fishburne), an elusive figure considered to be the most dangerous man alive, can answer his question -- What is the Matrix? Neo is contacted by Trinity (Carrie-Anne Moss), a beautiful stranger who leads him into an underworld where he meets Morpheus. They fight a brutal battle for their lives against a cadre of viciously intelligent secret agents. It is a truth that could cost Neo something more precious than his life. +Director +Lilly Wachowski , Lana Wachowski +Producer +Joel Silver +Screenwriter +Lilly Wachowski , Lana Wachowski +Distributor +Warner Bros. Pictures +Production Co +Silver Pictures , Village Roadshow Prod. +Rating +R (Sci-Fi Violence|Brief Language) +Genre +Sci-Fi , Action +Original Language +English +Release Date (Theaters) +Mar 31, 1999, Wide +Rerelease Date (Theaters) +Sep 22, 2023 +Release Date (Streaming) +Jan 1, 2009 +Box Office (Gross USA) +$171.4M +Runtime +2h 16m +Sound Mix +Dolby Stereo , DTS , SDDS , Surround , Dolby Digital , Dolby SR +Aspect Ratio +Scope (2.35:1) +What to Watch +In Theaters +At Home +TV Shows +Now Playing +VIEW ALL NOW PLAYING +No score yet. +The Keeper +- - +Certified fresh score. +The Crow +85% +No score yet. +The Commandant's Shadow +- - +No score yet. +The Hangman +- - +Certified fresh score. +In A Violent Nature +90% +Fresh score. +Ezra +75% +Coming soon +VIEW ALL COMING SOON +No score yet. +Jesus Thirsts: The Miracle of the Eucharist +- - +No score yet. +Queen Tut +- - +No score yet. +The Watchers +- - +No score yet. +Bad Boys: Ride or Die +- - +Advertise With Us +Help +About Rotten Tomatoes +What's the Tomatometer®? +Critic Submission +Licensing +Advertise With Us +Careers +JOIN THE NEWSLETTER + +Get the freshest reviews, news, and more delivered right to your inbox! + +FOLLOW US + +V3.1 Privacy Policy Terms and Policies Cookie Notice California Notice Ad Choices Accessibility +Copyright © Fandango. All rights reserved. From 17d0798694951a0dfeca604de8f5f2efbc9479fb Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Fri, 6 Sep 2024 22:16:31 +0000 Subject: [PATCH 2/7] Implement FalkorGraphQueryEngine --- .github/workflows/contrib-graph-rag-tests.yml | 11 ++++ .../graph_rag/falkor_graph_query_engine.py | 2 +- .../graph_rag/test_falkor_graph_rag.py | 52 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/.github/workflows/contrib-graph-rag-tests.yml b/.github/workflows/contrib-graph-rag-tests.yml index d5cb89ed5e6b..647f7fd39ad7 100644 --- a/.github/workflows/contrib-graph-rag-tests.yml +++ b/.github/workflows/contrib-graph-rag-tests.yml @@ -22,6 +22,7 @@ permissions: # contents: read # deployments: read jobs: +<<<<<<< HEAD GraphRagUnitTest: runs-on: ${{ matrix.os }} strategy: @@ -61,6 +62,8 @@ jobs: file: ./coverage.xml flags: unittests +======= +>>>>>>> 53214b0f72 (Implement FalkorGraphQueryEngine) GraphRagIntegrationTest-FalkorDB-Ubuntu: runs-on: ubuntu-latest strategy: @@ -90,6 +93,14 @@ jobs: run: | echo "AUTOGEN_USE_DOCKER=False" >> $GITHUB_ENV - name: Coverage +<<<<<<< HEAD +======= + env: + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} + AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} + OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} +>>>>>>> 53214b0f72 (Implement FalkorGraphQueryEngine) run: | pip install pytest-cov>=5 pytest test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py --skip-openai diff --git a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py index c10a6b40a566..ffc300585393 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -6,7 +6,7 @@ from graphrag_sdk.schema import Schema from .document import Document -from .graph_query_engine import GraphQueryEngine, GraphStoreQueryResult +from .graph_query_engine import GraphStoreQueryResult class FalkorGraphQueryResult(GraphStoreQueryResult): diff --git a/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py b/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py index e69de29bb2d1..36ac51a3eae7 100644 --- a/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py +++ b/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py @@ -0,0 +1,52 @@ +import os +import sys + +import pytest +from graphrag_sdk import KnowledgeGraph, Source +from graphrag_sdk.schema import Schema + +sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) +from conftest import reason, skip_openai # noqa: E402 + +try: + from autogen.agentchat.contrib.graph_rag.document import ( + Document, + DocumentType, + ) + from autogen.agentchat.contrib.graph_rag.falkor_graph_query_engine import ( + FalkorGraphQueryEngine, + GraphStoreQueryResult, + ) +except ImportError: + skip = True +else: + skip = False + +reason = "do not run on MacOS or windows OR dependency is not installed OR " + reason + + +@pytest.mark.skipif( + sys.platform in ["darwin", "win32"] or skip or skip_openai, + reason=reason, +) +def test_falkor_db_query_engine(): + # Arrange + test_schema = Schema() + actor = test_schema.add_entity("Actor").add_attribute("name", str, unique=True) + movie = test_schema.add_entity("Movie").add_attribute("title", str, unique=True) + test_schema.add_relation("ACTED", actor, movie) + + query_engine = FalkorGraphQueryEngine(schema=test_schema) + + source_file = "test/agentchat/contrib/graph_rag/the_matrix.txt" + input_docs = [Document(doctype=DocumentType.TEXT, path_or_url=source_file)] + + question = "Name a few actors who've played in 'The Matrix'" + + # Act + query_engine.init_db(input_doc=input_docs) + + query_result: GraphStoreQueryResult = query_engine.query(question=question) + + # Assert + assert query_result.answer.find("Keanu Reeves") >= 0 From 39e4b2aff33e2b302c1babadfbe480cb4538237e Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Fri, 6 Sep 2024 22:16:31 +0000 Subject: [PATCH 3/7] Implement FalkorGraphQueryEngine --- .github/workflows/contrib-graph-rag-tests.yml | 45 ------------------- 1 file changed, 45 deletions(-) diff --git a/.github/workflows/contrib-graph-rag-tests.yml b/.github/workflows/contrib-graph-rag-tests.yml index 647f7fd39ad7..6e024330f355 100644 --- a/.github/workflows/contrib-graph-rag-tests.yml +++ b/.github/workflows/contrib-graph-rag-tests.yml @@ -22,48 +22,6 @@ permissions: # contents: read # deployments: read jobs: -<<<<<<< HEAD - 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 - -======= ->>>>>>> 53214b0f72 (Implement FalkorGraphQueryEngine) GraphRagIntegrationTest-FalkorDB-Ubuntu: runs-on: ubuntu-latest strategy: @@ -93,14 +51,11 @@ jobs: run: | echo "AUTOGEN_USE_DOCKER=False" >> $GITHUB_ENV - name: Coverage -<<<<<<< HEAD -======= env: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} ->>>>>>> 53214b0f72 (Implement FalkorGraphQueryEngine) run: | pip install pytest-cov>=5 pytest test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py --skip-openai From 87451a30e2d8a3b248bb3bc67884216976b583b6 Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:53:07 +0000 Subject: [PATCH 4/7] Remove Falkor DB test for python 3.9 as it is not supported by the SDK --- .github/workflows/contrib-graph-rag-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/contrib-graph-rag-tests.yml b/.github/workflows/contrib-graph-rag-tests.yml index 6e024330f355..2df2d3c93a43 100644 --- a/.github/workflows/contrib-graph-rag-tests.yml +++ b/.github/workflows/contrib-graph-rag-tests.yml @@ -27,7 +27,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.9", "3.10", "3.11"] + python-version: ["3.10", "3.11"] services: falkordb: image: falkordb/falkordb:edge From 82dd60bb5c40c1ad5dc89eff1ffcfa8b9dc523c9 Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:24:11 +0000 Subject: [PATCH 5/7] Add comments and finalize the tests --- .../graph_rag/falkor_graph_query_engine.py | 17 +++++++++++++++++ .../contrib/graph_rag/test_falkor_graph_rag.py | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py index ffc300585393..730ace230617 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -14,6 +14,9 @@ class FalkorGraphQueryResult(GraphStoreQueryResult): class FalkorGraphQueryEngine: + """ + This is a wrapper for Falkor DB KnowledgeGraph. + """ def __init__( self, @@ -29,6 +32,9 @@ def __init__( self.knowledge_graph = KnowledgeGraph(name, host, port, username, password, model, schema) def init_db(self, input_doc: List[Document] | None): + """ + Build the knowledge graph with input documents. + """ sources = [] for doc in input_doc: if os.path.exists(doc.path_or_url): @@ -41,6 +47,17 @@ 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: + """ + Query the knowledage graph with a question and optional message history. + + Args: + question: a human input question. + n_results: number of returned results. + kwargs: + messages: a list of message history. + + Returns: FalkorGraphQueryResult + """ messages = kwargs.pop("messages", []) answer, messages = self.knowledge_graph.ask(question, messages) return FalkorGraphQueryResult(answer=answer, results=[], messages=messages) diff --git a/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py b/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py index 36ac51a3eae7..2e16d5d0bde3 100644 --- a/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py +++ b/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py @@ -30,6 +30,12 @@ reason=reason, ) def test_falkor_db_query_engine(): + """ + Test Falkor DB Query Engine. + 1. create a test Falkor DB Query Engine with a schema. + 2. Initialize it with an input txt file. + 3. Query it with a question and verify the result contains the critical information. + """ # Arrange test_schema = Schema() actor = test_schema.add_entity("Actor").add_attribute("name", str, unique=True) @@ -50,3 +56,9 @@ def test_falkor_db_query_engine(): # Assert assert query_result.answer.find("Keanu Reeves") >= 0 + for message in query_result.messages: + if isinstance(message, dict) and "role" in message and message["role"] == "user": + assert "content" in message + assert message["content"] is question + return + pytest.fail("Question not found in message history.") From 16850b814fdfcc362f492fc203903ffc161cf7cd Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Sat, 14 Sep 2024 21:07:47 +0000 Subject: [PATCH 6/7] Update FalkorGraphQueryEngine doc string and misc minor changes --- .../contrib/graph_rag/falkor_graph_query_engine.py | 12 ++++++++++++ .../contrib/graph_rag/graph_query_engine.py | 1 - .../contrib/graph_rag/test_falkor_graph_rag.py | 5 +---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py index 730ace230617..8aae624932d6 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -28,7 +28,19 @@ def __init__( model: str = "gpt-4-1106-preview", schema: Schema | None = None, ): + """ + Initialize a Falkor DB knowledge graph. + Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/main/graphrag_sdk/kg.py + Args: + name (str): Knowledge graph name. + host (str): FalkorDB hostname. + port (int): FalkorDB port number. + username (str|None): FalkorDB username. + password (str|None): FalkorDB password. + model (str): OpenAI model to use for Falkor DB to build and retrieve from the graph. + schema: Falkor DB knowledge graph schema (ontology), https://github.com/FalkorDB/GraphRAG-SDK/blob/main/graphrag_sdk/schema/schema.py + """ self.knowledge_graph = KnowledgeGraph(name, host, port, username, password, model, schema) def init_db(self, input_doc: List[Document] | None): diff --git a/autogen/agentchat/contrib/graph_rag/graph_query_engine.py b/autogen/agentchat/contrib/graph_rag/graph_query_engine.py index 28ef6ede84a6..ca29fa165cde 100644 --- a/autogen/agentchat/contrib/graph_rag/graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/graph_query_engine.py @@ -34,7 +34,6 @@ def init_db(self, input_doc: List[Document] | None = None): Args: input_doc: a list of input documents that are used to build the graph in database. - Returns: GraphStore """ pass diff --git a/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py b/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py index 2e16d5d0bde3..8814479da9a1 100644 --- a/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py +++ b/test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py @@ -1,13 +1,10 @@ -import os import sys import pytest +from conftest import reason, skip_openai # noqa: E402 from graphrag_sdk import KnowledgeGraph, Source from graphrag_sdk.schema import Schema -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -from conftest import reason, skip_openai # noqa: E402 - try: from autogen.agentchat.contrib.graph_rag.document import ( Document, From 6778a5a9d0eb509517debe1adb4f3a68e1480c60 Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Sat, 14 Sep 2024 21:25:45 +0000 Subject: [PATCH 7/7] Update docstr --- autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py | 1 + 1 file changed, 1 insertion(+) diff --git a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py index 8aae624932d6..0bb1f51a05b3 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -40,6 +40,7 @@ def __init__( password (str|None): FalkorDB password. model (str): OpenAI model to use for Falkor DB to build and retrieve from the graph. schema: Falkor DB knowledge graph schema (ontology), https://github.com/FalkorDB/GraphRAG-SDK/blob/main/graphrag_sdk/schema/schema.py + If None, Falkor DB will auto generate a schema from the input docs. """ self.knowledge_graph = KnowledgeGraph(name, host, port, username, password, model, schema)