Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes unneeded ImportErrors #50

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Next

### Changed

- Renamed the `type` property to `role` on `Message` nodes in `Neo4jChatMessageHistory`.

### Added

- Introduced a `delete_session_node` parameter to the `clear` method in `Neo4jChatMessageHistory` for optional deletion of the `Session` node.

## 0.3.0

### Added
Expand Down
9 changes: 1 addition & 8 deletions libs/neo4j/langchain_neo4j/chat_message_histories/neo4j.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Optional, Union

import neo4j
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.messages import BaseMessage, messages_from_dict
from langchain_core.utils import get_from_dict_or_env
Expand Down Expand Up @@ -29,14 +30,6 @@ def __init__(
*,
graph: Optional[Neo4jGraph] = None,
):
try:
import neo4j
except ImportError:
raise ImportError(
"Could not import neo4j python package. "
"Please install it with `pip install neo4j`."
)

# Make sure session id is not null
if not session_id:
raise ValueError("Please ensure that the session_id parameter is provided")
Expand Down
8 changes: 1 addition & 7 deletions libs/neo4j/langchain_neo4j/graphs/neo4j_graph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from hashlib import md5
from typing import Any, Dict, List, Optional, Type

import neo4j
from langchain_core.utils import get_from_dict_or_env

from langchain_neo4j.graphs.graph_document import GraphDocument
Expand Down Expand Up @@ -337,13 +338,6 @@ def __init__(
enhanced_schema: bool = False,
) -> None:
"""Create a new Neo4j graph wrapper instance."""
try:
import neo4j
except ImportError:
raise ImportError(
"Could not import neo4j python package. "
"Please install it with `pip install neo4j`."
)

url = get_from_dict_or_env({"url": url}, "url", "NEO4J_URI")
# if username and password are "", assume Neo4j auth is disabled
Expand Down
9 changes: 1 addition & 8 deletions libs/neo4j/langchain_neo4j/vectorstores/neo4j_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Type,
)

import neo4j
import numpy as np
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
Expand Down Expand Up @@ -514,14 +515,6 @@ def __init__(
graph: Optional[Neo4jGraph] = None,
embedding_dimension: Optional[int] = None,
) -> None:
try:
import neo4j
except ImportError:
raise ImportError(
"Could not import neo4j python package. "
"Please install it with `pip install neo4j`."
)

# Allow only cosine and euclidean distance strategies
if distance_strategy not in [
DistanceStrategy.EUCLIDEAN_DISTANCE,
Expand Down
145 changes: 42 additions & 103 deletions libs/neo4j/tests/integration_tests/chains/test_graph_database.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,24 @@
"""Test Graph Database Chain."""

import os
from typing import Dict
from unittest.mock import MagicMock

import pytest
from langchain_core.language_models import BaseLanguageModel

from langchain_neo4j.chains.graph_qa.cypher import GraphCypherQAChain
from langchain_neo4j.graphs.neo4j_graph import Neo4jGraph
from tests.llms.fake_llm import FakeLLM


def test_connect_neo4j() -> None:
"""Test that Neo4j database is correctly instantiated and connected."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

graph = Neo4jGraph(
url=url,
username=username,
password=password,
)

output = graph.query('RETURN "test" AS output')
expected_output = [{"output": "test"}]
assert output == expected_output


def test_connect_neo4j_env() -> None:
"""Test that Neo4j database environment variables."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")
os.environ["NEO4J_URI"] = url
os.environ["NEO4J_USERNAME"] = username
os.environ["NEO4J_PASSWORD"] = password
graph = Neo4jGraph()

output = graph.query('RETURN "test" AS output')
expected_output = [{"output": "test"}]
assert output == expected_output
del os.environ["NEO4J_URI"]
del os.environ["NEO4J_USERNAME"]
del os.environ["NEO4J_PASSWORD"]


def test_cypher_generating_run() -> None:
@pytest.mark.usefixtures("clear_neo4j_database")
def test_cypher_generating_run(neo4j_credentials: Dict[str, str]) -> None:
"""Test that Cypher statement is correctly generated and executed."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

graph = Neo4jGraph(
url=url,
username=username,
password=password,
url=neo4j_credentials["url"],
username=neo4j_credentials["username"],
password=neo4j_credentials["password"],
)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
# Create two nodes and a relationship
graph.query(
"CREATE (a:Actor {name:'Bruce Willis'})"
Expand Down Expand Up @@ -85,18 +46,14 @@ def test_cypher_generating_run() -> None:
assert output == expected_output


def test_cypher_top_k() -> None:
@pytest.mark.usefixtures("clear_neo4j_database")
def test_cypher_top_k(neo4j_credentials: Dict[str, str]) -> None:
"""Test top_k parameter correctly limits the number of results in the context."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

