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

Add CypherRetriever for parameterized Cypher queries #307

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

oskarhane
Copy link
Member

This PR adds a new CypherRetriever that enables direct database access through parameterized Cypher queries. Key features include:

  • Type-safe parameter validation
  • Support for optional parameters
  • Custom result formatting
  • Documentation and examples

Description

Note

Please provide a description of the work completed in this PR below

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Documentation update
  • Project configuration change

How Has This Been Tested?

  • Unit tests
  • E2E tests
  • Manual tests

Checklist

The following requirements should have been met (depending on the changes in the branch):

  • Documentation has been updated
  • Unit tests have been updated
  • E2E tests have been updated
  • Examples have been updated
  • New files have copyright header
  • CHANGELOG.md updated if appropriate

This PR adds a new CypherRetriever that enables direct database access through parameterized Cypher queries. Key features include:
- Type-safe parameter validation
- Support for optional parameters
- Custom result formatting
- Documentation and examples

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@oskarhane oskarhane requested a review from a team as a code owner March 14, 2025 14:47
@oskarhane
Copy link
Member Author

@CodiumAI-Agent /update_changelog

@CodiumAI-Agent
Copy link

Changelog updates: 🔄

2025-03-14

Added

  • Introduced CypherRetriever for parameterized Cypher queries with type-safe validation and custom result formatting.
  • Updated tests and documentation to support the new retriever.

to commit the new content to the CHANGELOG.md file, please type:
'/update_changelog --pr_update_changelog.push_changelog_changes=true'

Graph Traversals
---------------

The CypherRetriever is particularly useful for complex graph traversals:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I see the point here, especially the example does not show a really complex graph traversal?

count = record["count"] if record else 0

if count == 0:
print("No movie data found. Creating sample data...")
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use the neo4j+s://demo.neo4jlabs.com database here, so we're sure it's already populated.

)
except ValidationError as e:
raise RetrieverInitializationError(
[
Copy link
Contributor

Choose a reason for hiding this comment

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

I've never used this syntax but I think from e will show the same informations (loc, etc)? In such case we do not need ErrorDestail, which looks like an internal pydantic thing.

)

# Validate the type of each parameter
for param_name, param_value in parameters.items():
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be merged into a single loop?

"""Definition of a Cypher query parameter."""

type: CypherParameterType
description: str
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the description be optional? We're not using it (yet)

self,
driver: neo4j.Driver,
query: str,
parameters: Dict[str, Dict[str, Any]],
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can use list[CypherParameterDefinition] with the @validate_call decorator (see for instance here)

from neo4j_graphrag.types import RetrieverResultItem


class TestCypherRetriever(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

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

A bit nitpicking here but we do not use class-based tests in this code base, only functions. So unless there is a strong reason to use a class, I'd prefer we stick to using functions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants