Skip to content

Commit

Permalink
Makes GraphStore a Protocol (#35)
Browse files Browse the repository at this point in the history
* Makes GraphStore a Protocol

* Updated CHANGELOG
  • Loading branch information
alexthomas93 authored Jan 10, 2025
1 parent 06db048 commit 158699e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Made the `source` parameter of `GraphDocument` optional and updated related methods to support this.
- Suppressed AggregationSkippedNull warnings raised by the Neo4j driver in the Neo4jGraph class when fetching the enhanced_schema.
- Updated `GraphStore` to be a Protocol, enabling compatibility with `GraphCypherQAChain` without requiring inheritance.

### Fixed

Expand Down
19 changes: 7 additions & 12 deletions libs/neo4j/langchain_neo4j/graphs/graph_store.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
from abc import abstractmethod
from typing import Any, Dict, List
from typing import Any, Dict, List, Protocol, runtime_checkable

from langchain_neo4j.graphs.graph_document import GraphDocument


class GraphStore:
@runtime_checkable
class GraphStore(Protocol):
"""Abstract class for graph operations."""

@property
@abstractmethod
def get_schema(self) -> str:
"""Return the schema of the Graph database"""
pass
...

@property
@abstractmethod
def get_structured_schema(self) -> Dict[str, Any]:
"""Return the schema of the Graph database"""
pass
...

@abstractmethod
def query(self, query: str, params: dict = {}) -> List[Dict[str, Any]]:
"""Query the graph."""
pass
...

@abstractmethod
def refresh_schema(self) -> None:
"""Refresh the graph schema information."""
pass

@abstractmethod
def add_graph_documents(
self, graph_documents: List[GraphDocument], include_source: bool = False
) -> None:
"""Take GraphDocument as input as uses it to construct a graph."""
pass
...
8 changes: 7 additions & 1 deletion libs/neo4j/tests/unit_tests/chains/test_graph_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from tests.llms.fake_llm import FakeLLM


class FakeGraphStore(GraphStore):
class FakeGraphStore:
@property
def get_schema(self) -> str:
"""Returns the schema of the Graph database"""
Expand All @@ -63,6 +63,12 @@ def add_graph_documents(
pass


def test_graph_store() -> None:
"""Tests that FakeGraphStore satisfies the GraphStore protocol requirements."""
graph = FakeGraphStore()
assert isinstance(graph, GraphStore)


def test_graph_cypher_qa_chain_prompt_selection_1() -> None:
# Pass prompts directly. No kwargs is specified.
qa_prompt_template = "QA Prompt"
Expand Down

0 comments on commit 158699e

Please sign in to comment.