Skip to content

Commit

Permalink
Add comments and finalize the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
randombet committed Sep 9, 2024
1 parent 87451a3 commit 82dd60b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class FalkorGraphQueryResult(GraphStoreQueryResult):


class FalkorGraphQueryEngine:
"""
This is a wrapper for Falkor DB KnowledgeGraph.
"""

def __init__(
self,
Expand All @@ -29,6 +32,9 @@ def __init__(
self.knowledge_graph = KnowledgeGraph(name, host, port, username, password, model, schema)

def init_db(self, input_doc: List[Document] | None):
"""
Build the knowledge graph with input documents.
"""
sources = []
for doc in input_doc:
if os.path.exists(doc.path_or_url):
Expand All @@ -41,6 +47,17 @@ def add_records(self, new_records: List) -> bool:
raise NotImplementedError("This method is not supported by Falkor DB SDK yet.")

def query(self, question: str, n_results: int = 1, **kwargs) -> FalkorGraphQueryResult:
"""
Query the knowledage graph with a question and optional message history.
Args:
question: a human input question.
n_results: number of returned results.
kwargs:
messages: a list of message history.
Returns: FalkorGraphQueryResult
"""
messages = kwargs.pop("messages", [])
answer, messages = self.knowledge_graph.ask(question, messages)
return FalkorGraphQueryResult(answer=answer, results=[], messages=messages)
12 changes: 12 additions & 0 deletions test/agentchat/contrib/graph_rag/test_falkor_graph_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
reason=reason,
)
def test_falkor_db_query_engine():
"""
Test Falkor DB Query Engine.
1. create a test Falkor DB Query Engine with a schema.
2. Initialize it with an input txt file.
3. Query it with a question and verify the result contains the critical information.
"""
# Arrange
test_schema = Schema()
actor = test_schema.add_entity("Actor").add_attribute("name", str, unique=True)
Expand All @@ -50,3 +56,9 @@ def test_falkor_db_query_engine():

# Assert
assert query_result.answer.find("Keanu Reeves") >= 0
for message in query_result.messages:
if isinstance(message, dict) and "role" in message and message["role"] == "user":
assert "content" in message
assert message["content"] is question
return
pytest.fail("Question not found in message history.")

0 comments on commit 82dd60b

Please sign in to comment.