TOP_K = 1

graph = Neo4jGraph(
url=url,
username=username,
password=password,
url=neo4j_credentials["url"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small detail, but I think:

Suggested change
url=neo4j_credentials["url"],
graph = Neo4jGraph(**neo4j_credentials)

could save us 0.1 seconds in the future :-P (OK we've invented copy/paste, I know ^^)

username=neo4j_credentials["username"],
password=neo4j_credentials["password"],
)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
Expand Down Expand Up @@ -126,16 +83,13 @@ def test_cypher_top_k() -> None:
assert len(output) == TOP_K


def test_cypher_intermediate_steps() -> None:
@pytest.mark.usefixtures("clear_neo4j_database")
def test_cypher_intermediate_steps(neo4j_credentials: Dict[str, str]) -> None:
"""Test the returning of the intermediate steps."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

graph = Neo4jGraph(
url=url,
username=username,
password=password,
url=neo4j_credentials["url"],
username=neo4j_credentials["username"],
password=neo4j_credentials["password"],
)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
Expand Down Expand Up @@ -173,16 +127,13 @@ def test_cypher_intermediate_steps() -> None:
assert context == expected_context


def test_cypher_return_direct() -> None:
@pytest.mark.usefixtures("clear_neo4j_database")
def test_cypher_return_direct(neo4j_credentials: Dict[str, str]) -> None:
"""Test that chain returns direct results."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

graph = Neo4jGraph(
url=url,
username=username,
password=password,
url=neo4j_credentials["url"],
username=neo4j_credentials["username"],
password=neo4j_credentials["password"],
)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
Expand Down Expand Up @@ -211,16 +162,13 @@ def test_cypher_return_direct() -> None:
assert output == expected_output


def test_function_response() -> None:
@pytest.mark.usefixtures("clear_neo4j_database")
def test_function_response(neo4j_credentials: Dict[str, str]) -> None:
"""Test returning a function response."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

graph = Neo4jGraph(
url=url,
username=username,
password=password,
url=neo4j_credentials["url"],
username=neo4j_credentials["username"],
password=neo4j_credentials["password"],
)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
Expand Down Expand Up @@ -251,16 +199,13 @@ def test_function_response() -> None:
assert output == expected_output


def test_exclude_types() -> None:
@pytest.mark.usefixtures("clear_neo4j_database")
def test_exclude_types(neo4j_credentials: Dict[str, str]) -> None:
"""Test exclude types from schema."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

graph = Neo4jGraph(
url=url,
username=username,
password=password,
url=neo4j_credentials["url"],
username=neo4j_credentials["username"],
password=neo4j_credentials["password"],
)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
Expand Down Expand Up @@ -290,16 +235,13 @@ def test_exclude_types() -> None:
assert chain.graph_schema == expected_schema


def test_include_types() -> None:
@pytest.mark.usefixtures("clear_neo4j_database")
def test_include_types(neo4j_credentials: Dict[str, str]) -> None:
"""Test include types from schema."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

graph = Neo4jGraph(
url=url,
username=username,
password=password,
url=neo4j_credentials["url"],
username=neo4j_credentials["username"],
password=neo4j_credentials["password"],
)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
Expand Down Expand Up @@ -330,16 +272,13 @@ def test_include_types() -> None:
assert chain.graph_schema == expected_schema


def test_include_types2() -> None:
@pytest.mark.usefixtures("clear_neo4j_database")
def test_include_types2(neo4j_credentials: Dict[str, str]) -> None:
"""Test include types from schema."""
url = os.environ.get("NEO4J_URI", "bolt://localhost:7687")
username = os.environ.get("NEO4J_USERNAME", "neo4j")
password = os.environ.get("NEO4J_PASSWORD", "pleaseletmein")

graph = Neo4jGraph(
url=url,
username=username,
password=password,
url=neo4j_credentials["url"],
username=neo4j_credentials["username"],
password=neo4j_credentials["password"],
)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
Expand Down
Loading