From 9642846acba5179c781dd2100d84ed6874ae68ba Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:43:58 +0000 Subject: [PATCH 1/7] Falkor DB integration init commit --- .../graph_rag/falkor_graph_query_engine.py | 4 +- .../contrib/graph_rag/graph_rag_capability.py | 2 +- notebook/agentchat_graph_rag_falkordb.ipynb | 169 ++++++++++++++++++ setup.py | 2 +- 4 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 notebook/agentchat_graph_rag_falkordb.ipynb 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 0bb1f51a05b3..4c216f969c93 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -30,7 +30,7 @@ def __init__( ): """ Initialize a Falkor DB knowledge graph. - Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/main/graphrag_sdk/kg.py + Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/2-move-away-from-sql-to-json-ontology-detection/graphrag_sdk/kg.py Args: name (str): Knowledge graph name. @@ -39,7 +39,7 @@ def __init__( 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 + schema: Falkor DB knowledge graph schema (ontology), https://github.com/FalkorDB/GraphRAG-SDK/blob/2-move-away-from-sql-to-json-ontology-detection/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) diff --git a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py index b6412305e069..f4a44cab465a 100644 --- a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +++ b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py @@ -51,6 +51,6 @@ def __init__(self, query_engine: GraphQueryEngine): """ initialize graph rag capability with a graph query engine """ - ... + self.query_engine = query_engine def add_to_agent(self, agent: ConversableAgent): ... diff --git a/notebook/agentchat_graph_rag_falkordb.ipynb b/notebook/agentchat_graph_rag_falkordb.ipynb new file mode 100644 index 000000000000..f7d74787a680 --- /dev/null +++ b/notebook/agentchat_graph_rag_falkordb.ipynb @@ -0,0 +1,169 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: graphrag_sdk==0.1.3b0 in /home/autogen-ai/.local/lib/python3.11/site-packages (0.1.3b0)\n", + "Requirement already satisfied: bs4<0.0.3,>=0.0.2 in /home/autogen-ai/.local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (0.0.2)\n", + "Requirement already satisfied: falkordb<2.0.0,>=1.0.4 in /home/autogen-ai/.local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (1.0.8)\n", + "Requirement already satisfied: openai<2.0.0,>=1.30.3 in /usr/local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (1.49.0)\n", + "Requirement already satisfied: sqlalchemy<3.0.0,>=2.0.30 in /usr/local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (2.0.35)\n", + "Requirement already satisfied: typing-extensions<5.0.0,>=4.12.1 in /usr/local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (4.12.2)\n", + "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.11/site-packages (from bs4<0.0.3,>=0.0.2->graphrag_sdk==0.1.3b0) (4.12.3)\n", + "Requirement already satisfied: redis<6.0.0,>=5.0.1 in /home/autogen-ai/.local/lib/python3.11/site-packages (from falkordb<2.0.0,>=1.0.4->graphrag_sdk==0.1.3b0) (5.1.0)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (4.6.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.9.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.27.2)\n", + "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.5.0)\n", + "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.10.9)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (4.66.5)\n", + "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.11/site-packages (from sqlalchemy<3.0.0,>=2.0.30->graphrag_sdk==0.1.3b0) (3.1.1)\n", + "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (3.10)\n", + "Requirement already satisfied: certifi in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (2024.8.30)\n", + "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.0.5)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.14.0)\n", + "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.11/site-packages (from beautifulsoup4->bs4<0.0.3,>=0.0.2->graphrag_sdk==0.1.3b0) (2.6)\n", + "Note: you may need to restart the kernel to use updated packages.\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Obtaining file:///workspaces/autogen\n", + " Installing build dependencies ... \u001b[?25ldone\n", + "\u001b[?25h Checking if build backend supports build_editable ... \u001b[?25ldone\n", + "\u001b[?25h Getting requirements to build editable ... \u001b[?25ldone\n", + "\u001b[?25h Preparing editable metadata (pyproject.toml) ... \u001b[?25ldone\n", + "\u001b[?25hRequirement already satisfied: openai>=1.3 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.49.0)\n", + "Requirement already satisfied: diskcache in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (5.6.3)\n", + "Requirement already satisfied: termcolor in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (2.4.0)\n", + "Requirement already satisfied: flaml in /home/autogen-ai/.local/lib/python3.11/site-packages (from autogen==0.3.0) (2.2.0)\n", + "Requirement already satisfied: numpy<2,>=1.17.0 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.26.4)\n", + "Requirement already satisfied: python-dotenv in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.0.1)\n", + "Requirement already satisfied: tiktoken in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (0.7.0)\n", + "Requirement already satisfied: pydantic!=2.6.0,<3,>=1.10 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.10.9)\n", + "Requirement already satisfied: docker in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (7.1.0)\n", + "Requirement already satisfied: packaging in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (24.1)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.6.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (1.9.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (0.27.2)\n", + "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (0.5.0)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.66.5)\n", + "Requirement already satisfied: typing-extensions<5,>=4.11 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.12.2)\n", + "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.11/site-packages (from docker->autogen==0.3.0) (2.32.3)\n", + "Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.11/site-packages (from docker->autogen==0.3.0) (2.2.3)\n", + "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.11/site-packages (from tiktoken->autogen==0.3.0) (2024.9.11)\n", + "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai>=1.3->autogen==0.3.0) (3.10)\n", + "Requirement already satisfied: certifi in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (2024.8.30)\n", + "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (1.0.5)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (0.14.0)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests>=2.26.0->docker->autogen==0.3.0) (3.3.2)\n", + "Building wheels for collected packages: autogen\n", + " Building editable for autogen (pyproject.toml) ... \u001b[?25ldone\n", + "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=988e26254d7445afe8e6de69c9b7e3c8a30a4207fda4a88a503050fc0ebc88c9\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-emvq4y0f/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", + "Successfully built autogen\n", + "Installing collected packages: autogen\n", + "Successfully installed autogen-0.3.0\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install graphrag_sdk==0.1.3b0\n", + "%pip install -e /workspaces/autogen" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "ConnectionError", + "evalue": "Error 111 connecting to 127.0.0.1:3000. Connection refused.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mConnectionRefusedError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:358\u001b[0m, in \u001b[0;36mAbstractConnection.connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 358\u001b[0m sock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mretry\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall_with_retry\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 359\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43merror\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdisconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43merror\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 360\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 361\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mtimeout:\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/retry.py:62\u001b[0m, in \u001b[0;36mRetry.call_with_retry\u001b[0;34m(self, do, fail)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 62\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdo\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_supported_errors \u001b[38;5;28;01mas\u001b[39;00m error:\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:359\u001b[0m, in \u001b[0;36mAbstractConnection.connect..\u001b[0;34m()\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 358\u001b[0m sock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mretry\u001b[38;5;241m.\u001b[39mcall_with_retry(\n\u001b[0;32m--> 359\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m, \u001b[38;5;28;01mlambda\u001b[39;00m error: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdisconnect(error)\n\u001b[1;32m 360\u001b[0m )\n\u001b[1;32m 361\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mtimeout:\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:731\u001b[0m, in \u001b[0;36mConnection._connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 730\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 731\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m err\n\u001b[1;32m 732\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msocket.getaddrinfo returned an empty list\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:719\u001b[0m, in \u001b[0;36mConnection._connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 718\u001b[0m \u001b[38;5;66;03m# connect\u001b[39;00m\n\u001b[0;32m--> 719\u001b[0m \u001b[43msock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43msocket_address\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 721\u001b[0m \u001b[38;5;66;03m# set the socket_timeout now that we're connected\u001b[39;00m\n", + "\u001b[0;31mConnectionRefusedError\u001b[0m: [Errno 111] Connection refused", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mConnectionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m movie \u001b[38;5;241m=\u001b[39m movie_schema\u001b[38;5;241m.\u001b[39madd_entity(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMovie\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39madd_attribute(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtitle\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28mstr\u001b[39m, unique\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 11\u001b[0m movie_schema\u001b[38;5;241m.\u001b[39madd_relation(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mACTED\u001b[39m\u001b[38;5;124m\"\u001b[39m, actor, movie)\n\u001b[0;32m---> 13\u001b[0m query_engine \u001b[38;5;241m=\u001b[39m \u001b[43mFalkorGraphQueryEngine\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mIMDB\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 15\u001b[0m \u001b[43m \u001b[49m\u001b[43mhost\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m127.0.0.1\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3000\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[43mschema\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmovie_schema\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 20\u001b[0m input_documents \u001b[38;5;241m=\u001b[39m [Document(doctype\u001b[38;5;241m=\u001b[39mDocumentType\u001b[38;5;241m.\u001b[39mTEXT, path_or_url\u001b[38;5;241m=\u001b[39minput_path)]\n\u001b[1;32m 22\u001b[0m query_engine\u001b[38;5;241m.\u001b[39minit_db(input_doc\u001b[38;5;241m=\u001b[39minput_documents)\n", + "File \u001b[0;32m/workspaces/autogen/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py:45\u001b[0m, in \u001b[0;36mFalkorGraphQueryEngine.__init__\u001b[0;34m(self, name, host, port, username, password, model, schema)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 22\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 23\u001b[0m name: \u001b[38;5;28mstr\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 29\u001b[0m schema: Schema \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 30\u001b[0m ):\n\u001b[1;32m 31\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 32\u001b[0m \u001b[38;5;124;03m Initialize a Falkor DB knowledge graph.\u001b[39;00m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;124;03m Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/2-move-away-from-sql-to-json-ontology-detection/graphrag_sdk/kg.py\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;124;03m If None, Falkor DB will auto generate a schema from the input docs.\u001b[39;00m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 45\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mknowledge_graph \u001b[38;5;241m=\u001b[39m \u001b[43mKnowledgeGraph\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43musername\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mschema\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/graphrag_sdk/kg.py:43\u001b[0m, in \u001b[0;36mKnowledgeGraph.__init__\u001b[0;34m(self, name, host, port, username, password, model, schema)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname should be a non empty string\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 42\u001b[0m \u001b[38;5;66;03m# connect to database\u001b[39;00m\n\u001b[0;32m---> 43\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdb \u001b[38;5;241m=\u001b[39m \u001b[43mFalkorDB\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhost\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43musername\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43musername\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpassword\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgraph \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdb\u001b[38;5;241m.\u001b[39mselect_graph(name)\n\u001b[1;32m 46\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel \u001b[38;5;241m=\u001b[39m model\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/falkordb/falkordb.py:101\u001b[0m, in \u001b[0;36mFalkorDB.__init__\u001b[0;34m(self, host, port, password, socket_timeout, socket_connect_timeout, socket_keepalive, socket_keepalive_options, connection_pool, unix_socket_path, encoding, encoding_errors, charset, errors, retry_on_timeout, retry_on_error, ssl, ssl_keyfile, ssl_certfile, ssl_cert_reqs, ssl_ca_certs, ssl_ca_path, ssl_ca_data, ssl_check_hostname, ssl_password, ssl_validate_ocsp, ssl_validate_ocsp_stapled, ssl_ocsp_context, ssl_ocsp_expected_cert, max_connections, single_connection_client, health_check_interval, client_name, lib_name, lib_version, username, retry, connect_func, credential_provider, protocol)\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 29\u001b[0m host\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocalhost\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 67\u001b[0m protocol\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m\n\u001b[1;32m 68\u001b[0m ):\n\u001b[1;32m 70\u001b[0m conn \u001b[38;5;241m=\u001b[39m redis\u001b[38;5;241m.\u001b[39mRedis(host\u001b[38;5;241m=\u001b[39mhost, port\u001b[38;5;241m=\u001b[39mport, db\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, password\u001b[38;5;241m=\u001b[39mpassword,\n\u001b[1;32m 71\u001b[0m socket_timeout\u001b[38;5;241m=\u001b[39msocket_timeout,\n\u001b[1;32m 72\u001b[0m socket_connect_timeout\u001b[38;5;241m=\u001b[39msocket_connect_timeout,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 98\u001b[0m credential_provider\u001b[38;5;241m=\u001b[39mcredential_provider,\n\u001b[1;32m 99\u001b[0m protocol\u001b[38;5;241m=\u001b[39mprotocol)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mIs_Sentinel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconn\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[1;32m 102\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msentinel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice_name \u001b[38;5;241m=\u001b[39m Sentinel_Conn(conn, ssl)\n\u001b[1;32m 103\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msentinel\u001b[38;5;241m.\u001b[39mmaster_for(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice_name, ssl\u001b[38;5;241m=\u001b[39mssl)\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/falkordb/sentinel.py:5\u001b[0m, in \u001b[0;36mIs_Sentinel\u001b[0;34m(conn)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mIs_Sentinel\u001b[39m(conn):\n\u001b[0;32m----> 5\u001b[0m info \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minfo\u001b[49m\u001b[43m(\u001b[49m\u001b[43msection\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mserver\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mredis_mode\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m info \u001b[38;5;129;01mand\u001b[39;00m info[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mredis_mode\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msentinel\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/commands/core.py:1000\u001b[0m, in \u001b[0;36mManagementCommands.info\u001b[0;34m(self, section, *args, **kwargs)\u001b[0m\n\u001b[1;32m 998\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexecute_command(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mINFO\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 999\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1000\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute_command\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mINFO\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msection\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/client.py:559\u001b[0m, in \u001b[0;36mRedis.execute_command\u001b[0;34m(self, *args, **options)\u001b[0m\n\u001b[1;32m 558\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexecute_command\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions):\n\u001b[0;32m--> 559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_command\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/client.py:565\u001b[0m, in \u001b[0;36mRedis._execute_command\u001b[0;34m(self, *args, **options)\u001b[0m\n\u001b[1;32m 563\u001b[0m pool \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection_pool\n\u001b[1;32m 564\u001b[0m command_name \u001b[38;5;241m=\u001b[39m args[\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m--> 565\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection \u001b[38;5;129;01mor\u001b[39;00m \u001b[43mpool\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_connection\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcommand_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 566\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 567\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m conn\u001b[38;5;241m.\u001b[39mretry\u001b[38;5;241m.\u001b[39mcall_with_retry(\n\u001b[1;32m 568\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_send_command_parse_response(\n\u001b[1;32m 569\u001b[0m conn, command_name, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions\n\u001b[1;32m 570\u001b[0m ),\n\u001b[1;32m 571\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m error: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_disconnect_raise(conn, error),\n\u001b[1;32m 572\u001b[0m )\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:1423\u001b[0m, in \u001b[0;36mConnectionPool.get_connection\u001b[0;34m(self, command_name, *keys, **options)\u001b[0m\n\u001b[1;32m 1419\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_in_use_connections\u001b[38;5;241m.\u001b[39madd(connection)\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;66;03m# ensure this connection is connected to Redis\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1424\u001b[0m \u001b[38;5;66;03m# connections that the pool provides should be ready to send\u001b[39;00m\n\u001b[1;32m 1425\u001b[0m \u001b[38;5;66;03m# a command. if not, the connection was either returned to the\u001b[39;00m\n\u001b[1;32m 1426\u001b[0m \u001b[38;5;66;03m# pool before all data has been read or the socket has been\u001b[39;00m\n\u001b[1;32m 1427\u001b[0m \u001b[38;5;66;03m# closed. either way, reconnect and verify everything is good.\u001b[39;00m\n\u001b[1;32m 1428\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:364\u001b[0m, in \u001b[0;36mAbstractConnection.connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTimeoutError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTimeout connecting to server\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 363\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m--> 364\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_error_message(e))\n\u001b[1;32m 366\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sock \u001b[38;5;241m=\u001b[39m sock\n\u001b[1;32m 367\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", + "\u001b[0;31mConnectionError\u001b[0m: Error 111 connecting to 127.0.0.1:3000. Connection refused." + ] + } + ], + "source": [ + "from graphrag_sdk.schema import Schema\n", + "\n", + "from autogen.agentchat.contrib.graph_rag.document import Document, DocumentType\n", + "from autogen.agentchat.contrib.graph_rag.falkor_graph_query_engine import FalkorGraphQueryEngine\n", + "\n", + "# Auto generate graph schema from unstructured data\n", + "input_path = \"../test/agentchat/contrib/graph_rag/the_matrix.txt\"\n", + "\n", + "movie_schema = Schema()\n", + "actor = movie_schema.add_entity(\"Actor\").add_attribute(\"name\", str, unique=True)\n", + "movie = movie_schema.add_entity(\"Movie\").add_attribute(\"title\", str, unique=True)\n", + "movie_schema.add_relation(\"ACTED\", actor, movie)\n", + "\n", + "query_engine = FalkorGraphQueryEngine(\n", + " name=\"IMDB\",\n", + " host=\"127.0.0.1\",\n", + " port=3000,\n", + " schema=movie_schema,\n", + ")\n", + "\n", + "input_documents = [Document(doctype=DocumentType.TEXT, path_or_url=input_path)]\n", + "\n", + "query_engine.init_db(input_doc=input_documents)\n", + "\n", + "# Query your data\n", + "question = \"Name a few actors who've played in 'The Matrix'\"\n", + "answer, messages = query_engine.query(question)\n", + "print(f\"Answer: {answer}\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.10" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/setup.py b/setup.py index 63c212598914..4ecc5082f7f1 100644 --- a/setup.py +++ b/setup.py @@ -60,7 +60,7 @@ retrieve_chat_pgvector = [*retrieve_chat, "pgvector>=0.2.5"] graph_rag_falkor_db = [ - "graphrag_sdk", + "graphrag_sdk==0.1.3b0", ] if current_os in ["Windows", "Darwin"]: From 6d1adcd6804aa56eb164f6b0080f21ceed4b70c3 Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Tue, 15 Oct 2024 23:33:48 +0000 Subject: [PATCH 2/7] Integrate FalkorGraphQueryEngine --- .../graph_rag/falkor_graph_query_engine.py | 3 +- notebook/agentchat_graph_rag_falkordb.ipynb | 60 +++++++++---------- 2 files changed, 31 insertions(+), 32 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 4c216f969c93..d0670de102be 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -1,5 +1,5 @@ import os -from dataclasses import field +from dataclasses import dataclass, field from typing import List from graphrag_sdk import KnowledgeGraph, Source @@ -9,6 +9,7 @@ from .graph_query_engine import GraphStoreQueryResult +@dataclass class FalkorGraphQueryResult(GraphStoreQueryResult): messages: list = field(default_factory=list) diff --git a/notebook/agentchat_graph_rag_falkordb.ipynb b/notebook/agentchat_graph_rag_falkordb.ipynb index f7d74787a680..9167fecec81f 100644 --- a/notebook/agentchat_graph_rag_falkordb.ipynb +++ b/notebook/agentchat_graph_rag_falkordb.ipynb @@ -65,10 +65,14 @@ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests>=2.26.0->docker->autogen==0.3.0) (3.3.2)\n", "Building wheels for collected packages: autogen\n", " Building editable for autogen (pyproject.toml) ... \u001b[?25ldone\n", - "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=988e26254d7445afe8e6de69c9b7e3c8a30a4207fda4a88a503050fc0ebc88c9\n", - " Stored in directory: /tmp/pip-ephem-wheel-cache-emvq4y0f/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", + "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=e98cc4d562793824fed6eb82a0882d331170bf9161260ff45ff3dc32570f0bb5\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-0wb4pw7w/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", "Successfully built autogen\n", "Installing collected packages: autogen\n", + " Attempting uninstall: autogen\n", + " Found existing installation: autogen 0.3.0\n", + " Uninstalling autogen-0.3.0:\n", + " Successfully uninstalled autogen-0.3.0\n", "Successfully installed autogen-0.3.0\n", "Note: you may need to restart the kernel to use updated packages.\n" ] @@ -81,38 +85,32 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": {}, "outputs": [ { - "ename": "ConnectionError", - "evalue": "Error 111 connecting to 127.0.0.1:3000. Connection refused.", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mConnectionRefusedError\u001b[0m Traceback (most recent call last)", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:358\u001b[0m, in \u001b[0;36mAbstractConnection.connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 358\u001b[0m sock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mretry\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall_with_retry\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 359\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43merror\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdisconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43merror\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 360\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 361\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mtimeout:\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/retry.py:62\u001b[0m, in \u001b[0;36mRetry.call_with_retry\u001b[0;34m(self, do, fail)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 62\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdo\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_supported_errors \u001b[38;5;28;01mas\u001b[39;00m error:\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:359\u001b[0m, in \u001b[0;36mAbstractConnection.connect..\u001b[0;34m()\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 358\u001b[0m sock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mretry\u001b[38;5;241m.\u001b[39mcall_with_retry(\n\u001b[0;32m--> 359\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m, \u001b[38;5;28;01mlambda\u001b[39;00m error: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdisconnect(error)\n\u001b[1;32m 360\u001b[0m )\n\u001b[1;32m 361\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mtimeout:\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:731\u001b[0m, in \u001b[0;36mConnection._connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 730\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 731\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m err\n\u001b[1;32m 732\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msocket.getaddrinfo returned an empty list\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:719\u001b[0m, in \u001b[0;36mConnection._connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 718\u001b[0m \u001b[38;5;66;03m# connect\u001b[39;00m\n\u001b[0;32m--> 719\u001b[0m \u001b[43msock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43msocket_address\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 721\u001b[0m \u001b[38;5;66;03m# set the socket_timeout now that we're connected\u001b[39;00m\n", - "\u001b[0;31mConnectionRefusedError\u001b[0m: [Errno 111] Connection refused", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[0;31mConnectionError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[4], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m movie \u001b[38;5;241m=\u001b[39m movie_schema\u001b[38;5;241m.\u001b[39madd_entity(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMovie\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39madd_attribute(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtitle\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28mstr\u001b[39m, unique\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 11\u001b[0m movie_schema\u001b[38;5;241m.\u001b[39madd_relation(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mACTED\u001b[39m\u001b[38;5;124m\"\u001b[39m, actor, movie)\n\u001b[0;32m---> 13\u001b[0m query_engine \u001b[38;5;241m=\u001b[39m \u001b[43mFalkorGraphQueryEngine\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mIMDB\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 15\u001b[0m \u001b[43m \u001b[49m\u001b[43mhost\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m127.0.0.1\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3000\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[43mschema\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmovie_schema\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 20\u001b[0m input_documents \u001b[38;5;241m=\u001b[39m [Document(doctype\u001b[38;5;241m=\u001b[39mDocumentType\u001b[38;5;241m.\u001b[39mTEXT, path_or_url\u001b[38;5;241m=\u001b[39minput_path)]\n\u001b[1;32m 22\u001b[0m query_engine\u001b[38;5;241m.\u001b[39minit_db(input_doc\u001b[38;5;241m=\u001b[39minput_documents)\n", - "File \u001b[0;32m/workspaces/autogen/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py:45\u001b[0m, in \u001b[0;36mFalkorGraphQueryEngine.__init__\u001b[0;34m(self, name, host, port, username, password, model, schema)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 22\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 23\u001b[0m name: \u001b[38;5;28mstr\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 29\u001b[0m schema: Schema \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 30\u001b[0m ):\n\u001b[1;32m 31\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 32\u001b[0m \u001b[38;5;124;03m Initialize a Falkor DB knowledge graph.\u001b[39;00m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;124;03m Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/2-move-away-from-sql-to-json-ontology-detection/graphrag_sdk/kg.py\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;124;03m If None, Falkor DB will auto generate a schema from the input docs.\u001b[39;00m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 45\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mknowledge_graph \u001b[38;5;241m=\u001b[39m \u001b[43mKnowledgeGraph\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43musername\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mschema\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/graphrag_sdk/kg.py:43\u001b[0m, in \u001b[0;36mKnowledgeGraph.__init__\u001b[0;34m(self, name, host, port, username, password, model, schema)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname should be a non empty string\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 42\u001b[0m \u001b[38;5;66;03m# connect to database\u001b[39;00m\n\u001b[0;32m---> 43\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdb \u001b[38;5;241m=\u001b[39m \u001b[43mFalkorDB\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhost\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43musername\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43musername\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpassword\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgraph \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdb\u001b[38;5;241m.\u001b[39mselect_graph(name)\n\u001b[1;32m 46\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel \u001b[38;5;241m=\u001b[39m model\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/falkordb/falkordb.py:101\u001b[0m, in \u001b[0;36mFalkorDB.__init__\u001b[0;34m(self, host, port, password, socket_timeout, socket_connect_timeout, socket_keepalive, socket_keepalive_options, connection_pool, unix_socket_path, encoding, encoding_errors, charset, errors, retry_on_timeout, retry_on_error, ssl, ssl_keyfile, ssl_certfile, ssl_cert_reqs, ssl_ca_certs, ssl_ca_path, ssl_ca_data, ssl_check_hostname, ssl_password, ssl_validate_ocsp, ssl_validate_ocsp_stapled, ssl_ocsp_context, ssl_ocsp_expected_cert, max_connections, single_connection_client, health_check_interval, client_name, lib_name, lib_version, username, retry, connect_func, credential_provider, protocol)\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 29\u001b[0m host\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocalhost\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 67\u001b[0m protocol\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m\n\u001b[1;32m 68\u001b[0m ):\n\u001b[1;32m 70\u001b[0m conn \u001b[38;5;241m=\u001b[39m redis\u001b[38;5;241m.\u001b[39mRedis(host\u001b[38;5;241m=\u001b[39mhost, port\u001b[38;5;241m=\u001b[39mport, db\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, password\u001b[38;5;241m=\u001b[39mpassword,\n\u001b[1;32m 71\u001b[0m socket_timeout\u001b[38;5;241m=\u001b[39msocket_timeout,\n\u001b[1;32m 72\u001b[0m socket_connect_timeout\u001b[38;5;241m=\u001b[39msocket_connect_timeout,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 98\u001b[0m credential_provider\u001b[38;5;241m=\u001b[39mcredential_provider,\n\u001b[1;32m 99\u001b[0m protocol\u001b[38;5;241m=\u001b[39mprotocol)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mIs_Sentinel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconn\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[1;32m 102\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msentinel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice_name \u001b[38;5;241m=\u001b[39m Sentinel_Conn(conn, ssl)\n\u001b[1;32m 103\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msentinel\u001b[38;5;241m.\u001b[39mmaster_for(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice_name, ssl\u001b[38;5;241m=\u001b[39mssl)\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/falkordb/sentinel.py:5\u001b[0m, in \u001b[0;36mIs_Sentinel\u001b[0;34m(conn)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mIs_Sentinel\u001b[39m(conn):\n\u001b[0;32m----> 5\u001b[0m info \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minfo\u001b[49m\u001b[43m(\u001b[49m\u001b[43msection\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mserver\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mredis_mode\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m info \u001b[38;5;129;01mand\u001b[39;00m info[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mredis_mode\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msentinel\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/commands/core.py:1000\u001b[0m, in \u001b[0;36mManagementCommands.info\u001b[0;34m(self, section, *args, **kwargs)\u001b[0m\n\u001b[1;32m 998\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexecute_command(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mINFO\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 999\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1000\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute_command\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mINFO\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msection\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/client.py:559\u001b[0m, in \u001b[0;36mRedis.execute_command\u001b[0;34m(self, *args, **options)\u001b[0m\n\u001b[1;32m 558\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexecute_command\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions):\n\u001b[0;32m--> 559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_command\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/client.py:565\u001b[0m, in \u001b[0;36mRedis._execute_command\u001b[0;34m(self, *args, **options)\u001b[0m\n\u001b[1;32m 563\u001b[0m pool \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection_pool\n\u001b[1;32m 564\u001b[0m command_name \u001b[38;5;241m=\u001b[39m args[\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m--> 565\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection \u001b[38;5;129;01mor\u001b[39;00m \u001b[43mpool\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_connection\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcommand_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 566\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 567\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m conn\u001b[38;5;241m.\u001b[39mretry\u001b[38;5;241m.\u001b[39mcall_with_retry(\n\u001b[1;32m 568\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_send_command_parse_response(\n\u001b[1;32m 569\u001b[0m conn, command_name, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions\n\u001b[1;32m 570\u001b[0m ),\n\u001b[1;32m 571\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m error: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_disconnect_raise(conn, error),\n\u001b[1;32m 572\u001b[0m )\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:1423\u001b[0m, in \u001b[0;36mConnectionPool.get_connection\u001b[0;34m(self, command_name, *keys, **options)\u001b[0m\n\u001b[1;32m 1419\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_in_use_connections\u001b[38;5;241m.\u001b[39madd(connection)\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;66;03m# ensure this connection is connected to Redis\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1424\u001b[0m \u001b[38;5;66;03m# connections that the pool provides should be ready to send\u001b[39;00m\n\u001b[1;32m 1425\u001b[0m \u001b[38;5;66;03m# a command. if not, the connection was either returned to the\u001b[39;00m\n\u001b[1;32m 1426\u001b[0m \u001b[38;5;66;03m# pool before all data has been read or the socket has been\u001b[39;00m\n\u001b[1;32m 1427\u001b[0m \u001b[38;5;66;03m# closed. either way, reconnect and verify everything is good.\u001b[39;00m\n\u001b[1;32m 1428\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:364\u001b[0m, in \u001b[0;36mAbstractConnection.connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTimeoutError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTimeout connecting to server\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 363\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m--> 364\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_error_message(e))\n\u001b[1;32m 366\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sock \u001b[38;5;241m=\u001b[39m sock\n\u001b[1;32m 367\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", - "\u001b[0;31mConnectionError\u001b[0m: Error 111 connecting to 127.0.0.1:3000. Connection refused." + "name": "stderr", + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], + "source": [ + "import os\n", + "\n", + "import autogen\n", + "\n", + "config_list = autogen.config_list_from_json(env_or_file=\"../OAI_CONFIG_LIST\")\n", + "os.environ[\"OPENAI_API_KEY\"] = config_list[0][\"api_key\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], "source": [ "from graphrag_sdk.schema import Schema\n", "\n", @@ -129,8 +127,8 @@ "\n", "query_engine = FalkorGraphQueryEngine(\n", " name=\"IMDB\",\n", - " host=\"127.0.0.1\",\n", - " port=3000,\n", + " host=\"172.18.0.3\",\n", + " port=6379,\n", " schema=movie_schema,\n", ")\n", "\n", @@ -140,7 +138,7 @@ "\n", "# Query your data\n", "question = \"Name a few actors who've played in 'The Matrix'\"\n", - "answer, messages = query_engine.query(question)\n", + "answer = query_engine.query(question)\n", "print(f\"Answer: {answer}\")" ] } From 0f1854d55c30d1dffdcc8da6d2898014ae91950f Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Tue, 29 Oct 2024 03:05:56 +0000 Subject: [PATCH 3/7] Implement single question FalkorDB Graph Rag Agent Capability --- .../graph_rag/falkor_graph_rag_capability.py | 86 ++++++++++++++ .../contrib/graph_rag/graph_rag_capability.py | 2 +- notebook/agentchat_graph_rag_falkordb.ipynb | 111 +++++++++++++++--- 3 files changed, 180 insertions(+), 19 deletions(-) create mode 100644 autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py diff --git a/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py b/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py new file mode 100644 index 000000000000..4db190e4177d --- /dev/null +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py @@ -0,0 +1,86 @@ +from typing import Dict, Optional, Union + +from autogen import UserProxyAgent + +from .falkor_graph_query_engine import FalkorGraphQueryEngine, FalkorGraphQueryResult +from .graph_rag_capability import GraphRagCapability + + +class FalkorGraphRagCapability(GraphRagCapability): + """ + A graph rag capability uses a graph query engine to give a conversable agent the graph rag ability. + + An agent class with graph rag capability could + 1. create a graph in the underlying database with input documents. + 2. retrieved relevant information based on messages received by the agent. + 3. generate answers from retrieved information and send messages back. + + For example, + graph_query_engine = GraphQueryEngine(...) + graph_query_engine.init_db([Document(doc1), Document(doc2), ...]) + + graph_rag_agent = ConversableAgent( + name="graph_rag_agent", + max_consecutive_auto_reply=3, + ... + ) + graph_rag_capability = GraphRagCapbility(graph_query_engine) + graph_rag_capability.add_to_agent(graph_rag_agent) + + user_proxy = UserProxyAgent( + name="user_proxy", + code_execution_config=False, + is_termination_msg=lambda msg: "TERMINATE" in msg["content"], + human_input_mode="ALWAYS", + ) + user_proxy.initiate_chat(graph_rag_agent, message="Name a few actors who've played in 'The Matrix'") + + # ChatResult( + # chat_id=None, + # chat_history=[ + # {'content': 'Name a few actors who've played in \'The Matrix\'', 'role': 'graph_rag_agent'}, + # {'content': 'A few actors who have played in The Matrix are: + # - Keanu Reeves + # - Laurence Fishburne + # - Carrie-Anne Moss + # - Hugo Weaving', + # 'role': 'user_proxy'}, + # ...) + + """ + + def __init__(self, query_engine: FalkorGraphQueryEngine): + """ + initialize graph rag capability with a graph query engine + """ + self.query_engine = query_engine + + def add_to_agent(self, agent: UserProxyAgent): + self.graph_rag_agent = agent + + # Validate the agent config + if agent.llm_config not in (None, False): + raise Exception( + "Graph rag capability limits the query to graph DB, llm_config must be a dict or False or None." + ) + + # Register a hook for processing the last message. + agent.register_hook(hookable_method="process_last_received_message", hook=self.process_last_received_message) + + # Append extra info to the system message. + agent.update_system_message( + agent.system_message + "\nYou've been given the special ability to use graph rag to retrieve information." + ) + + def process_last_received_message(self, message: Union[Dict, str]): + question = self._get_last_question(message) + result: FalkorGraphQueryResult = self.query_engine.query(question) + return result.answer + + def _get_last_question(self, message: Union[Dict, str]): + if isinstance(message, str): + return message + if isinstance(message, Dict): + if "content" in message: + return message["content"] + return None diff --git a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py index f4a44cab465a..b6412305e069 100644 --- a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +++ b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py @@ -51,6 +51,6 @@ def __init__(self, query_engine: GraphQueryEngine): """ initialize graph rag capability with a graph query engine """ - self.query_engine = query_engine + ... def add_to_agent(self, agent: ConversableAgent): ... diff --git a/notebook/agentchat_graph_rag_falkordb.ipynb b/notebook/agentchat_graph_rag_falkordb.ipynb index 9167fecec81f..7a56f923316c 100644 --- a/notebook/agentchat_graph_rag_falkordb.ipynb +++ b/notebook/agentchat_graph_rag_falkordb.ipynb @@ -2,9 +2,16 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 15, "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "1136172.97s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" + ] + }, { "name": "stdout", "output_type": "stream", @@ -31,7 +38,23 @@ "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.0.5)\n", "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.14.0)\n", "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.11/site-packages (from beautifulsoup4->bs4<0.0.3,>=0.0.2->graphrag_sdk==0.1.3b0) (2.6)\n", - "Note: you may need to restart the kernel to use updated packages.\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "1136178.53s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Obtaining file:///workspaces/autogen\n", " Installing build dependencies ... \u001b[?25ldone\n", @@ -65,8 +88,8 @@ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests>=2.26.0->docker->autogen==0.3.0) (3.3.2)\n", "Building wheels for collected packages: autogen\n", " Building editable for autogen (pyproject.toml) ... \u001b[?25ldone\n", - "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=e98cc4d562793824fed6eb82a0882d331170bf9161260ff45ff3dc32570f0bb5\n", - " Stored in directory: /tmp/pip-ephem-wheel-cache-0wb4pw7w/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", + "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=20d08c707c24d67ad0f271f71cef54b81aa54a044da58030e67204fc994ee53b\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-038ayx1o/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", "Successfully built autogen\n", "Installing collected packages: autogen\n", " Attempting uninstall: autogen\n", @@ -74,6 +97,9 @@ " Uninstalling autogen-0.3.0:\n", " Successfully uninstalled autogen-0.3.0\n", "Successfully installed autogen-0.3.0\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } @@ -85,18 +111,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 8, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n" - ] - } - ], + "outputs": [], "source": [ "import os\n", "\n", @@ -108,7 +125,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -134,13 +151,71 @@ "\n", "input_documents = [Document(doctype=DocumentType.TEXT, path_or_url=input_path)]\n", "\n", - "query_engine.init_db(input_doc=input_documents)\n", - "\n", + "query_engine.init_db(input_doc=input_documents)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Answer: FalkorGraphQueryResult(answer=\"A few actors who've played in 'The Matrix' are:\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", results=[], messages=[{'role': 'system', 'content': 'You are a graph database expert fluent in the Cypher query language\\n with access to the following knowledge graph: The knowladge graph schema is as follows:\\nThe graph contains the following node labels:\\nMovie\\nActor\\nThe Movie label is associated with the following attribute(s):\\ntitle which is of type str\\nThe Actor label is associated with the following attribute(s):\\nname which is of type str\\nThe graph contains the following relationship types:\\nACTED\\nThe ACTED relationship connects the following labels:\\nActor is connected via ACTED to Movie\\nThis is the end of the knowladge graph schema description.\\n'}, {'role': 'user', 'content': \"Name a few actors who've played in 'The Matrix'\"}, ChatCompletionMessage(content=None, refusal=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_COgmJtXljCeUL7BQzyf0iY2n', function=Function(arguments='{\\n \"query\": \"MATCH (m:Movie {title: \\'The Matrix\\'})<-[:ACTED]-(a:Actor) RETURN a.name\"\\n}', name='run_cypher_query'), type='function')]), {'tool_call_id': 'call_COgmJtXljCeUL7BQzyf0iY2n', 'role': 'tool', 'name': 'run_cypher_query', 'content': \"[['Keanu Reeves'], ['Laurence Fishburne'], ['Carrie-Anne Moss'], ['Hugo Weaving']]\"}, {'role': 'assistant', 'content': \"A few actors who've played in 'The Matrix' are:\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\"}])\n" + ] + } + ], + "source": [ "# Query your data\n", "question = \"Name a few actors who've played in 'The Matrix'\"\n", "answer = query_engine.query(question)\n", "print(f\"Answer: {answer}\")" ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33muser_proxy\u001b[0m (to user_proxy):\n", + "\n", + "Name a few actors who've played in 'The Matrix'\n", + "\n", + "--------------------------------------------------------------------------------\n" + ] + }, + { + "data": { + "text/plain": [ + "ChatResult(chat_id=None, chat_history=[{'content': \"Name a few actors who've played in 'The Matrix'\", 'role': 'assistant', 'name': 'user_proxy'}, {'content': \"The following actors have played in 'The Matrix':\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", 'role': 'user', 'name': 'user_proxy'}], summary=\"The following actors have played in 'The Matrix':\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", cost={'usage_including_cached_inference': {'total_cost': 0}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=['exit'])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from autogen import UserProxyAgent\n", + "from autogen.agentchat.contrib.graph_rag.falkor_graph_rag_capability import FalkorGraphRagCapability\n", + "\n", + "graph_rag_agent = UserProxyAgent(\n", + " name=\"user_proxy\",\n", + " code_execution_config=False,\n", + " is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n", + " human_input_mode=\"ALWAYS\",\n", + ")\n", + "graph_rag_capability = FalkorGraphRagCapability(query_engine)\n", + "graph_rag_capability.add_to_agent(graph_rag_agent)\n", + "\n", + "graph_rag_agent.initiate_chat(graph_rag_agent, message=\"Name a few actors who've played in 'The Matrix'\")" + ] } ], "metadata": { From a24c16a38a27946a859e2e1c78502915c90b9b90 Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:43:58 +0000 Subject: [PATCH 4/7] Falkor DB integration init commit --- .../graph_rag/falkor_graph_query_engine.py | 4 +- .../contrib/graph_rag/graph_rag_capability.py | 2 +- notebook/agentchat_graph_rag_falkordb.ipynb | 169 ++++++++++++++++++ setup.py | 2 +- 4 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 notebook/agentchat_graph_rag_falkordb.ipynb 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 0bb1f51a05b3..4c216f969c93 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -30,7 +30,7 @@ def __init__( ): """ Initialize a Falkor DB knowledge graph. - Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/main/graphrag_sdk/kg.py + Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/2-move-away-from-sql-to-json-ontology-detection/graphrag_sdk/kg.py Args: name (str): Knowledge graph name. @@ -39,7 +39,7 @@ def __init__( 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 + schema: Falkor DB knowledge graph schema (ontology), https://github.com/FalkorDB/GraphRAG-SDK/blob/2-move-away-from-sql-to-json-ontology-detection/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) diff --git a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py index b6412305e069..f4a44cab465a 100644 --- a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +++ b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py @@ -51,6 +51,6 @@ def __init__(self, query_engine: GraphQueryEngine): """ initialize graph rag capability with a graph query engine """ - ... + self.query_engine = query_engine def add_to_agent(self, agent: ConversableAgent): ... diff --git a/notebook/agentchat_graph_rag_falkordb.ipynb b/notebook/agentchat_graph_rag_falkordb.ipynb new file mode 100644 index 000000000000..f7d74787a680 --- /dev/null +++ b/notebook/agentchat_graph_rag_falkordb.ipynb @@ -0,0 +1,169 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: graphrag_sdk==0.1.3b0 in /home/autogen-ai/.local/lib/python3.11/site-packages (0.1.3b0)\n", + "Requirement already satisfied: bs4<0.0.3,>=0.0.2 in /home/autogen-ai/.local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (0.0.2)\n", + "Requirement already satisfied: falkordb<2.0.0,>=1.0.4 in /home/autogen-ai/.local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (1.0.8)\n", + "Requirement already satisfied: openai<2.0.0,>=1.30.3 in /usr/local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (1.49.0)\n", + "Requirement already satisfied: sqlalchemy<3.0.0,>=2.0.30 in /usr/local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (2.0.35)\n", + "Requirement already satisfied: typing-extensions<5.0.0,>=4.12.1 in /usr/local/lib/python3.11/site-packages (from graphrag_sdk==0.1.3b0) (4.12.2)\n", + "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.11/site-packages (from bs4<0.0.3,>=0.0.2->graphrag_sdk==0.1.3b0) (4.12.3)\n", + "Requirement already satisfied: redis<6.0.0,>=5.0.1 in /home/autogen-ai/.local/lib/python3.11/site-packages (from falkordb<2.0.0,>=1.0.4->graphrag_sdk==0.1.3b0) (5.1.0)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (4.6.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.9.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.27.2)\n", + "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.5.0)\n", + "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.10.9)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/site-packages (from openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (4.66.5)\n", + "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.11/site-packages (from sqlalchemy<3.0.0,>=2.0.30->graphrag_sdk==0.1.3b0) (3.1.1)\n", + "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (3.10)\n", + "Requirement already satisfied: certifi in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (2024.8.30)\n", + "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.0.5)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.14.0)\n", + "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.11/site-packages (from beautifulsoup4->bs4<0.0.3,>=0.0.2->graphrag_sdk==0.1.3b0) (2.6)\n", + "Note: you may need to restart the kernel to use updated packages.\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Obtaining file:///workspaces/autogen\n", + " Installing build dependencies ... \u001b[?25ldone\n", + "\u001b[?25h Checking if build backend supports build_editable ... \u001b[?25ldone\n", + "\u001b[?25h Getting requirements to build editable ... \u001b[?25ldone\n", + "\u001b[?25h Preparing editable metadata (pyproject.toml) ... \u001b[?25ldone\n", + "\u001b[?25hRequirement already satisfied: openai>=1.3 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.49.0)\n", + "Requirement already satisfied: diskcache in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (5.6.3)\n", + "Requirement already satisfied: termcolor in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (2.4.0)\n", + "Requirement already satisfied: flaml in /home/autogen-ai/.local/lib/python3.11/site-packages (from autogen==0.3.0) (2.2.0)\n", + "Requirement already satisfied: numpy<2,>=1.17.0 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.26.4)\n", + "Requirement already satisfied: python-dotenv in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.0.1)\n", + "Requirement already satisfied: tiktoken in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (0.7.0)\n", + "Requirement already satisfied: pydantic!=2.6.0,<3,>=1.10 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.10.9)\n", + "Requirement already satisfied: docker in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (7.1.0)\n", + "Requirement already satisfied: packaging in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (24.1)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.6.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (1.9.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (0.27.2)\n", + "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (0.5.0)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.66.5)\n", + "Requirement already satisfied: typing-extensions<5,>=4.11 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.12.2)\n", + "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.11/site-packages (from docker->autogen==0.3.0) (2.32.3)\n", + "Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.11/site-packages (from docker->autogen==0.3.0) (2.2.3)\n", + "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.11/site-packages (from tiktoken->autogen==0.3.0) (2024.9.11)\n", + "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai>=1.3->autogen==0.3.0) (3.10)\n", + "Requirement already satisfied: certifi in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (2024.8.30)\n", + "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (1.0.5)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (0.14.0)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests>=2.26.0->docker->autogen==0.3.0) (3.3.2)\n", + "Building wheels for collected packages: autogen\n", + " Building editable for autogen (pyproject.toml) ... \u001b[?25ldone\n", + "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=988e26254d7445afe8e6de69c9b7e3c8a30a4207fda4a88a503050fc0ebc88c9\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-emvq4y0f/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", + "Successfully built autogen\n", + "Installing collected packages: autogen\n", + "Successfully installed autogen-0.3.0\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install graphrag_sdk==0.1.3b0\n", + "%pip install -e /workspaces/autogen" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "ConnectionError", + "evalue": "Error 111 connecting to 127.0.0.1:3000. Connection refused.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mConnectionRefusedError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:358\u001b[0m, in \u001b[0;36mAbstractConnection.connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 358\u001b[0m sock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mretry\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall_with_retry\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 359\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43merror\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdisconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43merror\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 360\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 361\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mtimeout:\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/retry.py:62\u001b[0m, in \u001b[0;36mRetry.call_with_retry\u001b[0;34m(self, do, fail)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 62\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdo\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_supported_errors \u001b[38;5;28;01mas\u001b[39;00m error:\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:359\u001b[0m, in \u001b[0;36mAbstractConnection.connect..\u001b[0;34m()\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 358\u001b[0m sock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mretry\u001b[38;5;241m.\u001b[39mcall_with_retry(\n\u001b[0;32m--> 359\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m, \u001b[38;5;28;01mlambda\u001b[39;00m error: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdisconnect(error)\n\u001b[1;32m 360\u001b[0m )\n\u001b[1;32m 361\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mtimeout:\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:731\u001b[0m, in \u001b[0;36mConnection._connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 730\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 731\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m err\n\u001b[1;32m 732\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msocket.getaddrinfo returned an empty list\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:719\u001b[0m, in \u001b[0;36mConnection._connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 718\u001b[0m \u001b[38;5;66;03m# connect\u001b[39;00m\n\u001b[0;32m--> 719\u001b[0m \u001b[43msock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43msocket_address\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 721\u001b[0m \u001b[38;5;66;03m# set the socket_timeout now that we're connected\u001b[39;00m\n", + "\u001b[0;31mConnectionRefusedError\u001b[0m: [Errno 111] Connection refused", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mConnectionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m movie \u001b[38;5;241m=\u001b[39m movie_schema\u001b[38;5;241m.\u001b[39madd_entity(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMovie\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39madd_attribute(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtitle\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28mstr\u001b[39m, unique\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 11\u001b[0m movie_schema\u001b[38;5;241m.\u001b[39madd_relation(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mACTED\u001b[39m\u001b[38;5;124m\"\u001b[39m, actor, movie)\n\u001b[0;32m---> 13\u001b[0m query_engine \u001b[38;5;241m=\u001b[39m \u001b[43mFalkorGraphQueryEngine\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mIMDB\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 15\u001b[0m \u001b[43m \u001b[49m\u001b[43mhost\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m127.0.0.1\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3000\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[43mschema\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmovie_schema\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 20\u001b[0m input_documents \u001b[38;5;241m=\u001b[39m [Document(doctype\u001b[38;5;241m=\u001b[39mDocumentType\u001b[38;5;241m.\u001b[39mTEXT, path_or_url\u001b[38;5;241m=\u001b[39minput_path)]\n\u001b[1;32m 22\u001b[0m query_engine\u001b[38;5;241m.\u001b[39minit_db(input_doc\u001b[38;5;241m=\u001b[39minput_documents)\n", + "File \u001b[0;32m/workspaces/autogen/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py:45\u001b[0m, in \u001b[0;36mFalkorGraphQueryEngine.__init__\u001b[0;34m(self, name, host, port, username, password, model, schema)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 22\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 23\u001b[0m name: \u001b[38;5;28mstr\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 29\u001b[0m schema: Schema \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 30\u001b[0m ):\n\u001b[1;32m 31\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 32\u001b[0m \u001b[38;5;124;03m Initialize a Falkor DB knowledge graph.\u001b[39;00m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;124;03m Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/2-move-away-from-sql-to-json-ontology-detection/graphrag_sdk/kg.py\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;124;03m If None, Falkor DB will auto generate a schema from the input docs.\u001b[39;00m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 45\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mknowledge_graph \u001b[38;5;241m=\u001b[39m \u001b[43mKnowledgeGraph\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43musername\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mschema\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/graphrag_sdk/kg.py:43\u001b[0m, in \u001b[0;36mKnowledgeGraph.__init__\u001b[0;34m(self, name, host, port, username, password, model, schema)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname should be a non empty string\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 42\u001b[0m \u001b[38;5;66;03m# connect to database\u001b[39;00m\n\u001b[0;32m---> 43\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdb \u001b[38;5;241m=\u001b[39m \u001b[43mFalkorDB\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhost\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43musername\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43musername\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpassword\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgraph \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdb\u001b[38;5;241m.\u001b[39mselect_graph(name)\n\u001b[1;32m 46\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel \u001b[38;5;241m=\u001b[39m model\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/falkordb/falkordb.py:101\u001b[0m, in \u001b[0;36mFalkorDB.__init__\u001b[0;34m(self, host, port, password, socket_timeout, socket_connect_timeout, socket_keepalive, socket_keepalive_options, connection_pool, unix_socket_path, encoding, encoding_errors, charset, errors, retry_on_timeout, retry_on_error, ssl, ssl_keyfile, ssl_certfile, ssl_cert_reqs, ssl_ca_certs, ssl_ca_path, ssl_ca_data, ssl_check_hostname, ssl_password, ssl_validate_ocsp, ssl_validate_ocsp_stapled, ssl_ocsp_context, ssl_ocsp_expected_cert, max_connections, single_connection_client, health_check_interval, client_name, lib_name, lib_version, username, retry, connect_func, credential_provider, protocol)\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 29\u001b[0m host\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocalhost\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 67\u001b[0m protocol\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m\n\u001b[1;32m 68\u001b[0m ):\n\u001b[1;32m 70\u001b[0m conn \u001b[38;5;241m=\u001b[39m redis\u001b[38;5;241m.\u001b[39mRedis(host\u001b[38;5;241m=\u001b[39mhost, port\u001b[38;5;241m=\u001b[39mport, db\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, password\u001b[38;5;241m=\u001b[39mpassword,\n\u001b[1;32m 71\u001b[0m socket_timeout\u001b[38;5;241m=\u001b[39msocket_timeout,\n\u001b[1;32m 72\u001b[0m socket_connect_timeout\u001b[38;5;241m=\u001b[39msocket_connect_timeout,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 98\u001b[0m credential_provider\u001b[38;5;241m=\u001b[39mcredential_provider,\n\u001b[1;32m 99\u001b[0m protocol\u001b[38;5;241m=\u001b[39mprotocol)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mIs_Sentinel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconn\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[1;32m 102\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msentinel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice_name \u001b[38;5;241m=\u001b[39m Sentinel_Conn(conn, ssl)\n\u001b[1;32m 103\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msentinel\u001b[38;5;241m.\u001b[39mmaster_for(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice_name, ssl\u001b[38;5;241m=\u001b[39mssl)\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/falkordb/sentinel.py:5\u001b[0m, in \u001b[0;36mIs_Sentinel\u001b[0;34m(conn)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mIs_Sentinel\u001b[39m(conn):\n\u001b[0;32m----> 5\u001b[0m info \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minfo\u001b[49m\u001b[43m(\u001b[49m\u001b[43msection\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mserver\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mredis_mode\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m info \u001b[38;5;129;01mand\u001b[39;00m info[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mredis_mode\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msentinel\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/commands/core.py:1000\u001b[0m, in \u001b[0;36mManagementCommands.info\u001b[0;34m(self, section, *args, **kwargs)\u001b[0m\n\u001b[1;32m 998\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexecute_command(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mINFO\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 999\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1000\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute_command\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mINFO\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msection\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/client.py:559\u001b[0m, in \u001b[0;36mRedis.execute_command\u001b[0;34m(self, *args, **options)\u001b[0m\n\u001b[1;32m 558\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexecute_command\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions):\n\u001b[0;32m--> 559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_command\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/client.py:565\u001b[0m, in \u001b[0;36mRedis._execute_command\u001b[0;34m(self, *args, **options)\u001b[0m\n\u001b[1;32m 563\u001b[0m pool \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection_pool\n\u001b[1;32m 564\u001b[0m command_name \u001b[38;5;241m=\u001b[39m args[\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m--> 565\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection \u001b[38;5;129;01mor\u001b[39;00m \u001b[43mpool\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_connection\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcommand_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 566\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 567\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m conn\u001b[38;5;241m.\u001b[39mretry\u001b[38;5;241m.\u001b[39mcall_with_retry(\n\u001b[1;32m 568\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_send_command_parse_response(\n\u001b[1;32m 569\u001b[0m conn, command_name, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions\n\u001b[1;32m 570\u001b[0m ),\n\u001b[1;32m 571\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m error: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_disconnect_raise(conn, error),\n\u001b[1;32m 572\u001b[0m )\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:1423\u001b[0m, in \u001b[0;36mConnectionPool.get_connection\u001b[0;34m(self, command_name, *keys, **options)\u001b[0m\n\u001b[1;32m 1419\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_in_use_connections\u001b[38;5;241m.\u001b[39madd(connection)\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;66;03m# ensure this connection is connected to Redis\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1424\u001b[0m \u001b[38;5;66;03m# connections that the pool provides should be ready to send\u001b[39;00m\n\u001b[1;32m 1425\u001b[0m \u001b[38;5;66;03m# a command. if not, the connection was either returned to the\u001b[39;00m\n\u001b[1;32m 1426\u001b[0m \u001b[38;5;66;03m# pool before all data has been read or the socket has been\u001b[39;00m\n\u001b[1;32m 1427\u001b[0m \u001b[38;5;66;03m# closed. either way, reconnect and verify everything is good.\u001b[39;00m\n\u001b[1;32m 1428\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", + "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:364\u001b[0m, in \u001b[0;36mAbstractConnection.connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTimeoutError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTimeout connecting to server\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 363\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m--> 364\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_error_message(e))\n\u001b[1;32m 366\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sock \u001b[38;5;241m=\u001b[39m sock\n\u001b[1;32m 367\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", + "\u001b[0;31mConnectionError\u001b[0m: Error 111 connecting to 127.0.0.1:3000. Connection refused." + ] + } + ], + "source": [ + "from graphrag_sdk.schema import Schema\n", + "\n", + "from autogen.agentchat.contrib.graph_rag.document import Document, DocumentType\n", + "from autogen.agentchat.contrib.graph_rag.falkor_graph_query_engine import FalkorGraphQueryEngine\n", + "\n", + "# Auto generate graph schema from unstructured data\n", + "input_path = \"../test/agentchat/contrib/graph_rag/the_matrix.txt\"\n", + "\n", + "movie_schema = Schema()\n", + "actor = movie_schema.add_entity(\"Actor\").add_attribute(\"name\", str, unique=True)\n", + "movie = movie_schema.add_entity(\"Movie\").add_attribute(\"title\", str, unique=True)\n", + "movie_schema.add_relation(\"ACTED\", actor, movie)\n", + "\n", + "query_engine = FalkorGraphQueryEngine(\n", + " name=\"IMDB\",\n", + " host=\"127.0.0.1\",\n", + " port=3000,\n", + " schema=movie_schema,\n", + ")\n", + "\n", + "input_documents = [Document(doctype=DocumentType.TEXT, path_or_url=input_path)]\n", + "\n", + "query_engine.init_db(input_doc=input_documents)\n", + "\n", + "# Query your data\n", + "question = \"Name a few actors who've played in 'The Matrix'\"\n", + "answer, messages = query_engine.query(question)\n", + "print(f\"Answer: {answer}\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.10" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/setup.py b/setup.py index 64004e772435..e22974b0f46e 100644 --- a/setup.py +++ b/setup.py @@ -60,7 +60,7 @@ retrieve_chat_pgvector = [*retrieve_chat, "pgvector>=0.2.5"] graph_rag_falkor_db = [ - "graphrag_sdk", + "graphrag_sdk==0.1.3b0", ] if current_os in ["Windows", "Darwin"]: From a289a43a393b6544ec4bdcec057609b0a9259b72 Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Tue, 15 Oct 2024 23:33:48 +0000 Subject: [PATCH 5/7] Integrate FalkorGraphQueryEngine --- .../graph_rag/falkor_graph_query_engine.py | 3 +- notebook/agentchat_graph_rag_falkordb.ipynb | 60 +++++++++---------- 2 files changed, 31 insertions(+), 32 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 4c216f969c93..d0670de102be 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py @@ -1,5 +1,5 @@ import os -from dataclasses import field +from dataclasses import dataclass, field from typing import List from graphrag_sdk import KnowledgeGraph, Source @@ -9,6 +9,7 @@ from .graph_query_engine import GraphStoreQueryResult +@dataclass class FalkorGraphQueryResult(GraphStoreQueryResult): messages: list = field(default_factory=list) diff --git a/notebook/agentchat_graph_rag_falkordb.ipynb b/notebook/agentchat_graph_rag_falkordb.ipynb index f7d74787a680..9167fecec81f 100644 --- a/notebook/agentchat_graph_rag_falkordb.ipynb +++ b/notebook/agentchat_graph_rag_falkordb.ipynb @@ -65,10 +65,14 @@ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests>=2.26.0->docker->autogen==0.3.0) (3.3.2)\n", "Building wheels for collected packages: autogen\n", " Building editable for autogen (pyproject.toml) ... \u001b[?25ldone\n", - "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=988e26254d7445afe8e6de69c9b7e3c8a30a4207fda4a88a503050fc0ebc88c9\n", - " Stored in directory: /tmp/pip-ephem-wheel-cache-emvq4y0f/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", + "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=e98cc4d562793824fed6eb82a0882d331170bf9161260ff45ff3dc32570f0bb5\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-0wb4pw7w/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", "Successfully built autogen\n", "Installing collected packages: autogen\n", + " Attempting uninstall: autogen\n", + " Found existing installation: autogen 0.3.0\n", + " Uninstalling autogen-0.3.0:\n", + " Successfully uninstalled autogen-0.3.0\n", "Successfully installed autogen-0.3.0\n", "Note: you may need to restart the kernel to use updated packages.\n" ] @@ -81,38 +85,32 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": {}, "outputs": [ { - "ename": "ConnectionError", - "evalue": "Error 111 connecting to 127.0.0.1:3000. Connection refused.", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mConnectionRefusedError\u001b[0m Traceback (most recent call last)", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:358\u001b[0m, in \u001b[0;36mAbstractConnection.connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 358\u001b[0m sock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mretry\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall_with_retry\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 359\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43merror\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdisconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43merror\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 360\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 361\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mtimeout:\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/retry.py:62\u001b[0m, in \u001b[0;36mRetry.call_with_retry\u001b[0;34m(self, do, fail)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 62\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdo\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_supported_errors \u001b[38;5;28;01mas\u001b[39;00m error:\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:359\u001b[0m, in \u001b[0;36mAbstractConnection.connect..\u001b[0;34m()\u001b[0m\n\u001b[1;32m 357\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 358\u001b[0m sock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mretry\u001b[38;5;241m.\u001b[39mcall_with_retry(\n\u001b[0;32m--> 359\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m, \u001b[38;5;28;01mlambda\u001b[39;00m error: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdisconnect(error)\n\u001b[1;32m 360\u001b[0m )\n\u001b[1;32m 361\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mtimeout:\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:731\u001b[0m, in \u001b[0;36mConnection._connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 730\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 731\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m err\n\u001b[1;32m 732\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msocket.getaddrinfo returned an empty list\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:719\u001b[0m, in \u001b[0;36mConnection._connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 718\u001b[0m \u001b[38;5;66;03m# connect\u001b[39;00m\n\u001b[0;32m--> 719\u001b[0m \u001b[43msock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43msocket_address\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 721\u001b[0m \u001b[38;5;66;03m# set the socket_timeout now that we're connected\u001b[39;00m\n", - "\u001b[0;31mConnectionRefusedError\u001b[0m: [Errno 111] Connection refused", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[0;31mConnectionError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[4], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m movie \u001b[38;5;241m=\u001b[39m movie_schema\u001b[38;5;241m.\u001b[39madd_entity(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMovie\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39madd_attribute(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtitle\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28mstr\u001b[39m, unique\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 11\u001b[0m movie_schema\u001b[38;5;241m.\u001b[39madd_relation(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mACTED\u001b[39m\u001b[38;5;124m\"\u001b[39m, actor, movie)\n\u001b[0;32m---> 13\u001b[0m query_engine \u001b[38;5;241m=\u001b[39m \u001b[43mFalkorGraphQueryEngine\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mIMDB\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 15\u001b[0m \u001b[43m \u001b[49m\u001b[43mhost\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m127.0.0.1\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3000\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[43mschema\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmovie_schema\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 20\u001b[0m input_documents \u001b[38;5;241m=\u001b[39m [Document(doctype\u001b[38;5;241m=\u001b[39mDocumentType\u001b[38;5;241m.\u001b[39mTEXT, path_or_url\u001b[38;5;241m=\u001b[39minput_path)]\n\u001b[1;32m 22\u001b[0m query_engine\u001b[38;5;241m.\u001b[39minit_db(input_doc\u001b[38;5;241m=\u001b[39minput_documents)\n", - "File \u001b[0;32m/workspaces/autogen/autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py:45\u001b[0m, in \u001b[0;36mFalkorGraphQueryEngine.__init__\u001b[0;34m(self, name, host, port, username, password, model, schema)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 22\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 23\u001b[0m name: \u001b[38;5;28mstr\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 29\u001b[0m schema: Schema \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 30\u001b[0m ):\n\u001b[1;32m 31\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 32\u001b[0m \u001b[38;5;124;03m Initialize a Falkor DB knowledge graph.\u001b[39;00m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;124;03m Please also refer to https://github.com/FalkorDB/GraphRAG-SDK/blob/2-move-away-from-sql-to-json-ontology-detection/graphrag_sdk/kg.py\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;124;03m If None, Falkor DB will auto generate a schema from the input docs.\u001b[39;00m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 45\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mknowledge_graph \u001b[38;5;241m=\u001b[39m \u001b[43mKnowledgeGraph\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43musername\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mschema\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/graphrag_sdk/kg.py:43\u001b[0m, in \u001b[0;36mKnowledgeGraph.__init__\u001b[0;34m(self, name, host, port, username, password, model, schema)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname should be a non empty string\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 42\u001b[0m \u001b[38;5;66;03m# connect to database\u001b[39;00m\n\u001b[0;32m---> 43\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdb \u001b[38;5;241m=\u001b[39m \u001b[43mFalkorDB\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhost\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mport\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43musername\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43musername\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpassword\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgraph \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdb\u001b[38;5;241m.\u001b[39mselect_graph(name)\n\u001b[1;32m 46\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel \u001b[38;5;241m=\u001b[39m model\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/falkordb/falkordb.py:101\u001b[0m, in \u001b[0;36mFalkorDB.__init__\u001b[0;34m(self, host, port, password, socket_timeout, socket_connect_timeout, socket_keepalive, socket_keepalive_options, connection_pool, unix_socket_path, encoding, encoding_errors, charset, errors, retry_on_timeout, retry_on_error, ssl, ssl_keyfile, ssl_certfile, ssl_cert_reqs, ssl_ca_certs, ssl_ca_path, ssl_ca_data, ssl_check_hostname, ssl_password, ssl_validate_ocsp, ssl_validate_ocsp_stapled, ssl_ocsp_context, ssl_ocsp_expected_cert, max_connections, single_connection_client, health_check_interval, client_name, lib_name, lib_version, username, retry, connect_func, credential_provider, protocol)\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 29\u001b[0m host\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocalhost\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 67\u001b[0m protocol\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m\n\u001b[1;32m 68\u001b[0m ):\n\u001b[1;32m 70\u001b[0m conn \u001b[38;5;241m=\u001b[39m redis\u001b[38;5;241m.\u001b[39mRedis(host\u001b[38;5;241m=\u001b[39mhost, port\u001b[38;5;241m=\u001b[39mport, db\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, password\u001b[38;5;241m=\u001b[39mpassword,\n\u001b[1;32m 71\u001b[0m socket_timeout\u001b[38;5;241m=\u001b[39msocket_timeout,\n\u001b[1;32m 72\u001b[0m socket_connect_timeout\u001b[38;5;241m=\u001b[39msocket_connect_timeout,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 98\u001b[0m credential_provider\u001b[38;5;241m=\u001b[39mcredential_provider,\n\u001b[1;32m 99\u001b[0m protocol\u001b[38;5;241m=\u001b[39mprotocol)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mIs_Sentinel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconn\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[1;32m 102\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msentinel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice_name \u001b[38;5;241m=\u001b[39m Sentinel_Conn(conn, ssl)\n\u001b[1;32m 103\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msentinel\u001b[38;5;241m.\u001b[39mmaster_for(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice_name, ssl\u001b[38;5;241m=\u001b[39mssl)\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/falkordb/sentinel.py:5\u001b[0m, in \u001b[0;36mIs_Sentinel\u001b[0;34m(conn)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mIs_Sentinel\u001b[39m(conn):\n\u001b[0;32m----> 5\u001b[0m info \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minfo\u001b[49m\u001b[43m(\u001b[49m\u001b[43msection\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mserver\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mredis_mode\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m info \u001b[38;5;129;01mand\u001b[39;00m info[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mredis_mode\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msentinel\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/commands/core.py:1000\u001b[0m, in \u001b[0;36mManagementCommands.info\u001b[0;34m(self, section, *args, **kwargs)\u001b[0m\n\u001b[1;32m 998\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexecute_command(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mINFO\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 999\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1000\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute_command\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mINFO\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msection\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/client.py:559\u001b[0m, in \u001b[0;36mRedis.execute_command\u001b[0;34m(self, *args, **options)\u001b[0m\n\u001b[1;32m 558\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexecute_command\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions):\n\u001b[0;32m--> 559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_command\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/client.py:565\u001b[0m, in \u001b[0;36mRedis._execute_command\u001b[0;34m(self, *args, **options)\u001b[0m\n\u001b[1;32m 563\u001b[0m pool \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection_pool\n\u001b[1;32m 564\u001b[0m command_name \u001b[38;5;241m=\u001b[39m args[\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m--> 565\u001b[0m conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection \u001b[38;5;129;01mor\u001b[39;00m \u001b[43mpool\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_connection\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcommand_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 566\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 567\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m conn\u001b[38;5;241m.\u001b[39mretry\u001b[38;5;241m.\u001b[39mcall_with_retry(\n\u001b[1;32m 568\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_send_command_parse_response(\n\u001b[1;32m 569\u001b[0m conn, command_name, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions\n\u001b[1;32m 570\u001b[0m ),\n\u001b[1;32m 571\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m error: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_disconnect_raise(conn, error),\n\u001b[1;32m 572\u001b[0m )\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:1423\u001b[0m, in \u001b[0;36mConnectionPool.get_connection\u001b[0;34m(self, command_name, *keys, **options)\u001b[0m\n\u001b[1;32m 1419\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_in_use_connections\u001b[38;5;241m.\u001b[39madd(connection)\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;66;03m# ensure this connection is connected to Redis\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1424\u001b[0m \u001b[38;5;66;03m# connections that the pool provides should be ready to send\u001b[39;00m\n\u001b[1;32m 1425\u001b[0m \u001b[38;5;66;03m# a command. if not, the connection was either returned to the\u001b[39;00m\n\u001b[1;32m 1426\u001b[0m \u001b[38;5;66;03m# pool before all data has been read or the socket has been\u001b[39;00m\n\u001b[1;32m 1427\u001b[0m \u001b[38;5;66;03m# closed. either way, reconnect and verify everything is good.\u001b[39;00m\n\u001b[1;32m 1428\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", - "File \u001b[0;32m~/.local/lib/python3.11/site-packages/redis/connection.py:364\u001b[0m, in \u001b[0;36mAbstractConnection.connect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTimeoutError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTimeout connecting to server\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 363\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m--> 364\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_error_message(e))\n\u001b[1;32m 366\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sock \u001b[38;5;241m=\u001b[39m sock\n\u001b[1;32m 367\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", - "\u001b[0;31mConnectionError\u001b[0m: Error 111 connecting to 127.0.0.1:3000. Connection refused." + "name": "stderr", + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], + "source": [ + "import os\n", + "\n", + "import autogen\n", + "\n", + "config_list = autogen.config_list_from_json(env_or_file=\"../OAI_CONFIG_LIST\")\n", + "os.environ[\"OPENAI_API_KEY\"] = config_list[0][\"api_key\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], "source": [ "from graphrag_sdk.schema import Schema\n", "\n", @@ -129,8 +127,8 @@ "\n", "query_engine = FalkorGraphQueryEngine(\n", " name=\"IMDB\",\n", - " host=\"127.0.0.1\",\n", - " port=3000,\n", + " host=\"172.18.0.3\",\n", + " port=6379,\n", " schema=movie_schema,\n", ")\n", "\n", @@ -140,7 +138,7 @@ "\n", "# Query your data\n", "question = \"Name a few actors who've played in 'The Matrix'\"\n", - "answer, messages = query_engine.query(question)\n", + "answer = query_engine.query(question)\n", "print(f\"Answer: {answer}\")" ] } From 11b7ce9c7286210184a9fd8ae5e22e43d120742e Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Tue, 29 Oct 2024 03:05:56 +0000 Subject: [PATCH 6/7] Implement single question FalkorDB Graph Rag Agent Capability --- .../graph_rag/falkor_graph_rag_capability.py | 86 ++++++++++++++ .../contrib/graph_rag/graph_rag_capability.py | 2 +- notebook/agentchat_graph_rag_falkordb.ipynb | 111 +++++++++++++++--- 3 files changed, 180 insertions(+), 19 deletions(-) create mode 100644 autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py diff --git a/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py b/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py new file mode 100644 index 000000000000..4db190e4177d --- /dev/null +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py @@ -0,0 +1,86 @@ +from typing import Dict, Optional, Union + +from autogen import UserProxyAgent + +from .falkor_graph_query_engine import FalkorGraphQueryEngine, FalkorGraphQueryResult +from .graph_rag_capability import GraphRagCapability + + +class FalkorGraphRagCapability(GraphRagCapability): + """ + A graph rag capability uses a graph query engine to give a conversable agent the graph rag ability. + + An agent class with graph rag capability could + 1. create a graph in the underlying database with input documents. + 2. retrieved relevant information based on messages received by the agent. + 3. generate answers from retrieved information and send messages back. + + For example, + graph_query_engine = GraphQueryEngine(...) + graph_query_engine.init_db([Document(doc1), Document(doc2), ...]) + + graph_rag_agent = ConversableAgent( + name="graph_rag_agent", + max_consecutive_auto_reply=3, + ... + ) + graph_rag_capability = GraphRagCapbility(graph_query_engine) + graph_rag_capability.add_to_agent(graph_rag_agent) + + user_proxy = UserProxyAgent( + name="user_proxy", + code_execution_config=False, + is_termination_msg=lambda msg: "TERMINATE" in msg["content"], + human_input_mode="ALWAYS", + ) + user_proxy.initiate_chat(graph_rag_agent, message="Name a few actors who've played in 'The Matrix'") + + # ChatResult( + # chat_id=None, + # chat_history=[ + # {'content': 'Name a few actors who've played in \'The Matrix\'', 'role': 'graph_rag_agent'}, + # {'content': 'A few actors who have played in The Matrix are: + # - Keanu Reeves + # - Laurence Fishburne + # - Carrie-Anne Moss + # - Hugo Weaving', + # 'role': 'user_proxy'}, + # ...) + + """ + + def __init__(self, query_engine: FalkorGraphQueryEngine): + """ + initialize graph rag capability with a graph query engine + """ + self.query_engine = query_engine + + def add_to_agent(self, agent: UserProxyAgent): + self.graph_rag_agent = agent + + # Validate the agent config + if agent.llm_config not in (None, False): + raise Exception( + "Graph rag capability limits the query to graph DB, llm_config must be a dict or False or None." + ) + + # Register a hook for processing the last message. + agent.register_hook(hookable_method="process_last_received_message", hook=self.process_last_received_message) + + # Append extra info to the system message. + agent.update_system_message( + agent.system_message + "\nYou've been given the special ability to use graph rag to retrieve information." + ) + + def process_last_received_message(self, message: Union[Dict, str]): + question = self._get_last_question(message) + result: FalkorGraphQueryResult = self.query_engine.query(question) + return result.answer + + def _get_last_question(self, message: Union[Dict, str]): + if isinstance(message, str): + return message + if isinstance(message, Dict): + if "content" in message: + return message["content"] + return None diff --git a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py index f4a44cab465a..b6412305e069 100644 --- a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +++ b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py @@ -51,6 +51,6 @@ def __init__(self, query_engine: GraphQueryEngine): """ initialize graph rag capability with a graph query engine """ - self.query_engine = query_engine + ... def add_to_agent(self, agent: ConversableAgent): ... diff --git a/notebook/agentchat_graph_rag_falkordb.ipynb b/notebook/agentchat_graph_rag_falkordb.ipynb index 9167fecec81f..7a56f923316c 100644 --- a/notebook/agentchat_graph_rag_falkordb.ipynb +++ b/notebook/agentchat_graph_rag_falkordb.ipynb @@ -2,9 +2,16 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 15, "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "1136172.97s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" + ] + }, { "name": "stdout", "output_type": "stream", @@ -31,7 +38,23 @@ "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (1.0.5)\n", "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.30.3->graphrag_sdk==0.1.3b0) (0.14.0)\n", "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.11/site-packages (from beautifulsoup4->bs4<0.0.3,>=0.0.2->graphrag_sdk==0.1.3b0) (2.6)\n", - "Note: you may need to restart the kernel to use updated packages.\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "1136178.53s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Obtaining file:///workspaces/autogen\n", " Installing build dependencies ... \u001b[?25ldone\n", @@ -65,8 +88,8 @@ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests>=2.26.0->docker->autogen==0.3.0) (3.3.2)\n", "Building wheels for collected packages: autogen\n", " Building editable for autogen (pyproject.toml) ... \u001b[?25ldone\n", - "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=e98cc4d562793824fed6eb82a0882d331170bf9161260ff45ff3dc32570f0bb5\n", - " Stored in directory: /tmp/pip-ephem-wheel-cache-0wb4pw7w/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", + "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=20d08c707c24d67ad0f271f71cef54b81aa54a044da58030e67204fc994ee53b\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-038ayx1o/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", "Successfully built autogen\n", "Installing collected packages: autogen\n", " Attempting uninstall: autogen\n", @@ -74,6 +97,9 @@ " Uninstalling autogen-0.3.0:\n", " Successfully uninstalled autogen-0.3.0\n", "Successfully installed autogen-0.3.0\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } @@ -85,18 +111,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 8, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n" - ] - } - ], + "outputs": [], "source": [ "import os\n", "\n", @@ -108,7 +125,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -134,13 +151,71 @@ "\n", "input_documents = [Document(doctype=DocumentType.TEXT, path_or_url=input_path)]\n", "\n", - "query_engine.init_db(input_doc=input_documents)\n", - "\n", + "query_engine.init_db(input_doc=input_documents)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Answer: FalkorGraphQueryResult(answer=\"A few actors who've played in 'The Matrix' are:\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", results=[], messages=[{'role': 'system', 'content': 'You are a graph database expert fluent in the Cypher query language\\n with access to the following knowledge graph: The knowladge graph schema is as follows:\\nThe graph contains the following node labels:\\nMovie\\nActor\\nThe Movie label is associated with the following attribute(s):\\ntitle which is of type str\\nThe Actor label is associated with the following attribute(s):\\nname which is of type str\\nThe graph contains the following relationship types:\\nACTED\\nThe ACTED relationship connects the following labels:\\nActor is connected via ACTED to Movie\\nThis is the end of the knowladge graph schema description.\\n'}, {'role': 'user', 'content': \"Name a few actors who've played in 'The Matrix'\"}, ChatCompletionMessage(content=None, refusal=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_COgmJtXljCeUL7BQzyf0iY2n', function=Function(arguments='{\\n \"query\": \"MATCH (m:Movie {title: \\'The Matrix\\'})<-[:ACTED]-(a:Actor) RETURN a.name\"\\n}', name='run_cypher_query'), type='function')]), {'tool_call_id': 'call_COgmJtXljCeUL7BQzyf0iY2n', 'role': 'tool', 'name': 'run_cypher_query', 'content': \"[['Keanu Reeves'], ['Laurence Fishburne'], ['Carrie-Anne Moss'], ['Hugo Weaving']]\"}, {'role': 'assistant', 'content': \"A few actors who've played in 'The Matrix' are:\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\"}])\n" + ] + } + ], + "source": [ "# Query your data\n", "question = \"Name a few actors who've played in 'The Matrix'\"\n", "answer = query_engine.query(question)\n", "print(f\"Answer: {answer}\")" ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33muser_proxy\u001b[0m (to user_proxy):\n", + "\n", + "Name a few actors who've played in 'The Matrix'\n", + "\n", + "--------------------------------------------------------------------------------\n" + ] + }, + { + "data": { + "text/plain": [ + "ChatResult(chat_id=None, chat_history=[{'content': \"Name a few actors who've played in 'The Matrix'\", 'role': 'assistant', 'name': 'user_proxy'}, {'content': \"The following actors have played in 'The Matrix':\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", 'role': 'user', 'name': 'user_proxy'}], summary=\"The following actors have played in 'The Matrix':\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", cost={'usage_including_cached_inference': {'total_cost': 0}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=['exit'])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from autogen import UserProxyAgent\n", + "from autogen.agentchat.contrib.graph_rag.falkor_graph_rag_capability import FalkorGraphRagCapability\n", + "\n", + "graph_rag_agent = UserProxyAgent(\n", + " name=\"user_proxy\",\n", + " code_execution_config=False,\n", + " is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n", + " human_input_mode=\"ALWAYS\",\n", + ")\n", + "graph_rag_capability = FalkorGraphRagCapability(query_engine)\n", + "graph_rag_capability.add_to_agent(graph_rag_agent)\n", + "\n", + "graph_rag_agent.initiate_chat(graph_rag_agent, message=\"Name a few actors who've played in 'The Matrix'\")" + ] } ], "metadata": { From c31bd22cb1b84df79c0d2fb04a98f2e7c9b4936c Mon Sep 17 00:00:00 2001 From: Aristo <6344553+randombet@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:33:51 +0000 Subject: [PATCH 7/7] Integrate FalkorDB to answer multiple questions --- .../graph_rag/falkor_graph_rag_capability.py | 57 ++----- notebook/agentchat_graph_rag_falkordb.ipynb | 152 ++++++++++++------ 2 files changed, 117 insertions(+), 92 deletions(-) diff --git a/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py b/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py index 4db190e4177d..1860047500bc 100644 --- a/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py +++ b/autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py @@ -1,4 +1,4 @@ -from typing import Dict, Optional, Union +from typing import Dict, Union from autogen import UserProxyAgent @@ -8,45 +8,10 @@ class FalkorGraphRagCapability(GraphRagCapability): """ - A graph rag capability uses a graph query engine to give a conversable agent the graph rag ability. - - An agent class with graph rag capability could - 1. create a graph in the underlying database with input documents. - 2. retrieved relevant information based on messages received by the agent. - 3. generate answers from retrieved information and send messages back. - - For example, - graph_query_engine = GraphQueryEngine(...) - graph_query_engine.init_db([Document(doc1), Document(doc2), ...]) - - graph_rag_agent = ConversableAgent( - name="graph_rag_agent", - max_consecutive_auto_reply=3, - ... - ) - graph_rag_capability = GraphRagCapbility(graph_query_engine) - graph_rag_capability.add_to_agent(graph_rag_agent) - - user_proxy = UserProxyAgent( - name="user_proxy", - code_execution_config=False, - is_termination_msg=lambda msg: "TERMINATE" in msg["content"], - human_input_mode="ALWAYS", - ) - user_proxy.initiate_chat(graph_rag_agent, message="Name a few actors who've played in 'The Matrix'") - - # ChatResult( - # chat_id=None, - # chat_history=[ - # {'content': 'Name a few actors who've played in \'The Matrix\'', 'role': 'graph_rag_agent'}, - # {'content': 'A few actors who have played in The Matrix are: - # - Keanu Reeves - # - Laurence Fishburne - # - Carrie-Anne Moss - # - Hugo Weaving', - # 'role': 'user_proxy'}, - # ...) + The Falkor graph rag capability integrate FalkorDB graphrag_sdk version: 0.1.3b0. + Ref: https://github.com/FalkorDB/GraphRAG-SDK/tree/2-move-away-from-sql-to-json-ontology-detection + For usage, please refer to example notebook/agentchat_graph_rag_falkordb.ipynb """ def __init__(self, query_engine: FalkorGraphQueryEngine): @@ -55,7 +20,14 @@ def __init__(self, query_engine: FalkorGraphQueryEngine): """ self.query_engine = query_engine + # Graph DB query history. + self._history = [] + def add_to_agent(self, agent: UserProxyAgent): + """ + Add FalkorDB graph RAG capability to a UserProxyAgent. + The restriction to a UserProxyAgent to make sure the returned message does not contain information retrieved from the graph DB instead of any LLMs. + """ self.graph_rag_agent = agent # Validate the agent config @@ -73,8 +45,13 @@ def add_to_agent(self, agent: UserProxyAgent): ) def process_last_received_message(self, message: Union[Dict, str]): + """ + Query FalkorDB before return the message. + The history with FalkorDB is also logged and updated. + """ question = self._get_last_question(message) - result: FalkorGraphQueryResult = self.query_engine.query(question) + result: FalkorGraphQueryResult = self.query_engine.query(question, self._history) + self._history = result.messages return result.answer def _get_last_question(self, message: Union[Dict, str]): diff --git a/notebook/agentchat_graph_rag_falkordb.ipynb b/notebook/agentchat_graph_rag_falkordb.ipynb index 7a56f923316c..ffbab4cd0314 100644 --- a/notebook/agentchat_graph_rag_falkordb.ipynb +++ b/notebook/agentchat_graph_rag_falkordb.ipynb @@ -1,15 +1,31 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Using FalkorGraphRagCapability with UserProxyAgent for Graph RAG Question Answering\n", + "\n", + "AutoGen provides graph rag integration with Agent Capability. This is an example to integrate FalkorDB (a Knowledge Graph Database)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Install Falkor DB SDK" + ] + }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "1136172.97s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" + "1222214.22s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" ] }, { @@ -48,7 +64,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "1136178.53s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" + "1222219.78s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" ] }, { @@ -61,42 +77,42 @@ "\u001b[?25h Checking if build backend supports build_editable ... \u001b[?25ldone\n", "\u001b[?25h Getting requirements to build editable ... \u001b[?25ldone\n", "\u001b[?25h Preparing editable metadata (pyproject.toml) ... \u001b[?25ldone\n", - "\u001b[?25hRequirement already satisfied: openai>=1.3 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.49.0)\n", - "Requirement already satisfied: diskcache in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (5.6.3)\n", - "Requirement already satisfied: termcolor in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (2.4.0)\n", - "Requirement already satisfied: flaml in /home/autogen-ai/.local/lib/python3.11/site-packages (from autogen==0.3.0) (2.2.0)\n", - "Requirement already satisfied: numpy<2,>=1.17.0 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.26.4)\n", - "Requirement already satisfied: python-dotenv in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.0.1)\n", - "Requirement already satisfied: tiktoken in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (0.7.0)\n", - "Requirement already satisfied: pydantic!=2.6.0,<3,>=1.10 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (1.10.9)\n", - "Requirement already satisfied: docker in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (7.1.0)\n", - "Requirement already satisfied: packaging in /usr/local/lib/python3.11/site-packages (from autogen==0.3.0) (24.1)\n", - "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.6.0)\n", - "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (1.9.0)\n", - "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (0.27.2)\n", - "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (0.5.0)\n", - "Requirement already satisfied: sniffio in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (1.3.1)\n", - "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.66.5)\n", - "Requirement already satisfied: typing-extensions<5,>=4.11 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.0) (4.12.2)\n", - "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.11/site-packages (from docker->autogen==0.3.0) (2.32.3)\n", - "Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.11/site-packages (from docker->autogen==0.3.0) (2.2.3)\n", - "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.11/site-packages (from tiktoken->autogen==0.3.0) (2024.9.11)\n", - "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai>=1.3->autogen==0.3.0) (3.10)\n", - "Requirement already satisfied: certifi in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (2024.8.30)\n", - "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (1.0.5)\n", - "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.0) (0.14.0)\n", - "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests>=2.26.0->docker->autogen==0.3.0) (3.3.2)\n", + "\u001b[?25hRequirement already satisfied: openai>=1.3 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (1.49.0)\n", + "Requirement already satisfied: diskcache in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (5.6.3)\n", + "Requirement already satisfied: termcolor in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (2.4.0)\n", + "Requirement already satisfied: flaml in /home/autogen-ai/.local/lib/python3.11/site-packages (from autogen==0.3.2) (2.2.0)\n", + "Requirement already satisfied: numpy<2,>=1.17.0 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (1.26.4)\n", + "Requirement already satisfied: python-dotenv in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (1.0.1)\n", + "Requirement already satisfied: tiktoken in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (0.7.0)\n", + "Requirement already satisfied: pydantic!=2.6.0,<3,>=1.10 in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (1.10.9)\n", + "Requirement already satisfied: docker in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (7.1.0)\n", + "Requirement already satisfied: packaging in /usr/local/lib/python3.11/site-packages (from autogen==0.3.2) (24.1)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.2) (4.6.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.2) (1.9.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.2) (0.27.2)\n", + "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.2) (0.5.0)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.2) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.2) (4.66.5)\n", + "Requirement already satisfied: typing-extensions<5,>=4.11 in /usr/local/lib/python3.11/site-packages (from openai>=1.3->autogen==0.3.2) (4.12.2)\n", + "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.11/site-packages (from docker->autogen==0.3.2) (2.32.3)\n", + "Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.11/site-packages (from docker->autogen==0.3.2) (2.2.3)\n", + "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.11/site-packages (from tiktoken->autogen==0.3.2) (2024.9.11)\n", + "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai>=1.3->autogen==0.3.2) (3.10)\n", + "Requirement already satisfied: certifi in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.2) (2024.8.30)\n", + "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.2) (1.0.5)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.3->autogen==0.3.2) (0.14.0)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests>=2.26.0->docker->autogen==0.3.2) (3.3.2)\n", "Building wheels for collected packages: autogen\n", " Building editable for autogen (pyproject.toml) ... \u001b[?25ldone\n", - "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.0-0.editable-py3-none-any.whl size=16273 sha256=20d08c707c24d67ad0f271f71cef54b81aa54a044da58030e67204fc994ee53b\n", - " Stored in directory: /tmp/pip-ephem-wheel-cache-038ayx1o/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", + "\u001b[?25h Created wheel for autogen: filename=autogen-0.3.2-0.editable-py3-none-any.whl size=16216 sha256=6a1d2928c2581b5d66f17c8cafed5444dab21fcb5a52b4ba34fe43abbc4055ec\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-9wh6yuuq/wheels/d4/86/1f/a538740a449c67aa3ff8343698c29d70fc94236f70bde92144\n", "Successfully built autogen\n", "Installing collected packages: autogen\n", " Attempting uninstall: autogen\n", " Found existing installation: autogen 0.3.0\n", " Uninstalling autogen-0.3.0:\n", " Successfully uninstalled autogen-0.3.0\n", - "Successfully installed autogen-0.3.0\n", + "Successfully installed autogen-0.3.2\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", @@ -106,7 +122,21 @@ ], "source": [ "%pip install graphrag_sdk==0.1.3b0\n", - "%pip install -e /workspaces/autogen" + "\n", + "# For debug only,\n", + "# %pip install -e /workspaces/autogen" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Set OpenAI API" ] }, { @@ -119,10 +149,21 @@ "\n", "import autogen\n", "\n", - "config_list = autogen.config_list_from_json(env_or_file=\"../OAI_CONFIG_LIST\")\n", + "config_list = autogen.config_list_from_json(env_or_file=\"OAI_CONFIG_LIST\")\n", "os.environ[\"OPENAI_API_KEY\"] = config_list[0][\"api_key\"]" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create Knowledge Graph with Your Own Data\n", + "\n", + "Note that, you would need to have a Falkor DB running ready. \n", + "In this example, Falker DB endpint is already set at host=\"172.18.0.3\" and port=6379.\n", + "For how to set up Falkor DB, please refer to https://docs.falkordb.com/" + ] + }, { "cell_type": "code", "execution_count": 10, @@ -155,28 +196,15 @@ ] }, { - "cell_type": "code", - "execution_count": 9, + "cell_type": "markdown", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Answer: FalkorGraphQueryResult(answer=\"A few actors who've played in 'The Matrix' are:\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", results=[], messages=[{'role': 'system', 'content': 'You are a graph database expert fluent in the Cypher query language\\n with access to the following knowledge graph: The knowladge graph schema is as follows:\\nThe graph contains the following node labels:\\nMovie\\nActor\\nThe Movie label is associated with the following attribute(s):\\ntitle which is of type str\\nThe Actor label is associated with the following attribute(s):\\nname which is of type str\\nThe graph contains the following relationship types:\\nACTED\\nThe ACTED relationship connects the following labels:\\nActor is connected via ACTED to Movie\\nThis is the end of the knowladge graph schema description.\\n'}, {'role': 'user', 'content': \"Name a few actors who've played in 'The Matrix'\"}, ChatCompletionMessage(content=None, refusal=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_COgmJtXljCeUL7BQzyf0iY2n', function=Function(arguments='{\\n \"query\": \"MATCH (m:Movie {title: \\'The Matrix\\'})<-[:ACTED]-(a:Actor) RETURN a.name\"\\n}', name='run_cypher_query'), type='function')]), {'tool_call_id': 'call_COgmJtXljCeUL7BQzyf0iY2n', 'role': 'tool', 'name': 'run_cypher_query', 'content': \"[['Keanu Reeves'], ['Laurence Fishburne'], ['Carrie-Anne Moss'], ['Hugo Weaving']]\"}, {'role': 'assistant', 'content': \"A few actors who've played in 'The Matrix' are:\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\"}])\n" - ] - } - ], "source": [ - "# Query your data\n", - "question = \"Name a few actors who've played in 'The Matrix'\"\n", - "answer = query_engine.query(question)\n", - "print(f\"Answer: {answer}\")" + "## Create a UserProxyAgent for FalkorDB and Answer Questions" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -184,19 +212,39 @@ "output_type": "stream", "text": [ "\u001b[33muser_proxy\u001b[0m (to user_proxy):\n", - "\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ "Name a few actors who've played in 'The Matrix'\n", "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33muser_proxy\u001b[0m (to user_proxy):\n", + "\n", + "\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33muser_proxy\u001b[0m (to user_proxy):\n", + "\n", + "List additional actors\n", + "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "text/plain": [ - "ChatResult(chat_id=None, chat_history=[{'content': \"Name a few actors who've played in 'The Matrix'\", 'role': 'assistant', 'name': 'user_proxy'}, {'content': \"The following actors have played in 'The Matrix':\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", 'role': 'user', 'name': 'user_proxy'}], summary=\"The following actors have played in 'The Matrix':\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", cost={'usage_including_cached_inference': {'total_cost': 0}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=['exit'])" + "ChatResult(chat_id=None, chat_history=[{'content': \"Name a few actors who've played in 'The Matrix'\", 'role': 'assistant', 'name': 'user_proxy'}, {'content': \"A few actors who have played in 'The Matrix' are:\\n\\n- Keanu Reeves\\n- Laurence Fishburne\\n- Carrie-Anne Moss\\n- Hugo Weaving\", 'role': 'user', 'name': 'user_proxy'}, {'content': '', 'role': 'assistant', 'name': 'user_proxy'}, {'content': None, 'role': 'user', 'name': 'user_proxy'}, {'content': 'List additional actors', 'role': 'assistant', 'name': 'user_proxy'}, {'content': 'The additional actors in the knowledge graph, ordered alphabetically by name, are:\\n\\n- Carrie-Anne Moss\\n- Hugo Weaving\\n- Keanu Reeves\\n- Lana Wachowski\\n- Laurence Fishburne\\n- Lilly Wachowski', 'role': 'user', 'name': 'user_proxy'}], summary='The additional actors in the knowledge graph, ordered alphabetically by name, are:\\n\\n- Carrie-Anne Moss\\n- Hugo Weaving\\n- Keanu Reeves\\n- Lana Wachowski\\n- Laurence Fishburne\\n- Lilly Wachowski', cost={'usage_including_cached_inference': {'total_cost': 0}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=['', 'List additional actors', 'exit'])" ] }, - "execution_count": 16, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" }